﻿var isIE = document.all?true:false
if (!isIE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = checkMouse;

var UnpinnedMenus;


Sys.Application.add_load(initUnpinnedMenus);


function initUnpinnedMenus()
{
    UnpinnedMenus = getCookie("UnpinnedMenus");

    if (UnpinnedMenus != null)
    {
        menuarr = UnpinnedMenus.split(';');
        for (var x = 0; x < menuarr.length-1; x++)
        {
            container = document.getElementById(menuarr[x]);
            if (container != null && document.getElementById(menuarr[x] + 'Pin') != null)
            {           
                document.getElementById(container.id +'Pin').src = document.getElementById(container.id +'Pin').src.replace('/pinned.gif', '/unpinned.gif');
                container.style.width = container.offsetWidth;
                container.parentNode.style.minWidth = '0px';
                container.style.position = 'absolute';
                container.style.zIndex = -1;
            }
        }
    }
}

function TogglePinning(container, enforceMinWidth)
{
    if (UnpinnedMenus == null)
        UnpinnedMenus = "";
        
    container = document.getElementById(container);
    
    if (UnpinnedMenus.indexOf(container.id + ';') < 0)
    {
        UnpinnedMenus += container.id + ";";
        document.getElementById(container.id +'Pin').src = document.getElementById(container.id +'Pin').src.replace("/pinned.gif", "/unpinned.gif");
        container.style.width = container.offsetWidth;
        container.parentNode.style.minWidth = '0px';
        container.style.position = 'absolute';
        container.style.zIndex = -1;
    }
    else
    {
        UnpinnedMenus = UnpinnedMenus.replace(container.id + ";", "");
        document.getElementById(container.id +'Pin').src = document.getElementById(container.id +'Pin').src.replace("/unpinned.gif", "/pinned.gif");

        container.style.width = 'auto';

        if (enforceMinWidth)
        {
            totalWidth = 0;
            childnodes = container.parentNode.childNodes;
            for (var x = 0; x< childnodes.length; x++)
            {
                totalWidth += childnodes[x].offsetWidth;
            }
            container.parentNode.style.minWidth = totalWidth;
        }
        container.style.position = 'relative';
        container.style.zIndex = 100;
    }
    
    today = new Date();
    today.setFullYear(today.getFullYear()+5);
    setCookie("UnpinnedMenus", UnpinnedMenus, today);
}




function checkMouse(e) {
    if (UnpinnedMenus == null || UnpinnedMenus == "")
      return;
      
  if (isIE) {
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } else {
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  if (tempX < 0){tempX = 0;}
  if (tempY < 0){tempY = 0;}
  

      
  menuarr = UnpinnedMenus.split(';');
  for (var x = 0; x < menuarr.length-1; x++)
  {
      container = document.getElementById(menuarr[x]);
      if (container != null)
      {
        var hitWidth = 0;
        if (container.childNodes[0].style.display == 'none' && container.childNodes[0].offsetLeft < 1)
           hitWidth = 25;
        else
            hitWidth = container.offsetLeft+container.offsetWidth;
      
        if ((tempY > container.offsetTop && tempY < container.offsetTop+container.offsetHeight)
            && (tempX > container.offsetLeft && tempX < hitWidth))
        {
            container.childNodes[0].style.display = 'block';
            container.style.zIndex = 100;
        }
        else
        {
           container.childNodes[0].style.display = 'none';
           container.style.zIndex = -1;
        }
      }
  }
  
  return true;
}



var depth = 0;
var clicked;
var currentDrawerID = "";

function startNavList(menuid, menuclass, parentclass, drawersLevel) {
		navRoot = document.getElementById(menuid);
		Recurse(navRoot, menuclass, parentclass, drawersLevel);
		
		currentDrawerID = getCookie("CurrentDrawerID");
		
		if (currentDrawerID != null && currentDrawerID != "")
		{
	        if (document.getElementById(currentDrawerID) != null)
		        document.getElementById(currentDrawerID).style.display='inline';
		}    

}

function Recurse(startNode, hoverClass, parentClass, drawersLevel) {
	
	depth++;
		
	for (var x=0; x < startNode.childNodes.length; x++)
	{
		node = startNode.childNodes[x];
				
		if (node.nodeName=="LI")
		{
			if (node.getElementsByTagName("UL").length > 0) {
				node.className+= " "+parentClass;
			}

    			    node.onmouseover=function() 
			    {
	    			clicked = false;
		    		if (isIE) this.className+=" "+hoverClass;
							    	
			    }
			
			
			/* checking for anchor inside */
			var anchorFound = false;

			for (var z=0; z < node.childNodes.length; z++) {
				if (!anchorFound) {
					var targetNode = node.childNodes[z];
					if (targetNode.nodeName == "A") {
						anchorFound = true;
						
						/*if (targetNode.href != '')
					        targetNode.href = "javascript:go('"+targetNode.href+"')";
				        else
				        {
				            targetNode.href = 'javascript:void(0)';
				            targetNode.style.cursor = 'default';
				        }*/
						
						if (depth > drawersLevel) 
						{
						    /*
						    node.onclick = function() {
						    if (!clicked) {
    						        clicked = true;
        	    	                
		    				        for (var h=0; h < this.childNodes.length; h++) {
			    				        if (this.childNodes[h].nodeName == "A") {
				    				        window.location = this.childNodes[h].getAttribute('HREF');
					    		        }
				    		        }
					            }
					        }
					        */
						} 
						else
						{
						
						    node.onclick=function() {
						        var ULTags = this.getElementsByTagName("UL");
    						    
						        if (ULTags.length > 0)
						        {
						            if (currentDrawerID != "" && currentDrawerID != ULTags[0].id)
						                if (document.getElementById(currentDrawerID) != null)
    						                document.getElementById(currentDrawerID).style.display='none';
    						        
    						        
                                    var ULStyle = ULTags[0].style;
                                    
                                    if (ULStyle.display == 'inline')
					                    ;//ULStyle.display = 'none';
					                else 
					                    ULStyle.display = 'inline';
    					                
					                currentDrawerID = ULTags[0].id;
					                var dt = new Date();
					                dt.setFullYear(dt.getFullYear()+10);
					                setCookie("CurrentDrawerID", currentDrawerID, dt, "/", "", false);
						        }
    						
    						
						        
					        }
					    }
						
					}
					else {
						node.onclick=function() {
							
							clicked = true;
						}
					}
				}
			}
			if (!anchorFound) {
				node.style.cursor='default';
			}
			
			if (isIE)
			{
				node.onmouseout=function() {
					this.className=this.className.replace(" "+hoverClass, "");
				};
			}
		}
		
		if (node.childNodes.length > 0) {
			Recurse(node, hoverClass, parentClass, drawersLevel);
		}
	}
	depth--;
}

var going = false;

function go(targetUrl) {
	if (!going) {
		document.location=targetUrl;
		going = true;
		window.status = "Loading";
		addDots();
	}
}

function addDots() {
	if (window.status.length < 20) {
		window.status += ".";
	}else{
		window.status = "Loading";
	}
	setTimeout(addDots, 200)
}





function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toUTCString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
