/* $Id: menu.js 20581 2011-11-21 17:04:05Z mwall $ */

/*
 * menu.js
 *
 * Menu Jquery bindings
 */
    
$(document).ready(function() {  
    $('#menu_top_container a img').hover(function(){
        var srcImage = $(this).attr('src');
        var srcValero = srcImage.indexOf("velaro");
        if( srcValero == -1) {
            // get current element name
            var imgID = $(this).attr('id');
            // find hidden image
            var altElement = document.getElementById(imgID + '_alt');
            var altImage = altElement.src;
            // swap images
            altElement.src = srcImage;
            $(this).attr('src' , altImage);
        }
    },function() {
        var srcImage = $(this).attr('src');
        var srcValero = srcImage.indexOf("velaro");
        if( srcValero == -1) {
            // get current element name
            var imgID = $(this).attr('id');
            // find hidden image
            var altElement = document.getElementById(imgID + '_alt');
            var altImage = altElement.src;
            // swap images
            altElement.src = srcImage;
            $(this).attr('src' , altImage);
        }
    });

    // need this for the partner istes with the currency div on the menu
    $('#menu_top_container div img').hover(function(){
        var imgID = $(this).attr('id');
        var srcImage = $(this).attr('src');
        // find hidden image
        var altElement = document.getElementById(imgID + '_alt');
        var altImage = altElement.src;
        // swap images
        altElement.src = srcImage;
        $(this).attr('src' , altImage);
    },function() {
        var imgID = $(this).attr('id');
        var srcImage = $(this).attr('src');
        // find hidden image
        var altElement = document.getElementById(imgID + '_alt');
        var altImage = altElement.src;
        // swap images
        altElement.src = srcImage;
        $(this).attr('src' , altImage);
    });

    // add FAQ click functionality
    $('#helpandfaq, #top-helpandfaq').live('click', function(e) {
        e.preventDefault();
        //If the checkout return policy link is displayed, ensure that the first category is set to "1"
        if($('#checkout_returns').length > 0){
            $('#checkout_returns').attr('first','1');
        }
        dialog('', '/ajax/helpandfaq/', {}, '#faq_close_button', undefined, addInteractivityHelpAndFaq);
    });

        //add the bevclark newsletter signup popup    
        var url=document.URL.split('#')[1];
        if(url == 'bevclark'){
            $.post('/ajax/bevclarksignup/format/json', {bevclark: 'true'}, function(data) {
                $('body').append('<div id="bev_clark_popup" style="display:none;"></div>');
                $('#bev_clark_popup').html(data.html);
                $('#bev_clark_popup').dialog({
                    position: 'center',
                    resizable: 'false',
                    dialogClass: 'bev_clark_signup',
                    width: '333px',
                    modal: true,
                    close: function(){
                        $('#bev_clark_popup').dialog('destroy');
                        //Remove the div and append it again because the dialog has some issues (the form will be un-usable upon dialog re-open)
                        $('#bev_clark_popup').remove();
                    }
                });
            }, 'json');
        }
   
    $('#bev_close_button').livequery('click', function(){
        $('#bev_clark_popup').dialog('close');
    });
    
    //Hanlde shop all menu display
    $('#shop_all_products').livequery(function(){
        $(this).hover(function(){
            menuClearAll($('#shop_all_menu_inner li[class*="grouping"]'));
            $('#shop_all_menu').show();
        },function(){
            $('#shop_all_menu').hide();
        })
    });
    
    //Handle click for shop by magazine in the shop all menu
    $('.mag_popout_inner').live('click', function(){
        window.location = '/shopbymagazine';
    });
    
    //Handle "Shop All Products" pop out
    $('#shop_all_menu_inner li[class*="grouping"]').hover(function(){
        $(this).siblings().stopTime();
        $(this).oneTime(200, function(){
            $('#shop_all_products').data('popout', 'false');
            menuClearAll($(this).siblings());
            $(this).children('img.shop_all_selection').show();
            $(this).find('img.shop_all_selection_mag').show();
            $(this).addClass('selected');
            var top = $('#shop_all_menu').offset().top;
            var left = $('#shop_all_menu').offset().left + 215;
            var height = $('#shop_all_menu').css('height');
            if($(this).children('.subcategory_container').length > 0){
                $(this).children('.subcategory_container').css('height', height);
                $(this).children('.subcategory_container').show();
                $(this).children('.subcategory_container').offset({top: top, left: left});
            }else{
                $(this).children('.shop_popout').css('height', height);
                $(this).children('.shop_popout').show();
                /* The following is an IE7 only fix*/
                var popWidth = $(this).children('.shop_popout').width();
                $(this).children('.shop_popout').css('width', popWidth + 'px');
                /***********************************/
                if(!$(this).children('.shop_popout').css('top') < 1){
                    $(this).children('.shop_popout').offset({top: top, left: left});
                }
            }
            $(this).find('img.nav_shop_mag_img').attr('src','/images/menu/nav_shop_by_magazine_active.jpg');
            $(this).find('img.nav_shop_mag_img_active').attr('src','/images/menu/nav_shop_by_magazine.jpg');
            
            $(this).find('img.arrow_base').attr('src','/images/menu/rightarrow_white.png');
            $(this).find('img.arrow_selected').attr('src','/images/menu/rightarrow.png');
        });
    });
});

