/**
 * This file contains all Javascript needed to handle the UL elements in the topmenu.
 * The html has the following structure:
 * - div - firstLevelDiv - contains all top custom pages in addition to some hardcoded LI's like "Produkter", "Handlevogn", "Logg inn".
 * - div - secondLevelDiv - contains all sub custom pages in addition to Pricebook LI's when the "Produkter" menu point is selected.
 * - div - thirdLevelDiv - only Products - contains first level catalog structure 
 * - div - forthLevelDiv - only Products - contains second level catalog structure 
 * - div - fifthLevelDiv - only Products - contains third level catalog structure 
 * 
 */


	var lastid, lastid2;
	var numberClicks = 0;
	
	/**
	* Takes as input the id of a UL element on the second level, hides the other levels
	* and display the subelements of the UL given as input
	*
	* @param the id of the UL to display
	*/
	function forthLevel(id){

		var lastUL = document.getElementById("catalog_forthlevel_"+lastid);
		
		if(lastid != null && id < 0){
			if(lastUL != null){
				lastUL.style.display = "none";
				//lastUL.style.visibility = "hidden";
				//closeFifthLevel();
			}
			
			return;
		}
		else if(lastid != null && lastid != id){
			if(lastUL != null){
				lastUL.style.display = "none";
				//lastUL.style.visibility = "hidden";
			}
		}

		lastid = id;
		var secondId = "catalog_forthlevel_"+id;
		$(".catalog_forthlevel > ul" ).each(
				function( intIndex ){
					//this.style.visibility = "hidden";
					this.style.display = "none";
				}
			);
	}
	
	
	/**
	* Takes as input the id of a UL element on the third level, hides the other levels
	* and display the subelements of the UL given as input
	*
	* @param the id of the UL to display
	*/
	function thirdLevel(id){
	
		var lastUL2;
		lastUL2 = document.getElementById("catalog_thirdlevel_"+lastid2);
		
		if(lastUL2 != null && id < 0){
			closeLevel(lastUL2);
			
			return;
		}
		else if(lastid2 != null && lastid2 != id){
			closeLevel(lastUL2);
		}
		
		lastid2 = id;
		var thirdId = "catalog_thirdlevel_"+id;
		var thirdUL = document.getElementById(thirdId);
		if(thirdUL != null)
			thirdUL.style.display = "block";
	}
	
	/**
	* This function closes the UL given as input.
	*
	* @param the UL to close
	*/
	function closeLevel(lastULX){
		lastULX.style.display = "none";
	}
	
	/**
	 * Removes the bottom border of the div with number given as input
	 * 
	 * @param divId
	 * @return
	 */
	function removeBorderBottom(divId){
		if(divId == 1)
			$('#firstLevelDiv').css("border-bottom", "1px solid #fff");
		else if(divId == 2)
			$('#secondLevelDiv').css("border-bottom", "1px solid #fff");
		else if(divId == 3)
			$('#thirdLevelDiv').css("border-bottom", "1px solid #fff");
		else if(divId == 4)
			$('#forthLevelDiv').css("border-bottom", "1px solid #fff");
		else if(divId == 5)
			$('#fifthLevelDiv').css("border-bottom", "1px solid #fff");
	}
	
	/**
	 * Adds the bottom border of the div with number given as input
	 * 
	 * @param divId
	 * @return
	 */
	function addBorderBottom(divId){
		if(divId == 1)
			$('#firstLevelDiv').css("border-bottom", "1px solid #C6C6C6");
		else if(divId == 2)
			$('#secondLevelDiv').css("border-bottom", "1px solid #C6C6C6");
		else if(divId == 3)
			$('#thirdLevelDiv').css("border-bottom", "1px solid #C6C6C6");
		else if(divId == 4)
			$('#forthLevelDiv').css("border-bottom", "1px solid #C6C6C6");
		else if(divId == 5)
			$('#fifthLevelDiv').css("border-bottom", "1px solid #C6C6C6");
	}
	
	/**
	* This function close the last selected element in third level
	*/
	function closeFifthLevel(){
		var lastUL2;
		if(lastid2 != null ){
			lastUL2 = document.getElementById("catalog_fifthlevel_"+lastid2);
			lastUL2.style.display = "none";
			
		}
	}
	
	function closeProductLevels(){
		
		$("#secondLevelDiv > ul#pricebookUL").css("display", "none");
		$("#thirdLevelDiv > ul").each(
			function( intIndex ){
				this.style.display = "none";
				removeBorderBottom(3);
			}
		);
		$("#forthLevelDiv > ul").each(
			function( intIndex ){
				this.style.display = "none";
				removeBorderBottom(4);
			}
		);
		$("#fifthLevelDiv > ul").each(
			function( intIndex ){
				this.style.display = "none";
				removeBorderBottom(5);
			}
		);
	}
	
	/**
	 * Opens the UL element in the third row which contains the first level catalog structures
	 */
	function openThirdProductLevel(){
		$('#thirdLevelDiv > ul').css("display", "block");
		addBorderBottom(3);
	}
	
	/**
	 * Opens the chooses UL element in the forth level div and
	 * closes the previous selected UL element in the forth level div
	 * 
	 * @param The id of the UL element we want to display
	 */
	function openForthLevelUlElement(id){
		closeAllForthLevelUlElement();
		addBorderBottom(4);
		$('#catalog_forthlevel_'+id).css("display", "block");
	}
	
	
	/**
	 * Closes all UL elements in the forth level div.
	 * This also closes fifth level, since we always want to close the
	 * sub level div when we close its parent.
	 */
	function closeAllForthLevelUlElement(){
		$("#forthLevelDiv > ul").each(
			function( intIndex ){
				this.style.display = "none";
			}
		);
		removeBorderBottom(4);
		removeBorderBottom(5);
		closeAllFifthLevelUlElement();
	}
	
	/**
	 * Opens the chooses UL element in the fifth level div and
	 * closes the previous selected UL element in the fifth level div
	 * 
	 * @param The id of the UL element we want to display
	 */
	function openFifthLevelUlElement(id){
		closeAllFifthLevelUlElement();
		addBorderBottom(5);
		$('#catalog_fifthlevel_'+id).css("display", "block");
	}
	
	
	/**
	 * Closes all UL elements in the fifth level div
	 */
	function closeAllFifthLevelUlElement(){
		removeBorderBottom(5);
		$("#fifthLevelDiv > ul").each(
			function( intIndex ){
				this.style.display = "none";
			}
		);
	}
	
	var lastSecondLevelUl;
	/**
	 * Shows the selected custom UL element with
	 * the id given as input
	 * 
	 * @param id The id of the UL tag we want to display
	 */
	function showCustomSecondLevel(id){

		closeAllSecondLevel();
		closeProductLevels();
		
		lastSecondLevelUl = id;
		
		var secondLevelUl = $('#customSecondLevel_'+id);
		if(secondLevelUl != null)
			secondLevelUl.css("display", "block");
	}
	
	/**
	 * Closes all UL elements in the second level row.
	 */
	function closeAllSecondLevel(){
		$( "#secondLevelDiv > ul" ).each(
			function( intIndex ){
				this.style.display = "none";
			}
		);
	
	}
	
	
	
	/**
	 * Closes all custom UL elements in the second level row
	 *
	 * @param hiddenType 'none' or 'hidden' depending on weather changing the 
	 * 		css property 'display' or 'visibility'
	 */
	function closeAllSecondLevelCustom(){
		$("#secondLevelDiv > ul.customSecondLevelUL").each(
			function( intIndex ){
				this.style.display = "none";
			}
		);
	}
	
	/**
	 * Closes the Produkter UL element in the second row
	 */
	function closeSecondLevelProductsUL(){
		$( "#secondLevelDiv > ul#pricebookUL" ).css("display", "none");
	}
	
	/**
	 * Opens the Produkter UL element in the second row
	 */
	function openSecondLevelProductsUL(){
		$( "#secondLevelDiv > ul#pricebookUL" ).css("display", "block");
	}
	
	/**
	 * Is runned when the Product LI element is hover'd on the first row.
	 * Closes all second level custom UL elements and opens all UL elements
	 * related to the Products.
	 */
	function products(){
		closeAllSecondLevel();
		
		openSecondLevelProductsUL();
		openThirdProductLevel();
	}
	
	/**
	 * Closes the current open UL's and opens the topmenu manager UL
	 */
	function openManagerUL(){
		closeAllSecondLevel();
		//closeSecondLevelProductsUL();
		closeProductLevels();
		
		$('#topmenu_manager_second_level_UL').css("display", "block");
	}
	
	/**
	 * Closes the topmenu manager UL
	 */
	function closeManagerUL(){
		$('#topmenu_manager_second_level_UL').css("display", "none");
	}
	