/************************************************************************************************************
	(C) www.dhtmlgoodies.com, October 2005
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/	
var expandFirstItemAutomatically = false;	// Expand first menu item automatically ?
var initMenuIdToExpand = false;	// Id of menu item that should be initially expanded. the id is defined in the <li> tag.
var expandMenuItemByUrl = true;	// Menu will automatically expand by url - i.e. if the href of the menu item is in the current location, it will expand
var initialMenuItemAlwaysExpanded = true;	// NOT IMPLEMENTED YET

var debug = false && navigator.userAgent.indexOf('MSIE') < 0;
var debugScroll = debug && false;
var debugInit = debug && true;
var debugExpand = debug && true;
var dhtmlgoodies_slmenuObj;
var divToScroll = false;
var ulToScroll = false;	
var divCounter = 1;
var otherDivsToScroll = new Array();
var divToHide = false;
var parentDivToHide = new Array();
var fullAnchorList = new Array();
var ulToHide = false;
var offsetOpera = 0;
if(navigator.userAgent.indexOf('Opera')>=0)offsetOpera=1;	
var slideMenuHeightOfCurrentBox = 0;
var objectsToExpand = new Array();
var initExpandIndex = 0;
var alwaysExpanedItems = new Array();

var overrideLoc = 'defaultNone.aspx';
	
function popMenusToShow()
{
	var obj = divToScroll;
	var endArray = new Array();
	while(obj && obj.tagName!='BODY'){
		if(obj.tagName=='DIV' && obj.id.indexOf('slideDiv')>=0){
			var objFound = -1;
			for(var no=0;no<otherDivsToScroll.length;no++){ 
				if(otherDivsToScroll[no]==obj){ 
					objFound = no;		
				}					
			}	
			if(objFound>=0){
				otherDivsToScroll.splice(objFound,1);	
			}		
		}	
		obj = obj.parentNode;
	}	
}
 
function showSubMenu(e,inputObj)
{ 
	if (debugScroll) console.log('showSubMenu()\nshowSubMenu: otherDivsToScroll.length = ' + otherDivsToScroll.length);
	if (debugScroll) console.log('showSubMenu: objectsToExpand.length = ' + objectsToExpand.length);
	if (this && this.tagName && this.tagName === 'A')inputObj = this.parentNode;
	if (debugScroll) console.log('showSubMenu: inputObj.parentNode.parentNode.id = ' + inputObj.parentNode.parentNode.id);
	
	//work out if this is a top-level menu or not and set the background colour if so. 
	if(inputObj && inputObj.parentNode.parentNode.id.indexOf('dhtmlgoodies_slidedown_menu') > -1)
		inputObj.style.backgroundColor = level1HoverColour;
	else if (inputObj) {
		if (debugScroll) console.log('showSubMenu: set ' + inputObj.parentNode.parentNode.id + ' height to 100%');
		inputObj.parentNode.parentNode.style.height = '100%';
	}
		
	if(inputObj && inputObj.tagName=='LI'){
		divToScroll = inputObj.getElementsByTagName('DIV')[0];
		if (debugScroll && divToScroll) console.log('showSubMenu: divToScroll = ' + divToScroll.id);
		for(var no=0;no<otherDivsToScroll.length;no++){ 
			if (debugScroll) console.log('showSubMenu: otherDivsToScroll[' + no + '] = ' + otherDivsToScroll[no].id);
			if(otherDivsToScroll[no]==divToScroll)return; 
		}
		inputObj.onmouseover = "";
		inputObj.onmouseout = "";
		
		//remove focus outline
		var aTag = inputObj.getElementsByTagName('A')[0];
		aTag.blur(); // most browsers 
		aTag.hideFocus = true; // internet explorer
		aTag.style.outline = 'none'; // mozilla
	} 
	hidingInProcess = false;
	if(otherDivsToScroll.length>0){
		if(divToScroll){
			if(otherDivsToScroll.length>0){
				if (debugScroll) console.log('showSubMenu: call popMenusToShow()');
				popMenusToShow();
				if (debugScroll) console.log('showSubMenu: otherDivsToScroll.length 2 = ' + otherDivsToScroll.length);
			}
			if(otherDivsToScroll.length>0){
				if (debugScroll) console.log('showSubMenu: call autoHideMenus()');
				autoHideMenus();
				hidingInProcess = true;
			}
		}
	}
	if(divToScroll && !hidingInProcess){
		divToScroll.style.display='';
		otherDivsToScroll.length = 0;
		otherDivToScroll = divToScroll.parentNode;
		if (debugScroll) console.log('showSubMenu: otherDivsToScroll.push ' + divToScroll.id);
		otherDivsToScroll.push(divToScroll);	
		while(otherDivToScroll && otherDivToScroll.tagName!='BODY'){
			if(otherDivToScroll.tagName=='DIV' && otherDivToScroll.id.indexOf('slideDiv')>=0){
				if (debugScroll) console.log('showSubMenu: otherDivsToScroll.push ' + otherDivToScroll.id);
				otherDivsToScroll.push(otherDivToScroll);
			}
			otherDivToScroll = otherDivToScroll.parentNode;
		}			
		ulToScroll = divToScroll.getElementsByTagName('UL')[0];
		if(divToScroll.style.height.replace('px','')/1<=1)scrollDownSub();
	}	
	
	return false;
}
 
function autoHideMenus()
{
	if (debugScroll) console.log('autoHideMenus: otherDivsToScroll.length = ' + otherDivsToScroll.length);
	if(otherDivsToScroll.length>0){
		divToHide = otherDivsToScroll[otherDivsToScroll.length-1];
		if (debugScroll) console.log('autoHideMenus: divToHide = ' + divToHide.id);
		parentDivToHide.length=0;
		var obj = divToHide.parentNode.parentNode.parentNode;
		while(obj && obj.tagName=='DIV'){			
			if(obj.id.indexOf('slideDiv')>=0)parentDivToHide.push(obj);
			obj = obj.parentNode.parentNode.parentNode;
			if (debug) console.log('autoHideMenus: parentDivToHide = ' + obj.id);
		}
		var tmpHeight = (divToHide.style.height.replace('px','')/1 - slideMenuHeightOfCurrentBox);
		if(tmpHeight<0)tmpHeight=0;
		if(slideMenuHeightOfCurrentBox)divToHide.style.height = tmpHeight  + 'px';
		ulToHide = divToHide.getElementsByTagName('UL')[0];
		slideMenuHeightOfCurrentBox = ulToHide.offsetHeight;
		scrollUpMenu();		
	}else{
		slideMenuHeightOfCurrentBox = 0;
		if (debugScroll) console.log('autoHideMenus: call showSubMenu');
		showSubMenu();			
	}
}
 
function scrollUpMenu()
{ 
	//change colour
	if (debugScroll) console.log('scrollUpMenu: divToHide = ' + divToHide.id);
	var parentLI = divToHide.parentNode;
	if (parentLI != null && parentLI.tagName == 'LI') {
		//parentLI.style.backgroundColor = '#556173';
		//parentLI.onmouseover = function() {this.style.backgroundColor = level1HoverColour;}
		//parentLI.onmouseout = function() {this.style.backgroundColor = level1NormalColour;}
		//var aTags = ulToHide = divToHide.getElementsByTagName('A');
		//for (var x=0; x<aTags.length; x++) {
		//	aTags[x].onmouseover = "function() {this.backgroundColor =  '' + level1HoverColour + '';}";
		//}
	}
		
	var height = divToHide.offsetHeight;
	//height-=15;
	height = 0;
	if(height<0)height=0;
	divToHide.style.height = height + 'px';
 
	for(var no=0;no<parentDivToHide.length;no++){	
		parentDivToHide[no].style.height = parentDivToHide[no].getElementsByTagName('UL')[0].offsetHeight + 'px'; 
	}
	if (debug) console.log('scrollUpMenu: height = ' + height);
	if(height>0){
		divToHide.style.display='none';
		//setTimeout('scrollUpMenu()',0);
	}else{
		//alert(parentLI.style.height);
		divToHide.style.display='none';
		otherDivsToScroll.length = otherDivsToScroll.length-1;
		if (debugScroll) console.log('scrollUpMenu: call showSubMenu');
		autoHideMenus();
	}
}	
 
function scrollDownSub()
{
	if(divToScroll){			
		if (debugScroll) console.log('scrollDownSub: divToScroll = ' + divToScroll.id);
		var height = divToScroll.offsetHeight/1;
		//if (debug) console.log('divToScroll.offsetHeight/1 = ' + height);
		var offsetMove =Math.min(15,(ulToScroll.offsetHeight - height));
		height = height +offsetMove ;
		divToScroll.style.height = height + 'px';
		if (debugScroll) console.log('scrollDownSub: divToScroll.style.height = ' + divToScroll.style.height);
		
		/*for(var no=1;no<otherDivsToScroll.length;no++){ 
			var tmpHeight = otherDivsToScroll[no].offsetHeight/1 + offsetMove; 
			//otherDivsToScroll[no].style.height = tmpHeight + 'px'; 
			if (debugScroll) console.log('otherDivsToScroll[' + no + '].style.height = ' + otherDivsToScroll[no].style.height);
			//otherDivsToScroll[no].style.height = ulToScroll.style.height
		}*/
		if (debugScroll) console.log('scrollDownSub: height = ' + height + ', ulToScroll.offsetHeight = ' + ulToScroll.offsetHeight);
		if(height != ulToScroll.offsetHeight) {
			divToScroll.style.height = ulToScroll.offsetHeight + 'px';
			if (debugScroll) console.log('scrollDownSub: call scrollDownSub');
			setTimeout('scrollDownSub()',0); 
		} else { 
			divToScroll = false; 
			ulToScroll = false;
			if (debugScroll) console.log('scrollDownSub: objectsToExpand.length = ' + objectsToExpand.length + ', initExpandIndex = ' + initExpandIndex);
			if(objectsToExpand.length>0 && initExpandIndex<(objectsToExpand.length-1)){
				initExpandIndex++;
				if (debugScroll) console.log('scrollDownSub: call showSubMenu(' + objectsToExpand[initExpandIndex].id + ')');
				showSubMenu(false,objectsToExpand[initExpandIndex]);
			}
		}
	}
}
	
