/* CB首页焦点图 修改于20090410*/
(function(){
	function $(id){
		return document.getElementById(id);
	}
	function $me(n){
		return document.createElement(n);
	}
	Function.prototype.getHandler = function (obj) { 
		var oThis = this; 
		var argu = Array.prototype.slice.call(arguments, 1); 
		return function (){ 
		oThis.apply(obj, argu); 
		}; 
	}; 
	var slideFocus = {
		jdtName:"hotpic", //焦点图最外层元素id
		width:313, //宽
		height:163,//高
		number:5,  //文章数量
		now:0, //默认停留位，0代表第一个
		time:6000,//切换时间（毫秒）
		wordInterval:null,
		picInterval:null,
		jdtIntervnal:null,
		moveRate:0.5,
		init: function(){
			this.number = longTitle.length; 
			jdt = $(this.jdtName);
			jdt_wrapper = $me("DIV");
			jdt_pics = $me("DIV");
			jdt_titles = $me("DIV");
			tempUL = $me("UL");
			jdt_wrapper.setAttribute("id","jdt_wrapper");
			jdt_pics.setAttribute("id","jdt_pics");
			jdt_pics.style.height = (this.number * 163) + "px";
			jdt_titles.setAttribute("id","jdt_titles");
			jdt_wrapper.appendChild(jdt_pics);
			jdt_wrapper.appendChild(jdt_titles);
			jdt_titles.appendChild(tempUL);
			jdt.appendChild(jdt_wrapper);
			for(var i=0;i<this.number;i++){
				var tempLink = $me("A");
				tempLink.href = linkHref[i];
				tempLink.title = longTitle[i];
				tempLink.id = "jdtP_" + i;
				var tempImg = $me("IMG");
				tempImg.src = picURL[i];
				tempImg.alt = longTitle[i];
				tempImg.width = this.width;
				tempImg.height = this.height;
				tempLink.appendChild(tempImg);
				jdt_pics.appendChild(tempLink);
				var tempLI = $me("LI");
				tempLI.id = "jdtL_" + i;
				if( i == 0 ){
					tempLI.className = "j_on";
					tempLI.style.width = "247px";
					tempLI.innerHTML = "<div id=\"jdtArrow\"><\/div>&nbsp;<b>"+(i+1)+"<\/b>&nbsp;<a href=\""+linkHref[i]+"\">"+longTitle[i] + "</a>";
				}else{
					tempLI.style.width = "15px";
					tempLI.innerHTML = "&nbsp;<b>"+(i+1)+"<\/b>";
				}
				tempUL.appendChild(tempLI);
				tempLI = null;
				$("jdtL_"+i).onclick = this.switchCont.getHandler(null,i);
				$("jdtL_"+i).onmouseover = this.overChangeBg.getHandler(null,i);
				$("jdtL_"+i).onmouseout = this.outChangeBg.getHandler(null,i);
			}
			slideFocus.internalRun();
			jdt_wrapper = null;
			jdt_pics = null;
			jdt_titles = null;
			tempUL = null;
		},
		overChangeBg:function(n){
			var ntab = $("jdtL_"+n);
			if(n != slideFocus.now){
				ntab.style.backgroundColor = "#ce0112";
				ntab.style.opacity =1;
				ntab.style.filter = "alpha(opacity=100)";
			}
		},
		outChangeBg:function(n){
			var ntab = $("jdtL_"+n);
			if(n != slideFocus.now){
				ntab.style.backgroundColor = "#000";
				ntab.style.opacity =0.7;
				ntab.style.filter = "alpha(opacity=70)";
			}
		},
		switchCont:function(n){
			var ntab = $("jdtL_"+n);
			ntab.style.backgroundColor = "#074F86";
			ntab.style.opacity =0.9;
			ntab.style.filter = "alpha(opacity=90)";

			if(n != slideFocus.now){
				$("jdtL_"+slideFocus.now).style.backgroundColor = "#000";
				ntab.style.opacity =0.7;
				ntab.style.filter = 70;
			}
			if( n == slideFocus.now) return;
			clearInterval(slideFocus.jdtInterval);
			clearInterval(slideFocus.picInterval);
			slideFocus.changePicPos(n);
			slideFocus.now = n;
			slideFocus.internalRun();
		},
		changePicPos:function(n){
			var ol = $("jdtL_" + slideFocus.now);	
			var xl = $("jdtL_" + n);
			xl.className = "j_on";	
			ol.className = "";
			xl.style.width = "247px";
			ol.style.width = "15px";
			xl.innerHTML = "<div id=\"jdtArrow\"><\/div>&nbsp;<b>"+(n+1)+"<\/b>&nbsp;<a href=\""+linkHref[n]+"\">"+longTitle[n] + "</a>";
			xl.style.backgroundColor = "#074F86";
			xl.style.opacity =0.9;
			xl.style.filter = "alpha(opacity=90)";
			ol.innerHTML = "&nbsp;<b>"+(slideFocus.now+1)+"<\/b>";
			ol.style.backgroundColor = "#000";
			ol.style.opacity =0.7;
			ol.style.filter = "alpha(opacity=70)";
			$("jdtArrow").style.left = ( 10 + 15 * n )+ "px";
			var jdt_pics = $("jdt_pics");
			var startY = 163 * slideFocus.now * -1;
			var endY = 163 * n * -1;
			var dist = startY - endY;
			
			slideFocus.picInterval = setInterval( function(){

				var alphaNum = 1 - Math.abs( (jdt_pics.offsetTop - endY ) / dist);
				if(alphaNum < 1 ) alphaNum = 1;

					if(document.all){
						var style = jdt_pics.style;
						style.opacity = Math.floor(alphaNum*100);
						style.filter = "alpha(opacity=" + Math.floor(alphaNum*100) + ")";
					}else{
						jdt_pics.style.opacity = alphaNum;
					}


				if(dist > 0 ){ //由下到上
					distMove = (jdt_pics.offsetTop - endY ) * slideFocus.moveRate ;
					if(distMove < 1) distMove = 1;
					jdt_pics.style.top = (jdt_pics.offsetTop + ( (-1) * distMove) ) + "px";
				}else{//由上到下
					distMove = (jdt_pics.offsetTop - endY) * slideFocus.moveRate ;
					if(distMove > 1) distMove = -1;
					jdt_pics.style.top =  jdt_pics.offsetTop - distMove  + "px";
				}
				var actualDist = Math.abs(jdt_pics.offsetTop - endY);
				if( !( actualDist > 0 )){
					jdt_pics.style.top = endY + "px";
					clearInterval(slideFocus.picInterval);
				}
			},50);
		},

		internalRun:function(){
			var num = slideFocus.now + 1;
			if(arguments.length != 0){
				num = n;
			}
			slideFocus.jdtInterval = setInterval(function(){
				if(num == slideFocus.number){ num = 0;}
				clearInterval(slideFocus.picInterval);
				slideFocus.changePicPos(num);
				slideFocus.now = num;
				num++;
			},slideFocus.time);
		}
	};
	slideFocus.init();
})();