////////////////////////////////////////////////////
/*
*   顶部栏目下拉菜单
*
*   Copyright By www.jspsoft.cn
*
*	版权所有   llc@jspsoft.cn
*   
*    2009-12-31
*
*   1、在需要鼠标移上显示菜单的元素上加入 code='#column_code#' onmouseenter='showColumnTopMenu(this,true,0,8)'
*   2、在页面里加入栏目XML：<xml id="xmldoc"><root transparent="100" bgcolor1="darkblue" fgcolor1="#ffffff">${jspcms.getColumnXML(2)}</root></xml>，其中bgcolor1和fgcolor1分别表示菜单项在鼠标移上时的背景色即前景色,transparent表示透明度，100为不透明
*/
////////////////////////////////////////////////////////////////////////////////////////
var isIE =   (navigator.appName=="Microsoft Internet Explorer");

function parseXML(st){
     if(isIE){
         var result = new ActiveXObject("Microsoft.XMLDOM");
		 result.async=false;
         result.loadXML(st);
     }else if (document.implementation &&document.implementation.createDocument){//FF
		  result= document.implementation.createDocument("","",null);	 
		  result.async=false;
		  result.loadXML(st);
	 }else{
		alert('浏览器不支持脚本,无法加载XML文件');
	 } 
	try{result.setProperty("SelectionLanguage","XPath");}catch(exp){}
    return result;
}

/////////////////////////////////////
var GetNodeValue = function(obj){
	var str = "";
	if(window.ActiveXObject){    //IE
		str = obj.text;
	}else{ //Mozilla
		try{
		   str = obj.childNodes[0].nodeValue;
		}catch(ex){
		   str = "";
		}
	}
	return str;
}

if(document.implementation && document.implementation.createDocument){
	XMLDocument.prototype.loadXML = function(xmlString){
		var childNodes = this.childNodes;
		for (var i = childNodes.length - 1; i >= 0; i--)this.removeChild(childNodes[i]);
		var dp = new DOMParser();
		var newDOM = dp.parseFromString(xmlString, "text/xml");
		var newElt = this.importNode(newDOM.documentElement, true);
		this.appendChild(newElt);
	};

// check for XPath implementation
if(document.implementation.hasFeature("XPath", "3.0")){
    // prototying the XMLDocument
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode){
			if( !xNode ) { xNode = this; }
			var oNSResolver = this.createNSResolver(this.documentElement)
			var aItems = this.evaluate(cXPathString, xNode, oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
			var aResult = [];
			for( var i = 0; i < aItems.snapshotLength; i++){
				aResult[i] = aItems.snapshotItem(i);
			}
			return aResult;
		}
		// prototying the Element
		Element.prototype.selectNodes = function(cXPathString){
		if(this.ownerDocument.selectNodes)
		{
		  return this.ownerDocument.selectNodes(cXPathString, this);
		}else{
			throw "For XML Elements Only";
		}
    }
}

// check for XPath implementation
if(document.implementation.hasFeature("XPath", "3.0"))
{
    // prototying the XMLDocument
    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
    {
		if( !xNode ) { xNode = this; }
		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}else{
			return null;
		}
    }  
    // prototying the Element
    Element.prototype.selectSingleNode = function(cXPathString)
    {  
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}else{
			throw "For XML Elements Only";
		}
    }
}
}

////////////////////////////////////
function getAttribute(name,list){
    if(!list)return false;
    for(var i=0;i<list.length;i++){
        if(list[i].nodeName.toLowerCase()==name.toLowerCase()){
            return list[i].nodeValue;
        }
    }
    return false;    
} 

function gourl(url){
    if(url==""){return;}
    window.open(url,"_blank");
}
///////////////////////////////////
var flowMenuThread=null;
var curflowMenuObj=null;

