$(function(){
    //鲜花榜、人气榜和活跃用户Tab
    $('.rankList').each(function(i,e){
        var _this = $('.rankList').eq(i);
        _this.find('.rankListTitle a').click(function(){
            var _self = _this.find('.rankListTitle a')
            var _table = _this.find('.rankListCon table');
            var index = _self.index(this);
            _self.removeClass('current');
            $(this).addClass('current').blur();
            _table.hide();
            _table.eq(index).show();
        });    
    });

    //活跃用户extend
    $('.rankListCon .extendTable').each(function(i){
        var _this = $(this);
        var ex = _this.find('tr:not(.tr):not(.tr2)');
        ex.mouseover(function(){
            ex.removeClass('expand');
            $(this).addClass('expand');
        });
    });

    //限制输入框字符
    $('.bsSearch .inputTxt').keyup(function(){
        var maxcount = 20;  //字符数
        var _this = $(this).get(0);
        if (_this.value.length > maxcount) {
            _this.value = _this.value.substr(0, maxcount);
        }
    });

})

	function totalTabs_card(tabID){
		document.getElementById(tabID).style.display = "block";
		var hpTabs = new Array();
		hpTabs[0] = "day_click_card";
		hpTabs[1] = "week_click_card";
        hpTabs[2] = "month_click_card";	
		var i;
		for (i=0;i<hpTabs.length;i++){
			if(tabID != hpTabs[i]){
                document.getElementById(hpTabs[i]).style.display = "none";
			}
		}
	}
///////////////////////////////////////////////////////////////////////////
	function game_Choose(tabID){
		document.getElementById(tabID).style.display = "block";
		var hpTabs = new Array();
		hpTabs[6] = "hot_game";hpTabs[7] = "a_d";
		hpTabs[8] = "e_h";hpTabs[9] = "i_l";
		hpTabs[10] = "m_p";hpTabs[11] = "q_t";	
		hpTabs[12] = "u_x";hpTabs[13] = "y_z";	
		var i;
		for (i=6;i<hpTabs.length;i++){
			if(tabID != hpTabs[i]){
				document.getElementById(hpTabs[i]).style.display = "none";
				document.getElementById(hpTabs[i] + "_btn").className="";
			}else{
				document.getElementById(hpTabs[i] + "_btn").className="span1 sp1";}
				if (i==7 && i==8 && i==9 &&  i==10 && i==11 && i==12) {
					document.getElementById(hpTabs[i] + "_btn").className="span2 sp2";
				}
			    if (i==13){
				   document.getElementById(hpTabs[i] + "_btn").className="span3 sp3";
				   }		
			    }
	}
	function bluetime_Tabs(tabID){
		document.getElementById(tabID).style.display = "block";
		var hpTabs = new Array();
		hpTabs[0] = "hour";
		hpTabs[1] = "week";	
		hpTabs[2] = "month";	
		var i;
		for (i=0;i<hpTabs.length;i++){
			if(tabID != hpTabs[i]){
				document.getElementById(hpTabs[i]).style.display = "none";
			}
		}
	}
	function glasstime_Tabs_card(tabID){
		document.getElementById(tabID).style.display = "block";
		var hpTabs = new Array();
		hpTabs[3] = "day_flower_card";
		hpTabs[4] = "week_flower_card";	
		hpTabs[5] = "month_flower_card";	
		var i;
		for (i=3;i<hpTabs.length;i++){
			if(tabID != hpTabs[i]){
				document.getElementById(hpTabs[i]).style.display = "none";
			}
		}
	}


//show and hide
function exChange(hideID,showID,tipsID){
	var status = [["visible","隐藏>>","lessb"],["hidden","查看详细>>","moreb"]];
	var node = document.getElementById(hideID);	
	var node1 = document.getElementById(showID);	
	var tips = document.getElementById(tipsID);
	
	var exNum = node.className == "hidden" ? 0 : 1 ; {
		node.className = status[exNum][0];
		//tips.firstChild.setAttribute("src", status[exNum][1]);
		tips.innerHTML = status[exNum][1];
		tips.className = status[exNum][2];
		var exNum1 = node1.className == "hidden" ? 0 : 1 ; {
			node1.className = status[exNum1][0];	
		}
	}	
}
function dialog(name,show)
	{	
		var div = document.getElementById(name);
		if (show){
			div.style.display='block';
		}
		else{
			div.style.display='none';
		}
}

