﻿
/****************************************
幻灯片:自设宽高
*********************************************/
//播放参数传递


var FilmMovie = new Object();
FilmMovie.loadNum={};
FilmMovie.CreateFilm=function (el,name,pl,nowFrame,style,width,height,size,spacetime,target)
{
        var imga=$("#Topactiveid");
        imga.attr("target",target); //设置链接是否打开新窗口
		this.defaultView=el;		    //对应的影片 图片
		this.parent=(pl!=null)?pl:"";	//影片容器
		
		this.count=0;	                //图片总数
		this.nowFrame=nowFrame||0;		//当前帧
		this.style=style;               //显示样式
		this.first=true;
		this.status=1;		            //影片状态，0停止，1播放,3暂停
		this.name=name;	
		this.spaceTime=spacetime;	    //默认播放时间5秒
		this.excessTime=0.7;	        //变换效果的转换时间
		this.lock=false;	            //锁定;
		this.size=(size!=null)?size:0;  //图片是否固定大小：是1，否0
        //this.myclass=true; 

		this.width=width;
		this.height=height;
		
		if(this.style == 1)
		{
			$("#TopclickButton").show();
		}
		else 
		{
			$("#TopclickButton").hide();
		}
		
		this.el=this;
		this.tranImg="../Images/Common/space.gif";
		this.theTimer=null;
		this.imgList=new Array();
		this.init();
}
FilmMovie.CreateFilm.prototype=
{
	init:function()
	{
		if(typeof(this.defaultView)=="string")
			this.defaultView=document.getElementById(this.defaultView);
		if(this.parent!=""&&typeof(this.parent)=="string")
			this.parent=document.getElementById(this.parent);
		if(this.frmSize!=""&&typeof(this.frmSize)=="string")
				this.frmSize=document.getElementById(this.frmSize);
		this.setCotainer();
	},
	setCotainer:function()
	{
	    this.parent.width = this.width;
        this.parent.height = this.height;
		
		return true;
	},
	loadError:function()		//图片是否都已装载
	{
		var i;
		var count;
		for(i=0;i<this.imgList.length;i++)
			if(FilmMovie.loadNum[this.defaultView.id+i]!=-1)
				break;
		return i==this.imgList.length;
	},
	error:function(errorCode)
	{
		if(errorCode==0)
		{
			this.lock=true;
			this.defaultView.parentNode.parentNode.innerHTML="载入图片失败";
		}
	},
	Add:function(name,src,url)
	{
		this.imgList[this.count]=new ImgLink(name,src,url);
		this.count++;
	},
	remove:function(num)
	{
		if(this.imgList.splice(num,1))
			this.count--;
	},
	Play:function()
	{
		if(!this.CheckMovie())
			return ;
		this.status=1;
		
		if(this.style==1)
			this.setBtn(this.name);
			
		if(this.count==1)
			this.GotoAndStop(1);
		
		else if(this.status==1)
			this.PlayNext();		
	},
	setBtn:function(name)
	{
		//列出按钮
		var str = "";
		for(var i=1;i<this.imgList.length+1;i++){
		str = str + "<a>" + i + "</a>";
		}
		$("#TopclickButton div").html(str);
		$("#TopclickButton a").attr("href","javascript:");
		$("#TopclickButton a:nth-child(" + this.nowFrame + ")").addClass("active");
		
		$("#TopclickButton a").each(function(index){
			$(this).hover(function(){
					$("#TopclickButton a").removeClass("active");
					$(this).addClass("active");
					var num = index + 1;
					this.theTimer=setTimeout(name+".GotoAndPlay("+num+")",1);
				}); 
		});
	},
	Goto:function(frame)
	{
		if(this.defaultView==null||this.lock) return ;
		
		if(frame>0&&frame<=this.count)
		{
			this.nowFrame=frame;			
			var imgObj=this.imgList[this.nowFrame-1].img;
			
//			if(this.first==true)		//删除预载入的图片
//			{
//				//this.defaultView.src=this.tranImg;
//				var pEl=this.defaultView.parentNode;
//				var pID=this.defaultView.id;	
//				pEl.removeChild(this.defaultView);				
//				//pEl.innerHTML='<img src="'+this.tranImg+'" border="0" id="'+pID+'"/>';
//				pEl.innerHTML = '<img src="" border="0" id="'+pID+'" />';
//				this.defaultView=Getelements(pID);
//				//this.defaultView.="hidden";
//				this.first=false;
//			}
			
			this.adjustImgSize();
			
			if(this.style==1)
				this.setBtn(this.name);
			
			this.SetTransition();
			
			this.defaultView.src=imgObj.src;
			
			this.PlayTransition();
			
			if(this.defaultView.parentNode.tagName=="A")
			{
				this.defaultView.parentNode.href=this.imgList[this.nowFrame-1].url;
				this.defaultView.alt=this.imgList[this.nowFrame-1].name||"";
			}			
			if(this.status==1&&this.count>1)
				this.theTimer = setTimeout(this.name+".PlayNext()", this.spaceTime);
		}
	},
	GotoAndStop:function(frame)
	{
		window.clearInterval(this.theTimer);
		this.status=0;

		this.Goto(frame);	
	},
	GotoAndPlay:function(frame)
	{
		this.status=1;
		window.clearInterval(this.theTimer);
		this.Goto(frame);
	},
	PlayPrev:function()
	{
		window.clearInterval(this.theTimer);
		this.Prev();
	},
	Prev:function()
	{
		if(this.nowFrame>1&&this.nowFrame<=this.count)
			this.nowFrame--;
		else
			this.nowFrame=this.count;	
		this.Goto(this.nowFrame);
	},	
	PlayNext:function()
	{
		window.clearInterval(this.theTimer);
		this.Next();
	},
	Next:function()
	{
		if(this.nowFrame<this.count)
			this.nowFrame++;
		else
			this.nowFrame=1;
		this.Goto(this.nowFrame);
	},
	Stop:function()
	{
		window.clearInterval(this.theTimer);
		this.status=0;
		this.nowFrame=0;
	},
	Pause:function()
	{
		window.clearInterval(this.theTimer);
		this.status=3;
	},
	SetTransition:function()
	{
		if(document.all&&this.defaultView!=null)
		{ 
			this.defaultView.style.filter="revealTrans(Transition=1,Duration="+this.excessTime+")";
			if(this.defaultView.filters.revealTrans.Transition==23)
			{
			    this.defaultView.filters.revealTrans.Transition=1;
			}
			else
			{
			    this.defaultView.filters.revealTrans.Transition++;
			}
			this.defaultView.filters.revealTrans.Apply();
		}
	},
	PlayTransition:function ()
	{ 
		if (document.all&&this.defaultView!=null)			
			this.defaultView.filters.revealTrans.play();
	},
	getParentElementSize:function()
	{
		this.setCotainer();
	},
	resizeScene:function(relObj)
	{
		var RelObj=relObj||this;
		try{
			RelObj.setCotainer();
			RelObj.adjustImgSize();	
		}
		catch(e)
		{
		}
	},
	adjustImgSize:function()
	{
		if(this.size==1)
		{
		    this.defaultView.style.height = this.height;
            this.defaultView.style.width = this.width;
		}
		else
		{
		    var objImg
		    if (getOs() == "MSIE") {
		        objImg = this.imgList[this.nowFrame - 1].img;
		    }
		    else {
		        objImg = $(this.imgList[this.nowFrame - 1].img);
		    }
		    var plyWidth=parseInt(objImg.width);
		    var plyHeight=parseInt(objImg.height);

		    var bWidth=plyWidth/this.width;
		    var bHeight=plyHeight/this.height;	
		
		    if(bWidth<=1&&bHeight<=1)
		    {
			    this.defaultView.style.height=plyHeight+"px";
			    this.defaultView.style.width=plyWidth+"px";
			    return ;
		    }
		    if(bWidth>=bHeight)
		    {
			    plyWidth=Math.floor(plyWidth/bWidth);
			    plyHeight=Math.floor(plyHeight/bWidth);		
		    }
		    else if(bWidth<=bHeight)
		    {
			    plyWidth=Math.floor(plyWidth/bHeight);
			    plyHeight=Math.floor(plyHeight/bHeight);
		    }
		
		    this.defaultView.style.height=plyHeight+"px";
		    this.defaultView.style.width=plyWidth+"px";
		 }
		
		window.status = '  ';
		
	},
	CheckMovie:function()
	{
		return this.count>=1;
	},
	getImgName:function()
	{
		return (this.nowFrame==0?"":this.imgList[this.nowFrame].name);
	},
	Dispose:function()
	{
/*		window.clearInterval(this.theTimer);
		this.defaultView=null;
		this.parent=null;
		this.imgList=null;
		this.frmSize=null;		*/
	}
}

