jQuery(document).ready(function() {

    // preload the dropdown images
    var cache = [];
    // Arguments are image paths relative to the current page.
    jQuery.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }

    jQuery.preLoadImages("../../en/images/structure/riderAlertsTopBlue.png", "../../images/structure/riderAlertsBottomBlue.png", "../../en/images/structure/rideNowTop.png", "../../en/images/structure/scheduleFinderTop.png", "../../en/images/structure/tripPlannerTop.png", "../../en/images/structure/riderAlertsTopRed.png", "../../images/structure/riderAlertsBottomRed.png");

    var panels = new Array();
    panels[0] = jQuery('#riderAlerts');
    //panels[1] = jQuery('#rideNow');
    //panels[2] = jQuery('#scheduleFinder');
    //panels[3] = jQuery('#tripPlanner');

    var dropdowns = new Array();
    dropdowns[0] = jQuery('#riderAlerts .quickDropDown');
    //dropdowns[1] = jQuery('#rideNow .quickDropDown');
    //dropdowns[2] = jQuery('#scheduleFinder .quickDropDown');
    //dropdowns[3] = jQuery('#tripPlanner .quickDropDown');

    var openPanel;
    jQuery.openTimer = 0;
    jQuery.closeTimer = 0;

    jQuery.keepButtonOpen = false;
    
    var dropDownCurrentlyVisible = false;
    var clickedAChild = false;

    function openPanel(panel) {
    
        //if (jQuery.trim(jQuery("#dropdownContentAreaContainer").text()) != "") || jQuery.trim(jQuery(".bottom").text()) != "" {
            // hide any panen currently visible
            jQuery('.quickDropDown').css({'display' : 'none' });
            // show the current panel
            panel.find('.quickDropDown').css({ 'display': 'block' });
            // reset our booleans
            dropDownCurrentlyVisible = true;
            clickedAChild = false;
            jQuery(".ui-datepicker:visible").hide();
        //}
    };
    
    function closePanel(panel) {
        // this check is to take care of the child click
        if (dropDownCurrentlyVisible && clickedAChild == false) {
            panel.find('.quickDropDown').css({ 'display': 'none' });
            dropDownCurrentlyVisible = false;
            
            
        };
    };
    
    function registerChildrenClickHandler(children)
    {
        // when the child element in the panel is clicked
        jQuery.each(children, function(index, child) {
            jQuery(child).click(function() { clickedAChild = true; });
            // if there is child within child within panel
            registerChildrenClickHandler(jQuery(child).children());
        });
    }
    
    jQuery.each(panels, function(index, panel) {
        panel.mouseenter(function() {
            jQuery.openTimer = setTimeout(function() { openPanel(panel) }, 250);
        });
        panel.mouseleave(function() {
            if(jQuery(".ui-datepicker:visible").length>0)
            {
                // hack!!!
            }
            else
            
            {
                clearTimeout(jQuery.openTimer);
                jQuery.closeTimer = setTimeout(function() { closePanel(panel)  }, 250);
                
            }
        });
        registerChildrenClickHandler(jQuery(panel).children());
    });
});
