//Create our Layers
var maxMenus = 0 ;	// Max Levels of Menus
var currentLevel = 1;
var arrImgs = new Array();
var LastLevel = 0;
var Menus = new Array();
var MenuTimer;
var MenuStatus = 0; var MenuCount = 0;
var log  ;
var debugActive = false;
var outer;
var outerLoaded = false;

document.write('<div id="inMenuOuter" style="visibility:hidden;"></div>');

function dd_init() {
		
	if(outer == null)
	{
		outer = document.getElementById("inMenuOuter");
		if(outer != null){
			outerLoaded = true;
		}
		/*if(outer == null){
			outer = document.createElement("div");
			outer.id = "inMenuOuter";
			outer.style.visibility = "hidden";
			if (document.body != null){
				//document.body.appendChild(outer);
				alert('body');
			}
		}
		*/
	}
	
	if(!outerLoaded){
		return;
	}
		
	// create x no of layers (inMenuDIV1, inMenuDIV2, inMenuDIV3 ....)	
	var newDiv = null;
	for (i=1; i <= maxMenus; i++) 
	{
	
		newDiv = document.getElementById("inMenuDIV" + i);
		if(newDiv == null){
			newDiv = document.createElement("div");
			newDiv.id = "inMenuDIV" + i
			newDiv.onmouseover = stayOpen
			newDiv.onmouseout = closeMenus
			newDiv.className = "menulist_Loader";
			newDiv.style.visibility = "hidden";
			newDiv.innerHTML = "Loading...";
			outer.appendChild(newDiv);
		}
	
	}

	clearTimeout(MenuTimer);
	MenuTimer = setInterval(function(){
		//If NOT hovering inside menu structure
		if (MenuStatus == 0) { 
			MenuCount += -1;
		}

		if (MenuCount == 0) 
		{
			for(var i=0; i<arrImgs.length; i++)
			{
				if (document.images) 
				{
					if(document[arrImgs[i]].src == eval(arrImgs[i] + "on.src"))
					{
						document[arrImgs[i]].src = eval(arrImgs[i] + "off.src");
					}
				}
			}
			hideMenus(1); 
			MenuCount += -1;
		}
		//This function will check to see if any menus are active
		//and if they have been open too long and need to be closed
	},200);
	
	
}

function arraySearch(arr, value)
{
	for(var i=0; i<arr.length; i++)
	{
		if (arr[i] == value)
		{
			return true;
		}
	}
	
	return false;
}

function MenuCache(ID, HTML) 
{
	this.ID = ID;
	this.HTML = HTML;
} 

function CheckCache(ID)
{
	for(var i=0; i<Menus.length; i++)
	{
		if(Menus[i].ID == ID)
		{
			cachedMenu = Menus[i];
			return cachedMenu;
		}
	}
	
	return null;
}

function writeLog(val) 
{
	// this function is for debugging purposes only
	//The debugger text appears at the bottom of the screen.
	
	var debugDiv = document.getElementById("Logger");
	
	if(debugDiv == null)
	{
		debugDiv = document.createElement("div");
		debugDiv.id = "Logger";
		
		document.body.appendChild(debugDiv);
	}
	
	if(debugActive)
	{
		var theVal = debugDiv.innerHTML;
		debugDiv.innerHTML = theVal + val;
	}
	
}

function closeMenus()
{
	MenuStatus = 0;
	MenuCount = 5;   // 1 seconds till close
	//This is the call that starts our countdown,  they left a menu item
}

function stayOpen()
{
	MenuStatus = 1;
	MenuCount = 5;
	//This is the call that cancels our countdown,  they re-entered a menu
}

function hideMenus(levels)
{
	//Hide the menus (all of them)
	for (i=levels; i <= maxMenus; i++) {
		var objMenu = document.getElementById('inMenuDIV' + i);
		objMenu.style.visibility = 'hidden';
	}
}

function appRoot()
{
	var URL = location.href;
	if (URL.indexOf('localhost') == -1)
		{	return('/'); }
	else
		{ return('/cms/'); }
}