function initSubItems(inputObj,currentDepth)
{		
	//if (debug) console.log('initSubItems, depth: ' + currentDepth);
	divCounter++;		
	var div = document.createElement('DIV');	// Creating new div		
	//div.style.overflow = 'hidden';	
	div.style.position = 'relative';
	div.style.display='none';
	div.style.height = '1px';
	div.id = 'slideDiv' + divCounter;
	div.className = 'slideMenuDiv' + currentDepth;		
	inputObj.parentNode.appendChild(div);	// Appending DIV as child element of <LI> that is parent of input <UL>		
	div.appendChild(inputObj);	// Appending <UL> to the div
	var menuItem = inputObj.getElementsByTagName('LI')[0];
	var menuItemCounter = 0;
	while(menuItem){
		if(menuItem.tagName=='LI'){
			menuItemCounter++;
			//menuItem.id = 'menu' + inputObj.parentNode.parentNode.id.substring(inputObj.parentNode.parentNode.id.length - 1) + '_' + menuItemCounter;
			var aTag = menuItem.getElementsByTagName('A')[0];
			aTag.className='slMenuItem_depth'+currentDepth;
			//menuItem.onmouseover = function() { if (this.staysHighlighted == null) { this.style.backgroundColor = '' + eval('level' + depth + 'HoverColour') + ''; } }
			//menuItem.onmouseout = function() { if (this.staysHighlighted == null) { this.style.backgroundColor = '' + eval('level' + depth + 'NormalColour') + '';} }
			var subUl = menuItem.getElementsByTagName('UL');
			if(subUl.length>0){
				initSubItems(subUl[0],currentDepth+1);					
				//aTag.onclick = showSubMenu;				
			}
		}			
		menuItem = menuItem.nextSibling;						
	}		
}
 
