/*
 * FileName: img rotate class
 * Author: Asfman
 * Date: 2008-7-29
 * Contact: http://jsframework.cn
 * parameter: width, height, containter, startIndex, timeout
 * Need js: none
 */
function Rotate(width, height, container, index, timeout){
			this.imgInfo = [];
			this.width = parseInt(width);
			this.height = parseInt(height);
			this.container = $(container);
			this.timeout = timeout || 3000;
			this.index = index || 0;
			this.oImg = null;
			this.timer = null;
			this.innerContainer = "asfman_" + uniqueID(6);
			this.order = "asfman_" + uniqueID(6);
			this.img = "asfman_" + uniqueID(6);
			this.template = "<div id='" + this.innerContainer + "'>\r\n" + "<div id='" + this.order + "'>\r\n{order}\r\n</div>\r\n</div>";
			//add css 
			var styleCss = "#" + this.innerContainer + "{overflow: hidden; position: relative; width: " + this.width + "px; height: " + this.height + "px;}\r\n" +
						   "#" + this.order + "{position: absolute; left: 12px; bottom: 3px;}\r\n" +
						   "#" + this.order + " a{width: 12px; line-height: 13px; height: 13px; font-size: 11px; text-align: center; margin: 3px 5px 0 3px; float: left;}\r\n" +
						   "#" + this.order + " a:link, #" + this.order + " a:visited{text-decoration: none;}\r\n"+
						   "#" + this.order + " a:hover{text-decoration:none;}\r\n" + 
						   ".rotate_normal{background: transparent url(images/button1.jpg) no-repeat scroll; color: #000; }\r\n" + 
						   ".rotate_active{background: transparent url(images/button2.jpg) no-repeat scroll; color: #000; }\r\n" + 
						   "#" + this.innerContainer + " img{border: 0; width: " + this.width + "px; height: " + this.height + "px;}\r\n";
			function uniqueID(n){
					var str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
					for(var ret = "", i = 0; i < n; i++){
						ret += str.charAt(Math.floor(Math.random()*62));
					}
					return ret;
			};
			void function(cssText){
					var oStyle = document.createElement("style");
					oStyle.type = "text/css"; 
					oStyle.styleSheet ? oStyle.styleSheet.cssText = cssText : oStyle.appendChild(document.createTextNode(cssText));
					document.getElementsByTagName("head")[0].appendChild(oStyle);
		   }(styleCss);
		   function $(str){return document.getElementById(str);};
		   function addListener(o, type, fn){
					var func = function(){
					  return function(){fn.call(o);}
					}();
				   o.attachEvent ? o.attachEvent("on" + type, func) : o.addEventListener ? o.addEventListener(type, func, false) : o["on" + type] = func;
				   return func;
		   }
		   if(Rotate.initialize == undefined){
			   Rotate.prototype.addImg = function(obj){//{url: imgUrl, link: linkUrl, alt: alt, txt: txt}
						  if(obj.constructor == Array)
								for(var i = 0, l =  obj.length; i < l; i++) this.imgInfo.push(obj[i]);
						  else
								this.imgInfo.push(obj);
			   };
			   Rotate.prototype.show = function(){
				      var _this = this;
					  var order = "";
					  for(var i = 1, l = this.imgInfo.length; i <= l; i++)
						   order += "<a href='javascript: void(0)'>" + i + "</a>";
					  this.template = this.template.replace(/{order}/, order);
					  this.container.innerHTML = this.template;
					  for(var j = 0; j < l; j++){
						 addListener($(this.order).getElementsByTagName("a")[j], "click", clickFunc);
						 addListener($(this.order).getElementsByTagName("a")[j], "focus", function(){this.blur();});
						 $(this.order).getElementsByTagName("a")[j].className = "rotate_normal";
					  }
					  $(this.order).getElementsByTagName("a")[this.index].className = "rotate_active";
					  function clickFunc(){
							  if(_this.timer) clearInterval(_this.timer);
							  var n = (this.innerHTML.replace(/^\s+|\s+$/g,"")|0)-1;
							  _this.change(n);
							  _this.index = n;
							  _this.timer = setInterval(function(){_this.autoStart();}, _this.timeout);
					  }
					  this.oA = document.createElement("a");
					  this.oA.href = "javascript: void(0);";
					  if(this.imgInfo[this.index].link){
						  this.oA.href = this.imgInfo[this.index].link
						  this.oA.target = "_blank";
					  }
					  $(this.innerContainer).appendChild(this.oA);
					  this.oImg = new Image();
					  this.oImg.src = this.imgInfo[this.index].url;
					  this.oImg.style.filter = "revealTrans(duration=1,transition=23)";
					  this.oA.appendChild(this.oImg);
					  addListener(this.oA, "focus", function(){this.blur();});
					  this.timer = setInterval(function(){_this.autoStart();}, this.timeout);

			   };
			   Rotate.prototype.autoStart = function(){
					this.index++;
					var n = this.index >= this.imgInfo.length ? this.index = 0 : this.index;
					this.change(n);
			   };
			   Rotate.prototype.change = function(index){
					  (this.oImg.filters && this.oImg.filters.revealTrans) ?  (
							 this.oImg.filters.revealTrans.Transition = Math.floor(Math.random()*23),
							 this.oImg.filters.revealTrans.apply(),
							 this.oImg.src = this.imgInfo[index].url,
							 this.oImg.filters.revealTrans.play()) : this.oImg.src = this.imgInfo[index].url;
					  this.oA.href = this.imgInfo[index].link ? this.imgInfo[index].link : "javascript:void(0)";
					  for(var k = 0, length =  this.imgInfo.length; k < length; k++)
						 $(this.order).getElementsByTagName("a")[k].className = "rotate_normal";
					  $(this.order).getElementsByTagName("a")[index].className = "rotate_active";
			   };
			   Rotate.initialize = true;
		}//end of init
}
/*
one call example:
<div id="container"></div>
<script>
	var o = new Rotate(348, 145, "container");
	o.addImg([{url:"http://www.21cp.net/upload/images/200805270907501987824918.jpg", link: "http://bbs.51js.com/"},
	{url:"http://www.21cp.net/upload/images/2008061011073379882228317.jpg", link: "http://www.21cp.net/"}]);
	 o.show();
 </script>
*/