FilmMovie.CreateCommandButton=function(Film,btnPlay,btnStop,btnPrev,btnNext)
{
		this.film=Film;
		this.btnPlay=btnPlay;
		//this.btnStop=btnStop;
		this.btnPrev=btnPrev;
		this.btnNext=btnNext;
		this.PauseSrc="../Images/Common/bt_pause.gif";
		this.PlaySrc="../Images/Common/bt_play.gif";
		
}

FilmMovie.CreateCommandButton.prototype=
{
	Init:function()
	{
		var PlayButton=getElementObj(this.btnPlay);
		if(PlayButton!=null)
		{						
			if(PlayButton.PlaySrc!=null)
			{
				var loadImg=new Image();
				loadImg.src=PlayButton.PlaySrc;
			}			
			if(PlayButton.PauseSrc!=null)
			{
				var loadImg=new Image();
				loadImg.src=PlayButton.PauseSrc;
			}
		}
	},
	
	SetStop:function()
	{		
		var fmObj=getElementObj(this.btnStop);		
		fmObj.FilmSource=this;
		fmObj.onclick=function()
		{
			this.FilmSource.film.Stop();		
			
			var tPlay=getElementObj(this.FilmSource.btnPlay);
			if(tPlay!=null)
			{
				tPlay.src=tPlay.PlaySrc;
				tPlay.src=this.FilmSource.PlaySrc;
			}
			
			return false;
		}
	},
	
	SetPlay:function()
	{
		var fmObj=getElementObj(this.btnPlay);		
		fmObj.FilmSource=this;
		fmObj.onclick=function()
		{
			if(this.FilmSource.film.status!=1)
			{
				this.FilmSource.film.Play();
				
				var tPlay=getElementObj(this.FilmSource.btnPlay);
				if(tPlay!=null)
				{
					tPlay.src=tPlay.PauseSrc;
					tPlay.src=this.FilmSource.PauseSrc;
				}
			}
			else
			{
				this.FilmSource.film.Pause();
				
				var tPlay=getElementObj(this.FilmSource.btnPlay);
				
				if(tPlay!=null)
				{
					tPlay.src=tPlay.PlaySrc;
					tPlay.src=this.FilmSource.PlaySrc;
				}
			}
			return false;
		}
	},
	
	SetPrev:function()
	{
		var fmObj=getElementObj(this.btnPrev);
		fmObj.FilmSource=this.film;
		fmObj.onclick=function()
		{
			this.FilmSource.PlayPrev();
			return false;
		}
	},
	
	SetNext:function()
	{
		var fmObj=getElementObj(this.btnNext);
		fmObj.FilmSource=this.film;
		fmObj.onclick=function()
		{
			this.FilmSource.PlayNext();
			return false;
		}
	},
	
	FilmBind:function()
	{
		this.Init();
		//this.SetStop();
		this.SetPlay();
		this.SetPrev();
		this.SetNext();	
	}
}

function ImgLink(Name,imgSrc,URL)
{
	this.name=Name;
	this.url=URL;
	this.img=new Image();
	this.img.src=imgSrc;
}

function ImgButton(domEl,imgSrc1,imgSrc2)
{
	this.domEl=domEl;
	this.value=value;
	this.imgMouseOver=imgSrc2;
	this.imgMouse=imgSrc1;
	this.domEl.buttonObj=this;
	this.domEl.onclick=this.clickHandler;
}

function Getelements() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }
  return elements;
}

ImgButton.prototype.clickHandler=function()
{
	
}

function getElementObj(id)
{
    return document.getElementById(id);
}

