var util = {
    //返回对象
    G: function(htmlElementId){
        return (typeof (htmlElementId)=='object')?id:document.getElementById(htmlElementId);
    },
    //给对象绑定事件
    addLoadEvent: function(o, event, func){
    	if(o.attachEvent){
    		o.attachEvent('on'+event, func);    
    	}else if(o.addEventListener){      
    		o.addEventListener(event, func, false);    
    	}
    },
    bindFn: function(el, fn, args) {
        return function(){
            return fn.apply(el, args);
        }
    },
        //样式定义
    setStyle: function(H, C){
        H.className = C;
    }
};
/*
function g(id){ 
	return (typeof (id)=='object')?id:document.getElementById(id); 
}

function attachEvent2obj(o,event,func){
	if(o.attachEvent){
		o.attachEvent('on'+event,func);    
	}else if(o.addEventListener){      
		o.addEventListener(event,func,false);    
	}
}
*/

/**
 *响应键盘Enter键
 *
 **/
function pressKey(e,key,callback)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode){
		code = e.keyCode;
	}else if (e.which){ 
		code = e.which;
	}
	if(code == key)
	{
		callback();
	}
}


function direct2(url, delay)  {
    setTimeout(function(){location.href=url;}, delay);
}


function addFavorite(url,title)
{
    if (document.all){
        window.external.addFavorite(url,title);
    }else if (window.sidebar){
        window.sidebar.addPanel(title, url, "");
    }
}

