﻿/*---------公共JS函数库（界面）---------*/
/*--------Lzeasy 2010.6-------*/
///显示或隐藏对象///
///<param=menuobj>需显示或隐藏对的ID</param>
///<param=obj>需显示或隐藏对的ID</param>
///<param=show>显示时的样式类名</param>
///<param=hidd>隐藏时的样式类名</param>
function SHObj(menuobj,obj,show,hidd)
{
    var menuobj = $(menuobj);
    var obj = $(obj);
    if (obj.style.display != 'none')
    {
        obj.style.display = "none";
        if(menuobj != null)
        {menuobj.className = show;}
    }
    else
    {
        obj.style.display = "block";
        if(menuobj != null)
        {menuobj.className = hidd;}
    }
}
///Li隔行变色，点击、移动变色///
///<param=row1>奇数行样式class</param>
///<param=row2>偶数行样式class</param>
///<param=liclick>点击样式class</param>
///<param=limove>移入样式class</param>
///多个指定ul的li///
///<param=obj>多个ul的ID串，以|分隔</param>
function MoreLiBg(obj,row1,row2,liclick,limove)
{
    var theval = obj.split('|');
    for(var i=0; i<theval.length; i++)
    { LiBackground(theval[i],row1,row2,liclick,limove); }
}
///单个指定ul的li///
///<param=obj>ul的Id</param>
function LiBg(obj,row1,row2,liclick,limove)
{
    var uls = $(obj);
    var lis = uls.getElementsByTagName('li');
    for(var i = 0; i < lis.length; i++)
    {
		lis[i].className=(i%2 == 0) ? row1 : row2;
		lis[i].onclick = function()
		{
		    for(var i = 0; i < lis.length; i++)
		    {
				if(lis[i].className == liclick && lis[i] != this)
				{ lis[i].className = (i == 0) ? row1 : row2; }
			}
			this.className = (this.className != liclick) ? liclick : ((Currrow(lis,this)%2 == 0) ? row1 : row2);
		}
		lis[i].onmouseover = function()
		{
			if(this.className != liclick)
			{ this.className = limove; }
		}
		lis[i].onmouseout = function()
		{
			if(this.className != liclick)
			{ this.className = (Currrow(lis,this)%2 == 0) ? row1 : row2; }
		}
	}
}
///页面中指定ID/所有表格隔行换色，移动变色///
///tr1--奇数行样式类--
///tr2--偶数行样式类--
///intr--鼠标移入行样式类--
///click--点击高亮样式类--
///obj为指定表格对象ID,tr2无值时不进行隔行换色,click无值时不进行点击高亮
function TrBg(obj,tr1,tr2,intr,click)
{
    var trs;
    if(obj.length > 0)
    {
        obj = $(obj);
        trs = obj.document.getElementsByTagName('tr');
    }
    else
    { trs = document.getElementsByTagName('tr'); }
	for(var i = 0; i < trs.length; i++)
	{
	    var th = trs[i].getElementsByTagName('th');
	    if(th.length == 0)
	    {
            if(tr2.length > 0)//隔行变色
            {
		        trs[i].className = (i%2 == 0) ? tr1 : tr2;
		        trs[i].onmouseout = function()
		        {
		            if(click.length == 0 || this.className != click)
		            { this.className = (Currrow(trs,this)%2 == 0) ? tr1 : tr2; }
		        }
		        trs[i].onmouseover = function()
		        {
		            if(click.length == 0 || this.className != click)
		            { this.className = intr; }
		        }
		    }
		    else//不隔行变色
		    {
		        trs[i].className = tr1;
		        trs[i].onmouseout = function()
		        {
		            if(click.length == 0 || this.className != click)
		            { this.className = tr1; }
		        }
		        trs[i].onmouseover = function()
		        {
		            if(click.length == 0 || this.className != click)
		            { this.className = intr; }
		        }
		    }
		    if(click.length > 0 && tr2.length > 0)//隔行变色，且点击高亮
		    {
		        trs[i].onclick = function()
		        {
		            for(var i = 0; i < trs.length; i++)
		            {
				        if(trs[i].className == click && trs[i] != this)
				        { trs[i].className = (i == 0) ? tr1 : tr2; }
			        }
			        this.className = (this.className != click) ? click : ((Currrow(trs,this)%2 == 0) ? tr1 : tr2);
		        }
		    }
		    if(click.length > 0 && tr2.length == 0)
		    {
		        trs[i].onclick = function()
		        { this.className = (this.className != click) ? click : tr1; }
		    }
		}
	}
}
///取鼠标所在行行数///
///<param=lis>li对象</param>
///<param=lii>返回对比行数</param>
function Currrow(lis,lii)
{
	for(var i = 0; i < lis.length; i++)
	{
	    if(lis[i] == lii)
	    { return i; }
	}
}
///显示提示窗口///
///<param=tiptxt>提示内容</param>
///<param=flag>显示或隐藏</param>
function ShowTip(tiptxt,flag)
{
    var tip = $("tipcont");
    var txt = $("tiptxt");
    if(flag == true)
    {
        txt.innerHTML = tiptxt;
        tip.style.display = 'block';
    }
    else
    { tip.style.display = 'none'; }
}
///文字大小增减///
///<param=hiddenid>存储当前字体大小的隐藏域</param>
///<param=obj>需要增减字体大小对象的ID</param>
///<param=tags>增加或减少操作标记</param>
function TxtChangeSize(hiddenid,obj,tags)
{
    var hdid = $(hiddenid);
    var obj = $(obj);
    var tsize = hdid.value;
    if(tags == "add")
    { tsize = parseFloat(tsize) + 0.1; }
    if(tags == "minus")
    { tsize = parseFloat(tsize) - 0.1; }
    if(tsize > 1.1 && tsize < 3 )
    {
        tsize = tsize.toFixed(1);
        hdid.value = tsize;
        obj.style.fontSize = tsize + "em";
        obj.style.lineHeight = tsize * 20 + "px";
    }
}
///改变文本内容配色///
///<param=obj>文本内容对象的ID</param>
///<param=clasname>样式类名</param>
function ChangeTextColor(obj,clasname)
{
    var obj = $(obj);
    obj.className = clasname;
}
///返回页面顶部///
function GoTop()
{
    var speed = 1.2;
    var time = 10;
    var dy = 0;
    var by = 0;
    var wy = 0;
    if(document.documentElement)
    { dy = document.documentElement.scrollTop||0; }
    wy = window.scrollY||0;
    var y = Math.max(wy,Math.max(by,dy));
    window.scrollTo(0,Math.floor(y/speed));
    if(y > 0)
    { setTimeout("GoTop()",time); }
}
///导航条鼠标事件///
function NavTowX(obj,obj2,classname)
{
    obj = $(obj);
    obj2 = $(obj2);
    obj.style.display = (obj.style.display == 'block') ? 'none' : 'block';
    obj2.className = classname;
}
/***一级导航菜单***/
function OneNav(xmlPath,head,loops,foot)
{
    var xmlDoc = GetXmlData();
    xmlDoc.async = false;
    xmlDoc.load(xmlPath);
    var rootNode = xmlDoc.documentElement.childNodes;
    var navtxt = head;
    var rl = rootNode.length;
    var temp = "";
    for (var i = 0; i < rl; i++)
    {
        if(rootNode[i].nodeType == 1)
        {
            temp += loops.replace("url", rootNode[i].getAttribute("link"));
            temp = temp.replace("column", rootNode[i].getAttribute("columname"));
        }
    }
    navtxt += temp + foot;
    document.write(navtxt);
    xmlDoc = null;
}
//二级导航菜单//
//<param=xmlPath>菜单XML文件地址</param>
function DropDownMenu(xmlPath,head,foot)
{
    var xmlDoc = GetXmlData();
    xmlDoc.async = false;
    xmlDoc.load(xmlPath);
    //取根节点的子节点即第一个节点
    var rootNode = xmlDoc.documentElement.childNodes;
    //重构过的XML内容存储变量
    var navtxt = head;
    var rl = rootNode.length;
    for (var i = 0; i < rl; i++)
	{
	    //因为非IE浏览器取到的节点数包含了空格等废值，所以要判断取到的节点是否有效
	    if(rootNode[i].nodeType == 1)
	    {
	        //输出层中的ID存储变量
            var ids = "nav" + i;
            var aid = "snav" + i;
            //如果没有子节点
            if(rootNode[i].childNodes.length < 1)
            {
              navtxt += "<li><a href=\"" + rootNode[i].getAttribute("link") + "\" id=\"" + aid + "\" class=\"mmenu\">" + rootNode[i].getAttribute("columname") + "</a>";
            navtxt += "<div class=\"minnav\" id=\"" + ids + "\"></div></li>";
                continue;
            }
            navtxt += "<li onmouseover=\"NavTowX('" + ids + "','" + aid + "','menuin');\" onmouseout=\"NavTowX('" + ids + "','" + aid +"','mmenu');\"><a href=\"" + rootNode[i].getAttribute("link") + "\" id=\"" + aid + "\" class=\"mmenu\">" + rootNode[i].getAttribute("columname") + "</a>";
            navtxt += "<div class=\"subnav hidden\" id=\"" + ids + "\">";
            navtxt += DropDownSub(rootNode[i]);
            navtxt += "</div></li>";
	    }
	}
	navtxt += foot;
	document.write(navtxt);
	xmlDoc = null;
}
//遍历子节点，反回节点重构内容
function DropDownSub(subnode)
{
    subnode = subnode.childNodes;
    var subtxt = "";
    for (var i = 0; i < subnode.length; i++)
    {
        if(subnode[i].nodeType == 1)
	    {
            subtxt += "<a href=\"" + subnode[i].getAttribute("link") + "\">" + subnode[i].getAttribute("columname") + "</a> | ";
        }
    }
    subtxt = subtxt.substring(0, subtxt.length - 3);
    return subtxt;
}