function initSlideDownMenu()
{
	if (debug) { console.log('initSlideDownMenu()\n\tdebug = ' + debug + '\n\tdebugExpand = ' + debugExpand); }
	dhtmlgoodies_slmenuObj = document.getElementById('dhtmlgoodies_slidedown_menu');
	dhtmlgoodies_slmenuObj.style.visibility='visible';
	var mainUl = dhtmlgoodies_slmenuObj.getElementsByTagName('UL')[0];
	idMenuItems(mainUl, 1);
	objectsToExpand.sort(function(a, b) { if (a.id > b.id) { return 1; } else { return -1; } });
	
	if (debugExpand) { 
		var out = ''; 
		for (var x=0; x<objectsToExpand.length; x++)
			out += '\n\t' + objectsToExpand[x].id;		
		console.log('objectsToExpand: ' + out);
	}
	var mainMenuItem = mainUl.getElementsByTagName('LI')[0];
	
	while (mainMenuItem) {
		if (mainMenuItem.tagName=='LI') {			
			var subUl = mainMenuItem.getElementsByTagName('UL');
			if (subUl.length>0)
				initSubItems(subUl[0],2);
		}			
		mainMenuItem = mainMenuItem.nextSibling;	
	}
 
	if(expandMenuItemByUrl)
	{
		if (objectsToExpand.length > 2) {
			for (var x = objectsToExpand.length; x > 0; x--) {
				if (debugInit) { console.log('initSlideDownMenu: opening ' + objectsToExpand[objectsToExpand.length - x].id); }
				showSubMenu(false,objectsToExpand[objectsToExpand.length - x]);
			}
		} else
			showSubMenu(false,objectsToExpand[0]);
				
		initExpandIndex = objectsToExpand.length = 1;
	}		
}