function setHomepage(url)
{
    if (document.all){
        document.body.style.behavior='url(#default#homepage)';
        document.body.setHomePage(url);
    }else if (window.sidebar){
        if(window.netscape){
            try{
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }catch (e){
                alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true" );
            }
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage',url);
    }
}

var absPath = '/';

function Arcomment(sid, pos, page){
    if (!page) {page =1;}
	document.getElementById('loading_'+ pos).style.display = 'block';
	var x = new Ajax('text_'+ pos,'HTML','');
	var url = absPath+ 'ajax/ajaxComment.php';//'/comment/'+pos+'/'+sid+'.html';
	
	 x.post(url, 'action=' + pos + '&sid=' + sid + '&pageno=' + page,function(r){
	    // alert(r);return false;
    util.G('loading_'+pos).style.display = 'none';
    util.G(pos).innerHTML=r;
  });
}
function Ncomment(method,params,rep){
  //GetObj('textloading').style.display = 'block';
  //alert(url);
  var x = new Ajax('textloading','HTML','');
  x.post(absPath+ 'ajax/ajaxComment.php',params, function(r){
    util.G('textloading').style.display = 'none';
    util.G(rep).innerHTML=r;
  });
}

function toPage(str) {
    var sid = '';
    try{
        len = str.length;
        str = str.substr(0, len-5);
        params = str.split('_');
        s1 = params[0].split(',');
        s2 = params[1].split(',');
        action = s1[0];
        sid = s1[1];
        pageno = s2[1];
        Ncomment('POST', 'action='+action+'&sid='+sid+'&pageno='+ pageno, 'normal');
    }catch(e){}
}
//针对热门回复的快速恢复处理
function _FixHotReplyID(ReplyID){
     		if (isNaN(ReplyID)){
     			return ReplyID.substr(1);
     		}else{
     			return ReplyID;
     		}
}

//针对回复的Ajax投票
function vote4Reply(ReplyID, mode){
		util.G('AxajTip3'+ReplyID).style.display = 'none';
	     var x = new Ajax('AxajTip2'+ReplyID,'HTML','AjaxTipWarning');

     		x.get(absPath+'ajax/ajaxVote.php?tid=' + _FixHotReplyID(ReplyID) + '&' + mode + '=1',function(r){
     			//alert(r.substr(0,1));return false;
     		    if(r.substr(0,1) == '0'){
     				if(r.substr(1,1) == '1'){
     					  util.G('Y'+ReplyID).innerHTML = r.substr(2)
     					}else{
     						util.G('N'+ReplyID).innerHTML = r.substr(2,10)
     						};
				/*alert('谢谢您的参与');
     			}else{alert('你已经投过票了');}
     			})*/
     			_ShowAxajTip('AxajTip3'+ReplyID, '谢谢参与','AjaxTipComplete');
     			}else{
     				_ShowAxajTip('AxajTip3'+ReplyID, '你已经投过票了','AjaxTipWarning');
     				}
     			})
}

//针对举报的Ajax
function _ReplyReport(ReplyID){
		GetObj('AxajTip3'+ReplyID).style.display = 'none';
	     var x = new Ajax('AxajTip2'+ReplyID,'HTML','AjaxTipWarning');

     		x.get('/Ajax.report.php?tid=' +  _FixHotReplyID(ReplyID) ,function(r){
     			
				if(r.substr(0,1) == '0'){
     				
				/*alert('谢谢您的参与');
     			}else{alert('你已经投过票了');}
     			})*/
     			_ShowAxajTip('AxajTip3'+ReplyID, '感谢您的举报','AjaxTipComplete');
     			}else if(r.substr(0,1) == '1'){
     				_ShowAxajTip('AxajTip3'+ReplyID, '请勿重复举报','AjaxTipWarning');
     				}
				  else if(r.substr(0,1) == '2'){
				    _ShowAxajTip('AxajTip3'+ReplyID, '您今天已经举报了很多次，谢谢您的参与。','AjaxTipWarning');				     
				  }
				  else {
     				_ShowAxajTip('AxajTip3'+ReplyID, '出错了','AjaxTipWarning');
     				}
     			})
}


function _ShowAxajTip(divobj,ajaxReq,reqClass){
		var AxajTipDiv = util.G(divobj);
		ajaxReq = '<div style="float:right;cursor:pointer;"><img src="'+absPath+'source/images/'+ reqClass +'.gif" style="margin: 7px 7px;" onclick="_HideAxajTip(this);"></div><div>' + ajaxReq + '<div>';
		AxajTipDiv.className = reqClass;
		AxajTipDiv.innerHTML = ajaxReq;
		AxajTipDiv.style.display = 'block';
	}

function _HideAxajTip(myobj){
	myobj.parentNode.parentNode.style.display='none';
}


//更改字体大小
var status0='';
var curfontsize=12;
var curlineheight=15;
function fontZoomA(){
  if(curfontsize>8){
    document.getElementById('news_content').style.fontSize=(--curfontsize)+'pt';
   // document.getElementById('newsbody').style.lineHeight=(--curlineheight)+'pt';
  //document.getElementById('newshome').style.fontSize=(--curfontsize)+'pt';
 document.getElementById('news_content').style.lineHeight=(--curlineheight)+'pt';
  }
}
function fontZoomB(){
  if(curfontsize<64){
    document.getElementById('news_content').style.fontSize=(++curfontsize)+'pt';
 document.getElementById('news_content').style.lineHeight=(++curlineheight)+'pt';
 // document.getElementById('newshome').style.fontSize=(++curfontsize)+'pt';
// document.getElementById('newshome').style.lineHeight=(++curlineheight)+'pt';
  }
}

function dig(sid){
  var x = new Ajax('digloading','HTML','');
  url=absPath+'ajax/ajaxDig.php?sid='+sid;

  x.get(url,function(r){
      if(r.substr(0,1) == '0'){
          util.G('dignum').innerHTML = r.substr(1,10);
          util.G('digtip').innerHTML = '谢谢参与';
          //util.G('digtip').style.display = 'block';
          util.G('digtxt').innerHTML = util.G('digtxt').getAttribute("oktext");
      }else if(r.substr(0,1) == '1'){
          util.G('digtip').innerHTML = '已推荐';
          //util.G('digtip').style.display = 'block';
          util.G('digtxt').innerHTML = util.G('digtxt').getAttribute("oktext");
      }else{
          util.G('digtip').innerHTML = r;
      }
  });
}

//show replyDiv By Y2kSaTaN
var ReTitle = '';
var currentReplyWin = null;
function ReplyComment(ReplyID,sID){
			var ReplyDiv = util.G('Reply'+ReplyID);
			util.G('AxajTip3'+ReplyID).style.display='none';
			ReplyDiv.style.display='block';
			if (currentReplyWin) {
			    currentReplyWin.style.display='none';
			}
            currentReplyWin = ReplyDiv;
			if(ReplyDiv.innerHTML.length == 0){
				replydivHtml = '<div class="Header"><div class="replytitle">快速回复</div></div>';
				//replydivHtml += '<form name="comment' + ReplyID + '" action="/comment.php" method="post" onsubmit="return checkReply(this,'+ReplyID+')">'
				replydivHtml += '<input name="tid" type="hidden"  value="'+ ReplyID +'" /><input type="hidden" name="sid" value="' + sID + '"/>'
				replydivHtml += '<input class="input" name="nowsubject" type="hidden" id="subject' + ReplyID + '" value="' + ReTitle + '"/>';
				replydivHtml += '<div class="Content">'
	      	 	replydivHtml += '<div class="row"><input class="input" name="nowname" type="text" id="name' + ReplyID + '" value="' + readCookie('sky84GuestName') + '"/><label for="name'+ReplyID+'">名字 (必填)</label></div>'
	      	 	replydivHtml += '<div class="row"><input class="input" name="nowemail" type="text" value="' + readCookie('sky84GuestEmail') + '" id="email' + ReplyID + '"/><label for="email' + ReplyID + '">邮箱 (必填，永不公开)</label></div>'
	      	 	replydivHtml += '<div class="row"><input class="input" name="nowpage" type="text" id="page' + ReplyID + '" value="'+ readCookie('sky84GuestPage') +'"/><label for="page' + ReplyID + '">网址</label></div>'
	      	 	
	      	 	replydivHtml += '<div class="row"><textarea style="width:80%" name="comment" id="comment' + ReplyID + '" ></textarea></div>'
	      	 	//replydivHtml += '<div class="row"><label>您的评论:</label><textarea name="comment" id="comment' + ReplyID + '" <!--onfocus="_Showvaldiv(\'' + ReplyID + '\');"-->></textarea></div>'
	      	 	//replydivHtml += '<div class="row" style="display:none;" id="valdiv' + ReplyID + '"><label>　验证码:</label><input type="text" class="input" name="valimg" id="valimg' + ReplyID + '" style="width: 40px;"/><img src="" name="safecode" style="cursor:pointer"  border="0" align="absbottom" id="safecode' + ReplyID + '" alt="更换一张验证码图片" onclick="reloadcode(\''+ ReplyID + '\');"> [<a href="javascript:reloadcode(\'' + ReplyID + '\');">刷新验证码</a>] [<a href="javascript:fixCode(\'' + ReplyID + '\');">修正出错</a>]</div>'
	      	 	//<span class="tip" id="tip_comment' + ReplyID + '"></span>
	      	 	replydivHtml += '<div id="AxajTip' + ReplyID + '" class="AjaxTipWarning"></div>'
	      	 	replydivHtml += '<div class="row" id="bottom' + ReplyID + '" style="text-align:right;"><input type="button" class="button" value="提交" id="button' + ReplyID + '" onClick="_postReply(\'' + ReplyID + '\',' + sID +')"/><input type="button" class="button" value="关闭" onclick="_HideReply(\'' + ReplyID +'\')"></div></div>'
	      	 	//replydivHtml += '</form>';
	      	 	//热门评论特别处理
	      	 	if (isNaN(ReplyID)){
	      	 		ReplyDiv.style.width='320px';
	      	 		replydivHtml = replydivHtml.replace(/\<input class="input"/g,'<input class="input" style="width: 150px;"');
	      	 		replydivHtml = replydivHtml.replace('<textarea name="comment"','<textarea name="comment" style="width: 200px;"');
	      	 		}
			
	      	 	ReplyDiv.innerHTML = replydivHtml;
	      	 	
      	 	}else{
      	 		//_HideReply(ReplyID);
      	 		}
}

//显示验证码
function _Showvaldiv(ReplyID){
		var Valdiv = util.G('valdiv'+ReplyID);
		if (Valdiv.style.display=='none'){
			Valdiv.style.display='block';
			reloadcode(ReplyID);
			}
	}

//隐藏快速回复表单
function _HideReply(ReplyID){
	var ReplyDiv = util.G('Reply'+ReplyID);
	ReplyDiv.style.display='none';
	//ReplyDiv.innerHTML = '';
}

//Axaj回复
function _postReply(ReplyID,sID) {
    util.G('AxajTip3'+ReplyID).style.display = 'none';
    util.G('AxajTip'+ReplyID).style.display = 'none';
    var inobj = util.G('name'+ReplyID);
    if (inobj.value=='') {
        _ShowAxajTip('AxajTip'+ReplyID,'请填写昵称！','AjaxTipWarning');
        inobj.focus();
        return false;
    }
    var inobj = util.G('email'+ReplyID);
    if (!(inobj.value=='')) {
        if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(inobj.value)){
        _ShowAxajTip('AxajTip'+ReplyID,'请输入格式正确的邮箱地址！','AjaxTipWarning');
        inobj.focus(); return false;}
    } /*else {
        _ShowAxajTip('AxajTip'+ReplyID,'请填写邮箱地址！','AjaxTipWarning');
        inobj.focus();
        return false;
    }*/

    var inobj = util.G('comment'+ReplyID);
    if(inobj.value==''){	_ShowAxajTip('AxajTip'+ReplyID,'请填写评论后再发表','AjaxTipWarning');inobj.focus();return false;}

		//var inobj = util.G('valimg'+ReplyID);
		//if(inobj.value==''){	_ShowAxajTip('AxajTip'+ReplyID,'请填写验证码后再发表','AjaxTipWarning');inobj.focus();return false;}
	   StopButton('button'+ReplyID, 15);

   var x = new Ajax('AxajTip2'+ReplyID,'HTML','AjaxTipWarning');

   //tid sid valimg_main comment nowsubject
   var queryString = 'tid=' + _FixHotReplyID(ReplyID) + '&sid=' + sID +  '&name=' + escape(util.G('name'+ReplyID).value) + '&comment=' + escape(Replace(util.G('comment'+ReplyID).value)) +  '&subject=' + util.G('subject'+ReplyID).value+  '&page=' + util.G('page'+ReplyID).value + '&email=' + util.G('email'+ReplyID).value /*+ '&valimg_main=' + util.G('valimg'+ReplyID).value */+'';
	//记忆访客信息
	createCookie('sky84GuestName', util.G('name'+ReplyID).value, 365);
	createCookie('sky84GuestPage', util.G('page'+ReplyID).value, 365);
	createCookie('sky84GuestEmail', util.G('email'+ReplyID).value, 365);
	//alert(queryString);
    var ajaxReq = '',reqClass='AjaxTipWarning';
   x.post(absPath +'ajax/ajaxPostComment.php?act=postc',queryString,function(r){
		switch(r.substr(0,1)){
		case '0':
		        ajaxReq = '您要评论的新闻不存在';
		        break;
		//case '1':
		//        ajaxReq = '验证码不正确';
		//        break;
		case '2':
		       ajaxReq = '30秒内不允许再次评论';
		        break;
		case "3":
		        ajaxReq = '请填写评论后再提交';
		        break;
		case "4":
		        ajaxReq = '评论字数超过限制';
		        break;
		case "5":
		        ajaxReq = '发送评论成功.';
		        reqClass = 'AjaxTipComplete';
		        util.G('normal').innerHTML = r.substr(3);
		        _ShowAxajTip('AxajTip3'+ReplyID,ajaxReq,reqClass);
				try{util.G('comment'+ReplyID).value='';util.G('valdiv'+ReplyID).style.display='none';}catch(e){}
				return true;
		        break;
		case "6":
			    ajaxReq = '您输入了不适当字词，请纠正';
		        break;
		case "7":
			    ajaxReq = '您的评论需审核后才能显示';
		        reqClass = 'AjaxTipdelay';
		        _ShowAxajTip('AxajTip3'+ReplyID,ajaxReq,reqClass);
				try{util.G('comment'+ReplyID).value='';/*util.G('valdiv'+ReplyID).style.display='none';*/}catch(e){}
				return true;
		        break;
		case "8":
			    ajaxReq = '此资讯不开放评论，请见谅';
		        break;
		case "9":
				ajaxReq ='评论系统维护中';
				break;
		default:
		        ajaxReq = '未知错误' + r;
		}
		_ShowAxajTip('AxajTip'+ReplyID,ajaxReq,reqClass);
 		//reloadcode(ReplyID);
	});
}


