/************************************************************************************************************
(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.

Update log:

	March, 15th: Fixed problem with sliding in MSIE

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/
var klik_expandFirstItemAutomatically = false;	// Expand first menu item automatically ?
var klik_initMenuIdToExpand = false;	// Id of menu item that should be initially expanded. the id is defined in the <li> tag.
var klik_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 klik_initialMenuItemAlwaysExpanded = true;	// NOT IMPLEMENTED YET

var klik_dhtmlgoodies_slmenuObj;
var klik_divToScroll = false;
var klik_ulToScroll = false;
var klik_divCounter = 1;
var klik_otherDivsToScroll = new Array();
var klik_divToHide = false;
var klik_parentDivToHide = new Array();
var klik_ulToHide = false;
var klik_offsetOpera = 0;
if(navigator.userAgent.indexOf('Opera')>=0)klik_offsetOpera=1;
var klik_slideMenuHeightOfCurrentBox = 0;
var klik_objectsToExpand = new Array();
var klik_initExpandIndex = 0;
var klik_alwaysExpanedItems = new Array();

function klik_popMenusToShow()
{
	var obj = klik_divToScroll;
	var endArray = new Array();
	while(obj && obj.tagName!='BODY'){
		if(obj.tagName=='DIV' && obj.id.indexOf('slideDiv')>=0){
			var objFound = -1;
			if (!closeSubMenu){
				for(var no=0;no<klik_otherDivsToScroll.length;no++){
					if(klik_otherDivsToScroll[no]==obj){
						objFound = no;
					}
				}
				if(objFound>=0){
					klik_otherDivsToScroll.splice(objFound,1);
				}
			}
		}
		obj = obj.parentNode;
	}
}

var closeSubMenu = false, isAnimation = false;

function klik_showSubMenu(e,inputObj)
{
	if(this && this.tagName)inputObj = this.parentNode;
	if(inputObj && inputObj.tagName=='LI'){
		klik_divToScroll = inputObj.getElementsByTagName('DIV')[0];
		for(var no=0;no<klik_otherDivsToScroll.length;no++){
			if(klik_otherDivsToScroll[no]==klik_divToScroll){
				closeSubMenu = true;
			}
		}
	}
	hidingInProcess = false;
	if(klik_otherDivsToScroll.length>0){
		
		if(klik_divToScroll && !isAnimation){
			if(klik_otherDivsToScroll.length>0){
				klik_popMenusToShow();
			}
			if(klik_otherDivsToScroll.length>0){
				klik_autoHideMenus();
				hidingInProcess = true;
			}
		}
	}
	if(klik_divToScroll && !hidingInProcess){
		klik_divToScroll.style.display='';
		klik_otherDivsToScroll.length = 0;
		otherDivToScroll = klik_divToScroll.parentNode;
		klik_otherDivsToScroll.push(klik_divToScroll);
		while(otherDivToScroll && otherDivToScroll.tagName!='BODY'){
			if(otherDivToScroll.tagName=='DIV' && otherDivToScroll.id.indexOf('slideDiv')>=0){
				klik_otherDivsToScroll.push(otherDivToScroll);

			}
			otherDivToScroll = otherDivToScroll.parentNode;
		}
		klik_ulToScroll = klik_divToScroll.getElementsByTagName('UL')[0];
		if(klik_divToScroll.style.height.replace('px','')/1<=1)klik_scrollDownSub();
		
	}
}



function klik_autoHideMenus()
{
	if(klik_otherDivsToScroll.length>0 || closeSubMenu){
		klik_divToHide = klik_otherDivsToScroll[klik_otherDivsToScroll.length-1];
		klik_parentDivToHide.length=0;
		if (klik_divToHide != null){
			var obj = klik_divToHide.parentNode.parentNode.parentNode;
			while(obj && obj.tagName=='DIV'){
				if(obj.id.indexOf('slideDiv')>=0)klik_parentDivToHide.push(obj);
				obj = obj.parentNode.parentNode.parentNode;
			}
			var tmpHeight = (klik_divToHide.style.height.replace('px','')/1 - klik_slideMenuHeightOfCurrentBox);
			if(tmpHeight<0)tmpHeight=0;
			if (tmpHeight == 0)
			tmpHeight = klik_divToHide.style.height.replace('px','')/1;//closeDivHeight;
			if(klik_slideMenuHeightOfCurrentBox){klik_divToHide.style.height = tmpHeight  + 'px';}
			klik_ulToHide = klik_divToHide.getElementsByTagName('UL')[0];
			klik_slideMenuHeightOfCurrentBox = klik_ulToHide.offsetHeight;
			klik_scrollUpMenu();
		}
	}else{
		klik_slideMenuHeightOfCurrentBox = 0;
		klik_showSubMenu();
	}
}


function klik_scrollUpMenu()
{
	if (klik_divToHide != null){
		var height = klik_divToHide.offsetHeight;
		height-=15;
		if(height<0)height=0;
		klik_divToHide.style.height = height + 'px';
	
		for(var no=0;no<klik_parentDivToHide.length;no++){
			klik_parentDivToHide[no].style.height = klik_parentDivToHide[no].getElementsByTagName('UL')[0].offsetHeight + 'px';
		}
		if(height>0){
			setTimeout('klik_scrollUpMenu()',5);
			isAnimation = true;
		}else{
			klik_divToHide.style.display='none';
			klik_otherDivsToScroll.length = klik_otherDivsToScroll.length-1;
			klik_autoHideMenus();
			isAnimation = false;
		}
	}
}

function klik_scrollDownSub()
{
	if(klik_divToScroll){
		if (closeSubMenu)
			closeSubMenu = false;
		var height = klik_divToScroll.offsetHeight/1;
		var offsetMove =Math.min(15,(klik_ulToScroll.offsetHeight - height));
		height = height +offsetMove ;
		klik_divToScroll.style.height = height + 'px';

		for(var no=1;no<klik_otherDivsToScroll.length;no++){
			var tmpHeight = klik_otherDivsToScroll[no].offsetHeight/1 + offsetMove;
			klik_otherDivsToScroll[no].style.height = tmpHeight + 'px';
		}
		if(height<klik_ulToScroll.offsetHeight){setTimeout('klik_scrollDownSub()',5); isAnimation = true;}
		else {
			klik_divToScroll = false;
			klik_ulToScroll = false;
			if(klik_objectsToExpand.length>0 && klik_initExpandIndex<(klik_objectsToExpand.length-1)){
				klik_initExpandIndex++;

				klik_showSubMenu(false,klik_objectsToExpand[klik_initExpandIndex]);
			}
			isAnimation = false;
		}
	}
}

function klik_initSubItems(inputObj,currentDepth)
{
	klik_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' + klik_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];
	while(menuItem){
		if(menuItem.tagName=='LI'){
			var aTag = menuItem.getElementsByTagName('A')[0];
			aTag.className='slMenuItem_depth'+currentDepth;
			var subUl = menuItem.getElementsByTagName('UL');
			if(subUl.length>0){
				klik_initSubItems(subUl[0],currentDepth+1);
			}
			aTag.onclick = klik_showSubMenu;
		}
		menuItem = menuItem.nextSibling;
	}
}

function klik_initSlideDownMenu()
{
	klik_dhtmlgoodies_slmenuObj = document.getElementById('dhtmlgoodies_slidedown_menu');
	klik_dhtmlgoodies_slmenuObj.style.visibility='visible';
	var mainUl = klik_dhtmlgoodies_slmenuObj.getElementsByTagName('UL')[0];
	var mainMenuItem = mainUl.getElementsByTagName('LI')[0];
	mainItemCounter = 1;
	while(mainMenuItem){
		if(mainMenuItem.tagName=='LI'){
			var aTag = mainMenuItem.getElementsByTagName('A')[0];
			aTag.className='slMenuItem_depth1';
			var subUl = mainMenuItem.getElementsByTagName('UL');
			if(subUl.length>0){
				mainMenuItem.id = 'mainMenuItem' + mainItemCounter;
				klik_initSubItems(subUl[0],2);
				aTag.onclick = klik_showSubMenu;
				mainItemCounter++;
			}
		}
		mainMenuItem = mainMenuItem.nextSibling;
	}

	if(location.search.indexOf('mainMenuItemToSlide')>=0){
		var items = location.search.split('&');
		for(var no=0;no<items.length;no++){
			if(items[no].indexOf('mainMenuItemToSlide')>=0){
				values = items[no].split('=');
				klik_showSubMenu(false,document.getElementById('mainMenuItem' + values[1]));
				klik_initMenuIdToExpand = false;
			}
		}
	}else if(klik_expandFirstItemAutomatically>0){
		if(document.getElementById('mainMenuItem' + klik_expandFirstItemAutomatically)){
			klik_showSubMenu(false,document.getElementById('mainMenuItem' + klik_expandFirstItemAutomatically));
			klik_initMenuIdToExpand = false;
		}
	}

	if(klik_expandMenuItemByUrl)
	{
		var aTags = klik_dhtmlgoodies_slmenuObj.getElementsByTagName('A');
		for(var no=0;no<aTags.length;no++){
			var hrefToCheckOn = aTags[no].href;
			if(location.href.indexOf(hrefToCheckOn)>=0 && hrefToCheckOn.indexOf('#')<hrefToCheckOn.length-1){
				klik_initMenuIdToExpand = false;
				var obj = aTags[no].parentNode;
				while(obj && obj.id!='dhtmlgoodies_slidedown_menu'){
					if(obj.tagName=='LI'){
						var subUl = obj.getElementsByTagName('UL');
						if(klik_initialMenuItemAlwaysExpanded)klik_alwaysExpanedItems[obj.parentNode] = true;
						if(subUl.length>0){
							klik_objectsToExpand.unshift(obj);
						}
					}
					obj = obj.parentNode;
				}
				klik_showSubMenu(false,klik_objectsToExpand[0]);
				break;
			}
		}
	}

	if(klik_initMenuIdToExpand)
	{
		klik_objectsToExpand = new Array();
		var obj = document.getElementById(klik_initMenuIdToExpand)
		while(obj && obj.id!='dhtmlgoodies_slidedown_menu'){
			if(obj.tagName=='LI'){
				var subUl = obj.getElementsByTagName('UL');
				if(klik_initialMenuItemAlwaysExpanded)klik_alwaysExpanedItems[obj.parentNode] = true;
				if(subUl.length>0){
					klik_objectsToExpand.unshift(obj);
				}
			}
			obj = obj.parentNode;
		}

		klik_showSubMenu(false,klik_objectsToExpand[0]);

	}



}
