
// ******************************************** TOP MENU *********************************************** //
//																										 //
// Self invoking anonymous function - this avoids poluting the namespace, thus eventual conflicts.		 //
// This function complements the CSS menu implementation.											     //
//																										 //
// ***************************************************************************************************** //

(
	function(){
		
		$(document).ready(
	
			function(){
			
				//get number of sections 
				var noSections = $('li[name=menuSection]').length;
				var currentSection;
				var arrSectionObjects = [];
				
				// ************************************** SET CURRENT SECTION HANDLER ************************************** //
				//																											 //
				// This function ads the class to the jQuery object.														 //
				// It prevents flickering of the section on mouse out.														 //
				// this is neccessary because the CSS event handlers are defined before the reading of the object with jQuery//
				// Also deselects the other sections.																		 //																											 //
				//																											 //
				// ********************************************************************************************************* //
				
				function setCurrentSectionHandler(sectionId,sectionObject){
													   
					sectionObject.mouseover(
					
						function(){
							
							// ************* DESELECT OTHER SECTIONS ************* //
							
							for(i=0; i<noSections; i++){
								
								if(sectionId != i){
									
									//get other sections objects
									var otherSectionObject = arrSectionObjects[i];
						
									//set section link class
									otherSectionObject.children().removeClass("selectedTab")
									otherSectionObject.children().addClass("hide");
								}
								
							}
							
							// *************************************************** //

							//set current section link class
							sectionObject.children().removeClass("hide")
							sectionObject.children().addClass("selectedTab");
							
							//set current section
							currentSection = sectionId;
						}
					);
				}
				
				// ********************************* ADD SECTIONS EVENT HANDLERS ****************************** //
				//																			 			        //
				//	Puts every object into a different execution context so that it's reference is not lost 	//
				//  on 'sectionObject' reinitialisation	inside the for loop.This way every object gets an event	//
				//  handler attached during	the loop execution.												    //
				//																			  					//
				// ******************************************************************************************** //
				
				for(i=0; i<noSections; i++){
					
					//get section object
					var sectionObject = $("#section_"+i);
					
					//build section objects array 
					arrSectionObjects[i] = sectionObject;
					
					//add event handler
					setCurrentSectionHandler(i,sectionObject);

				}
				
				// ******************** DESELECT CURRENT SECTION ON MOUSE LEAVE ******************** //
				
				$("#menu_container").mouseleave(
				
					function(){
						
						//get section object
						var sectionObject = $("#section_"+currentSection);
						
						//set section link class
						sectionObject.children().removeClass("selectedTab")
						sectionObject.children().addClass("hide");
						
					}
				);
				
				// ********************************************************************************** //
			}
			
		)
	}

)();
