var isIE = (navigator.userAgent.indexOf("MSIE")!=-1) ? 1 : 0;
var isFF = (navigator.userAgent.indexOf("Firefox")!=-1) ? 1 : 0;
var SNSURL = 'http://sns.tiao-tiao.com/';
var HOMEURL = 'http://www.tiao-tiao.com/';

if(isIE){
	navigator.userAgent.match(/MSIE (.*)\.0; /);
	var IE_VERSION = RegExp.$1;
}

var domain ='.tiao-tiao.com';

var URL = location.href;
var PATHNAME = location.pathname;

function JumpSNS(){
	var user_sns_domain = '';
	//var user_sns_domain = get_cookie('user_sns_domain');
	if(user_sns_domain==''){
		location.href = SNSURL;
	}else{
		location.href = SNSURL+user_sns_domain;
	}
}

function _$(elem){
	return document.getElementById(elem);
}
function _$tag(o,tag){
	return o.getElementsByTagName(tag); 	
}
function _$$(o){
	return document.getElementsByName(o); 	
}
function hide(elem){
	_$(elem).style.display = 'none';
}

function block(elem){
	_$(elem).style.display = 'block';
}

$(function(){
	//分页
	//按回车分页
	document.onkeydown=function(e){
		if(window.event) e=window.event;
		
		if(EventUtil.keyCode(e)==13 && document.activeElement.id=='pageNO'){
			_$('JumpPageButton').click();
		}
	}

	if(_$('JumpPageButton')!=null){
		 _$('JumpPageButton').onclick = function(){
			var ispage = _$('pageNO').value.match(/^(\d+)$/);
			var no = RegExp.$1;
			if(!ispage){
				//alert('请正确输入页码！');
				show_tishi_info('分页提示','请正确输入页码！');
				return false;
			}
			var url='';
			
			URL = URL.replace(/#(.*)$/,'');
			var anchor = '#'+RegExp.$1;
			if(anchor=='#'+no){
				anchor='';
			}
			
			if(URL.match(/\?/)){
				if(URL.match(/page=/)){
					url = URL.replace(/([\?|&])?page=(\d+)$/,'$1page='+no);
				}else{
					url = URL+'&page='+no;
				}
			}else{
				url = '?page='+no;
			}
			location.href = url+anchor;
		}
	}
})

var DOM = {
	child:function(c,l){
		var i=0,la=0,c1 = c.childNodes;
		do{
			if(c1[i].nodeType==1){
				if(la==l){return c1[i];}
				la++;
			}
			i++;
		}while(i<c1.length);
	},
	childNode:function(parent){
		var c1 = parent.childNodes,arr = [],i=0;
		do{
			if(c1[i].nodeType==1){
				arr.push(c1[i]);
			}
			i++;
		}while(i<c1.length);
		return arr;
	},
	next:function(o){
		for(var i=0;i<1000;i++){
			if(o.nextSibling.nodeType==1){
				return o.nextSibling;
				break;
			}
		}
	}
}

function setCurrCss(){
	var id = curcatid;
	if(!_$(id)){
		return false;		
	}
	if(typeof arguments[1]=='undefined'){css='current';}else{css=arguments[1];}
	_$(id).className = _$(id).classNmae+' '+css;
}

function isLogin(){
	if(!userid || parseInt(userid)<1){
		return false;
	}
	return true;
}


function comment(cur_id,index){
	if(!isLogin()){
		return false;
	}
	var boxs=['faceBox','starsBox'];
	for(var i=0;i<boxs.length;i++){
		if(boxs[i]==cur_id || _$(boxs[i])==null){continue;}
		_$(boxs[i]).style.display='none';
	}
	if(_$(cur_id).style.display=='none'){
		_$(cur_id).style.display='block';
		return 0;
	}else if(_$(cur_id).style.display=='block' && index==0){
		_$(cur_id).style.display='none';
		return 0;
	}
	
	if(index==0){
		return 0;
	}
}


function biaoqing(index){
	if(comment('faceBox',index)==0){
		return;	
	}
	
	if(index<10){index='0'+index;}
	_$('content').value=_$('content').value+'[bq='+index+"]";
	_$('faceBox').style.display='none';
	EventUtil.MaxStr('content','maxstr',200);
}

function pingxing(starcent){
	var contentid = $("#comment_content_id").val();
	//打星
	
	$.post("/include/ajax/pingxing.php",{'contentid':contentid,'starcent':starcent},function(txt,s){
		if(parseInt(txt)>0){
			parent.document.getElementById('startShowstyle').style.width = txt;				
		}else if(txt==0){
			alert('评星失败');
		}else{
			alert('您已经评过星!');
		}
		
	});
}

//全站外链地址的，全部target
$(function(){
	$(".newsArea a[href^=http://]").each(function(){
		if(!this.href.match(/^http:\/\/(www|sns|d)\.tiao-tiao\.com/)){
			this.target="blank";	
		}											  
	})
})

function indexpos(){
	var pos = _$$('indexpos');
	var last = pos[pos.length-1];
	last.outerText = last.innerText.substr(0,last.innerText.length-2);
}

function addOptions(getid,toid){
	var d = _$(getid).childNodes,op;
	for(var i=0;i<d.length;i++){
		var txt=d[i].innerText;
		op=new Option(txt,txt);
		_$(toid).options.add(op);
	}
}

function get_cookie(name){
	var cookie=unescape(document.cookie);
	if(cookie.match(new RegExp(name+"=([0-9,a-zA-Z]+)"))){
		return RegExp.$1;
	}else{
		return '';	
	}
}

function set_cookie(name,value,expiredays){
	var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
	document.cookie = name+"=" + escape(value)+ "; expires ="+ exdate.toGMTString()+";path=/;domain="+domain+";";
}

function ajax_activity(o){
	var field=o+curcatid;
	if(get_cookie(field)>0){
		//alert('你已经执行过！');
		show_tishi_info('','你已经执行过');
		return;
	}
	set_cookie(field,1,360);
	
	var time=new Date().getTime();
	$.get("/save/ajax.php",{'contentid':curcatid,'action':o,'i':time},function(data,status){
		if(data=='ok'){
			_$(o).innerHTML=parseInt(_$(o).innerHTML)+1;
			return ;
		}else{
			//alert('失败！\t请重试');
			show_tishi_info('','失败！请重试');
			return ;
		}
	});
}

function setSelectOption(sid,svalue){
	if(svalue==''){
		return ;
	}
	
	var o = _$(sid),len = o.options.length;
	for(var i=0;i<len;i++){
		if(o.options[i].value==svalue){
			//alert(i);
			o.options[i].selected = true;
			return ;
		}
	}
}

//显示的透明层框
//指定位置的层
function set_position_TRAN_BG(_tips,left,top){
	
	var TRAN_BG = $(parent._$('TRAN_BG'));
	TRAN_BG.css({'height':(parent.document.body.scrollHeight+parent.document.documentElement.scrollTop)}).show();
	TRAN_BG = null;
	
	var TRAN_BG_IFRAME = $(parent._$('TRAN_BG_IFRAME'));
	TRAN_BG_IFRAME.css({'height':(parent.document.body.scrollHeight+parent.document.documentElement.scrollTop)});
	TRAN_BG_IFRAME = null;
	
	
	//增加关闭
	$(parent._$('show_tishi_info_box_bj')).append('<div class="dlk_cha"><a href="###"><img src="/images/dlk_cha.png" onclick="close_tishi_info()" width="22" height="22" /></a></div>');
	$(parent._$('show_tishi_info')).html(_tips).show();
	
	//显示层
	parent.$("#show_tishi_info_box").show();
	
	
	if(top==undefined){
		var top = parent.document.documentElement.clientHeight;
	}
	
	top = (top-parent._$('show_tishi_info').clientHeight)/2+parent.document.documentElement.scrollTop;

	left = (left-parent._$('show_tishi_info').clientWidth)/2;
	
	parent.$("#show_tishi_info").add("#show_tishi_info_box").css('top',top+'px');
	parent.$("#show_tishi_info").add("#show_tishi_info_box").css('left',left+'px');

}


function set_TRAN_BG(_tips){
	
	var TRAN_BG = $(parent._$('TRAN_BG'));
	
	TRAN_BG.css({'height':(parent.document.body.scrollHeight)}).show();
	TRAN_BG = null;
	
	var TRAN_BG_IFRAME = $(parent._$('TRAN_BG_IFRAME'));
	if(TRAN_BG_IFRAME[0].innerHTML!=''){
		TRAN_BG_IFRAME.css({'height':(parent.document.body.scrollHeight)});
		TRAN_BG_IFRAME = null;
	}
	
	//增加关闭
	$(parent._$('show_tishi_info_box_bj')).append('<div class="dlk_cha"><a href="###" onclick="close_tishi_info()"><img src="/images/dlk_cha.png" width="22" height="22" /></a></div>');
	$(parent._$('show_tishi_info')).html(_tips).show();
	
	//显示层
	parent.$("#show_tishi_info_box").show();
	
	var top = (parent.document.documentElement.clientHeight-parent._$('show_tishi_info').clientHeight)/2+parent.document.documentElement.scrollTop+parent.document.body.scrollTop
	var left = (parent.document.body.offsetWidth-parent._$('show_tishi_info').clientWidth)/2;
	//alert(left)
	parent.$("#show_tishi_info").add("#show_tishi_info_box").css('top',top+'px');
	parent.$("#show_tishi_info").add("#show_tishi_info_box").css('left',left+'px');

}

function show_tishi_info(){
	var arg = arguments;
	var show_tishi_info_title = arg[0];	//标题
	if(show_tishi_info_title==''){
		show_tishi_info_title = '提示信息';	
	}
	var show_tishi_info_content = arg[1];	//内容
	
	
	var tips = '<div id="show_tishi_info_title" style=" font-size:14px; font-weight:bold; text-align:center; color:#fff; margin-top:30px; margin-left:80px; margin-right:80px;">'+show_tishi_info_title+'</div>\
        <div id="show_tishi_info_content" style=" color:#fff; margin-top:30px; margin-left:80px; text-align:center; margin-right:80px;">'+show_tishi_info_content+'</div>';
	
	set_TRAN_BG(tips);
	
}

function close_tishi_info(){
	_$('TRAN_BG').style.display='none';
	_$('show_tishi_info_box').style.display = 'none';
}

function array_search(elem,array){
	for(var i=0;i<array.length;i++){
		if(array[i] == elem){
			return i;
		}
	}
}

function selectCBAll(_this,checkbox){
	if($(_this).is(":checked")){
		$("input[type=checkbox][name="+checkbox+"[]]").attr('checked','checked')
	}else{
		$("input[type=checkbox][name="+checkbox+"[]]").attr('checked',false)
	}
}

function Search(){
	var q = $.trim($("#s_input").val());
	if(q=='' || q=='请输入关键字'){
		show_tishi_info('','请输入关健字再搜索！');
		return ;
	}
	location.href = '/search?word='+encodeURIComponent(q);
}


//扩展jquery
//元素不可用
$.fn.disabled = function(){
	return $(this).attr('disabled',true);
}
//取消元素不可用
$.fn.undisabled = function(){
	return $(this).attr('disabled',false);
}




//首页picList
var curPicListIndex = 0;
var lastPicListIndex = 0;
$(function(){
	var _n = $("#sPicList li").length;
	$("#picDisDIV a").eq(0).css('display','block');
	// 添加轮换任务
	var _play = setInterval(play,3000);
	function play(){
		$("#sPicList > li").eq((curPicListIndex+1)%_n).click()
	}		
	$(".gx_hdp").hover(function(){clearInterval(_play);}, function(){_play = setInterval(play,3000);})
	$("#sPicList > li").each(function(index){
		$(this).click(function(){
			if(curPicListIndex==index){
				return false;
			}
			
			lastPicListIndex = curPicListIndex;
			curPicListIndex = index;

			$("#picDisDIV a").eq(lastPicListIndex).fadeOut(500);
			$("#picDisDIV a").eq(index).fadeIn(500);
			
			
			$(this).siblings().removeClass('gx_hdp_r_bj','gx_hdp_r_b').addClass('gx_hdp_r_b');
			$(this).addClass('gx_hdp_r_bj');
		
		})
		
	});

	
	//去掉空链接
	$("#picDisDIV a").not("[href^=http://]").attr({'target':'_self','href':'javascript:;'})
	
	$("#chaopic > div").each(function(){
		$(this).mouseover(function(){
			var cppicid = this.id.substring(6,20);
			$(".gx_c_wz").css('display','none');
			$("#cptxt_"+cppicid).css('display','block');
		})
	})
	
})


function jumpCpage(no){
	var url='';
	
	if(URL.match(/\?/)){
		if(URL.match(/Cpage=/)){
			url = URL.replace(/([\?|&])?Cpage=(\d+)$/,'$1Cpage='+no);
		}else{
			url = URL+'&Cpage='+no;
		}
	}else{
		url = '?Cpage='+no;
	}
	location.href = url;
}

var disIframe = function(ifr_id,ifr_url){
	$("#"+ifr_id).attr('src',ifr_url);
}

var hasClass = function(obj,className){
	if(obj.className.indexOf(className)!=-1)
		return 1;
	else
		return 0;
}

/*字符串函数*/
String.prototype.urlencode = function(){
	return encodeURIComponent(this);
}

String.prototype.isEmail = function(){
	return this.match(/^[\w.-]+@[\w.-]+\.[a-zA-z]+$/);	
}


//用户操作
function chkLogin(f){
	if(f.email.value=='' || !f.email.value.isEmail()){
		alert('请正确输入用户名！');
		return false;
	}
	if(f.passwd.value==''){
		alert('请输入密码！')
		return false;
	}
}

function getInnerText(o){
	if(isFF){
		return o.textContent;
	}else{
		return o.innerText;	
	}
}

