﻿function switchTab(identify,index,count) {
	for(i=0;i<count;i++) {
		var CurTabObj = document.getElementById("Tab_"+identify+"_"+i) ;
		var CurListObj = document.getElementById("List_"+identify+"_"+i) ;
		if (i != index) {
			fRemoveClass(CurTabObj , "upH2") ;
			fRemoveClass(CurListObj , "upBox") ;
		}
	}
	try {
		for (ind=0;ind<CachePic['recommend'][index].length ;ind++ ) {
			var picobj = document.getElementById("recommend_pic_"+index+"_"+ind) ;
			//if (picobj.src == "http://images.movie.xunlei.com/img_default.gif") {
				picobj.src = CachePic['recommend'][index][ind] ;
			//}
		}
	}
	catch (e) {}
	
	fAddClass(document.getElementById("Tab_"+identify+"_"+index),"upH2") ;
	fAddClass(document.getElementById("List_"+identify+"_"+index),"upBox") ;
}

function fAddClass(XEle, XClass) {
  if(!XClass) throw new Error("XClass 不能为空!");
  if(XEle.className!="") {
    var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
    XEle.className = XEle.className.replace(Re, "");
	var OldClassName = XEle.className.replace(/^\s+|\s+$/g,"") ;
	if (OldClassName == "" ) {
		 XEle.className = XClass;
	}
	else {
		XEle.className = OldClassName + " " + XClass;
	}
  }
  else XEle.className = XClass;
}

function fRemoveClass(XEle, XClass) {
  if(!XClass) throw new Error("XClass 不能为空!");
  var OldClassName = XEle.className.replace(/^\s+|\s+$/g,"") ;
  if(OldClassName!="") {
    var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
    XEle.className = OldClassName.replace(Re, "");
  }
}
var pre_searchtype = "search" ;
function changeSearchType(type) {
	if ($("searchType_"+pre_searchtype)) {
		$("searchType_"+pre_searchtype).className = "" ;
	}
	pre_searchtype = type ;
	$("searchType_"+type).className = "currA" ;
	setResType(type) ;
	if ($("searchbox1").value == '搜索超过10万内容') {
		$("searchbox1").value = "" ;
	}
	else {
		$("searchbox1").value = $("searchbox1").value + "" ;
	}
	$("searchbox1").focus() ;
}
function $(item){
	return document.getElementById(item);
}
//document.domain='xunlei.com';
function c_getCookie(sName){
	var sSearch = sName + "=";
	var iOffset = document.cookie.indexOf(sSearch);
	if (iOffset != -1) {
		iOffset += sSearch.length;
		var iEnd = document.cookie.indexOf(";", iOffset);
		if (iEnd == -1)
			iEnd = document.cookie.length;
		return unescape(document.cookie.substring(iOffset, iEnd));
	}else 
		return "";
}
function c_setCookie(sName,sValue,sHours){
	if(arguments.length>2){
		var expireDate=new Date(new Date().getTime()+sHours*3600000);
		document.cookie = sName + "=" + escape(sValue) + "; path=/; domain=xunlei.com; expires=" + expireDate.toGMTString();
	}else
		document.cookie = sName + "=" + escape(sValue) + "; path=/; domain=xunlei.com"; 
}
//向左滚动
ScrollCrossLeft={interval:0,count:0,duration:0,step:0,srcObj:null,callback:null};
ScrollCrossLeft.doit=function(obj,b,c,d){
	var s=ScrollCrossLeft;
	obj.style.marginLeft=cpu(s.count,b,c,d)+'px';
	s.count++;
	if(s.count==d){
		clearInterval(s.interval);
		s.count=0;
		obj.style.marginLeft=b+c+'px';
		s.callback();
	}
	function cpu(t,b,c,d) {return c*((t=t/d-1)*t*t+1)+b;};
}
ScrollCrossLeft.scroll=function(obj,step,span,beign,callback,duration){
	var s=ScrollCrossLeft;
	s.duration=duration;
	s.callback=callback;
	s.interval=setInterval(function(){s.doit(obj,beign,step*span,duration)},10);
}
//首页
var HotPlay={current:0,step:468,a:'a_hp_',div:'div_hp_content',ul:'ul_hp',clickflag:0}
var Movie_red={current:0,step:230,a:'a_m_red_',div:'div_m_red_content',ul:'ul_m_red',clickflag:0}
var Movie_new={current:0,step:230,a:'a_m_new_',div:'div_m_new_content',ul:'ul_m_new',clickflag:0}
var TV_red={current:0,step:230,a:'a_tv_red_',div:'div_tv_red_content',ul:'ul_tv_red',clickflag:0}
var TV_new={current:0,step:230,a:'a_tv_new_',div:'div_tv_new_content',ul:'ul_tv_new',clickflag:0}


