var change_country_visible = false;

$(document).ready(function()
{
    $('#change_country_div').click(function() {
        if (change_country_visible)
            $('#change_country_popup').slideUp('fast');
        else
            $('#change_country_popup').slideDown('fast');
        change_country_visible = !change_country_visible;
    });
    
    $('#change_country_popup .popup_close').click(function() {
        $('#change_country_popup').slideUp('fast');
        change_country_visible = false;
    });

    $('#checkout_button').click(function() {
        top.location = '/cart';
    });
    
    //Handle click on the retailer menu
    $('#wsretailers').live('click',function(){
        if($(this).attr('displayed') == 'yes'){
            $(this).unbind('click');
            $('#ws_background').unbind('load');
            $('#wsretailers_links').hide('slide',{direction: "right"},500, function(){
                $('#ws_background').attr('src','/images/header/wstarretailers_shadow.png');
            });
            $(this).attr('displayed', 'no');
                
        }else{
            //Make sure the image has compressed before the user can re-open the menu
            if($(this).height() < 95){
                $('#ws_background').load(function(){
                    $('#wsretailers_links').show('slide',{direction: "right"},500);
                });
                $('#ws_background').attr('src','/images/header/wstarretailers_tall_shadow.png');
                $(this).attr('displayed', 'yes');
            }
        }
    });
    
    //Handle hover on the retailer menu
    $('#wsretailers').livequery(function(){
        $(this).hover(function(){
            swapImage($(this).find('img#icon'));
            $(this).find('#text').toggleClass('retailer_highlight');
        },function(){
            swapImage($(this).find('img#icon'));
            $(this).find('#text').toggleClass('retailer_highlight');
        })
    });
    
    //Handle menu at the top of the header
    $('.top_menu_tab').livequery(function(){
        $(this).click(function(){
            $(this).children('.top_menu_tab_body').show();
            $(this).children('.top_menu_tab_flap').children('.tab_bracket').show();
            $(this).children('.top_menu_tab_flap').addClass('top_menu_tab_flap_displayed'); 
        });
        
        $(this).hover(function(){
            swapImage($(this).children('.top_menu_tab_flap').children('.down_arrow')); 
        }, function(){
            $(this).children('.top_menu_tab_flap').children('.tab_bracket').hide(); 
            swapImage($(this).children('.top_menu_tab_flap').children('.down_arrow')); 
            $(this).children('.top_menu_tab_body').hide();
            $(this).children('.top_menu_tab_flap').removeClass('top_menu_tab_flap_displayed'); 
        });
    })
    
    //Handle the menu at the bottom of the header (account/country settings)
    $('.account_menu_tab').livequery(function(){
        $(this).click(function(){
            //Check is other menus of the same type are already open
            if($('.account_menu_tab_flap_displayed').length == 0){
                if($(this).children('.flap_account_status').length == 0 ){
                    $(this).children('.account_menu_tab_flap').addClass('account_menu_tab_flap_displayed'); 
                    $(this).children('.account_menu_tab_flap').children('.tab_bracket_account_menu').show();
                    $(this).children('.account_menu_tab_body').show();
                }else{
                    $(this).children('.account_menu_tab_flap').addClass('account_menu_tab_flap_color_displayed'); 
                }
            }else{
                //If other menus of the same type are displayed, just change the color of the text
                $(this).children('.account_menu_tab_flap').addClass('account_menu_tab_flap_color_displayed'); 
            }
        });
        
        $(this).hover(function(){
            //Always swap the arrow
            swapImage($(this).children('.account_menu_tab_flap').children('.down_arrow')); 
        }, function(){
            if($(this).data('infoClose') == 'true'){
                $(this).oneTime(1000, function(){
                    myAccountClose(this);
                });
                $(this).data('infoClose', 'false');
            }else if($(this).data('hover') != 'true'){
                myAccountClose(this);
            }
        });
        
        $(this).mouseover(function(){
           $(this).stopTime(); 
        });
    });
    
    //Handle the close button for the country settings
    $('#country_close').live('click', function(){
        $('.tab_bracket_account_menu').hide(); 
        $('.account_menu_tab_body').hide();
        $('.account_menu_tab_flap').removeClass('account_menu_tab_flap_displayed');
        $('.selectContainer .options').removeClass('selectDisplay');
    });
    
    //Handle the apply button for the country settings
    $('#country_apply').live('click', function(){
        $('#country_settings').ajaxSubmit({
            url : '/ajax/countrysettings/format/json',
            success: function (data) {
                if(data.success){
                    location.reload(true);
                }
            }
        });
    });
    
    //Handle account login
    $('.flap_account_status').live('click', function(){
        weddingstarLoginPopup();
    });
    
    //Handle popup close
    $('#login_popup_close').click(function(){
        $('#login_popup').dialog('destroy');
    });
    
    //Handle register form submission
    $("#login_popup form#wishlist_register").livequery(function() {
        $(this).ajaxForm({
            url: '/account/register_popup/format/html',
            beforeSubmit: function() {
            },
            success: function(response) {
                if ($(response).find('.error-notice').size() == 0) {
                    $('#login_popup').dialog('destory');
                    $('#login_popup').remove();
                    
                } else {
                    $("#login_popup").html(response);
                }
            }
        });
    });
    
    //Handle login form submission
    $("#login_popup form#wishlist_login").live('submit', function() {
        if($('#site').length > 0){
            var site = '&site=' + $('#site').val();
        }else{
            var site = '';
        }
        if($('#site').length > 0){
            var site = '&site=' + $('#site').val();
        }else{
            var site = '';
        }
        var page = location.pathname.substring(1);
        var input = $(this).formSerialize() + '&page=' + page + site;
        
        $.post('/ajax/accountlogin', input, function(json)
        {
            if (json.dealer) {
                window.location = "http://controlpanel." + json.host;
            } else if (json.tote) {
                location.reload(true);
            } else {
                $("#login_popup #account_login").html(json.account);
            }
        }, 'json');
        return false;
    });
    
    // account logout
    $(".account_menu_tab #log_out").live('click', function(e) {
        e.preventDefault();
        userLogout();
    });
    
    //Handle information popup
    $('a#saved_shopping_bag').click(function(e){
        e.preventDefault();
        $('#saved_shopping_bag_info').show();
    });
    
    //Handle information popup
    $('a#wishlist_info').click(function(e){
        e.preventDefault();
        $('#wish_list_info').show();
    });
    
    //Stop the shopping bag popup from displaying after viewing account info
    $('.account_info_close').click(function(){
       $(this).parent().parent().hide();
       $('.account_menu_tab').data('infoClose', 'true');
    });
});

/**
 * Close the "my account" tab
 * 
 * @param current The current DOM object
 */
function myAccountClose(current)
{
    $('#saved_shopping_bag_info').hide();
    $('#wish_list_info').hide();
    if($(current).find('.flap_country').length == 0 && $(current).find('.flap_country_wholesale').length == 0){
        if($(current).children('.flap_account_status').length == 0 ){
            $(current).children('.account_menu_tab_flap').children('.tab_bracket_account_menu').hide(); 
            $(current).children('.account_menu_tab_body').hide();
            $(current).children('.account_menu_tab_flap').removeClass('account_menu_tab_flap_displayed'); 
        }
    }
    $(current).children('.account_menu_tab_flap').removeClass('account_menu_tab_flap_color_displayed'); 
    swapImage($(current).children('.account_menu_tab_flap').children('.down_arrow')); 
}