var ulCounter = 0;
function idMenuItems(ul, depth) {
	while (ul) {
		if (ul.tagName === 'UL') {
			if (depth === 1) {
				ulCounter++;
				ul.id = 'menuUL_' + ulCounter;
			} else {
				ul.id = ul.parentNode.id + '_UL'
			}
			if (debugInit) { console.log('idMenuItems: configure ul: ' + ul.id); }
			
			//get all the li's in this ul
			var li = ul.getElementsByTagName('LI')[0];
			var liCounter = 0;
			while (li) {
				if (li.tagName === 'LI') {
					//if (depth === 1) {
						li.onmouseover = function() { if (this.staysHighlighted == null) { this.style.backgroundColor = '' + eval('level' + depth + 'HoverColour') + ''; var img = this.getElementsByTagName('A')[0].getElementsByTagName('IMG'); if (img.length > 0) {img[0].src = mainItemArrowDown;} } }
						li.onmouseout = function() { if (this.staysHighlighted == null) { this.style.backgroundColor = '' + eval('level' + depth + 'NormalColour') + ''; var img = this.getElementsByTagName('A')[0].getElementsByTagName('IMG'); if (img.length > 0) {img[0].src = mainItemArrowRight;} } }
						//li.style.backgroundColor = level1NormalColour;
						li.style.backgroundColor = eval('level' + depth + 'NormalColour');
					//} else {
					//	li.onmouseover = function() { if (this.staysHighlighted == null) { this.style.backgroundColor = '' + eval('level' + depth + 'HoverColour') + ''; } }
					//	li.onmouseout = function() { if (this.staysHighlighted == null) { this.style.backgroundColor = '' + eval('level' + depth + 'NormalColour') + '';} }
					//	li.style.backgroundColor = eval('level' + depth + 'NormalColour');
					//}
					
					liCounter++;
					li.id = ul.id + '_li' + liCounter;
					if (debugInit) { console.log('idMenuItems: configure li: ' + li.id + ' (depth: ' + depth + ')'); }
					
					//check for the opening URL
					var a = li.getElementsByTagName('A')[0];
					a.id = li.id + '_a';
					a.className = 'slMenuItem_depth' + depth;
					
					//padding left according to whether it has an arrow or not (ie. submenu)
					a.style.paddingLeft = eval('level' + depth + 'AnchorPaddingLeft');
					if (depth > 1 && a.getElementsByTagName('IMG').length === 0)
						a.style.paddingLeft = (parseInt(eval('level' + depth + 'AnchorPaddingLeft').substring(0, eval('level' + depth + 'AnchorPaddingLeft').length - 2)) + 15) + 'px';
					
					//console.log(parseInt(eval('level' + depth + 'AnchorPaddingLeft').substring(0, eval('level' + depth + 'AnchorPaddingLeft').length - 2)) + 14);
					//if (debugInit) { console.log('idMenuItems: a.paddingLeft: ' + a.style.paddingLeft); }
					
					if (typeof(aFontWeight) != 'undefined')
						a.style.fontWeight = aFontWeight;
					
					a.style.color = eval('aLevel' + depth + 'MouseOutColour');						
					a.onmouseover = function() { if (this.parentNode.staysHighlighted == null) { this.style.color =  eval('aLevel' + depth + 'MouseOverColour'); } }
					a.onmouseout = function() { if (this.parentNode.staysHighlighted == null) { this.style.color =  eval('aLevel' + depth + 'MouseOutColour'); } }
					fullAnchorList.unshift(a);
					
					if ((location.href.indexOf(a.href) >= 0 && a.href.indexOf('#') < a.href.length - 1) || a.href.indexOf(overrideLoc) >= 0 ) {
						if (debugExpand) { console.log('idMenuItems: url matches: ' + a.id); }
						var obj = a.parentNode;
						//alert(depth);
						var localDepth = depth + 1;
						objectsToExpand.unshift(obj);
						
						while(obj && obj.id!='dhtmlgoodies_slidedown_menu' && localDepth > 1){ 
							if (obj.tagName === 'LI') {
								localDepth--;
								if (localDepth > 0) {
									obj.staysHighlighted = 'x';
									if (debugExpand) { console.log('idMenuItems: highlighted node: ' + obj.id + ' (obj.parentNode.parentNode.id: ' + obj.parentNode.parentNode.id + '), localDepth = ' + localDepth); }
									
									//determine whether this is a main menu item (top level) or not and highlight accordingly
									if (obj.parentNode.parentNode.id === 'dhtmlgoodies_slidedown_menu') {
										obj.style.backgroundColor = level1HoverColour;
										a.style.color = eval('aLevel' + localDepth + 'MouseOverColour');
									} else {
										obj.style.backgroundColor = eval('level' + localDepth + 'HoverColour');
										if (a.href.indexOf('/racing/') > -1)
											a.style.color = eval('aLevel' + localDepth + 'MouseOverColour');
									}
									
									//point the arrow down if it exists
									var imgs = obj.getElementsByTagName('A')[0].getElementsByTagName('IMG');
									if (imgs.length > 0) {
										if (obj.getElementsByTagName('A')[0].href.indexOf('/racing/') > -1)
											obj.getElementsByTagName('A')[0].style.color = eval('aLevel' + depth + 'MouseOverColour');
										imgs[0].src = mainItemArrowDown; 
										if (debugInit) console.log(obj.id + ': ' + imgs[0].src);
									}
									
									var subUl = obj.getElementsByTagName('UL');
									if(initialMenuItemAlwaysExpanded)alwaysExpanedItems[obj.parentNode] = true; 
									if (subUl.length>0) {
										if (!objectsToExpand.contains(obj)) {
											objectsToExpand.unshift(obj);
											if (debugExpand) {console.log('idMenuItems: adding to objectsToExpand: ' + obj.id);}
										}
										
										if (!objectsToExpand.contains(subUl[0])) {
											objectsToExpand.unshift(subUl[0]);
											if (debugExpand) {console.log('idMenuItems: adding subUl to objectsToExpand: ' +subUl[0].id);}
										}
									}
								} else 
									alert('idMenuItems: highlighted node: ' + obj.id + ' (obj.parentNode.parentNode.id: ' + obj.parentNode.parentNode.id + '), localDepth = ' + localDepth);
							}
							obj = obj.parentNode;
						}
					}
					
					//get the submenus
					var uls = li.getElementsByTagName('UL');
					if (uls.length > 0)
						idMenuItems(uls[0], depth+1);
				}
				li = li.nextSibling;
			}
		}
		ul = ul.nextSibling;
	}
}

Array.prototype.contains = function (element) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] === element) {
			return true;
		}
	}
	return false;
}