function hidetips(obj)
{
	if(obj.getAttribute('value')==obj.defaultValue)
	obj.value='';
}

function redirect(url) {self.parent.location.replace(url);}

function expand(obj){
	
	  for(i=0;i<=9;i++){
		var item = document.getElementById("item_"+i);
		if (item) item.className = "";
	  }
   obj.className = "expand";
}
function expand1(obj){
	
	  for(i=0;i<=9;i++){
		var item = document.getElementById("item1_"+i);
		if (item) item.className = "";
	  }
   obj.className = "expand";
}
function expand2(obj){
	
	  for(i=0;i<=9;i++){
		var item = document.getElementById("item2_"+i);
		if (item) item.className = "";
	  }
   obj.className = "expand";
}

if(typeof(jQuery)!='undefined'){
	jQuery.copy=function(t,msg){
		if(typeof t=='undefined') t='';
		d=document;
		if (window.clipboardData){
			window.clipboardData.setData('Text',t);
		}
		else{
			var f='flashcopier';
			if(!d.getElementById(f)){
				var dd=d.createElement('div');
				dd.id=f;
				d.body.appendChild(dd);
			}
			d.getElementById(f).innerHTML='';
			var i='<embed src="js/copy.swf" FlashVars="clipboard='+encodeURIComponent(t)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
			d.getElementById(f).innerHTML=i;
		}
		 alert(msg);
	}
}

function boygirl_Tabs(tabID){
		document.getElementById(tabID).style.display = "block";
		var hpTabs = new Array();
		hpTabs[6] = "glory";
		hpTabs[7] = "tag";	
		hpTabs[8] = "vote";
		var i;
		for (i=6;i<hpTabs.length;i++){
			if(tabID != hpTabs[i]){
				document.getElementById(hpTabs[i]).style.display = "none";
				document.getElementById(hpTabs[i] + "_btn").className="";
			}else{
				if(i == 6)classgb = "dqian";
				else if(i == 7){classgb = "dqian dqiangg";}
				else if(i == 8){classgb = "dqian dqianshu";}
				document.getElementById(hpTabs[i] + "_btn").className= classgb;
				}
		}
	}

//加载图片函数
function onload_img(url,callback,t)
{
	var img = new Image();
	
	img.ttime = t;
	img.ttitle = url['title'];
	img.turl = url['url'];
	img.tclick = url['click'];
	img.src = url['middle'];
	//img.ttitlelen = url['titleLen'];
	
	if(img.complete)
	{
		callback.call(img);
		return ;
	}
	
	img.onload = function()
	{
		callback.call(img);
	};
}
		
var result =false;
var b;
var c;
var idKey=0;
	
function rollText(idKeys)
{
	idKey = parseInt(idKeys)+parseInt(idKey);	
	mLength = myObject.length;	//判断有几页
	
	if(idKey >= mLength)
	{
		idKey = 0;		//如果是最后一页，点下一页，从新回到第一页
	}
	if(idKey < 0)
	{
		idKey = mLength-1;		//如果是第一页，点上一页，从回到最后一页
	}
	
	for(j=0;j<=2;j++)
	{
		for(k=0;k<2;k++)
		{
			try
			{
				var tt = myObject[idKey][j][k]['title'];	//检查一下是否有值。没有就出错，隐藏它
				result = true;
			}
			catch(err)	//出错就隐藏他们，没有值
			{
				result  = false;
				var t = j*2+1+k;
				t = "a"+t;
				$("#"+t).hide();	
			}
			
			if(result == true)
			{
				var t2 = j*2+1+k;
				$("#a"+t2).show();
				onload_img(myObject[idKey][j][k],showImg,t2);	//去load图片		
			}
		}
	}
}	

