// jquery.simpleMenu.js
// --------------------
// jQuery code for dropdowns inspired by http://www.thatryan.com/
//
// Revision History
// REC 1709  2010-07-20  John Hildebrand  dropdown additions
// REC none  2011-08-03  John Hildebrand  hide dropdowns after click on a dropdown
// ===============================================================================

//global variables for jQuery
var sDDid;          //ID string value of the locked-down dropdown
var iDD_Max = 5;    //maximum number of dropdown columns
var iDD;            //dropdown number integer
var sID;            //dropdown number string
var sID_cookie;     //dropdown clicked ID string from cookie
var sDDprfx = "dd"; //dropdown ID prefix string
var bAryDDon = [false, false, false, false, false, false];  //Array of active dropdown flags
    
$(document).ready(function () {

    // dropdown "On" array initialization (not using the 1st entry, for "Home" on NavBar)
    
    sID_cookie = ReadCookie(gCOOKIE_DDIDCLK);
    //==alert("sID_cookie = " + sID_cookie);

	$('ul.sub_nav').hide();

    $('ul.main_nav li').hover(function () {
    //2011-08-03===$('ul.main_nav li').mouseenter(function () { //==2011-08-03
        //Process dropdown header hover ON 
		//$(this).find('> ul').stop(true, true).slideDown('slow'); //for slow slide-down
		//==alert("Hover event: this.id = " + this.id + ", sID_cookie = " + sID_cookie);
		if (this.id != sID_cookie)         //2011-08-03
		{
		    SetDD(true, iDD_Max, this.id, bAryDDon);  //Set this dropdown "On" in the array
		    $(this).find('> ul').show(); //for immediate slide-down
		    //==alert("Showing dropdown of this.id = " + this.id + ", sID_cookie = " + sID_cookie);
		}                                  //2011-08-03
		else
		{
		    //==alert("NOT showing dropdown of this.id = " + this.id + ", sID_cookie = " + sID_cookie + ", ... clearing cookie");
 		    WriteCookie(gCOOKIE_DDIDCLK, "", gEXPIRE_DDIDCLK);  //Clear the clicked dropdown cookie - 2011-08-03
 		    sID_cookie = ReadCookie(gCOOKIE_DDIDCLK);           //Reset the cookie variable value (needed for proper dropdown displays)
		}
		//==alert("Show this.id = " + this.id);  //==============================================
 	},
 	function() {
 	    //Process dropdown header hover OFF
 		//$(this).find('> ul').stop(true, true).slideUp('slow'); //for slow slide-up
 		if (this.id != sDDid)            //If this DD is NOT locked-down
 		{
 		    SetDD(false, iDD_Max, this.id, bAryDDon) //Set this dropdown "Off" in the array
 		    $(this).find('> ul').hide(); //  Hide this DD
 		}                                //End if
	});
	
	//Reset clear the clicked dropdown cookie after mouseout to enable dropdown display - 2011-08-03 vvv
    $('ul.main_nav li').mouseout(function () {
		if ( (this.id == sID_cookie) && (sID_cookie != "") )
		{
 		    WriteCookie(gCOOKIE_DDIDCLK, "", gEXPIRE_DDIDCLK);  //Clear the clicked dropdown cookie - 2011-08-03
 		    sID_cookie = ReadCookie(gCOOKIE_DDIDCLK);           //Reset the cookie variable value (needed for proper dropdown displays)
 		    //==alert("Clear cookie after mouseout from dropdown, this.id = " + this.id);
		}
	});
	//Reset clear the clicked dropdown cookie after mouseout to enable dropdown display - 2011-08-03 ^^^
	
	
    //==alert("Clearing the clicked dropdown cookie");      //2011-08-03
	//==WriteCookie(gCOOKIE_DDIDCLK, "", gEXPIRE_DDIDCLK);  //Clear the clicked dropdown cookie - 2011-08-03
	
	//Dropdown lock-down feature <== (obsolete as of 2011-08--02)
	//Hide dropdowns after clicking on a dropdown
	//==2011-08-03 10:18===$('ul.main_nav li').click(function() {
	$('ul.main_nav > li').click(function() {
	    //Process dropdown header click

		//alert("Clicked dropdown: this.id=|" + this.id + "|, sDDid=|" + sDDid + "|.");

	    for (iDD=1; iDD<=iDD_Max; iDD++)
	    {
	      sID = sDDprfx + iDD.toString();
	      if (this.id == sID)                  //If this is the DD clicked - 2011-08-02 vvv
	      {
	        $('#'+sID).find('> ul').hide();    //  Hide this DD
		    //==alert("Hide dropdown & set cookie after Click: this.id=|" + this.id + "|, sID=|" + sID + "|.");
		    WriteCookie(gCOOKIE_DDIDCLK, sID, gEXPIRE_DDIDCLK); //Define the clicked dropdown cookie
		    sID_cookie = ReadCookie(gCOOKIE_DDIDCLK);
	      }                                    //End if                    - 2011-08-02 ^^^
	    }
	    
	});  //End - Dropdown hide after click feature
	
	
});