function Replace(str){
 
   re = /%/g;             // 创建正则表达式模式。
   str = str.replace(re, "％");    //  替换 
   re = /\+/g;
   str = str.replace(re, "＋");
   return(str);                   // 返回替换后的字符串。
}




//读取Cookie
createCookie = function(nm,val,y){var exp='';if(y){var dt=new Date();dt.setTime(dt.getTime()+(y*86400000));exp='; expires='+dt.toGMTString();}document.cookie=nm+'='+escape(val)+exp+';path=/';}
//读取Cookie
readCookie = function(nm){var m='';if(window.RegExp){var re=new RegExp(';\\s*'+nm+'=([^;]*)','i');m=re.exec(';'+document.cookie);}return(m?unescape(m[1]):'');}

 function StopButton(id,min)
 { 
    if (!util.G(id)) {
        return;
    }
    util.G(id).disabled='disabled';
    util.G(id).value="发表评论("+min+")";
    if(--min>0 ) setTimeout("StopButton('"+id+"',"+min+")",1000);
    if(min<=0){util.G(id).value='发表评论';
           util.G(id).disabled='';}
}



////////////Ajax类
function Ajax(statusId,recvType,statusClass,cache) {
	var aj = new Object();
	aj.statusId = util.G(statusId);
	if(statusClass!==''){aj.statusId.className = statusClass;}
	aj.targetUrl = '';
	aj.sendString = '';
	aj.recvType = recvType ? recvType : 'HTML';//HTML XML
	aj.resultHandle = null;

	aj.createXMLHttpRequest = function() {
		var request = false;
		if(window.XMLHttpRequest) {
			request = new XMLHttpRequest();
			if(request.overrideMimeType) {
				request.overrideMimeType('text/xml');
			}
		} else if(window.ActiveXObject) {
			var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
			for(var i=0; i<versions.length; i++) {
				try {
					request = new ActiveXObject(versions[i]);
					if(request) {
						return request;
					}
				} catch(e) {
					//alert(e.message);
				}
			}
		}
		return request;
	}

	aj.XMLHttpRequest = aj.createXMLHttpRequest();

	aj.processHandle = function() {
		aj.statusId.style.display = 'block';
		if(aj.XMLHttpRequest.readyState == 1) {
			aj.statusId.innerHTML = '正在建立连接...';
			
		} else if(aj.XMLHttpRequest.readyState == 2) {
			aj.statusId.innerHTML = '正在发送数据...';
		} else if(aj.XMLHttpRequest.readyState == 3) {
			aj.statusId.innerHTML = '正在接受数据...';
		} else if(aj.XMLHttpRequest.readyState == 4) {
			if(aj.XMLHttpRequest.status == 200) {
				aj.statusId.innerHTML = '正在处理数据...';
				if(aj.recvType == 'HTML') {
					aj.resultHandle(aj.XMLHttpRequest.responseText);
				} else if(aj.recvType == 'XML') {
					aj.resultHandle(aj.XMLHttpRequest.responseXML);
				}
			aj.statusId.style.display = 'none';
			} else {
				aj.statusId.innerHTML = '';
			}
		}
	}

	aj.get = function(targetUrl, resultHandle,cache) {
		/*if(cache==''){
		cache=1;
		}
		if(cache==1){
		if (targetUrl.indexOf("?") > 0)
                {
                    targetUrl += "&randnum=" + Math.random();
                }
                else
                {
                    targetUrl += "?randnum=" + Math.random();
                }
		}*/
		aj.targetUrl = targetUrl;
		aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
		aj.resultHandle = resultHandle;
		if(window.XMLHttpRequest) {
			aj.XMLHttpRequest.open('GET', aj.targetUrl);
			aj.XMLHttpRequest.send(null);
		} else {
		        aj.XMLHttpRequest.open("GET", targetUrl, true);
		        aj.XMLHttpRequest.send();
		}
	}

	aj.post = function(targetUrl, sendString, resultHandle) {
		if (targetUrl.indexOf("?") > 0)
                {
                    targetUrl += "&randnum=" + Math.random();
                }
                else
                {
                    targetUrl += "?randnum=" + Math.random();
                }
		aj.targetUrl = targetUrl;
		aj.sendString = sendString;
		aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
		aj.resultHandle = resultHandle;
		aj.XMLHttpRequest.open('POST', targetUrl);
		aj.XMLHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		aj.XMLHttpRequest.setRequestHeader("charset","utf-8");
		aj.XMLHttpRequest.send(aj.sendString);
	}
	return aj;
}

