$(document).ready(function() {
    /*-----
    Initialize Cufon
    -----*/
    Cufon.replace('div.banner h1', { fontFamily: 'helveticaNeue' });
    Cufon.replace('div.banner h2', { fontFamily: 'helveticaNeue' });

    /*-----
    Top Navigation Hover
    -----*/
    function hideNavigation() {
        currentlyShowing = false;
        $('div#subNav').addClass('hide');
        clearTimeout(menuHideTimer);
    }
    // create a timer to hide menu when hovered
    var menuHideTimer;
    var allTopNavItems = $('ul.mainNav li a');
    var currentlyShowing = false;
    // On hover of any item in the top navigation
    $(allTopNavItems).hover(function() {
        // turn off hiding timer
        clearTimeout(menuHideTimer);
        // Determine which item I am hovering over (0-x), 0 would be About Menu
        var currentNavItem = $(allTopNavItems).index($(this));
        if (!currentlyShowing) {

            /*------ INSERT MENU CODE HERE
            Insert the menu loading code here which does the ajax call to load subNavigation
            user currentNavItem to determine which one to call
            -------*/
			//$("div#subNav").html("<ul class='clearfix'><li class='first'>&nbsp;</li><li class='last'>&nbsp;</li></ul>");
			
			$.post("_includes/cms/header_subnav.php", { 
					
					section : currentNavItem
					
				}, function(data) {
					
				$("div#subNav").html(data);
			 	//$('div#subNav').removeClass('hide');
					
			});	
        }
    }, function() {
        // set hiding hiding timer for menu on mouseout
        menuHideTimer = setTimeout(hideNavigation, 750);
    });
    $('div#subNav').hover(function() {
        // turn off hiding timer
        clearTimeout(menuHideTimer);
    }, function() {
        // set hiding hiding timer for menu on mouseout
        menuHideTimer = setTimeout(hideNavigation, 750);
    });

    /*-----
    Search Bar Focus and Blur color change
    -----*/
    // Search bar on focus darken input text color
    $('li.search input').focus(function() {
        if ($('li.search input').val() == "search") {
            $('li.search input').val('');
            $(this).css('color', '#262626');
        }
    });

    // Search bar on blur lighten input text color if empty
    $('li.search input').blur(function() {
        if ($('li.search input').val() == "") {
            $('li.search input').val('search');
            $(this).css('color', '#cacaca');
        }
    });

    // Search bar on focus darken input text color
    $('div.searchBox div.entryLine input').focus(function() {
        if ($('div.searchBox div.entryLine input').val() == "Enter keywords") {
            $('div.searchBox div.entryLine input').val('');
            $(this).css('color', '#262626');
        }
    });

    // Search bar on blur lighten input text color if empty
    $('div.searchBox div.entryLine input').blur(function() {
        if ($('div.searchBox div.entryLine input').val() == "") {
            $('div.searchBox div.entryLine input').val('Enter keywords');
            $(this).css('color', '#cacaca');
        }
    });

    /*-----
    Search By Drop Down
    -----*/
    // track if the menu is showing
    var menuShowing = false;
    // setup a timer variable
    var searchListTimer;
    // on click of the search link show the drop down menu
    $("div.searchBox a.searchBy").click(function() {
        if (menuShowing) {
            menuShowing = false;
            $('div.searchByDDList').hide();
        } else {
            // set the menu showing to prevent accidental hiding
            menuShowing = true;
            $('div.searchByDDList').show();
        }
        return false;
    });

    // function to hide the menu when needed
    function hideDDList() {
        menuShowing = false;
        $("div.searchByDDList").hide();
    }

    // on hover the searchBy menu
    $("div.searchBox a.searchBy").hover(function() {
        // if the menu is showing clear the timeout so it doesn't accidentally hide
        if (menuShowing) {
            clearTimeout(searchListTimer);
        }
    }, function() {
        // on mouseout set the menu to hide
        searchListTimer = setTimeout(hideDDList, 750);
    });

    // on hover of the drop down list
    $("div.searchByDDList").hover(function() {
        // clear the timeout so the menu doesn't hide
        clearTimeout(searchListTimer);
    }, function() {
        // on mouseout set the menu to hide
        searchListTimer = setTimeout(hideDDList, 200);
    });

    // On click of a search list item
    $("div.searchByDDList a").click(function() {
        // Get the title attribute of the list item and set it in the hidden input for passing the value to the server
        $('#searchBy').attr('value', $(this).attr('title'));
        // set the visible text in the search by box to your new selection
        $('.searchBy').html($(this).html());
        // clear the timer and hide the drop down menu immediately
        clearTimeout(searchListTimer);
        menuShowing = false;
        $("div.searchByDDList").hide();
        return false;
    });


    /*-----
    Navigation Flyout
    -----*/
    // setup fuction to hide flyout later
    function hideFlyOut() {
        $('div.flyOut').addClass('hide');
    }
    // set timer to store timeout event
    var flyOutTimer;
    // on hover of a link within a list item with a class of flyOut
    $('li.flyOut > a').hover(function() {
        // clear the timeout just in case
        clearTimeout(flyOutTimer);
        // hide any open flyOuts
        $('div.flyOut').addClass('hide');
        $('li.flyOut').css('z-index', 1);
        // show the flyout
        $(this).siblings('div.flyOut').removeClass('hide');
        $(this).parent('li').css('z-index', 6);
        // Adjust the position to center the flyout based on the amount of content in it
        $(this).siblings('div.flyOut').css('left', $(this).width() + 15);
        $(this).siblings('div.flyOut').css('top', $('div.flyOut').height() / 2 * -1 + 10);
    }, function() {
        // on mouseout hide the flyOut
        flyOutTimer = setTimeout(hideFlyOut, 750);
    });

    // on hover of the flyout menu
    $('div.flyOut').hover(function() {
        // clear the timeout so the menue doesn't hide accidentally
        clearTimeout(flyOutTimer);
    }, function() {
        // on mouseout hide the menu
        flyOutTimer = setTimeout(hideFlyOut, 200);
    });


    /*-----
    Support Icons ToolTip
    -----*/
    // on hover of a support icon
    $('div.supportIcons div.icon a').hover(function() {
        // adjust containers z-index to show it above the search bar
        $('div.icon').css('z-index', 4);
        // show tooltip
        $(this).siblings('span.toolTip').removeClass('hide');
        // adjust position of tooltip depending on height
        $(this).siblings('span.toolTip').css('top', $(this).siblings('span.toolTip').height() * -1);
    }, function() {
        // adjust containers z-index so the search drop down appears above
        $('div.icon').css('z-index', 1);
        // hide tooltip
        $(this).siblings('span.toolTip').addClass('hide');
    });

    /*-----
    Recent Cases ToolTip
    -----*/
    $('div.tabWrapper div.tabSection a.item').hover(function() {
        // show tooltip on hover
        $(this).find('span.toolTip').removeClass('hide');
        // adjust position of toolTip based on height of the tooltip
        $(this).find('span.toolTip').css('top', $(this).find('span.toolTip').height() * -1 - 10);
    }, function() {
        // hide tooltip
        $(this).find('span.toolTip').addClass('hide');
    });

    $('span.toolTip').hover(function() {
        // if the mouse hovers the tooltip hide it
        $(this).addClass('hide');
    }, function() { });


    /*-----
    Recent Cases Tabbed Section
    -----*/
    var allTabs = $('div.tabs div.rightTab a');
    var allTabbedContent = $('div.tabWrapper div.tabSection');
    // store the currently selected tab
    var previousTab = 0;
    // speed of the transition of the tabs
    var tabSpeed = 400;
    // check to see if tabs are currently animating
    var tabChanging = false;
    // hide all tab content on page load
    $(allTabbedContent).css('opacity', 0);
    // remove class hiding tabbed content
    $(allTabbedContent[0]).removeClass('hide');
    // fade on the first tabbed content
    $(allTabbedContent[0]).animate({ opacity: 1 }, 250, function() { });

    var tabEasingMethod = 'easeInQuart';

    // Clicking the homepage tabs
    $(allTabs).click(function() {
        // make sure tabs are not still animating
        if (!tabChanging) {
            // prevent double clicking
            tabChanging = true;
            // Get the clicked tabs index
            var currentTab = $(allTabs).index($(this));
            currentTab = allTabs.length - currentTab - 1;

            if (currentTab != previousTab) {
                // remove previously selected button
                $(allTabs).removeClass('selected');
                // make current tab selected
                $(this).addClass('selected');

                // Fade Tabs
                $(allTabbedContent[previousTab]).animate({ opacity: 0 }, tabSpeed, tabEasingMethod, function() {

                    $(allTabbedContent).addClass('hide');
                    // store tab for fading when next tab is clicked
                    previousTab = currentTab;

                    $(allTabbedContent[currentTab]).removeClass('hide');
                    // fade on new tabbed content
                    $(allTabbedContent[currentTab]).animate({ opacity: 1 }, tabSpeed, tabEasingMethod, function() {
                        // after the animation is complete allow other tabs to be clicked
                        tabChanging = false;
                    });
                });
            } else {
                tabChanging = false;
            }
        }
        return false;
    });
});

