﻿/*
 * last modified by cdchu
 * 2008-03-28
 */

$$.module.adpic={};

Ctrip.module.adpic=function(ele){
	var links=$$.module.adpic.href;
	var images=$$.module.adpic.src;
	if(!links||!images)
		return;

	var today=new Date();
	for(var i=0;i<links.length;i++)
		links[i]=links[i].replace(/=\+(\d+)/gi,function(s0,s1){
			return '='+dateAdd(today,parseInt(s1));
		});

	var button={
		width:16,
		height:16,
		image:'',
		distance:8,
		right:8,
		bottom:8,
		fontSize:11,
		fontFamily:'arial',
		alpha:100
	};
	var b=$$.module.adpic.button||{};
	for(var s in b)
		button[s]=b[s];
	var div=ele.parentNode;
	
	var offWidth=ele.offsetWidth;
	var offHeight=ele.offsetHeight;
	var target=ele.target||'_top';
	
	div.style.position='relative';
	div.style.textAlign='left';
	div.innerHTML='';
	ele=null;

	var pos=-1,atimer=0,gtimer=0,alpha=0,fore=0,back=0;
	var lnk=document.createElement('a');
	var cnt=links.length,lod=0;
	var img=new Array(cnt),btn=new Array(cnt);

	var bsty={
		top:(offHeight-button.height-button.bottom)+'px',
		width:button.width+'px',
		height:button.height+'px',
		//font:'bold '+button.fontSize+'px/'+button.height+'px '+button.fontFamily,
		//color:'#ffffff',
		textAlign:'center',
		position:'absolute',
		cursor:'pointer',
		display:'none',
		overflow:'hidden',
		zIndex:3
	};
	var msty='width:'+offWidth+'px;height:'+offHeight+'px;left:0;top:0;border:none;display:none;position:absolute;filter:alpha(opacity=100)';
	for(var i=0;i<links.length;i++){
		btn[i]=document.createElement('div');
		bsty.left=offWidth-button.right-cnt*button.width-(cnt-1)*button.distance+i*(button.width+button.distance)+'px';
		if(button.image)
			bsty.backgroundImage='url("'+button.image+'")';
		else
			btn[i].innerHTML=i+1;
		for(var s in bsty)
			btn[i].style[s]=bsty[s];
		//cdchu
		btn[i].className="page_item";
		//cdchu
		hilit(i,false);
		btn[i]._index=i;
		btn[i].onclick=click;
		lnk.appendChild(btn[i]);
		img[i]=document.createElement('img');
		img[i].style.cssText=msty;
		img[i].onload=load;
		img[i].src=images[i];
		lnk.appendChild(img[i]);
	}

	lnk.style.cssText='display:block;position:absolute;left:0px;top:0px;text-decoration:none';
	lnk.target=target;
	div.appendChild(lnk);
	lnk.href=links[0];
	img[0].style.display='block';

	function load(){
		this.onload=null;
		if(cnt<2||++lod!=cnt)
			return;
		for(var i=0;i<cnt;i++)
			btn[i].style.display='block';
		setCurrent(0);
	}
	function click(evt){
		if(back!=this._index){
			clearInterval(atimer);
			clearTimeout(gtimer);
			animate(this._index);
		}
		lnk.blur();
		return false;
	}
	function animate(newpos){
		if(newpos<0||arguments.length==0)
			newpos=(pos+1)%cnt;
		setForeBack(pos,newpos);
		atimer=setInterval(function(){
			setAlpha(alpha-3);
			if(alpha>=70&&alpha<73)
				setCurrent(newpos);
			else if(alpha==0)
				clearInterval(atimer);
		},15);
	}
	function setForeBack(newfore,newback){
		img[fore].style.display='none';
		img[back].style.display='none';
		fore=newfore;
		back=newback;
		setAlpha(100);
		setAlpha(100,img[back]);
		with(img[fore].style){
			display='block';
			zIndex=2;
		}
		with(img[back].style){
			display='block';
			zIndex=1;
		}
	}
	function setAlpha(n,im){
		alpha=n<0?0:n;
		im=im||img[fore];
		if(im.filters)
			im.filters[0].opacity=alpha;
		else
			im.style.opacity=alpha/100;
	}
	function setCurrent(newpos){
		if(pos==newpos)
			return;
		if(pos>=0)
			hilit(pos,false);
		pos=newpos;
		hilit(pos,true);
		lnk.href=links[pos];
		gtimer=setTimeout(animate,4000,-1);
	}
	function hilit(p,b){
		var st=btn[p].style;
		//cdchu
		btn[p].className=b?"page_item current_page_item":"page_item";
		return;
		//cdchu
		if(button.image)
			st.backgroundPosition=-p*button.width+'px '+(b?-button.height:0)+'px';
		else
			st.backgroundColor=b?'orange':'#999999';
		if(button.alpha==100)
			return;
		if("filters" in btn[p])
			st.filter='alpha(opacity='+(b?100:button.alpha)+')';
		else
			st.opacity=b?'0.9999':(button.alpha)/100;
	}
	function dateAdd(dt,n){
		var d=new Date(dt.getTime());
		d.setDate(d.getDate()+n);
		return [d.getFullYear(),d.getMonth()+1,d.getDate()].join('-');
	}
}