function addInteractivityHelpAndFaq()
{
    // initially, hide everything
    $('.faq_ajax_response .faq_questions .faq_allcaps_title_kpk').hide();
    $('.faq_ajax_response .faq_questions      .faq_question_kpk').hide();
    $('.faq_ajax_response .faq_answers           .faq_title_kpk').hide();
    $('.faq_ajax_response .faq_answers          .faq_answer_kpk').hide();

    // add hover and click capabilities to each element
    $('.faq_ajax_response .faq_categories .faq_category_kpk').each(function() {
        var jThis = $(this);
        jThis.click(function() {
            showFaqCategory(jThis.attr('wstar_category_id'));
        });
        jThis.hover(
            function() {
                jThis.addClass('faq_underline');
            },
            function() {
                jThis.removeClass('faq_underline');
            }
        );
    });

    $('.faq_ajax_response .faq_questions .faq_question_kpk').each(function() {
        var jThis = $(this);
        jThis.click(function() {
            showFaqAnswer(jThis.attr('wstar_question_id'));
        });
        jThis.hover(
            function() {
                jThis.addClass('faq_underline');
            },
            function() {
                jThis.removeClass('faq_underline');
            }
        );
    });

    showFirstFaqCategory();
}

function showFirstFaqCategory()
{
    //Check if the returns category should be the first category (checkout return policy display in step four of the checkout)
    if($('#checkout_returns').length > 0){
        firstcategory_id = $('#checkout_returns').attr('first');
        showFaqCategory(firstcategory_id);
    }else{
        firstcategory_id = $('.faq_ajax_response .faq_categories .faq_category_kpk:first').attr('wstar_category_id');
        showFaqCategory(firstcategory_id);
    }
        
}

function showFaqCategory(category_id)
{
    $('#ajax_display_content .faq_ajax_response .faq_answers').animate({left: 300}, function(){
        $('#ajax_display_content .faq_ajax_response .faq_answers').hide();
        $('#ajax_display_content .faq_ajax_response .faq_questions').animate({left: 0}, function(){
            $('#ajax_display_content .faq_ajax_response .faq_questions .faq_allcaps_title_kpk[wstar_category_id!="'+category_id+'"]').hide();
            $('#ajax_display_content .faq_ajax_response .faq_questions      .faq_question_kpk[wstar_category_id!="'+category_id+'"]').hide();
            $('#ajax_display_content .faq_ajax_response .faq_questions .faq_allcaps_title_kpk[wstar_category_id="'+category_id+'"]').show();
            $('#ajax_display_content .faq_ajax_response .faq_questions      .faq_question_kpk[wstar_category_id="'+category_id+'"]').show();
            $('#ajax_display_content .faq_ajax_response .faq_questions').animate({left: 300}, function(){
                showFirstFaqAnswer(category_id);
            });
        });
    });
}

function showFirstFaqAnswer(category_id)
{
    var firstquestion_id = $('.faq_ajax_response .faq_questions .faq_question_kpk[wstar_category_id="'+category_id+'"]:first').attr('wstar_question_id');
    showFaqAnswer(firstquestion_id);
}

function showFaqAnswer(question_id)
{
    $('#ajax_display_content .faq_ajax_response .faq_answers').animate({left: 300}, function(){
        $('#ajax_display_content .faq_ajax_response .faq_answers  .faq_title_kpk[wstar_question_id!="'+question_id+'"]').hide();
        $('#ajax_display_content .faq_ajax_response .faq_answers .faq_answer_kpk[wstar_question_id!="'+question_id+'"]').hide();
        $('#ajax_display_content .faq_ajax_response .faq_answers  .faq_title_kpk[wstar_question_id="'+question_id+'"]').show();
        $('#ajax_display_content .faq_ajax_response .faq_answers .faq_answer_kpk[wstar_question_id="'+question_id+'"]').show();
        $('#ajax_display_content .faq_ajax_response .faq_answers').show();
        $('#ajax_display_content .faq_ajax_response .faq_answers').animate({left: 600});
    });
}

/**
 * Clear all visible elements in the "Shop All Products" menu.
 */
function menuClearAll(current)
{
    if($(current).children('.subcategory_container').length > 0){
        $(current).children('.subcategory_container').hide();
    }else{
        $(current).children('.shop_popout').hide();
    }
    $(current).children('img.shop_all_selection').hide();
    $(current).find('img.shop_all_selection_mag').hide();
    $(current).removeClass('selected');
    
    $(current).find('img.nav_shop_mag_img').attr('src','/images/menu/nav_shop_by_magazine.jpg');
    $(current).find('img.nav_shop_mag_img_active').attr('src','/images/menu/nav_shop_by_magazine_active.jpg');
    
    $(current).find('img.arrow_selected').attr('src','/images/menu/rightarrow_white.png');
    $(current).find('img.arrow_base').attr('src','/images/menu/rightarrow.png');
}
