﻿var Menu = function(){
	this.trigger = null;	
	this.width = 180;	
	this.wraper = null;	
	this.currItem = null;	
	this.xml = null;	
	this.xmlString = null;	
	this.mainNode = null;	

	this.childNodes = [];	
};


Menu.prototype.regEvent = function(){
	
	var self = this;
	

	sl.addEventHandler(document ,"click" ,function(){
		self.close();
	});


	if (this.trigger)
	{
		sl.addEventHandler(document ,"contextmenu" ,function(e){
			if (e.preventDefault)
			{
				e.stopPropagation();
				e.preventDefault();    
			}
			return false;
		});

		sl.addEventHandler(this.trigger ,"mousedown" ,function(e){
			e = e?e:event;
			if (e.button == 2)
			{
				self.close();
				self.show(e);
			}
		});
	}
};


Menu.prototype.setAttribute = function(){
	

	for (var i=0;i<this.childNodes.length ;i++ )
	{
		this.setMenuAttribute(this.childNodes[i]);
	}


	var li = sl.getElement("li" ,this.wraper);
	for (var i=0;i<li.length ;i++ )
	{
		this.setItemAttribute(li[i]);
	}
};


Menu.prototype.setMenuAttribute = function(menu){

	menu.style.display = "none";
	menu.style.width = this.width +"px";
	var ul = sl.getElement("ul" ,menu);
	ul[ul.length-1].className = "last";
};


Menu.prototype.setItemAttribute = function(item){

	var self = this;

	if (item.getAttribute("enabled") != "false")
	{
		sl.addEventHandler(item ,"mouseover" ,function(){
			self.mouseover(item);
		});
		sl.addEventHandler(item ,"mouseout" ,function(){
			self.mouseout(item);	
		});

		if (item.getAttribute("child") != null)
		{
			item.innerHTML = "<span class=\"prevMark\">"
							 + item.innerHTML
							 + "</span><span class=\"mark\">►</span>";
				
			sl.addEventHandler(item ,"click" ,function(e){
				Menu.cancelBubble(e);	
			});
		}
	}
	else
	{
		sl.addEventHandler(item ,"click" ,function(e){
			Menu.cancelBubble(e);	
		});
		item.style.color = "#D7CFBE";
	}
	item.className = "out";
};


Menu.prototype.mouseover = function(o){
	this.closeChildren(o.parentNode.parentNode);
	o.className = "over";

	if (o.getAttribute("child") != null)
	{
		this.currItem = o;
		this.showChildren(o ,o.getAttribute("child"));
	}
};


Menu.prototype.mouseout = function(o){
	if (o != this.currItem)
	{
		o.className = "out";
	}
};


Menu.prototype.showChildren = function(o ,id){
	var pos = sl.getElementPos(o);
	var n = document.getElementById(id);
	if (n)
	{
		n.style.top = pos.y +"px";
		n.style.left = (pos.x + this.width - 15) +"px";
		n.style.display = "";
	}
};


Menu.prototype.closeChildren = function(parentNode){
	var li = parentNode.getElementsByTagName("li");
	var n;
	for (var i=0;i<li.length ;i++ )
	{
		li[i].className = "out";

		if (li[i].getAttribute("child") != null)
		{
			n = document.getElementById(li[i].getAttribute("child"));
			if (n)
			{
				this.closeChildren(n);
				n.style.display = "none";
			}
		}
	}
};


Menu.prototype.show = function(e ,align){
	Menu.cancelBubble(e);
	e = e?e:event;
	var ele = e.target?e.target:e.srcElement;
	var pos;
	if (this.trigger)
	{
		pos = sl.getMousePos(e);
	}
	else
	{
		pos = sl.getElementPos(ele);
		if (align == "left")
		{
			pos.y = pos.y + ele.offsetHeight;
		}
		if (align == "right")
		{
			pos.x = pos.x + ele.offsetWidth;
		}
	}

	this.mainNode.style.top = pos.y +"px";
	this.mainNode.style.left = pos.x +"px";
	this.mainNode.style.display = "";
};


Menu.prototype.close = function(){
	for (var i=0;i<this.childNodes.length ;i++ )
	{
		this.childNodes[i].style.display = "none";
	}
};