var $ = function(id){return document.getElementById(id)}
var tcposts_cbwidget_current_sidebar='hot';
function tcposts_cbwidget_toggle(selected,location){
        tcposts_widget_current=tcposts_cbwidget_current_sidebar;
        var current_element=$('tcposts_widget_'+tcposts_widget_current);
        var selected_element=$('tcposts_widget_'+selected);
        current_element.style.display='none';
        selected_element.style.display='block';
        var current_element_header=$('tcposts_widget_'+tcposts_widget_current+'_header');
        var selected_element_header=$('tcposts_widget_'+selected+'_header');
        current_element_header.className='boxy_header_link';
        selected_element_header.className='boxy_header_selected';
        tcposts_cbwidget_current_sidebar=selected;
}

function tcposts_cbwidget_over(selected) {
    if (selected == tcposts_cbwidget_current_sidebar)return;
    var selected_element_header=$('tcposts_widget_'+selected+'_header');
    selected_element_header.className='boxy_header_selected';
}
function tcposts_cbwidget_out(selected) {
    if (selected == tcposts_cbwidget_current_sidebar)return;
    var selected_element_header=$('tcposts_widget_'+selected+'_header');
    selected_element_header.className='boxy_header_link';
}

function preReply() {
    var name = readCookie('sky84GuestName');
    var email = readCookie('sky84GuestEmail') || $("email0").value;
    var page = readCookie('sky84GuestPage');
    if (name) {
        $("name0").value = name;
    }
    if (email) {
        //填充email
        $("email0").value = email;
        //读取头像
        c = hex_md5(email);
        $("guest_avatar").setAttribute("f", c);
        $("guest_avatar").src = "http://www.gravatar.com/avatar/"+ c + "?r=g&s=60";
    }
    if (page) {
        $("page0").value = page;
    }
}

function preGavatar() {
    var email = $("email0");
    var ava = $("guest_avatar");
    var tag = ava.getAttribute("f");
    if (email.value) {
        c = hex_md5(email.value);
        if (tag == c) {return;}
        if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email.value)) {
            return false;
        } else {
            //读取头像
            ava.src = "http://www.gravatar.com/avatar/"+ c + "?r=g&s=60";
            ava.setAttribute("f", c);
        }
    } else {
        email = readCookie('sky84GuestEmail');
        if (email) {
            c = hex_md5(email);
            if (tag == c) {return;}
            ava.src = "http://www.gravatar.com/avatar/"+ c + "?r=g&s=60";
            ava.setAttribute("f", c);
        }
    }
}

function asynJsLoader(add_to, js_file_path) {
    var script = document.createElement("script");
    script.setAttribute("type", 'text/javascript');
    script.setAttribute("src", js_file_path);
    add_to.appendChild(script);
}