$(document).ready(function(){
     
    setSpotlightByPage();
    
    //Constants
    var fadeInOutDelay = 500;
    var fadeInOutSpeed = 100;
    
    /**
     * Holds the location of where the user last visited.
     * menuLocation[0] = category such as cardio or strength
     * menuLocation[1] = zero based index of list items in col1
     * menuLocation[2] = zero based index of list item in col2
     */
    var menuLocation = new Array();
    var productCategory; //Cat in db.  Treadmills=21, Ascent=11, G7 single=31, etc
    
    //Remove all spotlights and return text color to default for all menu item
    //then put a spotlight on the navigation menu item change text color on hovered item
    $('#nav .menu li:not(".nav_separator")').mouseenter(function(){
        restoreNavLinks('#nav .menu li', '#808992'); 
        selectedNavLink(this);
        resetLinkAttrs('#content #overlay #col1 ul a');
        resetLinkAttrs('#content #overlay #col2 ul a');
        $('#content #overlay #col2, #content #overlay #col3').empty();     
    });
    
    /**
     * Fadein the overlay when mousing over navigation menu item.
     * Send the navigation category (cardio, strength, etc.) along with the id
     * of the first item to be displayed if the user hasn't previously
     * viewed any items.
     *
     * @param category, highlightItem, firstItem
     */
    $('#nav .menu li#main_2567, #nav .menu li#main_2556').mouseenter(function(){
        $('#sm_overlay').hide();
        var id = $(this).find('a').attr('id');
        var category = id.match(/\d+/);
        $.get('/service/highlight/' + category, function(highlight){
            overlayFadeinItems('cat-' + category, 0, highlight); 
        });
    });
    
    // overlay close button
    $('#overlayCloseButton').bind('click', function() {
      $('#overlay').stop(true, true).hide();
    });
    
    //Show the overlay when mouse is on it and keep spotlight and text color in nav item.
    $('#overlay').mouseenter(function(){
        $(this).stop(true, true).show();
        var spotlightItem = menuLocation[0].replace('cat-', 'nav_id_');
        selectedNavLink('#nav .menu li#' + spotlightItem);
        var category = ('#overlay #col1 .category').attr('id');
    });
    
    //Remove all spotlights and revert text color when mousing off of the nav menu and fadeout the overlay.
    $('#nav .menu').mouseleave(function(){
        restoreNavLinks('#nav .menu li', '#808992');
        setSpotlightByPage();
        $('#overlay, #sm_overlay').stop(true, true).delay(fadeInOutDelay).fadeOut(fadeInOutSpeed, function(){ 
        });
    });

    //When mouse leaves the overlay, remove effects on col1 menu items,
    //empty columns 2 & 3, remove spotlight and change nav text color back to default on nav menu.
    $('#overlay').mouseleave(function(){
        $(this).stop(true, true).delay(fadeInOutDelay).fadeOut(fadeInOutSpeed, function(){
            resetLinkAttrs('#content #overlay #col1 ul a');
            resetLinkAttrs('#content #overlay #col2 ul a');
            $('#content #overlay #col2, #content #overlay #col3').empty();
            restoreNavLinks('#nav .menu li', '#808992');
            setSpotlightByPage();
        });      
    });
    
    /* ### END OVERLAY FADEIN AND FADEOUT ON NAVIGATION HOVER ### */
   
    /* ### BEGIN LOGIC FOR DISPLAYING CONTENT IN COL 2 IN OVERLAY ### */
    
    //When hovering in col1, reset menu items and hide any col3 elements.
    $('#content #overlay #col1').mouseenter(function(){
        resetLinkAttrs('#content #overlay #col1 ul a');
        setLinkAttrs('#content #overlay #col1 #' +  menuLocation[0] + ' a:eq(' + menuLocation[1] + ')');
        $('#content #overlay #col3 .col3_elements').hide();
    });
    
    /**
     * General action for all col1 links on hover.
     * Set the current category and item hovered over in col1.
     * Reset all the links in the overlay then highlight the one moused over.
     * Load content for col 2 and col3 based on what is hovered in col1
     * Col2 content will display, col3 content is hidden.
    */
    var isCol1Requested = false;
    $('#content #overlay #col1 ul li a').mouseenter(function(){

        var that = $(this);
        menuLocation[0] = $(this).parents('.category').attr('id');
        menuLocation[1] = $(this).index('#' + menuLocation[0] + ' a');
        //alert(menuLocation[1]);
        resetLinkAttrs('#content #overlay #col1 ul a');
        setLinkAttrs(this);
        $('#content #overlay #col2, #content #overlay #col3').show(); //We have to display it in order to empty it.
        $('#content #overlay #col2, #content #overlay #col3').empty();
  //      $('#content #overlay #col2').css('background', 'url(/images/ajax-loader_col2.gif) no-repeat center center')
   //     $('#content #overlay #col3').css('background', 'url(/images/ajax-loader_col3.gif) no-repeat center center')
        productCategory = $(this).attr('id'); 
        productCategory = productCategory.substr(productCategory.search(/\d/),productCategory.length); 
        if(isCol1Requested==false) {
                setTimeout(function(){
                        if ((that.attr('class') == 'highlightLink')){
                            $.ajax({
                                url: '/service/index/' + productCategory,
                                beforeSend:  function() { 
                                    $('#content #overlay #col2').css('background', 'url(/images/ajax-loader_col2.gif) no-repeat center center'); 
                                },
                                success: function(data){
                                    isCol1Requested = true;
                                    var col2 = data.col2;
                                    loadCol2Content(col2.replace('\\', ''));
                                    isCol1Requested = false;
                                    $('#content #overlay #col2, #content #overlay #col3').css('background', 'none');
                                    //alert($('#content #overlay #col2 ul li a').attr('id'));
                                },
                                dataType: 'json'
                              });
                       }
             }, 500);
       }
    });
    
    //$('#content #overlay #col1 ul li a').mouseenter(function(){});
    
    //When mousing out of the list, reset all the links and highlight the one moused over.
    $('#content #overlay #col1 ul').live('mouseleave', function(){
        resetLinkAttrs('#content #overlay #col1 ul a');
        setLinkAttrs('#content #overlay #col1 #' +  menuLocation[0] + ' a:eq(' + menuLocation[1] + ')');
    });
        
    //General action for all  links on mouseleave.
    //Remove bg color, reset font color and hide double right arrow on link.
    $('#content #overlay #col1 ul a').mouseleave(function(){
        resetLinkAttrs('#content #overlay #col1 ul a');
    });
    
    /**
     * When col2 is hovered, hide col3 elements and reset links in col2.
     * Then set col1 link attributes based on what's in the menuLocation array.
     */
    $('#content #overlay #col2').live('mouseenter', function(){
        $('#content #overlay #col3 .col3_element').hide();
        resetLinkAttrs('#content #overlay #col2 ul a');
        setLinkAttrs('#content #overlay #col1 #' +  menuLocation[0] + ' a:eq(' + menuLocation[1] + ')');
    });
    
    //When col2 is hovered off, remove style from col1 menus.
    /*$('#content #overlay #col2').live('mouseleave', function(){
        resetLinkAttrs('#content #overlay #col1 ul a');
    });*/
    
    //This prevents looking the link attrs in the 10px left padding area on the ul.
    $('#content #overlay #col2 ul li').live('mouseleave', function(){
        setLinkAttrs('#content #overlay #col2 ul li:eq(' + menuLocation[2] +') a');
    });
    
    //When leaving ul, first clear all link styles and then highlight the current one.
    //This is so the link maintains state while still in col2 before moving to col3.
    $('#content #overlay #col2 ul').live('mouseleave', function(){
        resetLinkAttrs('#content #overlay #col2 ul a');
        setLinkAttrs('#content #overlay #col2 ul li:eq(' + menuLocation[2] +') a');
    });

    /* ### END LOGIC FOR DISPLAYING CONTENT IN COL 2 IN OVERLAY ### */
    
    
    /* ### BEGIN LOGIC FOR DISPLAYING CONTENT IN COL 3 IN OVERLAY ### */
    
    //General action for all col2 links on hover. Reset all links and then
    //change the bg color, display double right arrow.
    var isRequested = false;
    $('#content #overlay #col2 ul a').live('mouseenter', function(data){
        var that = $(this);
        menuLocation[2] = $(this).index('#content #overlay #col2 ul a');
        resetLinkAttrs('#content #overlay #col2 ul a');
        setLinkAttrs(this);
        var pdb_id = $(this).attr('id'); 
        if (pdb_id != ''){
            if(isRequested==false){
                setTimeout(function(){
                    if ((that.attr('class') == 'highlightLink')){
                        $.ajax({
                            url: '/service/col3/' + pdb_id,
                            beforeSend:  function() {  
                                            $('#content #overlay #col3').html('');  
                                            $('#content #overlay #col3').css('background', 'url(/images/ajax-loader_col3.gif) no-repeat center 195px'); 
                                        },
                            success: function(data){ 
                                        isRequested = true;
                                        var i = 0;
                                        var values = '';
                                        
                                        if ((that.attr('class') == 'highlightLink')){
                                            $('#content #overlay #col2, #content #overlay #col3').css('background', 'none');
                                            if($.browser.msie) {
                                              $('#content #overlay #col3').stop(true, true).hide();
                                            }
                                            else {
                                              $('#content #overlay #col3').stop(true, true).fadeOut(100);
                                            }
                                            
                                            
                                            $('#content #overlay #col3').hide();
                                            $('#overlay #col3').html(data.col3); 
                                            if($.browser.msie) {
                                              $('#content #overlay #col3').stop(true, true).show();
                                            }
                                            else {
                                              $('#content #overlay #col3').stop(true, true).fadeIn(500);
                                            }
                                        }
                                        //Remove bullet for last list item
                                        $('#content #overlay #col3 li:last-child').css('list-style', 'none');
                                        isRequested = false;
                                    },
                        dataType: 'json'
                        });
                    }
                }, 500);
            }
        }
    });
    
    $('#overlay #col3').ajaxError(function(e, xhr, opt){
        //alert("Error requesting " + opt.url + ": " + xhr.status + " " + xhr.statusText);
        isRequested = false;    
    });
    
    //General action for all col2 links on mouseleave.
    //Remove bg color and hide double right arrow on link.
    $('#content #overlay #col2 ul a').live('mouseleave', function(){
        resetLinkAttrs('#content #overlay #col2 ul a');
    });
    
    //When col3 is hovered off, hide detail it and remove style from col2 menus and hide col2.
    /*$('#content #overlay #col3').live('mouseleave', function(){

        //Don't use this because it matters if the user goes from col3 to either the overlay or to col2.

    });*/
    
    //When content in col3 is hovered over, keep the bg color and
    //double right arrow on the link on both col1 and col2.
    $('#content #overlay #col3').live('mouseenter', function(){
        setLinkAttrs('#content #overlay #col1 #' +  menuLocation[0] + ' a:eq(' + menuLocation[1] + ')');
        setLinkAttrs('#content #overlay #col2 ul li:eq(' + menuLocation[2] +') a');
        $('#content #overlay #col2, #content #overlay #col3').show();
        $(this).css('cursor', 'pointer');
    });
    
    $('#content #overlay #col3 .col3_element').live('click', function(){
        var url = $(this).attr('rel');
        window.location = url;
    });
    
    /* ### STYLES FOR COL 1 OF OVERLAY ###*/
    
    $('#content #overlay #col1 h3:eq(0)').css('background', 'url(/images/nav_icon_g7.gif) no-repeat left center');
    $('#content #overlay #col1 h3:eq(1)').css('background', 'url(/images/nav_icon_g3.gif) no-repeat left center');
    $('#content #overlay #col1 h3:eq(2)').css('background', 'url(/images/nav_icon_g1.gif) no-repeat left center');
    $('#content #overlay #col1 h3:eq(3)').css('background', 'url(/images/nav_icon_ls.gif) no-repeat left center');

    /*  ### SMALL OVERLAY ### */
        
    $('#nav .menu li#main_2569').mouseenter(function(){
        menuLocation[0] = 'partners';
        //$('#overlay').delay(fadeInOutDelay).fadeOut(fadeInOutSpeed);
        $('#overlay').hide();
        $('#sm_overlay .category').hide();
        $('#sm_overlay').stop(true, true).fadeIn(fadeInOutSpeed).css('left', '210px');
        $('#sm_overlay #main_2569').show();
    });
    
    $('#nav .menu li#main_2570').mouseenter(function(){
        menuLocation[0] = 'solutions';
        //$('#overlay').delay(fadeInOutDelay).fadeOut(fadeInOutSpeed);
        $('#overlay').hide();
        $('#sm_overlay .category').hide();
        $('#sm_overlay').stop(true, true).fadeIn(fadeInOutSpeed).css('left', '316px');
        $('#sm_overlay #main_2570').show();
    });
    
    $('#nav .menu li#main_2574').mouseenter(function(){
        menuLocation[0] = 'support';
        //$('#overlay').delay(fadeInOutDelay).fadeOut(fadeInOutSpeed);
        $('#overlay').hide();
        $('#sm_overlay .category').hide();
        $('#sm_overlay').stop(true, true).fadeIn(fadeInOutSpeed).css('left', '424px');
        $('#sm_overlay #main_2574').show();
    });
        
    $('#content #sm_overlay').mouseenter(function(){
        $(this).stop(true, true).show();
        restoreNavLinks('#nav .menu li', '#808992');
        selectedNavLink('#nav .menu li.' + menuLocation[0]);
        resetLinkAttrs('#content #sm_overlay ul li a');
    });
    
    $('#content #sm_overlay').mouseleave(function(){
        $(this).stop(true, true).delay(fadeInOutDelay).fadeOut(fadeInOutSpeed, function(){
            $(this).find('.category').hide();
            restoreNavLinks('#nav .menu li', '#808992');
            setSpotlightByPage();
        });
    });
            
    $('#content #sm_overlay ul li a').mouseenter(function(){
        resetLinkAttrs('#content #sm_overlay ul li a');
        setLinkAttrs(this);
    });
    
    /* ### FOOTER OVERLAY ###*/
    var footerMenu;
    
    $('#footer .menu li:even').css('min-width', '78px');
    
    $('#footer .menu li.footer_2571').mouseenter(function(){
        footerMenu = 'about';
        $('#footer_overlay').hide();
        $('#footer_overlay .category').hide();
        $('#footer_overlay').stop(true, true).fadeIn(fadeInOutSpeed);
        $('#footer #footer_overlay').css({'left' : '485px', 'bottom' : '0px'});
        $('#footer_overlay #footer_2571').show();
    });
    
    $('#footer .menu li.footer_2572').mouseenter(function(){
        footerMenu = 'distributors';
        $('#footer_overlay').hide();
        $('#footer_overlay .category').hide();
        $('#footer_overlay').stop(true, true).fadeIn(fadeInOutSpeed);
        $('#footer #footer_overlay').css({'left' : '570px', 'bottom' : '0px'});
        $('#footer_overlay #footer_2572').show();
    });
    
    $('#footer .menu li.showcases').mouseenter(function(){
        footerMenu = 'showcases';
        $('#footer_overlay').hide();
    });
    
    $('#footer .menu li.sitemap').mouseenter(function(){
        footerMenu = 'sitemap';
        $('#footer_overlay').hide();
    });
    
    $('#footer .menu li.contact').mouseenter(function(){
        footerMenu = 'contact';
        $('#footer_overlay').hide();
    });
    
    $('#footer .menu li.privacy').mouseenter(function(){
        footerMenu = 'privacy';
        $('#footer_overlay').hide();
    });
    
    $('#footer .menu li a').hover(function(){
        selectedNavLink($(this).parent());  
    }, function(){
        restoreNavLinks('#footer .menu li', '#545c65');
    });
    
    $('#footer #footer_overlay').mouseenter(function(){
        $(this).stop(true, true).show();
        selectedNavLink('#footer .menu li.' + footerMenu);
        resetLinkAttrs('#footer #footer_overlay ul li a');
    });
    
    $('#footer #footer_overlay, #footer .menu').mouseleave(function(){
        $('#footer #footer_overlay').stop(true, true).delay(fadeInOutDelay).fadeOut(fadeInOutSpeed, function(){
            restoreNavLinks('#footer .menu li', '#545c65');
            setSpotlightByPage();
        });
    });
    
    $('#footer #footer_overlay ul li a').mouseenter(function(){
        resetLinkAttrs('#footer #footer_overlay ul li a');
        setLinkAttrs(this);
    });
    
    /* ### METHODS FOR COMMON ACTIONS ### */
    
    /**
     * Checks to see if menuLocation was previously set OR if it set with items
     * from a different category than we are hovering over.  If either of those
     * cases are true, reset the menu items to the default of the first item.
     */
    function overlayFadeinItems(category, highlightItem, firstItem){
        if((!(isset(menuLocation[1]))) || (menuLocation[0] != category)){
            //alert('not set');
            menuLocation[0] = category;
            menuLocation[1] = highlightItem;
            menuLocation[2] = 0;
            productCategory = firstItem;
        }
        //alert(menuLocation[0] + ' ' + menuLocation[1] + ' ' + menuLocation[2]);
        $('#overlay').stop(true, true).fadeIn(fadeInOutSpeed);
        $('#overlay #col2, #overlay #col3').empty();
        $('#overlay #col1 .category').hide();
        $('#overlay #col1 #' + category).show();
        $('#overlay #col1 ul a').removeClass('highlightLink');
        $('#overlay #col1 ul a span').removeClass('showArrow');
        //alert(productCategory);
        $.ajax({
            url: '/service/index/' + productCategory, 
            beforeSend: function() { $('#content #overlay #col2').css('background', 'url(/images/ajax-loader_col2.gif) no-repeat center center'); },
            success: function(data){
                    var col2 = data.col2;
                    loadCol2Content(col2.replace('\\', ''));
                    $('#content #overlay #col1 #' +  menuLocation[0] + ' a:eq(' + menuLocation[1] + ')').addClass('highlightLink');
                    $('#content #overlay #col1 #' +  menuLocation[0] + ' a:eq(' + menuLocation[1] + ') span').addClass('showArrow');
                    $('#content #overlay #col2 ul li:eq(' + menuLocation[2] +') a').addClass('highlightLink');
                    $('#content #overlay #col2 ul li:eq(' + menuLocation[2] + ') a span').addClass('showArrow');
                    $('#content #overlay #col2, #content #overlay #col3').css('background', 'none');
                    jcarousellite();
                    var pdb_id =$('#content #overlay #col2 ul li:eq(' + menuLocation[2] +') a').attr('id');
                    $.get('/service/col3/' + pdb_id, function(data){ 
                        $('#content #overlay #col2, #content #overlay #col3').css('background', 'none');
                        // need to not use fade effects if browser is IE
                        if($.browser.msie) {
                          $('#content #overlay #col3').stop(true, true).hide();
                        }
                        else {
                          $('#content #overlay #col3').stop(true, true).fadeOut(fadeInOutSpeed);
                        }
                        $('#content #overlay #col3').hide();
                        $('#overlay #col3').html(data[0]); 
                        if($.browser.msie) {
                          $('#content #overlay #col3').stop(true, true).show();
                        }
                        else {
                          $('#content #overlay #col3').stop(true, true).fadeIn(fadeInOutSpeed);
                        }
                    }, 'json');
                }, 
            dataType: 'json'
            }
        );
    }
        
    //Loads the content for col 2 and col 3.  Col 2 content displays, col 3 is hidden onload.
    function loadCol2Content(col2Content){
        $('#content #overlay #col2, #content #overlay #col3').show(); //We have to display it in order to empty it.
        $('#content #overlay #col2, #content #overlay #col3').empty();
        $('#content #overlay #col2').removeClass();
        $('#content #overlay #col2').html(col2Content).stop(true, true).delay(1000);
        $('#content #overlay #col3 .col3_element').hide();
        jcarousellite();
    }
    
    //Reset links in overlays back to original state.
    function resetLinkAttrs(selector){
        $(selector).removeClass('highlightLink');
        $(selector).find('span').removeClass('showArrow');
    }
    
    //Attributes when links in overlay are hovered over.
    function setLinkAttrs(selector){
        $(selector).addClass('highlightLink');
        $(selector).find('span').addClass('showArrow');
    }
    
    //Restores navigation links to original state.
    function restoreNavLinks(selector, color){
        $(selector).removeClass('spotlight');
        $(selector).find('a').css('color', color);
    }
    
    //Changes color and adds spotlight to nav link when hovered over.
    function selectedNavLink(selector){
        $(selector).addClass('spotlight');
        $(selector).find('a').css('color', '#fff');
    }
    
    //Get the page we are on and set the spotlight.
    function setSpotlightByPage(){
        if($('#nav #product_category').length>0){
            var product_category = $('#nav #product_category').text();
            if(product_category==2){
                selectedNavLink('#nav .menu li#main_24');
            }
            if(product_category==3){
                selectedNavLink('#nav .menu li#main_20');
            }
        }
        if($('#nav #page_category').length>0 || $('#nav #page_name').length>0){
            var page_category = $('#nav #page_category').text(); 
            var page_name = $('#nav #page_name').text(); 
            //Partners
            if(page_category==45){
                selectedNavLink('#nav .menu li#main_2569');
            }
            //Solutions
            if(page_category==46){
                selectedNavLink('#nav .menu li#main_2570');
            }
            //Support
            if(page_category==56 || page_name=='manuals'){
                selectedNavLink('#nav .menu li#main_2574')
            }
            //About Matrix
            if(page_category==55){
                selectedNavLink('#footer .menu li.footer_2571')
            }
            //Distributors
            if(page_category==61){
                selectedNavLink('#footer .menu li.footer_2572')
            }
            //Showcases
            if(page_category==53){
                selectedNavLink('#footer .menu li.footer_2563')
            }
            if(page_name=='sitemap'){
                selectedNavLink('#footer .menu li.footer_3225')
            }
            if(page_name=='contact'){
                selectedNavLink('#footer .menu li.footer_3226')
            }
            if(page_name=='privacy-policy'){
                selectedNavLink('#footer .menu li.footer_3227')
            }
        }
    }
    
    //isset
    function isset(variable){
        if (typeof variable !== "undefined" && variable) {
            return true;
        }
        return false;
    }
 
      
    function jcarousellite(){
        $('div.carousel').jCarouselLite({
            btnNext: "#next",
            btnPrev: "#prev",
            vertical: true,
            visible: 16,
            circular: false,
            mouseWheel: true
        });
    }
    
});