function ThreeNav(xmlPath,head,loops,foot,columname,leves,nums)
{
    var xmlDoc = GetXmlData();
    xmlDoc.async = false;
    xmlDoc.load(xmlPath);
    var rootNode = xmlDoc.documentElement.childNodes;
    var navtxt = head;
    var rl = rootNode.length;
    for (var i = 0; i < rl; i++)
	{
	    //第一级如果没有子节点退出本次循环
	    if(rootNode[i].childNodes.length < 1)
	    { continue; }
//	    if(navtxt.length < 30)
//	    {
	        if(leves == true)//如果只取到第二级
	        {
	            //判断是否有子节点是否有值并且第一级栏目名为待取值栏目
	            if(rootNode[i].nodeType == 1 && rootNode[i].getAttribute("columname") == columname)
                { navtxt += ThreeNavSubs(rootNode[i],loops,nums); }
	        }
	        else
	        {
	            if(rootNode[i].nodeType == 1)//取到第三级就判断是否子节点有值，转到子级处理函数
	            { navtxt += ThreeNavSub(rootNode[i],loops,columname,nums); }
	        }
//	    }
	}
	navtxt += foot;
	document.write(navtxt);
	xmlDoc = null;
}
//遍历子节点，反回节点重构内容
function ThreeNavSub(subnode,loops,column,nums)
{
    subnode = subnode.childNodes;
    var subtxt = "";
    for (var i = 0; i < subnode.length; i++)
    {
        if(subnode[i].childNodes.length < 1)//如果不包含子节点
        { continue; }
        if(subnode[i].nodeType == 1 && subnode[i].getAttribute("columname") == column)
        { subtxt += ThreeNavSubs(subnode[i],loops,nums); }
    }
    return subtxt;
}
function ThreeNavSubs(sub,lop,nums)
{
    sub = sub.childNodes;
    var subt = "";
    var t ="";
    var sl = sub.length;
    if(nums > 0)
    { sl = nums;}
    for (var i = 0; i < sl; i++)
    {
        if(sub[i].nodeType != 1)
        {
            if(nums > 0)
            { sl++; }
            continue;
        }
        else
        {
            t = lop.replace("column", sub[i].getAttribute("columname"));
            t = t.replace("link", sub[i].getAttribute("link"));
        }
        subt += t;
    }
    return subt;
}
/*****读取JSON对象输出列表内容*****/
// json:Json对象名称
// num:输出数量
// head:列表头标签
// loops:重复输出标签
// foot:列表结束标签
// titlen:列表标题长度 如为0则表示不截取
// conlen:简要内容长度，如为0，则表示无简要内容
// ico:新信息是否显示new图标 0不显示，1显示
// dates:日期格式 0不显示
function ExportList(json, num, head, loops, foot, titlen, conlen, ico, dates)
{
    var obj = eval(json);
    var lists = obj.lists;
    var temp = "";
    var html = head;
    if(num > lists.length)
    { num = lists.length; }
    for(var i = 0; i < num; i++)
    {
        if(loops.indexOf("infotit") != -1)//如果包含了标题
        {
            if(titlen != 0)
            { temp = loops.replace("infotit", lists[i].Title.sub(titlen)); }
            else
            { temp = loops.replace("infotit", lists[i].Title); }
        }
        if(loops.indexOf("titles") != -1)
        { temp = temp.replace("titles", lists[i].Title); }
        if(loops.indexOf("link") != -1)
        { temp = temp.replace("link",lists[i].Url); }
        if(loops.indexOf("published") != -1)
        {
            var temptime = new Date(lists[i].Time.replace(/-/g,'/'));
            if(dates != 0)
            {
                switch (dates)
                {
                    case 1://09-01
                        temptime = temptime.format('yy-MM');
                    break;
                    case 2://2009-11-22
                        temptime = temptime.format('yyyy-MM-dd');
                    break;
                    case 3://2009年11月22日
                        temptime = temptime.format('yyyy年MM月dd日');
                    break;
                }
                temp = temp.replace("published",temptime);
            }
        }
        if(ico == 1)
        {
            var dtemp = new Date().getTime() - new Date(lists[i].Time.replace(/-/g,'/'));
            if(dtemp <= 7)//如果发布时间不超过7天算最新
            {  }//显示方式还未想好
        }
        if(loops.indexOf("infocont") != -1)
        {
            if(conlen > 0)
            { temp = temp.replace("infocont", lists[i].Content.sub(conlen)); }
            else
            { temp = temp.replace("infocont", lists[i].Content); }
        }
        html += temp;
    }
    html += foot;
    document.write(html);
}
/*****读取JSON对象输出广告*****/
// json:Json对象名称
// head:列表头标签
// foot:列表结束标签
function LoadAd(json,head,foot)
{
    var obj = eval(json);
    var lists = obj.ad;
    var html = head;
    var anner = lists[0].Anner;
    var link = lists[0].Url;
    var width = lists[0].Width;
    var height = lists[0].Height;
    var type = anner.substring(anner.lastIndexOf('.') + 1, anner.length);
    html += "<a href=\"" + link + "\" target=\"_blank\" class=\"link\"></a>";
    if(type == "jpg" || type == "gif" || type == "png")
    {
        html += "<img src=\"" + anner + "\" alt=\"\" style=\"width:" + width + ";height:" + height + "\" />";
    }
    if(type == "swf")
    {
        html += "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"" + width + "\" height=\"" + height + "\"><param name=\"movie\" value=\"" + anner + "\" /><param name=\"quality\" value=\"high\" /><param name=\"wmode\" value=\"Opaque\" /><embed pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" quality=\"high\" src=\"" + anner + "\" type=\"application/x-shockwave-flash\" wmode=\"Opaque\" width=\"" + width + "\" height=\"" + height + "\"></embed></object>";
    }
    html += foot;
    document.write(html);
}
/*****输出统计表格*****/
function ExportTable(json, num, loops)
{
    var obj = eval(json);
    var lists = obj.lists;
    var temp = loops;
    var html = "";
    if(num > lists.length)
    { num = lists.length; }
    for(var i = 0; i < num; i++)
    {
        if(loops.indexOf("Qsts") != -1)
        { temp = temp.replace("Qsts",lists[i].Qsts); }
        if(loops.indexOf("Qsmj") != -1)
        { temp = temp.replace("Qsmj",lists[i].Qsmj); }
        if(loops.indexOf("Czts") != -1)
        { temp = temp.replace("Czts",lists[i].Czts); }
        if(loops.indexOf("Czmj") != -1)
        { temp = temp.replace("Czmj",lists[i].Czmj); }
        if(loops.indexOf("Lbts") != -1)
        { temp = temp.replace("Lbts",lists[i].Lbts); }
        if(loops.indexOf("Lbmj") != -1)
        { temp = temp.replace("Lbmj",lists[i].Lbmj); }
        if(loops.indexOf("Lnts") != -1)
        { temp = temp.replace("Lnts",lists[i].Lnts); }
        if(loops.indexOf("Lnmj") != -1)
        { temp = temp.replace("Lnmj",lists[i].Lnmj); }
        if(loops.indexOf("Yfts") != -1)
        { temp = temp.replace("Yfts",lists[i].Yfts); }
        if(loops.indexOf("Yfmj") != -1)
        { temp = temp.replace("Yfmj",lists[i].Yfmj); }
        if(loops.indexOf("Time") != -1)
        { temp = temp.replace("Time", lists[i].Time); }
        if(loops.indexOf("Ldts") != -1)
        { temp = temp.replace("Ldts", lists[i].Ldts); }
        if(loops.indexOf("Ldmj") != -1)
        { temp = temp.replace("Ldmj", lists[i].Ldmj); }
        html += temp;
    }
    document.write(html);
}
///自动切换TAB///
///
function AutoSwitchTab(tab)
{
    for(var s in tab)
    {
	    MouseEvents(s);
	    AutoSwitch(s);
    }
}
///添加鼠标事件///
function MouseEvents(s)
{
	if(tab[s][5])
	{
		for(var i = 1;i <= tab[s][0];i++)
		{
			$(s + i).onmouseover = function()
			{
			    clearTimeout(tab[s][2]);
			    SingleSwitch(s,this.attributes.getNamedItem("id").nodeValue.split(s)[1]);
			}
			$(s + i).onmouseout = function()
			{
			    tab[s][4] = 0;
			    AutoSwitch(s);
			}
			$(tab[s][6] + s + i).onmouseover = function()
			{
			    clearTimeout(tab[s][2]);
			    SingleSwitch(s,this.attributes.getNamedItem("id").nodeValue.split(tab[s][6] + s)[1]);
			}
			$(tab[s][6] + s + i).onmouseout = function()
			{
			    tab[s][4] = 0;
			    AutoSwitch(s);
			}
		}
	}
}
///单次切换///
function SingleSwitch(s,id)
{
    for(var i = 1;i <= tab[s][0];i++)
    {
        if(i == id)
        {
            $(s + i).className = tab[s][7];
            $(tab[s][6] + s + i).className = tab[s][8];
        }
        else
        {
            $(s + i).className = "";
            $(tab[s][6] + s + i).className = tab[s][9];
        }
    }
}
///自动切换///
function AutoSwitch(s)
{
	if(tab[s][3])
	{
		for(var i = 1;i <= tab[s][0];i++)
		{ 
			if($(s+i).className == tab[s][7])
			{
				var id = tab[s][4]?(i%tab[s][0]?i + 1:1):i;
				break;
			}
		}
		SingleSwitch(s,id);
		tab[s][2] = setTimeout("AutoSwitch(\"" + s + "\");",tab[s][1]);
		tab[s][4] = 1;
	}
	else
	{ return false;	}
}
