﻿
var FilmMovie=new Object();
FilmMovie.loadNum={};
FilmMovie.CreateFilm=function (el,name,pl,frmSize,apply)
{
		this.defaultView=el;		//对应的影片 图片
		this.parent=(pl!=null)?pl:"";		//影片容器
		this.frmSize=frmSize||"";		//获取宽度大小的对象

		this.count=0;	//图片总数
		this.nowFrame=0;		//当前帧
		this.first=true;
		this.status=0;		//影片状态，0停止，1播放,3暂停
		this.name=name;	
		this.spaceTime=5000;	//默认播放时间5秒
		this.excessTime=0.7	;	//变换效果的转换时间
		this.lock=false;	//锁定;

		this.width=0;
		this.height=0;

		this.el=this;
	//	this.tranImg="images/space.gif";
		this.theTimer=null;
		this.imgList=new Array();
		if(apply==true)
			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()
	{
	
		var relSize,width,height;
		if(this.frmSize!="")
			relSize=this.frmSize;
		else
			relSize=this.parent;

		width=Math.floor(parseInt(relSize.clientWidth));
		if(this.frmSize!="")
		{
			height=Math.floor(width*(2.5/4));
			this.parent.style.height=height+"px";
		}
		else
			height=Math.floor(parseInt(relSize.clientHeight));
		this.width=width;
		this.height=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.count==1)
			this.GotoAndStop(1);
		else if(this.status==1)
			this.PlayNext();
	
	},
	Goto:function(frame,isStyle)
	{
		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(!imgObj.complete)//当前图片是否载入,当超时100*1000时表示访问失败
			{
				window.clearInterval(this.theTimer);
				var lab=this.defaultView.id+(this.nowFrame-1);
				if(!FilmMovie.loadNum[lab])	 FilmMovie.loadNum[lab]=0;	//第一次判断				
				if(FilmMovie.loadNum[lab]==-1)
				{					
					if(this.loadError())		//所有图片是否都载入失败
						this.error(0);
					else
						this.theTimer=setTimeout(this.name+".PlayNext()",1);
					return ;
				}
				FilmMovie.loadNum[lab]++;
				
				if(FilmMovie.loadNum[lab]>600)		//载入超过一分钟
				{
					FilmMovie.loadNum[lab]=-1;		//标记载入失败的图片
					this.theTimer=setTimeout(this.name+".PlayNext()",1);
				}
				else
					this.theTimer=setTimeout(this.name+".Goto("+frame+","+isStyle+")",100);
				return ;
			}
			if(this.first==true)		//删除预载入的图片
			{
				//this.defaultView.src=this.loaderImg;
				var pEl=this.defaultView.parentNode;
				var pID=this.defaultView.id;				
				pEl.removeChild(this.defaultView);				
				pEl.innerHTML='<img src="http://www.z6.cn/images/myphoto.gif" border="0" id="'+pID+'"/>';
				this.defaultView=$(pID);
				//this.defaultView.="hidden";
				this.first=false;
			}			
			if(isStyle!=false)
				this.SetTransition();
			this.adjustImgSize();
			this.defaultView.src=imgObj.src;
			if(isStyle!=false)
				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,isStyle)
	{
		window.clearInterval(this.theTimer);
		this.status=0;

		this.Goto(frame,isStyle);	
	},
	GotoAndPlay:function(frame,isStyle)
	{
		this.status=1;
		window.clearInterval(this.theTimer);

		this.Goto(frame);
	},
	PlayPrev:function(isStyle)
	{
		window.clearInterval(this.theTimer);
		this.Prev(isStyle);
	},
	Prev:function(isStyle)
	{
		if(this.nowFrame>1&&this.nowFrame<=this.count)
			this.nowFrame--;
		else
			this.nowFrame=this.count;	
		this.Goto(this.nowFrame,isStyle);
	},	
	PlayNext:function(isStyle)
	{
		window.clearInterval(this.theTimer);
		this.Next(isStyle);
	},
	Next:function(isStyle)
	{
		if(this.nowFrame<this.count)
			this.nowFrame++;
		else
			this.nowFrame=1;
		this.Goto(this.nowFrame,isStyle);
	},
	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="blendTrans(duration=1)";
			this.defaultView.filters.blendTrans.Apply();
		}
	},
	PlayTransition:function ()
	{ 
		if (document.all&&this.defaultView!=null)			
			this.defaultView.filters.blendTrans.play(this.excessTime);
	},
	getParentElementSize:function()
	{
		this.setCotainer();
	},
	resizeScene:function(relObj)
	{
		var RelObj=relObj||this;
		try{
			RelObj.setCotainer();
			RelObj.adjustImgSize();	
		}
		catch(e)
		{
		}
	},
	adjustImgSize:function()
	{
		var 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";
		
//		this.defaultView.style.height="100px";
//		this.defaultView.style.width="200px";
		
	},
	
	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;
		
}

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.PlaySrc!=null)
			{
				tPlay.src=tPlay.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.PauseSrc!=null)
				{
					tPlay.src=tPlay.PauseSrc;
				}
			}
			else
			{
				this.FilmSource.film.Pause();
				
				var tPlay=getElementObj(this.FilmSource.btnPlay);
				if(tPlay!=null&&tPlay.PlaySrc!=null)
				{
					tPlay.src=tPlay.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;
 }
ImgButton.prototype.clickHandler=function()
{
	
}

function getElementObj(id)
{
		return document.getElementById(id);
}