var g_blinkid = 0;
var page_title = document.title;
var temp = 0;
var flag = 0;

function s(s){if (!$(s)) return; $(s).style.display = "block";}
function h(s){if (!$(s)) return; $(s).style.display = "none";}
function sh(s){if (!$(s)) return; $(s).style.display = $(s).style.display == "none"?"block":"none";}
function hs(s){if (!$(s)) return; $(s).style.display = $(s).style.display == "block"?"none":"block";}
function vv(s){if (!$(s)) return; $(s).style.visibility = "visible";}
function vh(s){if (!$(s)) return; $(s).style.visibility = "hidden";}
function changeMenuClass(s){
	if (!$(s)) return;
	setflag("uti_txs");
	hy();
	$(s).style.display = $(s).style.display == "none"?"block":"none";
	$('uti_txs').className = $('uti_txs').className == "uti_tx"?"uti_tx_01":"uti_tx";
}
function setflag(s){
	if (!$(s)) return;
	if (document.all ? true : false){
		$(s).onmouseover = function () { hnmouseover(event); };
		$(s).onmouseout = function () { hnmouseout(event); };
	}
	else{
		$(s).addEventListener("mouseover", function (evnt) { hnmouseover(evnt); }, false);
		$(s).addEventListener("mouseout", function (evnt) { hnmouseout(evnt); }, false);
	}
}
function hnmouseover(event){flag = 1; }
function hnmouseout(event){ flag = 0; }
function init(){
	if (document.all ? true : false){
		document.onmousedown = function () { mousedown(event); };
	}
	else{
		document.addEventListener("mousedown", function (evnt) { mousedown(evnt); }, false);
	}
}
function mousedown(evnt){
	if(flag == 0){ 
		hy();
	}
}

function hy(){
	for(var i=1;i<=5;i++){
		h("hn"+i+"_l");
	}
}

function xs(n){
	hy();
	s("hn"+n+"_l");
	setflag("hn"+n+"_l");
	void(0);
}


function getpos(element)
{
        if ( arguments.length != 1 || element == null )
        {
               return null;
        }
        var elmt = element;
        var offsetTop = elmt.offsetTop;
        var offsetLeft = elmt.offsetLeft;
        var offsetWidth = elmt.offsetWidth;
        var offsetHeight = elmt.offsetHeight;
        while( elmt = elmt.offsetParent )
        {
                // add this judge
                if ( elmt.style.position == 'absolute'
//              || elmt.style.position == 'relative'
                || ( elmt.style.overflow != 'visible' && elmt.style.overflow != '' ) )
                {
                        break;
                }
                offsetTop += elmt.offsetTop;
                offsetLeft += elmt.offsetLeft;
        }
        return {top:offsetTop, left:offsetLeft, right:offsetWidth+offsetLeft, bottom:offsetHeight+offsetTop };
}

//DataLength
function b_strlen(fData)
{
	var intLength=0;
	for (var i=0;i<fData.length;i++)
	{
		if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))
			intLength=intLength+2;
		else
			intLength=intLength+1;   
	}
	return intLength;
}

function IsIE()
{
	return document.all ? true : false;
}

function copyToClipboard(txt)
{
	if (window.clipboardData)
	{
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", txt);
	}
	else if (navigator.userAgent.indexOf("Opera") != -1)
	{
		window.location = txt;
	}
	else if (window.netscape)
	{
		try
		{
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		}
		catch (e)
		{
			alert("您的firefox安全限制限制您进行剪贴板操作，请打开'about:config'将signed.applets.codebase_principal_support'设置为true'之后重试");
			return false;
		}
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip)
			return false;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans)
			return false;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes['@mozilla.org/supports-string;1'].createInstance(Components.interfaces.nsISupportsString);
		var copytext = txt;
		str.data = copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip)
			return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
	return true;
}



function _commentImageResize(thisobj , limit)
{
        if(thisobj.width > limit)
        {
                thisobj.height = parseInt(limit*thisobj.height/thisobj.width);
                thisobj.width = limit;
        }

}

String.prototype.trim = function()
{
    return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}
String.prototype.lTrim = function()
{
    return this.replace(/(^[\s]*)/g, "");
}
String.prototype.rTrim = function()
{
    return this.replace(/([\s]*$)/g, "");
}

function getDays(year , month)
{
	var dayarr = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	if(month == 2)
	{
		if((year%4 == 0 && year%100 != 0) || year%400 == 0 || year < 1900)
			return 29;
		else
			return dayarr[month-1];
	}
	else
	{
		return dayarr[month-1];
	}
}

function extractNodes(pNode)
{
	if(pNode.nodeType == 3)
	{
		return null;
	}
	var node,nodes = new Array();
        for(var i=0 ; node= pNode.childNodes[i] ; i++)
        {
		if(node.nodeType == 1)
		{
			nodes.push(node);
		}
        }
        return nodes;
}

function changeimg(s, s1, s2)
{
	$(s).src = (-1 == $(s).src.indexOf(s1)) ? s1 : s2;
}

function selected(id,val)
{
	v = $(id);
	vLength = v.options.length;
	for(var i=0;i<vLength;i++){
		if(v.options[i].value == val){
			v.options[i].selected = true;
		}
	}
}


function checkNewMsg()
{
	var url = "/home/newmsg";
	var pars = "";
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { checkNewMsgShow(req); } });
}

function checkNewMsgShow(req)
{
	var r = req.responseText;
	StopNewMsg();
	setTimeout(checkNewMsg,60000);

	//【新消息】
	eval ("r="+r);
	if(r.sysmsg > 0){
		$("head_sysmsg_num").innerHTML = "("+r.sysmsg+")";
	}else{
		$("head_sysmsg_num").innerHTML = "";
	}

	if(r.msg > 0){
		$("head_msg_num").innerHTML = "("+r.msg+")";

	}else{
		$("head_msg_num").innerHTML = "";
	}

	if(r.msg > 0 || r.sysmsg > 0){
		g_blinkid = setInterval(StarNewMsg, 1000);
	}
//	setTimeout(checkNewMsg, 5000);
	
}

function StarNewMsg()
{
	html1 = "<a href='/msg/'><img src='/happyblue/images/msg/ddtx_y.gif' border=0></a>";
	html2 = "<a href='/msg/'><img src='/happyblue/images/msg/ddtx_g.gif' border=0></a>";
	$("head_msgdiv").innerHTML = temp % 2 ? html2 : html1;
	
	document.title = temp % 2 ? "【　　　】 - " + page_title : "【新消息】 - " + page_title;
	temp++;

}

function StopNewMsg()
{
	if(g_blinkid){
		clearInterval(g_blinkid);
		$("head_msgdiv").innerHTML = "";
		document.title = page_title;
	}

}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function moreModule(){
	var h = $('module_list').style.height;
	if(h == '90px'){
		$('module_list').style.height = '100%';
		$('module_link').innerHTML = "更少";
	}else{
		$('module_list').style.height = '90px';
		$('module_link').innerHTML = "更多";
	}
}