// JavaScript Document
/*
	幻灯新闻控制JS
	作者:梦猫猫(追梦客)
	QQ:16991200
	MSN:flyingwork@hotmail.com
	如果需要自行修改，请保留以上信息
	article = {title:"title",link:"link",photo:"photo"}
	style 显示的按钮样式0左侧，1右侧，2左下，3右下
	小图按钮盒子左右2个 下有1个像素的填充,上有6个象素的填充
	小图的宽度高度是容器的五分之一,并在此基础上 宽减4,高减7
*/
	
	function MagicArticlePlayer(container,width,height,interval,style,showtitle,options)
	{
		this.__container__ = document.getElementById(container);
		this.__width__ = width;
		this.__height__ = height;
		this.__style__ = style;
		this.__showTitle__ = showtitle;
		
		//自定义样式
		//this.__titltBoxHeight__ = 24;//标题显示的区域高度
	    this.__currentButtonBgColor__ = "#F00";//当前按钮时的颜色
	    this.__buttonBgColor__ = "#333";//按钮默认背景色
	    this.__buttonColor__ = "#FFF";//字体颜色
	    //this.__color__ = "#000";
	    
		if(options!=null)
		{
		    //this.__titltBoxHeight__ = options.TitleBoxHeight?options.TitleBoxHeight:24;//标题显示的区域高度
		    this.__currentButtonBgColor__ =options.CurrentButtonBgColor?options.CurrentButtonBgColor:"#F00";//当前按钮时的颜色
		    this.__buttonBgColor__ = options.ButtonBgColor?options.ButtonBgColor:"#333";//按钮默认背景色
		    this.__buttonColor__ = options.ButtonColor?options.ButtonColor:"#FFF";//字体颜色
		    //this.__color__ = options.Color?options.Color:"#000";
		}
				
		this.MagicList = new Array();
		this.MagicButton = new Array();
		this.__Interval__ = interval;
		if(this.__Interval__==null)
		{
			this.__Interval__ = 1000;
		}
		this.__IntervalId__ = null;
		this.__PlayIndex__=0;
		
		
		this.Filters = new Array();//幻灯类用到的滤镜，IE使用
//		this.Filters .push("BlendTrans(duration=2);");
//		this.Filters .push("progid:DXImageTransform.Microsoft.Blinds(direction='down');");
//		this.Filters .push("progid:DXImageTransform.Microsoft.Barn ( duration=1,motion=in,orientation=horizontal );");
//		this.Filters .push("progid:DXImageTransform.Microsoft.Inset ( duration=1 ) ;");
//		this.Filters .push("progid:DXImageTransform.Microsoft.Pixelate ( enabled=false,duration=1,maxSquare=5 ) ;");
//		this.Filters .push("progid:DXImageTransform.Microsoft.RandomBars ( duration=1,orientation=horizontal ) ;");
//		this.Filters .push("progid:DXImageTransform.Microsoft.Iris ( duration=2,irisStyle=DIAMOND,motion=out );");
//		this.Filters .push("progid:DXImageTransform.Microsoft.GradientWipe ( duration=2,gradientSize=0.5,motion=forward );");
//		this.Filters .push("progid:DXImageTransform.Microsoft.Spiral ( duration=1,gridSizeX=2,gridSizeY=16 );");
//		this.Filters .push("progid:DXImageTransform.Microsoft.GradientWipe ( duration=2,gradientSize=0.25,motion=forward ) ;");
//		this.Filters .push("progid:DXImageTransform.Microsoft.Stretch ( duration=1,stretchStyle=PUSH ) ;");
//		this.Filters .push("progid:DXImageTransform.Microsoft.RandomDissolve ( duration=1 ) ;");
		this.Filters .push("progid:DXImageTransform.Microsoft.revealTrans ( duration=1,transition=12 ) ;");
//		this.Filters .push("progid:DXImageTransform.Microsoft.Iris ( duration=1,irisStyle=CROSS,motion=in ) ;");
	}
	MagicArticlePlayer.prototype.Add = function (article)
	{
		if(this.MagicList.length<5)
		{
			this.MagicList.push(article);
		}
	}
	MagicArticlePlayer.prototype.Render = function ()
	{
		if(this.__container__ == null)
		{
			return;
		}
		if(this.MagicList.length==0)
		{
		    return;
		}
		//初始化舞台盒子
		this.__container__.style.textAlign="left";
		this.__container__.style.overflow="hidden";
		this.__container__.style.position="relative";
		//this.__container__.style.height=this.__height__+"px";
		this.__container__.style.height = null;
		this.__container__.style.width=this.__width__+"px";
		this.__Stage__ = document.createElement("div");
//		this.__Stage__.style.height=this.__height__+"px";
//		if(this.__showTitle__)
//		{
//			this.__Stage__.style.height = (this.__height__-this.__titltBoxHeight__)+"px"
//		}
		this.__Stage__.style.width=this.__width__+"px";
//		switch(this.__style__)
//		{
//			case 0:
//			case 1:
//				//this.__Stage__.style.width=(this.__width__-12)+"px";
//				break;
//		}
		this.__Stage__.Control = this;
		this.__container__.appendChild(this.__Stage__);//创建添加舞台
		//创建图片按钮
		var _zindex = 10;
		var _this = this;
		var _smallImgSize = {width:18,height:12};
		for(var i=0;i<this.MagicList.length;i++)
		{
			var _box = document.createElement("a");
			_box.__article__ = this.MagicList[i];
			_box.target="_blank";
			_box.Control = this;
			this.MagicButton.push(_box);
			_box.href=this.MagicList[i].link;
//			_box.style.backgroundColor = "red";
			switch(this.__style__)
			{
				case 0:
					_box.style.top = ((_smallImgSize.height+1) * i)+"px";
					_box.style.left = "0px";
					break;
				case 1:
					_box.style.top = ((_smallImgSize.height+1) * i)+"px";
					_box.style.left = (parseInt(this.__width__)-(_smallImgSize.width+1))+"px";
					break;
				case 2:
					_box.style.top = (parseInt(this.__height__) - _smallImgSize.height)+"px";
					_box.style.left = ((_smallImgSize.width+1)*i)+"px";
					break;
				default:
					_box.style.top = (parseInt(this.__height__) - _smallImgSize.height)+"px";
					_box.style.left = (parseInt(this.__width__) - (_smallImgSize.width+1)*(this.MagicList.length-i))+"px";
					break;
			}
			_box.style.textDecoration = "none";
			_box.style.color = this.__buttonColor__;
			_box.style.fontFamily="Arial, Verdana";
			_box.style.fontWeight = "bold";
			_box.style.fontSize="9px";
			_box.style.backgroundColor = this.__buttonBgColor__;
			_box.style.lineHeight=(_smallImgSize.height)+"px";
			_box.style.textAlign="center";
			_box.style.position = "absolute";
			_box.style.width = (_smallImgSize.width)+"px";
			_box.style.height = (_smallImgSize.height)+"px";
			_box.style.padding = "0px";
			//_box.filters["Alpha"].Opacity = "60";
//			alert(_box.style.filter.alpha);
			_box.style.filter = "Alpha(Opacity=80)"
			_box.innerHTML = i+1;
			_box.__PlayIndex__ = i;
			_box.style.zIndex =_zindex;
			_zindex+=1;

			this.__container__.appendChild(_box);
			_box.onmouseover = function (evt)
			{
				var _event = evt?evt:event;
				this.Control.MouseOver(this,_event)
			}
			_box.onmouseout = function (evt)
			{
				var _event = evt?evt:event;
				this.Control.MouseOut(this,_event)
			}
		}
		this.MouseOver(this.MagicButton[0]);
		this.__IntervalId__ = window.setInterval(this.GetAutoPlayCommand(),this.__Interval__);
	}	
	MagicArticlePlayer.prototype.MouseOver = function (sender,evt)
	{
		if(this.__IntervalId__!=null)
		{
			window.clearInterval(this.__IntervalId__);
		}
		this.Transform(sender.__article__);
		for(var i=0;i<this.MagicButton.length;i++)
		{
			this.MagicButton[i].style.backgroundColor = this.__buttonBgColor__;
		}
		sender.style.backgroundColor = this.__currentButtonBgColor__;
		
		this.__PlayIndex__ = sender.__PlayIndex__+1;
		if(this.__PlayIndex__>=this.MagicList.length)
		{
		    this.__PlayIndex__ = 0;
		}
	}
	
	MagicArticlePlayer.prototype.MouseOut = function  (sender,evt)
	{
		this.__IntervalId__ = window.setInterval(this.GetAutoPlayCommand(),this.__Interval__);
	}
	//自动播放
	MagicArticlePlayer.prototype.AutoPlay = function ()
	{
		var _article = this.MagicList[this.__PlayIndex__];
		this.Transform(_article);
		for(var i=0;i<this.MagicButton.length;i++)
		{
			this.MagicButton[i].style.backgroundColor = this.__buttonBgColor__;
		}
		this.MagicButton[this.__PlayIndex__].style.backgroundColor = this.__currentButtonBgColor__;
		this.__PlayIndex__+=1;
		if(this.__PlayIndex__ > this.MagicList.length-1)
		{
			this.__PlayIndex__ = 0;
		}
	}
	
		//自动播放
	MagicArticlePlayer.prototype.Transform = function (article)
	{
		var _filterIndex=parseInt(Math.random()*this.Filters.length);
		if(_filterIndex>=this.Filters.length){
			_filterIndex = 0;
		}
		if(document.all){
		    try{
			    this.__Stage__.style.filter = this.Filters[_filterIndex];
			    this.__Stage__.filters[0].apply();
			    this.__Stage__.innerHTML = this.GetViewHtml(article);
			    this.__Stage__.filters[0].play();	
            }catch(ex){
                this.__Stage__.innerHTML = this.GetViewHtml(article);
            }
		}else{
			this.__Stage__.innerHTML = this.GetViewHtml(article);
		}
		
	}
	MagicArticlePlayer.prototype.GetAutoPlayCommand = function ()
	{
		var _this = this;
		var _fun = function ()
		{
			_this.AutoPlay();
		}
		return _fun;
	}
	//生成视图
	MagicArticlePlayer.prototype.GetViewHtml = function (article)
	{
		var _html = "<a href='"+article.link+"' target='_blank' class='PicBox'>";
		_html += "<img style='border:0px;' src='"+article.photo+"' width='"+parseInt(this.__width__)+"' height='"+parseInt(this.__height__)+"' alt='"+article.title+"' title='"+article.title+"' />";
		_html += "</a>";
		if(this.__showTitle__){
			//_html += "<a target='_blank' class='TitleBox' style='position:absolute;left:0px;bottom:0px;text-align:center;font-size:12px;color:"+this.__color__+";width:"+parseInt(this.__width__)+"px;height:"+this.__titltBoxHeight__+"px;z-index:8;line-height:"+this.__titltBoxHeight__ +"px;text-decoration: none;font-weight: bold;' href='"+article.link+"'>"+article.title+"</a>"
			//_html += "<div class='TitleBox' style='position:absolute;left:0px;bottom:0px;text-align:center;font-size:12px;color:"+this.__color__+";width:"+parseInt(this.__width__)+"px;height:"+this.__titltBoxHeight__+"px;z-index:8;line-height:"+this.__titltBoxHeight__ +"px;text-decoration: none;'>"
			_html += "<div class='TitleBox' style='left:0px;bottom:0px;text-align:center;font-size:12px;width:"+parseInt(this.__width__)+"px;z-index:8;text-decoration: none;'>"
			_html += "<a target='_blank' style='text-decoration: none;' href='"+article.link+"'>"+article.title+"</a>"
			_html += "</div>";
		}
		return _html;
	}