function showImg()
{
	imgUrl = this.src;		//源图片
	imgId = this.ttime;		//图片对应id
	imgTitle = this.ttitle;	//标题
	imgwUrl = this.turl;	//作品地址
	imgClick = this.tclick;	//人气
	//imgTitleLen = this.ttitlelen;
	
	$("#c"+imgId).attr("src",imgUrl);
	$("#c"+imgId).attr("title",imgTitle);
	$("#a"+imgId+" p:eq(0) a").attr("href",imgwUrl);
	$("#a"+imgId+" p:eq(1) a").html(imgTitle);
	//alert(imgTitle);
	$("#a"+imgId+" p:eq(1) a").attr("title",imgTitle);
	$("#a"+imgId+" p:eq(1) a").attr("href",imgwUrl);
	$("#a"+imgId+" p:eq(2) span").html(imgClick);
	
}

//发短信
function sendSms(bid)
{
	var url = 'http://www.aipai.com/sms.php?action=send&toid='+bid;
	window.open (url, "发短信");
}

//去投票
function gotoVote(param)
{
	var url = 'http://www.aipai.com/vote.php?action=voteAdd';
	window.open (url, "投票");	
}

//文字滚动
function startmarquee(lh,speed,delay,idName) { 
	var p=false; 
	var t; 
	var o=document.getElementById(idName); 
	o.innerHTML+=o.innerHTML; 
	o.style.marginTop=0; 
	o.onmouseover=function(){p=true;} 
	o.onmouseout=function(){p=false;} 
	
	function start()
	{ 
		t=setInterval(scrolling,speed); 
		if(!p) o.style.marginTop=parseInt(o.style.marginTop)-1+"px"; 
	}
	
	function scrolling()
	{ 
		if(parseInt(o.style.marginTop)%lh!=0){ 
		o.style.marginTop=parseInt(o.style.marginTop)-1+"px"; 
		if(Math.abs(parseInt(o.style.marginTop))>=o.scrollHeight/2) o.style.marginTop=0; 
		}else{ 
		clearInterval(t); 
		setTimeout(start,delay);
		}
	}
	
	
	setTimeout(start,delay); 
}
//startmarquee(22,20,1500); 

if ( "undefined" == typeof(EFWS) || !EFWS ) {
	var EFWS = {};
}