Menu.prototype.loadXml = function(){
	var self = this;
	if (this.xml)
	{
		sl.ajax(this.xml ,function(text){
			self.xmlString = text;
		} ,"get");
	}	
};


Menu.prototype.resolve = function(){
	var guid = Menu.getGuid();
	var xml = this.xmlString;
	xml = xml.replace("<root>" ,"<div id=\""+ guid +"\">");
	xml = xml.replace("</root>" ,"</div>");
	xml = xml.replace(/<menu/g ,"<div class=\"menu\"");
	xml = xml.replace(/<\/menu>/g ,"</div>");

	var div = document.createElement("div");
	div.innerHTML = xml;
	document.body.appendChild(div);
	
	this.wraper = sl.getElement("#"+ guid);
	this.childNodes = sl.getElement("div" ,this.wraper);
	this.mainNode = this.childNodes[0];
};


Menu.getGuid = function(){
	var guid = "";
	for(var i=1;i<=32;i++)
	{
		var n = Math.floor(Math.random()*16.0).toString(16);
		guid += n;
	}

	return guid;
}


Menu.cancelBubble = function(e){
	e = e?e : event;
	if (e.stopPropagation)
	{
		e.stopPropagation();
	}
	else
	{
		e.cancelBubble = true;
	}
};


Menu.prototype.init = function(){
	

	this.loadXml();


	this.resolve();

	this.regEvent();
	

	this.setAttribute();

};

var sl = window.sl = {

	/*
	** <summary>

    ** </summary>
	** <param name="target">监听对象</param>
	** <param name="eventType">事件类型</param>
	** <param name="handler">处理函数</param>
	*/
	addEventHandler:function(target, eventType, handler){
		if (target.addEventListener)
		{
			target.addEventListener(eventType, handler, false);
		} 
		else if (target.attachEvent) 
		{
			target.attachEvent("on" + eventType, handler);
		} 
		else 
		{
			target["on" + eventType] = handler;
		}
	} ,

	/*
	** <summary>

	** </summary>
	*/
	stringBuilder:function(){
		this.arr = [];
		

		this.push = function(item){
			this.arr.push(item);
		};


		this.clear = function(){
			this.arr.length = 0;
		};
		

		this.toString = function(){
			return this.arr.join("");
		};
	}
};


