$(function() {
    
    /**
     * load initial cart
     */

    $('div.page-shop.sidebar').load('/cart/view/');
    
    /**
     * drop products to cart canvas
     */

    $(".page-shop ul.products li").draggable({ 
       // create a clone & append it to 'body' 
       helper: function (e,ui) {
          return $(this).clone().appendTo('body').css('zIndex',5).show();
       } 
    });

    $("div.page-shop.sidebar").droppable({
        drop: function(event, ui) {
            $('#dialog').load(ui.draggable.children('a').attr('href'), function(response, status, xhr) {
                $('#col1, #col3').hide();
                $('#dialog').fadeIn();
            });
        }
    });

    /**
     * remove item from cart
     */

    $('ul.basket a.remove_from_cart').live('click', function() {
        $(this).parents('li:first').fadeOut('1000');
        $('div.page-shop.sidebar').load('/cart/remove/?item_id='+$(this).attr('remove'));
    });
    
    /**
     * display product info
     */

    $('ul.products a').click(function() {
        $('#dialog').load($(this).attr('href'), function(response, status, xhr) {
                $('#col1, #col3').hide();
                $('#dialog').fadeIn();
        });
        return false;
    });
    
    /**
     * add product to cart
     */
    
    $('.product_view .add_button button').live('click', function() {
        var product_id = $('.product_view input[name=product_id]').val();
        var qty = $('.product_view input[name=qty]').val();
        var model = (!$('.product_view select[name=model]').size())?'':$('.product_view select[name=model]').val();
        $('div.page-shop.sidebar').load('/cart/add/?product_id='+ product_id + '&quantity=' + qty + '&product_model=' + model);
        $('#col1, #col3').show();
        return false;
    });

    /**
     * checkout: open shipping dialog panel
     */

    $('button[name=checkout], .checkout_restart button').live('click', function() {
        $('#dialog').load('/shop/ship/', function() {
                $('#col1, #col3').hide();
                $('#dialog').fadeIn();
        });
        return false;
    });
    
    /**
     * checkout: calculate ship price from pobox
     */

    $('button[name=calculate_pobox]').live('click', function() {
        $('span.loading').show();
        var cep = ($('input[name=cep]').val())?$('input[name=cep]').val():'';
        if(parseInt(cep.length)==8) {
            $('#dialog').load('/shop/ship/?cep=' + cep);
        }
        return false;
    });
    
    /**
     * display shipping extra options
     */
    
    /**
     * user selected worldwide shipping
     */
    
    $('a.ship_worldwide').live('click', function() {
        $('h3.ship_menu').hide();
        $('.ship_worldwide_options').effect('slide', {'direction':'up', 'mode':'show'}, 300);
        
    });

    /**
     * user selected brazil shipping
     */

    $('a.ship_brazil').live('click', function() {
        $('.ship_menu').hide();
        $('.ship_brazil_options').fadeIn();
    });

    /**
     * select radio with background.
     * used in checkout - payment method
    */

    $('div.input_radio input').live('click', function() {
        $('div.input_radio').css('background-color','');
        $(this).parents('div.input_radio').css('background-color','#ececec');
        //$('form.input[type=submit]').attr('disabled',false);
    });
    
    /**
     * validate fan in checkout
     */

    $('a#fan_code_validate').live('click', function() {
        $.get("/fans/validate/" + $('input#fan_code_id').attr('value') + '/',  function(data){
            if(data == 'False') {
                $('#fan_code_error').fadeIn('slow');
            } else {
                $('#dialog_content').load('/shop/invoice/?cep='+ $('input#fan_code_id').attr('shipping_pobox') +'&service='+$('input#fan_code_id').attr('shipping_method'));
                $('.button_right .continue').fadeIn();
            }
        });
    });
    
    $('input#fan_code_check_id').live('click', function() {
        if($(this).attr('checked')) {
            $('div.fan_code_validate_form').fadeIn('slow');
        } else {
            $('div.fan_code_validate_form').hide();
        }
    });
    
    /**
     * shop menu
     */
    $('a.shop_menu').click(function() {
        $('#col1, #col3').show();
        $('div.page-shop').show();
        $('ul.products li').hide();
        $('ul.products li.type_' + $(this).attr('type_pk')).fadeIn();
        $('a.shop_menu').css('font-weight','normal');
        $(this).css('font-weight','bold');
        $('.page-shop ul.products').show();
        $('#dialog').hide();
    });
});