//Hover over a dropdown image
function imgHover(ID, SiteID, PageID, menuCSS, level, linkCount, direction)
{	
	var imgName = 'img' + ID;
	if(!arraySearch(arrImgs, imgName)){
		arrImgs[arrImgs.length] = imgName
	}
	
	if (document.images) {
		document[imgName].src = eval(imgName + "on.src");
	}
	
	for(var i=0; i<arrImgs.length; i++){
		if(arrImgs[i] != imgName){
			if (document.images) {
				if(document[arrImgs[i]].src == eval(arrImgs[i] + "on.src")){
					document[arrImgs[i]].src = eval(arrImgs[i] + "off.src");
				}
			}
		}
	}
	
	if(SiteID != -1){
		findPOS(ID,SiteID,PageID,menuCSS, level, linkCount, direction);
	}
}

//Hover over a menu item in the div.
function mHover(useChild, pageid, siteid, currentItem, ParentID, menuCSS, level, linkCount, direction, isDept, CatID, DeptID, parentQS)
{
	
	hideMenus(level);
	
	var items = document.getElementById('menu_' + ParentID).getElementsByTagName('a');
	for(var i=0; i<items.length; i++){
		if(items[i] != currentItem){
			if(items[i].className.indexOf(menuCSS + "_selected") >= 0){
				items[i].className = "";
			}
		}
		else{
			items[i].className = menuCSS + "_selected";
		}
	}

	if(useChild == 1){
		openChild(pageid, siteid, currentItem, ParentID, menuCSS, level, linkCount, direction, isDept, CatID, DeptID, parentQS);
	}
}

function findPOS(ID,SiteID,PageID,menuCSS, level, linkCount, direction)
{	
	// this is the first level menu
	if (level <= 0) level = 2 ;
	
	maxMenus = level ;
		
	dd_init();

	var imgID = 'img_' + ID;
	var menuID = 'menu_' + ID;
		
	if(!outerLoaded){
		return;
	}
	
	var objImg = document.getElementById(imgID);

	var ourLeft = getAbsoluteLeft(imgID,0);

	// hide every menu
	hideMenus(1);
	
	var objMenu = document.getElementById('inMenuDIV1');
	objMenu.style.left = ourLeft;
	
	//Look in the cache to see if the menu has been loaded
	var cachedMenu = CheckCache(ID);
       
    if (cachedMenu == null)
    {
		//we didn't find the menu in the cache, so lets load it via ajax
		objMenu.innerHTML = "Loading...";

		var url = appRoot() + 'Framework/RenderIncludes/AJAX_Callback/dropdown.aspx?siteid=' + SiteID + '&pageid=' + PageID + '&clevel=1&levels=' + maxMenus + '&linkCount=' + linkCount + '&direction=' + direction + '&cssclass=' + menuCSS + '&parentdiv=' + ID;
		var ajax = new AjaxDelegate(url, loadMenu,ID,'inMenuDIV1', objImg, direction,0);
		ajax.Fetch();
    }
    else
    {
		//Cool, we found it, now just call the same the function that gets the ajax response
		loadMenu("", cachedMenu.HTML, ID, 'inMenuDIV1', objImg, direction, 0);
    }    
    
    stayOpen();
}

function loadMenu(url, response, ID, menuID, objImg, direction, level)
{
	//var menuID = 'inMenuDIV'
	var DivInnerHTML;
	var objMenu = document.getElementById(menuID)
	objMenu.innerHTML = response;

	DivInnerHTML = objMenu.innerHTML;
	if (DivInnerHTML.indexOf(">No Sub Items<") == -1){
		setMenuTop(objMenu, objImg, direction,level);
		objMenu.style.visibility = 'visible';

		//Check if its already in the cache
		var cachedMenu = CheckCache(ID);
		
		if(cachedMenu == null)
		{
			//It's not in the cache, so lets go ahead and add it
			cachedMenu = new MenuCache(ID, objMenu.innerHTML)
			Menus.push(cachedMenu);
		}
		
	}else{
		objMenu.style.visibility = 'hidden';
	}
}