//load script without blocking
EFWS.Script = {
	loadScript: function(url, onload) {
		EFWS.Script.loadScriptDomElement(url, onload);
	},

	loadScripts: function(aUrls, onload) {
		// first pass: see if any of the scripts are on a different domain
		var nUrls = aUrls.length;
		var bDifferent = false;
		for ( var i = 0; i < nUrls; i++ ) {
			if ( EFWS.Script.differentDomain(aUrls[i]) ) {
				bDifferent = true;
				break;
			}
		}

		// pick the best loading function
		var loadFunc = EFWS.Script.loadScriptXhrInjection;
		if ( bDifferent ) {
			if ( -1 != navigator.userAgent.indexOf('Firefox') || 
				 -1 != navigator.userAgent.indexOf('Opera') ) {
				loadFunc = EFWS.Script.loadScriptDomElement;
			}
			else {
				loadFunc = EFWS.Script.loadScriptDocWrite;
			}
		}

		// second pass: load the scripts
		for ( var i = 0; i < nUrls; i++ ) {
			loadFunc(aUrls[i], ( i+1 == nUrls ? onload : null ), true);
		}
	},

	differentDomain: function(url) {
		if ( 0 === url.indexOf('http://') || 0 === url.indexOf('https://') ) {
			var mainDomain = document.location.protocol + 
				"://" + document.location.host + "/";
			return ( 0 !== url.indexOf(mainDomain) );
		}
		
		return false;
	},

	loadScriptDomElement: function(url, onload) {
		var domscript = document.createElement('script');
		domscript.src = url;
		if ( onload ) {
			domscript.onloadDone = false;
			domscript.onload = function() { 
				domscript.onloadDone = true; 
				onload(); 
			};
			domscript.onreadystatechange = function() {
				if ( "loaded" === domscript.readyState && !domscript.onloadDone ) {
					domscript.onloadDone = true;
					domscript.onload();
				}
			}
		}
		document.getElementsByTagName('head')[0].appendChild(domscript);
	},

	loadScriptDocWrite: function(url, onload) {
		document.write('<scr' + 'ipt src="' + url + 
					   '" type="text/javascript"></scr' + 'ipt>');
		if ( onload ) {
			// we can't tie it to the script's onload, so use window
			// thus, it doesn't fire as early as it might have
			EFWS.addHandler(window, "load", onload);
		}
	},

	queuedScripts: new Array(),

	loadScriptXhrInjection: function(url, onload, bOrder) {
		var iQueue = EFWS.Script.queuedScripts.length;
		if ( bOrder ) {
			var qScript = { response: null, onload: onload, done: false };
			EFWS.Script.queuedScripts[iQueue] = qScript;
		}

		var xhrObj = EFWS.Script.getXHRObject();
		xhrObj.onreadystatechange = function() { 
			if ( xhrObj.readyState == 4 ) {
				if ( bOrder ) {
					EFWS.Script.queuedScripts[iQueue].response = xhrObj.responseText;
					EFWS.Script.injectScripts();
				}
				else {
					var se = document.createElement('script');
					document.getElementsByTagName('head')[0].appendChild(se);
					se.text = xhrObj.responseText;
					if ( onload ) {
						onload();
					}
				}
			}
		};
		xhrObj.open('GET', url, true);
		xhrObj.send('');
	},

	injectScripts: function() {
		var len = EFWS.Script.queuedScripts.length;
		for ( var i = 0; i < len; i++ ) {
			var qScript = EFWS.Script.queuedScripts[i];
			if ( ! qScript.done ) {
				if ( ! qScript.response ) {
					// STOP! need to wait for this response
					break;
				}
				else {
					var se = document.createElement('script');
					document.getElementsByTagName('head')[0].appendChild(se);
					se.text = qScript.response;
					if ( qScript.onload ) {
						qScript.onload();
					}
					qScript.done = true;
				}
			}
		}
	},

	getXHRObject: function() {
		var xhrObj = false;
		try {
			xhrObj = new XMLHttpRequest();
		}
		catch(e){
			var aTypes = ["Msxml2.XMLHTTP.6.0", 
						  "Msxml2.XMLHTTP.3.0", 
						  "Msxml2.XMLHTTP", 
						  "Microsoft.XMLHTTP"];
			var len = aTypes.length;
			for ( var i=0; i < len; i++ ) {
				try {
					xhrObj = new ActiveXObject(aTypes[i]);
				}
				catch(e) {
					continue;
				}
				break;
			}
		}
		finally {
			return xhrObj;
		}
	}
};

EFWS.addHandler = function(elem, type, func) {
	if ( elem.addEventListener ) {
		elem.addEventListener(type, func, false);
	}
	else if ( elem.attachEvent ) {
		elem.attachEvent("on" + type, func);
	}
};