function showColumnTopMenu(sObj,showIt,offset_x,offset_y){
	if(!document||document.readyState!="complete"||!document.body||!$("xmldoc"))return;
	if(flowMenuThread){
		clearTimeout(flowMenuThread);
		flowMenuThread=null;
	}
	offset_x=(offset_x?offset_x:0);
	offset_y=(offset_y?offset_y:0);
	showIt=(showIt?showIt:true);

	if(!(curflowMenuObj=$("DownMenuPanel"))){
		curflowMenuObj=document.createElement("SPAN");
		curflowMenuObj.setAttribute("id","DownMenuPanel");
		curflowMenuObj.style.cssText="display:none;position:absolute;left:-1000;line-height:22px;background-color:white;z-index:9;border:1 #c8c8c8 solid;border-top:0;background-color:#F5F5F5;text-align:center;padding:5;z-index:999;";
		document.body.appendChild(curflowMenuObj);
	}
	var xmlobj=parseXML("<?xml version=\"1.0\" encoding=\"gb2312\"?>"+$("xmldoc").innerHTML).documentElement;

	if(!xmlobj||!curflowMenuObj||!sObj)return;
	
	var rect=getElementRect(sObj);

	curflowMenuObj.innerHTML="";
	var current_code=sObj.getAttribute("code");

	if(!current_code||current_code==""){alert("栏目代码为空");return hideColumnTopFlowMenu();}
	var item_count=0;
	var nodes=xmlobj.selectNodes("/root/a/b");
	var confNode=xmlobj.selectSingleNode("/root");
	
	var opacityValue=0;
    try{opacityValue=parseInt(confNode.getAttribute("transparent"));}catch(ex){}

	curflowMenuObj.style.filter="alpha(opacity="+opacityValue+");";

	var item_bg0=(confNode.getAttribute("bgcolor0")?confNode.getAttribute("bgcolor0"):"transparent");
	var item_fg0=(confNode.getAttribute("fgcolor0")?confNode.getAttribute("fgcolor0"):"black");
	var item_bg1=(confNode.getAttribute("bgcolor1")?confNode.getAttribute("bgcolor1"):"darkblue");
	var item_fg1=(confNode.getAttribute("fgcolor1")?confNode.getAttribute("fgcolor1"):"white");
	
	var tmphtml="";

	for(var i=0;i<nodes.length;i++){
		if(isIE){
			if(nodes[i].getAttribute("b").indexOf(current_code)==0&&nodes[i].getAttribute("b")!=current_code){
				//curflowMenuObj.insertAdjacentHTML("beforeEnd","<div><a onmouseover=\"this.style.backgroundColor='"+item_bg1+"';this.style.color='"+item_fg1+"';\" onmouseout=\"this.style.backgroundColor='"+item_bg0+"';this.style.color='"+item_fg0+"';\" style='display:table-cell;color:#000;height:20px;padding:3 10 0 10;' href='"+nodes[i].getAttribute("d")+"'>"+nodes[i].getAttribute("c")+"</a></div>");
				tmphtml+="<div><a onmouseover=\"this.style.backgroundColor='"+item_bg1+"';this.style.color='"+item_fg1+"';\" onmouseout=\"this.style.backgroundColor='"+item_bg0+"';this.style.color='"+item_fg0+"';\" style='display:table-cell;color:#000;height:20px;padding:3 10 0 10;' href='"+nodes[i].getAttribute("d")+"'>"+nodes[i].getAttribute("c")+"</a></div>";
				item_count++;
				
			}
		}else{
			var code1	=getAttribute("b",nodes[i].attributes);
			var href1	=getAttribute("d",nodes[i].attributes);
			var text11	=getAttribute("c",nodes[i].attributes);

			if(code1.indexOf(current_code)==0&&code1!=current_code){
				//curflowMenuObj.insertAdjacentHTML("beforeEnd","<div><a onmouseover=\"this.style.backgroundColor='"+item_bg1+"';this.style.color='"+item_fg1+"';\" onmouseout=\"this.style.backgroundColor='"+item_bg0+"';this.style.color='"+item_fg0+"';\" style='display:table-cell;color:#000;height:20px;padding:3 10 0 10;' href='"+nodes[i].getAttribute("d")+"'>"+nodes[i].getAttribute("c")+"</a></div>");
				tmphtml+="<div><a onmouseover=\"this.style.backgroundColor='"+item_bg1+"';this.style.color='"+item_fg1+"';\" onmouseout=\"this.style.backgroundColor='"+item_bg0+"';this.style.color='"+item_fg0+"';\" style='display:table-cell;color:#000;height:20px;padding:3 10 0 10;' href='"+nodes[i].getAttribute("d")+"'>"+nodes[i].getAttribute("c")+"</a></div>";
				item_count++;
			}
		}
	}
	if(item_count==0)return hideColumnTopFlowMenu();
	else curflowMenuObj.innerHTML=tmphtml;

	if(curflowMenuObj.style.display!=""&&!showIt)return;

	curflowMenuObj.style.left=rect.x+offset_x;
	curflowMenuObj.style.top=rect.y+rect.h-8+offset_y;
	curflowMenuObj.style.display=(showIt?"":"none");

	sObj.onmouseenter=function(){showColumnTopMenu(sObj,showIt,offset_x,offset_y);};
	sObj.onmouseleave=function(){hideColumnTopFlowMenu();};
	curflowMenuObj.onmouseenter=function(){showColumnTopMenu(sObj,showIt,offset_x,offset_y);};
	curflowMenuObj.onmouseleave=function(){hideColumnTopFlowMenu();};
}
function hideColumnTopFlowMenu(){
	flowMenuThread=setTimeout(function(){
		if(curflowMenuObj){
			curflowMenuObj.style.left=-1000;
			curflowMenuObj.style.display="none";
		}
	},200);
}