function setMenuTop(objMenu, objImg, direction,level)
{
	var ClientHeight;
	if(level == 0){
		if(direction == 'up'){
			objMenu.style.top = getAbsoluteTop(objImg,1) - objMenu.clientHeight;
		}
		else{
			ClientHeight = objImg.clientHeight;
			if (ClientHeight == 0){ClientHeight = objImg.offsetHeight;}
			objMenu.style.top = getAbsoluteTop(objImg,1) + ClientHeight;
		}
	}
	else{
		if(direction == 'up'){
			objMenu.style.top = getAbsoluteTop(objImg,1) - (objMenu.clientHeight - objImg.clientHeight);
		}
		else{
			objMenu.style.top = getAbsoluteTop(objImg,1);
		}
	}
}

function openChild(pageid, siteid, currentItem, ParentID, menuCSS, level, linkCount, direction, isDept, CatID, DeptID, parentQS)
{
	// display the second and all other levels of menus	
	stayOpen();

	var ID 
	var theDiv ;

	var objMenu

	theDiv = 'inMenuDIV' + level ;
	ID = 'SUB:' + pageid + siteid + DeptID
	objMenu = document.getElementById(theDiv);
	if(objMenu != null){
		objMenu.style.visibility = 'hidden';
	}

	w =  window.innerWidth || document.body.offsetWidth;
	h =  window.innerHeight || document.body.offsetHeight

	if (pageid != -1 && objMenu != null)	
	{
		var menuID = 'menu_' + ParentID;	
		var objMenuDiv = document.getElementById(menuID);
		var parentWidth = parseInt(getStyle(objMenuDiv,'width'));
		
		var imgID = 'img_' + ID;
		//var divID = 'div_' + ParentID
		var objDiv = document.getElementById(menuID);

		var ourTop = getAbsoluteTop(currentItem,1);
		var ourLeft = getAbsoluteLeft(objDiv,1) + parentWidth ;

		if ((ourLeft + parentWidth) > w) { ourLeft = getAbsoluteLeft(objDiv,1) - parentWidth }
		
		objMenu.style.top = ourTop;
		objMenu.style.left = ourLeft;
		
		var cachedMenu = CheckCache(ID);
		
		if (cachedMenu == null)
		{
			objMenu.innerHTML = "Loading...";
			
			//Fixes the problem of having menulist2, menulist23, menulist234, etc
			if(level >= 3){
				menuCSS = menuCSS.substring(0,menuCSS.length-1);
			}

			var url = appRoot() + 'Framework/RenderIncludes/AJAX_Callback/dropdown.aspx?siteid=' + siteid + '&pageid=' + pageid + '&clevel='+ level + '&levels=' + maxMenus + '&linkCount=' + linkCount + '&direction=' + direction + '&cssclass=' + menuCSS + level + '&parentdiv=' + ID + '&isDept=' + isDept + '&CatID=' + CatID + '&DeptID=' + DeptID + '&' + parentQS;
			var ajax = new AjaxDelegate(url, loadMenu,ID,theDiv,currentItem,direction,1);
			ajax.Fetch();
		}
		else
		{
			loadMenu("", cachedMenu.HTML, ID, theDiv, currentItem, direction, 1);
		}
		
		stayOpen();
	}
}

function getStyle(el, style) { 
   if(!document.getElementById) return;     
   var value = el.style[style]; 
    if(!value) 
        if(document.defaultView) 
            value = document.defaultView. 
                 getComputedStyle(el, "").getPropertyValue(style);
        else if(el.currentStyle) 
            value = el.currentStyle[style]; 
     return value; } 

function getAbsoluteLeft(objectId, isObj) {
	if (isObj == 0) { o = document.getElementById(objectId) } else {o = objectId}
	oLeft = o.offsetLeft
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oLeft += oParent.offsetLeft
		o = oParent
	} 	return oLeft }

function getAbsoluteTop(objectId, isObj) {
	if (isObj == 0) { o = document.getElementById(objectId) } else {o = objectId}
	oTop = o.offsetTop
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oTop += oParent.offsetTop
		o = oParent
	}	
	return oTop }
	
	
