function G(obj)
{
	return typeof(obj)=='object'?obj:document.getElementById(obj);
}
Array.prototype.addBefore=function(arr)
{
	if(this==[]) return arr;
	else return arr.concat(this);
}
Array.prototype.addAfter=function(arr)
{
	if(this==[]) return arr;
	else return this.concat(arr);
}
Array.prototype.indexOf=function(substr,start){
	var ta,rt,d='\0';
	if(start!=null){ta=this.slice(start);rt=start;}else{ta=this;rt=0;}
	var str=d+ta.join(d)+d,t=str.indexOf(d+substr+d);
	if(t==-1)return -1;rt+=str.slice(0,t).replace(/[^\0]/g,'').length;
	return rt;
}

function MM_swapImgRestore() 
{
  var i,x,a=document.MM_sr; 
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) 
{
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() 
{
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

ScrollImg=function(coverObj, imgNodeBox, contentFrame){
	_me=this;
	this.imgNodeBox=G(imgNodeBox);
	this.contentFrame=G(contentFrame);
	this.isContinue=false;
	this.imgIDList=[];
	this.obj=G(coverObj);
	this.oneStep=85;	//
	this.allSkip=0;				//
	this.goNum=1;				//
	this.skip=5;				//
	this.skipTime=10;			//
	this.skipSum=0;
	this.direction=1;
	this.selNode=null;
	this.getNode=null;
	this.addLen=0;
	this.bnt1=null;
	this.bnt2=null;
	this.disBnt=0;
	this.lastIsEnd=0;			//加载的图片中最后一张是否是"所有图片中最后一张图片"
	this.nowPicSn=0;
	this.allPicNum=0;
	this.isGoing=false;
	this.varminleft = -255;
	
	var isLoading=false;
	
	this.clear=function()
	{
		this.skipSum=0;
		this.allSkip=0;
		this.goNum=1;
		this.isGoing=false;
	}

	//滚动到下组图片后停止
	this.scroll=function()
	{	
		//滚动按钮默认不变灰  add by borce
		this.bnt1.style.filter="";
		this.bnt2.style.filter="";
		if(_me.checkStat())
		{
			//向前到本相册头, stop.  向后到 所有 相册尾||下一相册, stop.
			if(this.imgIDList[0].picSn<=1 && this.direction==-1)
			{	
				//点左按钮到尽头变灰  add by borce
				this.bnt1.style.filter="progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);";
				this.clear();
				this.disableBnt(-1);
				return false;
			}
			
			if(this.lastIsEnd && this.direction==1)
			{
				//点右按钮到尽头变灰  add by borce
				this.bnt2.style.filter="progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);";
				this.clear();
				this.disableBnt(1); 
				return false;
			}
			if(isLoading) return;
			isLoading=true;
			return false;
		}
		if(this.disBnt) this.enableBnt(this.disBnt);
		_me.allSkip=_me.oneStep*_me.goNum;
		if(_me.skipSum<_me.allSkip)
		{
			var nowLeft=parseInt(_me.obj.style.left);
			_me.obj.style.left=(nowLeft-_me.skip*_me.direction)+'px';
			_me.skipSum+=_me.skip;
			//alert(_me.obj.style.left+" "+_me.skipSum);
			setTimeout('_me.scroll()',_me.skipTime);
		}
		else
		{
			_me.clear();
			_me.scrollEnd();
		}
	}
	
	this.scrollEnd=function()
	{
	}
	
	this.setCnt=function(arg)
	{
		this.isContinue=arg;
	}
	
	this.go=function(dirc)
	{
		this.isGoing=true;
		this.goNum=1;
		this.direction=dirc;
		this.scroll();
		this.canGoon(this.goNum*this.oneStep/this.skip*this.skipTime);
		this.scrollEnd=function()
		{
			if(this.isContinue) this.scroll();
		}
	}
	
	this.sel=function(obj)
	{
		var node=obj;
		this.selScroll(node);
	}
	
	this.selScroll=function(selNode)
	{
		var node=selNode;
		var imgs=this.obj.getElementsByTagName('img');
		var iLen=imgs.length;
		var nodeNum=-1;
		for(var i=0;i<iLen;i++)
		{
			if(imgs[i]==node) {nodeNum=i; imgs[i].parentNode.className='selImg';}
			else if(imgs[i].parentNode.className!='albumImg') imgs[i].parentNode.className='spanImg';
		}

		this.gotoUrl(nodeNum);
	}
	
	this.checkStat=function()
	{
		var objLeft=parseInt(this.obj.style.left);
		var minLeft=this.minLeft();
		var imgs=this.obj.getElementsByTagName('img');
		if(this.direction==-1 && objLeft+1>0) {this.getNode=imgs[0]; return true;}
		if(this.direction==1 && objLeft-1<minLeft) {this.getNode=imgs[imgs.length-1]; return true;}
		//这里的1为故意增加的余量,防止判断过度
		return false;
	}
	
	//得到拼装的图片总的长度
	this.minLeft=function()
	{
		return this.varminleft;//this.oneStep*(this.obj.getElementsByTagName('img').length-1)*(-1);
	}
	
	this.addData=function(imgList)
	{
		this.addLen=imgList.length;
		if(this.direction==1) this.imgIDList=this.imgIDList.addAfter(imgList);
		else this.imgIDList=this.imgIDList.addBefore(imgList);	
		this.initNode(this.selNode.id);
	}
	
	this.initNode=function(selImgID)
	{
		while(this.imgNodeBox.firstChild)
			this.imgNodeBox.removeChild(this.imgNodeBox.firstChild);

		var iLen=this.imgIDList.length;
		for(var i=0;i<iLen;i++)
		{
			var nNode=this.imgIDList[i];
			var tempNode=null;
			tempNode=this.createNode(nNode);
			this.imgNodeBox.appendChild(tempNode);
		}
		this.init(selImgID||this.selNode.id);
		this.addLen=0;
	}

	this.init=function(imgID)
	{
		var imgid=imgID||this.selNode.id;
		var imgs=this.obj.getElementsByTagName('img');
		var iLen=imgs.length;
		for(var i=0;i<iLen;i++)
		{
			if(imgs[i].id==imgid) 
			{
				imgs[i].parentNode.className='selImg'; this.selNode=imgs[i];
				if(!isLoading) 
				{
					if(i>2 && iLen-i>2)  this.obj.style.left=(-1)*(i-2)*this.oneStep+'px';
					if(i<=2) this.obj.style.left="0px";
					else if(iLen-i<=2) this.obj.style.left=(-1)*this.oneStep*(iLen-1)+'px';
				}
				var objLeft=parseInt(this.obj.style.left);
				var minLeft=this.minLeft();
				if(objLeft+1>0 && this.imgIDList[0].picSn==1){ this.disableBnt(-1);}
				else if(objLeft-1<minLeft && this.lastIsEnd){this.disableBnt(1);}
			}
			else if(imgs[i].parentNode.className!='albumImg') imgs[i].parentNode.className='spanImg';
			imgs[i].onclick=function(evt){ if(_me.isGoing) return; else _me.sel(this);}
			imgs[i].parentNode.onclick=function(){if(_me.isGoing) return; else _me.sel(this.getElementsByTagName('img')[0]);}
		}
		
		if(this.addLen) if(this.direction==-1) this.obj.style.left=(-1)*this.addLen*this.oneStep+'px';
		else if(this.direction==1) this.obj.style.left=(-1)*(this.imgIDList.length-this.addLen-1)*this.oneStep+'px';
		
		if(isLoading) { isLoading=false; this.scroll();}
	}
	
	this.createNode=function(nNode)
	{
		var rNode=document.createElement('td');
		rNode.width='86';
		rNode.height='49';
		var imgID=nNode.imgID;
		rNode.id = 'td'+imgID;
		var nImgsrc="images/"+nNode.imgID+".jpg";
		//var nImgsrcSwap="images/"+nNode.imgID+"a.jpg";
		var nImg="<DIV><IMG id='"+imgID+"' name='"+imgID+"' width='86' height='49' src='"+nImgsrc+"'></DIV>";

		rNode.innerHTML=nImg;
		return rNode;
	}

	this.gotoUrl=function(nodenum)
	{
		this.contentFrame.src = this.imgIDList[nodenum].goUrl;
		document.getElementById("adTitle").innerText = this.imgIDList[nodenum].title;
		//全部按钮恢复原色 add by borce 
		var iLen=this.imgIDList.length;
		for(var i=0;i<iLen;i++)
		{
			var nNode=this.imgIDList[i];
			document.getElementById(nNode.imgID).src="images/" + nNode.imgID + ".jpg";
		}
		//点中按钮变色 add by borce
		document.getElementById(this.imgIDList[nodenum].imgID).src="images/"+this.imgIDList[nodenum].imgID+"a.jpg";
	}

	this.bntPicName=function(Isrc)
	{
		var srcArr=Isrc.split('/');
		return srcArr[srcArr.length-1];
	}
	
	this.setBnt=function(bnt1,bnt2)
	{
		this.bnt1=G(bnt1);
		this.bnt2=G(bnt2);
		if (this.bnt1 && this.bnt2) {
			this.bandBnt()
		};
	}
	
	this.bandBnt=function()
	{
		this.bnt1.onmouseover	=function(){_me.bnt1.style.cursor='hand'; }
		this.bnt1.onmouseout	=function(){_me.bnt1.style.cursor='auto'; }
		this.bnt2.onmouseover	=function(){_me.bnt2.style.cursor='hand'; }
		this.bnt2.onmouseout	=function(){_me.bnt2.style.cursor='auto'; }
		
		this.bnt1.onmousedown	=function(){if(_me.isGoing) return; _me.go(-1); _me.setCnt(1);  return false;}
		this.bnt1.ondragstart	=function(){return false;}
		this.bnt1.onmouseup		=function(){_me.setCnt(0);}
		this.bnt2.onmousedown	=function(){if(_me.isGoing) return;  _me.go(1); _me.setCnt(1); return false;}
		this.bnt2.ondragstart	=function(){return false;}
		this.bnt2.onmouseup		=function(){_me.setCnt(0);}

		document.onmouseup=function(){_me.setCnt(0);}
	}
	
	this.enableBnt=function(dBnt)
	{
		switch(dBnt)
		{
			case -1:
				//this.bnt1.src=this.bnt1.src.replace('h_prev_dis.gif','h_prev_no.gif');  
				this.bnt1.title=''; 
				this.bnt1.style.cursor='auto'; 
				break;
			case 1 :
				//this.bnt2.src=this.bnt2.src.replace('h_next_dis.gif','h_next_no.gif');  
				this.bnt2.title=''; 
				this.bnt2.style.cursor='auto'; 
				break;
		}
	}
	
	this.disableBnt=function(direc)
	{
		//direc, 哪个按钮, -1 向前, 1向后
		if(!direc) return;
		if(!this.bnt1 || !this.bnt2) return;
		this.disBnt=direc;
		switch(direc)
		{
			case -1: 
					this.bnt1.title='已到達第一項'; this.bnt1.style.cursor='default'; break;
			case 1:
					this.bnt2.title='已到達最後一項'; this.bnt2.style.cursor='default'; break;
		}
	}

	this.banNode = function(node)
	{
		if(node == null || node == "")
			return;
		G(node).style.display = "none";
		//this.imgNodeBox.removeChild(this.imgNodeBox.childNodes[num]);
		this.varminleft = -170;
	}

	this.canGoon=function(t)
	{
		this.isGoing=true;
		setTimeout("_me.isGoing=false;",t);
	}
	
	var keypress=false;
}