var B=BigNews={
	current:0,
	next:0,
	scrollInterval:0,
	autoScroller:0,
	smallpic:"SwitchSmaPic"
};
BigNews.turn=function(index,obj){
	clearInterval(BigNews.autoScroller);
	BigNews.scroll(index,obj);
}
BigNews.scroll=function(index,obj){
	if(obj.smallpic==null || obj.smallpic==""){
		clearInterval(BigNews.autoScroller);
		return;
	}
	var count=0;
	var step=obj.step;
	var duration=16;
	var b=BigNews;
	b.next=index;
	
	if(index==b.current){
		return;
	}

	/*if(index < SmallNews.current){
		SmallNews.go(SmallNews, index);
	} else if (index - 3 >= SmallNews.current) {
		SmallNews.go(SmallNews, index - 3);
	}*/

	clearInterval(b.scrollInterval);
	for(var i=0;i<obj.totalcount;i++){
		$(obj.smallpic+"_"+i).className='';
		if(obj.pictxt!=null && obj.pictxt!="")		
			$(obj.pictxt+"_"+i).style.display = "none" ;
	}
	$(obj.smallpic+"_"+index).className=obj.selectstyle;
	if(obj.pictxt!=null && obj.pictxt!="")	
		$(obj.pictxt+"_"+index).style.display = "block" ;
	var span=index-b.current;
	var begin_value=$(obj.bigpic).scrollTop;
	var chang_in_value=span*step+(b.current*step-begin_value);
	b.scrollInterval=setInterval(function(){doit(begin_value,chang_in_value)},10);
	function doit(b,c){
		$(obj.bigpic).scrollTop=cpu(count,b,c,duration);
		count++;
		if(count==duration){
			clearInterval(BigNews.scrollInterval);
			scrollInterval=0;
			count=0;
			$(obj.bigpic).scrollTop=b+c;
			BigNews.current=index;
		}
	}
	function cpu(t,b,c,d) {return c*((t=t/d-1)*t*t+1)+b;};
}
BigNews.auto=function(obj){
	//clearTimeout(BigNews.autoScroller);
	clearInterval(BigNews.autoScroller);
	BigNews.autoScroller=setInterval(function(){
		BigNews.scroll(BigNews.current==(obj.totalcount-1)?0:BigNews.current+1,obj);
	},obj.autotimeintval);
}
BigNews.pauseSwitch = function() {	
	//clearTimeout(BigNews.autoScroller);
	clearInterval(BigNews.autoScroller);
}
BigNews.init=function(obj){
	$(obj.bigpic).onmouseover = new Function("BigNews.pauseSwitch();") ;		
	$(obj.bigpic).onmouseout = new Function("BigNews.auto("+obj.objname+");") ;	
	for (i=0;i<obj.totalcount;i++) {	
		if(obj.smallpic!=null && obj.smallpic!="") {
		 $(obj.smallpic+"_"+i).onmouseover = new Function("BigNews.turn("+i+","+obj.objname+");BigNews.pauseSwitch();") ;		
		 $(obj.smallpic+"_"+i).onmouseout = new Function("BigNews.auto("+obj.objname+");") ;	
		}
	}
	BigNews.smallpic = obj.smallpic;
	
}



var SmallNews = {
	current:0,
	step:0,
	totalcount:0,
	a_pre:"",
	a_next:"",
	ul:'',
	clickflag:0
};
SmallNews.init=function(obj) {
	SmallNews.step = obj.step;
	SmallNews.totalcount = obj.totalcount;
	SmallNews.a_pre = obj.a_pre;
	SmallNews.a_next = obj.a_next;
	SmallNews.ul = obj.ul;
}
SmallNews.pre=function(){
	if(SmallNews.current<=0){
		return;
	}else{
		var index = SmallNews.current - 1;
		if(BigNews.current > index + 3) {
			$(BigNews.smallpic+"_"+ (index + 3)).onmouseover();
			$(BigNews.smallpic+"_"+ (index + 3)).onmouseout();
		}
		SmallNews.go(SmallNews,SmallNews.current-1);
	}
}
SmallNews.next=function(){
	if(SmallNews.current>=SmallNews.totalcount - 4){
		return;
	}else{
		var index = SmallNews.current + 1;
		if (BigNews.current < index) {
			$(BigNews.smallpic+"_"+index).onmouseover();
			$(BigNews.smallpic+"_"+index).onmouseout();
		} 
			SmallNews.go(SmallNews,SmallNews.current+1);
		
		
	}
}
SmallNews.go=function(obj,index){
	if(obj.current==index){return;}
	var span=-index+obj.current;
	if(obj.clickflag > 0){return;}
	obj.clickflag=1;
	if(obj.step>0)
		ScrollCrossUp.scroll($(obj.ul),obj.step,span,parseInt($(obj.ul).style.marginTop)||0,cb,10);
	else{
		cb();	
	}
	function cb(){
		obj.current=index;
		if(obj.current == 0) {
			$(obj.a_pre).className = "no-pre";
			$(obj.a_next).className = "next";
		}else if(obj.current > 0 && obj.current < obj.totalcount - 4) {
			$(obj.a_pre).className = "pre";
			$(obj.a_next).className = "next";
		}else if(obj.current == obj.totalcount - 4) {
			$(obj.a_pre).className = "pre";
			$(obj.a_next).className = "no-next";
		}
		obj.clickflag=0;
	}
}

//向上滚动
ScrollCrossUp={interval:0,count:0,duration:0,step:0,srcObj:null,callback:null};
ScrollCrossUp.doit=function(obj,b,c,d){
	var s=ScrollCrossUp;
	//obj.style.marginTop=cpu(s.count,b,c,d)+'px';
	s.count++;
	if(s.count>=d){
		clearInterval(s.interval);
		s.count=0;
		//obj.style.marginTop=b+c+'px';
		s.callback();
	}
	function cpu(t,b,c,d) {return c*((t=t/d-1)*t*t+1)+b;};
}
ScrollCrossUp.scroll=function(obj,step,span,beign,callback,duration){
	var s=ScrollCrossUp;
	s.duration=duration;
	s.callback=callback;
	s.interval=setInterval(function(){s.doit(obj,beign,step*span,duration)},10);

}

