$(document).ready(function(){

    //lang & currency
    $('#lang, #currency').prepend('<div><span></span></div>');
    $('#lang ul, #currency ul').hide();
    $('#lang div span').html($('#lang ul li.current a').html()).addClass($('#lang ul li.current a').attr('class'));
    $('#currency div span').html($('#currency ul li.current a').html()).addClass($('#currency ul li.current a').attr('class'));
    $('#lang div, #currency div').bind('click', function(e){
        if ($(this).siblings().css('display') == 'none'){
            $(this).addClass('expanded').siblings().fadeIn();
        }
        else {
            $(this).removeClass('expanded').siblings().fadeOut();
        }
        e.stopPropagation();
    }).hover(
        function(){ $(this).addClass('over') },
        function(){ $(this).removeClass('over') }
    );
    $(document).click(function(){
        if ($('#lang ul').css('display') == 'block'){
            $('#lang ul').fadeOut().siblings().removeClass('expanded');
        }
        if ($('#currency ul').css('display') == 'block'){
            $('#currency ul').fadeOut().siblings().removeClass('expanded');
        }
    });

    //menu
    $('#menu > li').each(function(){
        if ($(this).find('ul').length > 0){
            $(this).find('ul').bgiframe();
            $(this).hoverIntent(
                function(){ $(this).addClass('hover'); },
                function(){ $(this).removeClass('hover'); }
            );
        }
    });

    //config
    var maxWidth = 0;
    $('#config label').each(function(){
        if ($(this).width() > maxWidth){ maxWidth = $(this).width(); }
    });
    if (maxWidth < 50){ maxWidth = 50; }
    $('#config label').css('width', maxWidth);
    $('#config-list').css('width', maxWidth + 400).hide();
    $('#config h2').bind('click', function(){
        if ($('#config-list').css('display') == 'none'){
            $(this).addClass('expanded');
            $('#config-list').fadeIn().css('visibility', 'visible');
        }
        else {
            $(this).removeClass('expanded');
            $('#config-list').fadeOut().css('visibility', 'hidden');
        }
    }).hover(
        function(){ $(this).addClass('over') },
        function(){ $(this).removeClass('over') }
    );

    //search
    $('#search input[type="text"]').each(function(){
        var default_txt = $(this).val();
        $(this).focus(function(){
            if ($(this).val() == default_txt){
                $(this).val('');
            }
        }).blur(function(){
            if (jQuery.trim($(this).val()) == ''){
                $(this).val(default_txt);
            }
        });
    });

    //yourcart
    $('#yourcart .info').hide();
    var hasShown = $('#yourcart h2').hasClass('shown');
    if (hasShown){ $('#yourcart .info').show(); }
    $('#yourcart h2').bind('click', function(){
        var hasShown = $('#yourcart h2').hasClass('shown');
        if (hasShown){
            hideCart();
        }
        else showCart();
    });

});

function showCart(){
    $('#yourcart h2').addClass('shown');
    $('#yourcart .info').slideDown();
}
function hideCart(){
    $('#yourcart h2').removeClass('shown');
    $('#yourcart .info').slideUp();
}

function focusNewestItem(id){
    $('#' + id).css('background-color', '#fc6').animate({backgroundColor: '#fff'}, 1000);
}

var timer;
function notification(title){
    clearTimeout(timer);
    if ($('#notification').length == 0){
        $('body').prepend('<div id="notification"></div>');
    }
    var element = $('#notification');
    $('#notification').html(title).slideDown();
    timer = setTimeout('removeNotification()', 3000);
}
function removeNotification(){
    $('#notification').slideUp().html('');
}
