$(document).ready(function(){

    $('input[name="search"]').bind('focus', function(){
        $(this).val('');
    }).bind('focusout', function(){
        $(this).val('Search product');
    });

    $('#banner').cycle({ 
        fx:    'fade', 
        timeout: 4000,
	    delay: 1000,
	    pause:   1
    });

    $('.prod-img').scrollable({ size: 1, clickable: false }).navigator({ navi:'.navi' });

    $("#cat-nav").accordion({ navigation: true, header: "a.label", autoHeight: false });

    $('.prod-img .items a').fancybox();

    $('#id_differ').live('click', function(){
        $('.shipping div').toggleClass('hidden');
    });

    if($('#id_differ:checked').val() != null){
        $('.shipping div').addClass('hidden');
    } else {
        $('.shipping div').removeClass('hidden');
    }

    $('.qty').keyup(function(){
        var qty = $(this).val();
        var product = $('input[name=productname]').val();
        var variation = $('select[name="variation"] option:selected').val();
        if(product && qty){
            $.ajax({
                url: "/ajax/line-total/",
                dataType: 'json',
                data: { 'product': product, 'qty': qty, 'variation': variation },
                success: function(data){
                    if(data.success == 'true'){
                        $('.price').val('€ ' + data.price);
                        $('.no-stock').hide();
                    } else {
                        var error = $('<h6 class="no-stock">Let op! Slechts '+ data.stock +' nog beschikbaar!</h6>');
                        if($('.no-stock').length == 0){
                            $('.prod-info').after(error);
                        } else {
                            $('.no-stock').show();
                        }
                        $('.qty').val('1');
                        $('.price').val('€ ' + data.price);
                    }
                }
            });
        }
    });

    $('.cart-qty').keyup(function(){
        var qty = $(this).val();
        var row = $(this).closest('tr');
        var item_id = $(row).find('a.item').attr("rel");
        if(qty && item_id){
            $.getJSON("/ajax/cart-line/", { 'item_id': item_id, 'qty': qty }, function(data){
                if(data != "error"){
                    $('.error').hide();
                    $(row).find('.price p').html(data.price);
                    $('.price p.shipping').html(data.shipping);
                    $('.price p.total').html(data.total);
                    $('.price p.subtotal').html(data.subtotal);
                } else {
                    $('.cart-qty').val('1');
                }
            });
        }
    });

    $('#country').change(function(){
        var country = $('#country option:selected').val();
        if(country){
            $.getJSON("/ajax/shippingcost/", {'country': country }, function(data){
                $('.shipping').html(data.shipping);
                $('.total').html(data.total);
            });
        }
    });

    $('select[name="variation"]').change(function(){
        $('.error').hide();
        var variation = $('select[name="variation"] option:selected').val();
        var product = $('input[name=productname]').val();
        if(product && variation){
            $.get("/ajax/variation/", { 'product': product, 'variation': variation }, function(data){
                $('.price').val('€ ' + data);
                $('span.price').html('€ ' + data);
                $('.qty').val('1');
            });
        }
    });

});


