// JavaScript Document 
var ReveilleSoftwareJSLib = {};


/****************************************************************/
/**                Main Menu functions                         **/
/****************************************************************/
ReveilleSoftwareJSLib.MainMenu = {};

ReveilleSoftwareJSLib.MainMenu.timeout = 750; //time in milisceonds for menu to remain open after mouse has left active area.
ReveilleSoftwareJSLib.MainMenu.closetimer = 0;  //starts close timer at 0 for initial variable
ReveilleSoftwareJSLib.ddmenuitem	= 0; //Sets ddmenuitem to 0 during initial setup
ReveilleSoftwareJSLib.mopen_id = 0;


ReveilleSoftwareJSLib.MainMenu.TabClicked = function(e) {
    if (e.data.touch === 'yes') {
        e.preventDefault();
        ReveilleSoftwareJSLib.menu_open(e.data.dropdownid);
        return;
    }
    else {
        // not a touch screen, go to destination
        if (e.data.destselector) {
            window.location.href = $(e.data.destselector)[0].href;

        }
        else {
            window.location.href = e.data.dest;
        }
    }
}
 
// Actually Opens dropdown
ReveilleSoftwareJSLib.menu_open = function(id) {
    ReveilleSoftwareJSLib.ddmenuitem = document.getElementById(id); //assigns new element to the ddmenuitem variable, based item rollovered
    ReveilleSoftwareJSLib.MainMenu.CancelCloseTimer(); // cancel close timer

    if (ReveilleSoftwareJSLib.mopen_id === id) {
        if ($(ReveilleSoftwareJSLib.ddmenuitem).css('display') === 'block') {
            return;
        }
    }


    if (ReveilleSoftwareJSLib.ddmenuitem) ReveilleSoftwareJSLib.ddmenuitem.style.display = 'none'; //causes menu item that is cuurrently open to reset visiblity to none

    ReveilleSoftwareJSLib.ddmenuitem = document.getElementById(id); //assigns new element to the ddmenuitem variable, based item rollovered

    ReveilleSoftwareJSLib.mopen_id = id;
    $('#dropdown0,#dropdown1,#dropdown2,#dropdown3,#dropdown4,#dropdown5,#dropdown6,#dropdown7').not('#' + id).slideUp('fast', ReveilleSoftwareJSLib.MainMenu.mopen_id_callback);
   // ReveilleSoftwareJSLib.MainMenu.mopen_id_callback();
}

ReveilleSoftwareJSLib.MainMenu.mopen_id_callback = function()
{
    // calculate left for dropdown
    var position;
    var targetTab = ReveilleSoftwareJSLib.getDropDownOwner(ReveilleSoftwareJSLib.mopen_id);
    position = $(targetTab).position();
    $('#MainMenu_tab_0,#MainMenu_tab_1,#MainMenu_tab_2,#MainMenu_tab_3,#MainMenu_tab_4,#MainMenu_tab_5,#MainMenu_tab_6,#MainMenu_tab_7').not(targetTab).removeClass('MainMenuTabContainerSelected').addClass('MainMenuTabContainer');
    $(targetTab).removeClass('MainMenuTabContainer').addClass('MainMenuTabContainerSelected');
    $('#' + ReveilleSoftwareJSLib.mopen_id).css('left',position.left.toString() + 'px').slideDown('fast');

};

/* closes dropdown */
ReveilleSoftwareJSLib.MainMenu.CancelDropDown = function(event) {
    $(event.data.id).slideUp('fast');
    $(event.data.tab).removeClass('MainMenuTabContainerSelected').addClass('MainMenuTabContainer');
    ReveilleSoftwareJSLib.mopen_id = 0;
    ReveilleSoftwareJSLib.ddmenuitem = 0;
    event.preventDefault();   // do not allow this event to propagate. If allowed it can "bleed" through to controls under this div

};
                            
function mclose()// close showed layer
{

    $(ReveilleSoftwareJSLib.ddmenuitem).slideUp('fast');
    $(ReveilleSoftwareJSLib.getDropDownOwner(ReveilleSoftwareJSLib.ddmenuitem.id)).removeClass('MainMenuTabContainerSelected').addClass('MainMenuTabContainer');
	ReveilleSoftwareJSLib.ddmenuitem	= 0;
	ReveilleSoftwareJSLib.mopen_id = 0;
	
	
}
// Returns the jQuery ID selector for the dropdown id
ReveilleSoftwareJSLib.getDropDownOwner = function(id){
    switch (id)
    {
        case "dropdown0":
            return "#MainMenu_tab_0";
            break;        
        case "dropdown1":
            return "#MainMenu_tab_1";
            break;        
        case "dropdown2":
            return  "#MainMenu_tab_2";
            break;        
        case "dropdown3":
            return "#MainMenu_tab_3";
            break;
         case "dropdown4":
            return "#MainMenu_tab_4";
            break;        
        case "dropdown5":
            return "#MainMenu_tab_5";
            break;        
        case "dropdown6":
            return "#MainMenu_tab_6";
            break;        
        case "dropdown7":
            return "#MainMenu_tab_7";
            break;        
         default:
            return "#MainMenu_tab_1";
            break;        
    }

};
// Sets a timeout to delay closing the drop down. Set by the mouseout event of the menu tabs.
ReveilleSoftwareJSLib.MainMenu.menu_CloseTimer = function()
{
    ReveilleSoftwareJSLib.MainMenu.closetimer = setTimeout(mclose,ReveilleSoftwareJSLib.MainMenu.timeout);
}

// Cancels the menu close timer
ReveilleSoftwareJSLib.MainMenu.CancelCloseTimer =  function()
{
	if(ReveilleSoftwareJSLib.MainMenu.closetimer)
	{
		clearTimeout(ReveilleSoftwareJSLib.MainMenu.closetimer);
	    ReveilleSoftwareJSLib.MainMenu.closetimer = null;
	}
}