function beiTan()
{
	var bt_minTime = 60*10*1000;
	var bt_maxNum = 3;

	if(document.referrer.indexOf('video.baidu') > 0)
	{
	}
	else
	{
		var bid = Cookies.get("b");
	
		if (bid==null || bid.length <= 0)
		{
			var btTime = Cookies.get("ad_bt");
			btTime = btTime?btTime:0;

			var btNum = Cookies.get("ad_bt_num");
			btNum = btNum?btNum:0;
			
			if ((btTime == 0) && (btNum < bt_maxNum))
			{
				/*
				var btWho = Math.floor(Math.random()*10);
				if ((btWho % 2) == 0)
				{
					document.writeln("<script language=\"javascript\" src=\"http://pop.myiee.com/cri/pop2.php?uid=14018&ads=10135\"><\/script>");	
				}
				else
				{
					document.writeln("<script language=\"javascript\" src=\"http://www.UnionBig.com/C.Html?P=1986&T=1&A=&k=2&Z=285&S=3600&N=1\"><\/script>");
				}		
				*/
				
				btNum++;

				var date= new Date();			
				var nowTime = date.getTime();

				var exptime = nowTime + bt_minTime;
				var expdate = new Date();
				expdate.setTime(exptime);
		
				Cookies.set('ad_bt', '1', expdate, '/', 'aipai.com');

				var exptime = nowTime - (date.getHours()*3600+date.getMinutes()*60+date.getSeconds())*1000 + 86400*1000;
				expdate.setTime(exptime);
		
				Cookies.set('ad_bt_num', btNum, expdate, '/', 'aipai.com');
			}
		}
	}
}


//游戏分页
function scroll(n,id)
{	
	handle = document.getElementById(id);	
	handle.scrollTop = handle.scrollTop+n;
	
	if(handle.scrollTop == 24)
	{
		$("#"+id).append("<br /><br /><br /><br />");
		handle.scrollTop = handle.scrollTop+n;
	}
	
	if(handle.scrollTop == 48)
	{
		$("#"+id).append("<br /><br /><br />");
		handle.scrollTop = handle.scrollTop+n;
	}
	
	if(handle.scrollTop == 72)
	{
		$("#"+id).append("<br /><br />");
		handle.scrollTop = handle.scrollTop+n;
	}									
}

function UrlDecode(str){ 
	var ret=""; 
	for(var i=0;i<str.length;i++){ 
	   var chr = str.charAt(i); 
		if(chr == "+"){ 
		  ret+=" "; 
		}else if(chr=="%"){ 
		 var asc = str.substring(i+1,i+3); 
		 if(parseInt("0x"+asc)>0x7f){ 
		  ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6))); 
		  i+=5; 
		 }else{ 
		  ret+=asc2str(parseInt("0x"+asc)); 
		  i+=2; 
		 } 
		}else{ 
		  ret+= chr; 
		} 
	} 
	return ret; 	
}

function doFromBaidu()
{
	var url = document.referrer;
	//var url = "http://video.baidu.com/v?word=cf%CA%D3%C6%B5&ct=301989888&rn=20&pn=0&db=0&s=0&fbl=1024&oq=cf&f=3&rsp=0";

	if ((url.indexOf("baidu.com") == -1))
	{
		url = Cookies.get("from");
	}

	if (url == null)
	{
		url = 'no refer' ;
		//return;
	}

	 try
	 {
		//测试cookie
		var date= new Date();			
		var nowTime = date.getTime();
		var expdate = new Date();
		var exptime = nowTime + 9000;
		expdate.setTime(exptime);
		Cookies.set('jsTestTemp', url, expdate, '/', 'aipai.com');	
	 }
	 catch (err)
	 {
	 }
	
	if ((url.indexOf("baidu.com") != -1))
	{	
		var re,r,r_str;
		re = /.*word=([^&]*)\&.*/;
		r = url.match(re); // 尝试匹配搜索字符串。  
			
		if (r != null)
		{	
			try
			{
				r_str = UrlDecode(r[1]);
				$().ready(function(){
					$("#frombaidu_i_searchkey").val(r_str);
					$("#frombaidu_searchkey").html(r_str);
					//随机数	(1000到9999)
					var number = Math.random();
					number = (Math.ceil(number*10))*1000+Math.ceil(number*1000);
					$("#search_num").html(number);
					//显示百度框
					$("#frombaidu_div").show();
					Cookies.clear('from');

					//调用播放器的函数
					try
					{
						document["player"].setKeyWords(r_str);
					}
					catch (err)
					{
						//window["player"].setKeyWords(r_str);
					}
				})
			}
			catch (err)
			{
				//alert(url);
			}
		}
	}
}