/*
** <summary>
** 获取对象
** </summary>
** <param name="selector">选择参数</param>
** <param name="parentNode">父节点</param>
*/
sl.getElement = function(selector ,parentNode){
	/*
	selector参数解释(均支持以,隔开的多重选择)
	#)前缀根据id返回对象
	=)根据属性返回对象
	 )无符号则根据tagName返回对象
	*/

	/*#)前缀根据id返回对象*/
	var execId = function(selector){
		var selector = selector.replace(/#| /g ,"");
		var arr = selector.split(",") ,rets = [] ,temp;

		for (var i=0;i<arr.length ;i++ )
		{
			temp = document.getElementById(arr[i]);
			if (temp)
			{
				rets.push(temp);
			}
		}

		return (rets.length > 1)? rets : rets[0];
	};

	/*=)根据属性返回对象*/
	var execAttribute = function(selector ,parentNode){
		var parentNode = parentNode? parentNode : document;
		var objects = parentNode.getElementsByTagName("*");
		var arr = selector.split("=") ,rets = [] ,temp;

		for (var i=0;i<objects.length ;i++ )
		{
			if (objects[i].getAttribute(arr[0]) == arr[1])
			{
				rets.push(objects[i]);
			}
		}

		return (rets.length > 1)? rets : rets[0];
	};
	

	var execTagName = function(selector ,parentNode){
		var parentNode = parentNode? parentNode : document;
		return parentNode.getElementsByTagName(selector);
	};


	if (selector.indexOf("#") != -1)
	{
		return execId(selector ,parentNode);
	}
	else if (selector.indexOf("=") != -1)
	{
		return execAttribute(selector ,parentNode);	
	}
	else
	{
		return execTagName(selector ,parentNode);
	}
};

/*
** <summary>
** ajax应用
** </summary>
** <param name="url">资源地址</param>
** <param name="callback">回调方法</param>
** <param name="method">请求方式</param>
** <param name="data">所发送数据</param>
*/
sl.ajax = function(url ,callback ,method ,data){
	var method = method ? method : "get";
	var data = data ? data : null;
	var url = (method == "get") ? (url +"?"+ data) : url;
	var http = window.ActiveXObject? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	
	var doGet = function(){
		http.open(method ,url ,false);
		http.setRequestHeader("If-Modified-Since" , "0" );
		http.send(null);
		callback(http.responseText);
	};
	
	var doPost = function(){
		http.open(method ,url ,false);
		http.setRequestHeader("content-length" ,data ?data.length : 0);
		http.setRequestHeader("Content-Type" ,"application/x-www-form-urlencoded");
		http.send(data);
		callback(http.responseText);
	};
	
	if (method == "post")
	{
		doPost();
	}
	else
	{
		doGet();
	}
	http = null;
};

/*
** <summary>
** URl参数处理
** </summary>
** <param name="arg">参数</param>
*/
sl.request = function(arg){
	
	var queryString = function(arg){
		var uri = window.location.search;
		var re = new RegExp(""+ arg +"\=([^\&\?]*)", "ig");
		return ((uri.match(re))?(uri.match(re)[0].substr(arg.length+1)):null);
	};

	var queryStrings = function(){
		var uri = window.location.search;
		var re = /\w*\=([^\&\?]*)/ig;
		var retval=[];
		while ((arr = re.exec(uri)) != null)
		retval.push(arr[0]);
		return retval;
	};

	
	if (arg)
	{
		return queryString(arg);
	}
	else
	{
		return queryStrings();
	}
};

/*
** <summary>
** Cookie处理
** </summary>
** <param name="name">cookie键名</param>
** <param name="value">cookie键值</param>
** <param name="option">可选参数</param>
*/
sl.cookie = {

	setCookie:function(name, value, option){ 
		var str = name +"="+ escape(value); 
		if(option){ 
			if(option.expireHours){ 
				var d = new Date(); 
				d.setTime(d.getTime()+option.expireHours*3600*1000); 
				str += "; expires="+d.toGMTString(); 
			} 
			if(option.path) str += "; path="+option.path; 
			if(option.domain) str += "; domain="+option.domain; 
			if(option.secure) str += "; true"; 
		}
		document.cookie = str; 
	} ,

	
	getCookie:function(name){ 
		var arr = document.cookie.split("; "); 
		if(arr.length == 0) return ""; 
		for(var i=0; i <arr.length; i++)
		{ 
			tmp = arr[i].split("="); 
			if(tmp[0] == name)
			{
				return unescape(tmp[1]); 
			}
		} 
		return "";
	} ,

	
	delCookie:function(name){ 
		this.setCookie(name,"",{expireHours:-1});
	} ,

	getLength:function(){
		return document.cookie.split("; ").length;
	}
};

/*
** <summary>
** 获取鼠标位置
** </summary>
** <param name="e">事件对象</param>
*/
sl.getMousePos = function (e){
	var e = e?e : event;
	if (e.pageX || e.pageY)
	{
		return { x: e.pageX, y: e.pageY };
	}
	else
	{
		return { x: e.clientX + document.documentElement.scrollLeft - document.body.clientLeft,
				 y: e.clientY + document.documentElement.scrollTop  - document.body.clientTop 
		};
	}
};

/*
** <summary>
** 获取对象位置
** </summary>
** <param name="element">对象</param>
*/
sl.getElementPos = function(element){
	var offsetTop = element.offsetTop;
	var offsetLeft = element.offsetLeft;

	while(element = element.offsetParent) 
	{
		offsetTop += element.offsetTop;
		offsetLeft += element.offsetLeft;
	}
	
	return {x:offsetLeft ,y:offsetTop};
};


// JavaScript Document
function goTopSem(){
        var obj=document.getElementById("goTopBtn");
        function getScrollTop(){
                return document.documentElement.scrollTop;
            }
        function setScrollTop(value){
                document.documentElement.scrollTop=value;
            }    
        window.onscroll=function(){getScrollTop()>0?obj.style.display="":obj.style.display="none";}
        obj.onclick=function(){
            var goTop=setInterval(scrollMove,10);
            function scrollMove(){
                    setScrollTop(getScrollTop()/1.1);
                    if(getScrollTop()<1)clearInterval(goTop);
                }
        }
    }
