$(document).ready(function(){

	/** Global ******************************************************/

    // Navigation
	$('ul.sf-menu').superfish({
    	animation:   {opacity:'show',height:'show'}
    });

    // External links in new window
    $('a[rel=external]').click(function(){
        url = $(this).attr('href');
        window.open(url);
        return false;
    });

    // Empty some inputs on focus
    $('#search input').emptyOnFocus();
    $('#footer div.newsletter input').emptyOnFocus();

    // Template
    $("#manufacturer-select").change(function() {
        window.location.href = "/brands/" + this.value;
    });

    // Mini basket status update
    var basketMsg = $("#basket-update");
    if (basketMsg.length == 1) {
        setTimeout(function() {
            basketMsg.fadeOut(2000);
        }, 5000);
    }
    
    $("#basket-update #close, #basket-update #continue").click(function() {
        $("#basket-update").fadeOut();
        return false;
    });

	/** Homepage ****************************************************/

    if( $('body').hasClass('home') ){

        // Products carousel
    	$('.home #product-carousel .items').jCarouselLite({
            btnNext: ".next",
            btnPrev: ".prev",
            visible: 4,
            scroll: 1,
            circular: false
        });

        // Large banner
        $('#large').cycle({
            timeout: 6000
        });

        // Featured products
        $('#featured .cycle').cycle();

    }

    /** Product View ************************************************/
    $(".decrease-qty").click(function() {
        if ($(".qty-input").val() > 1) {
            $(".qty-input").val(parseInt($(".qty-input").val()) - 1);
        }
    })
    $(".increase-qty").click(function() {
        $(".qty-input").val(parseInt($(".qty-input").val()) + 1);
    })

    $(".custom-options select").change(function() {

        $("#stock-price-buy").load(
            "/ajax/catalog/get_price_and_stock/" + productID,
            $("#product-form").serialize()
        );
        
        if (typeof valuesStockIDs === "undefined") {
            return;
        }

        var values = "";        
        $(".custom-options select").each(function() {
            values += $(this).val() + "-";
        });
        values = values.substring(0, values.length - 1);

        var stockID = valuesStockIDs[values];
        
        $("#image-box").load('/ajax/catalog/get_var_images_box/' + stockID, initZoom);

    });


    $(".custom-options img").ajaxStart(function() {
        $(this).css("visibility", "visible");
    })
    $(".custom-options img").ajaxSuccess(function() {
        $(this).css("visibility", "hidden");
    })
    
    $("#add-to-basket-submit").click(function() {
        $(this).parents("form").attr("action", "/basket/add");
    });

    /** Basket ******************************************************/
    $("#shipping-location-select").change(function() {
        $("#basket-form").submit();
    });
    $("#del-services input").click(function() {
        $("#basket-form").submit();
    });
    
    
    $("#decrease-qty").click(function() {
        if ($(".qty-input").val() > 1) {
            $(".qty-input").val(parseInt($(".qty-input").val()) - 1);
        }
    })
    $("#increase-qty").click(function() {
        $(".qty-input").val(parseInt($(".qty-input").val()) + 1);
    })
    
    $(".basket-qty a").click(function() {
        $("#basket-form").submit();
        return false;
    });

    /** Checkout ****************************************************/
    $("#new-billing a").click(function() {
        $("#new-billing a").hide();
        $("#choose-billing").hide(0, function() {
            $("#new-billing-form").show(0, function() {
                $("#cancel-new-billing").show();
            });
        });
        $("#choose-billing .select-address input").attr("checked", false);
        $("#new-billing-address").val(1);
        $("#billingFlashMessage").hide();
        return false;
    });
    $("#cancel-new-billing a").click(function() {
        $("#cancel-new-billing").hide();
        $("#new-billing").show();
        $("#new-billing-form").hide(0, function() {
            $("#choose-billing").show(0, function() {
                $("#new-billing a").show();
            });
        });
        $("#new-billing-address").val(0);
        return false;
    });
    
    if ($("#deliver-to-billing").is(':checked') === true) {
        $("#delivery-adddress-box").hide();
    }           
    $("#deliver-to-billing").change(function() {
        if (!$(this).is(":checked")) {
            $("#delivery-adddress-box").slideDown();
        }
        else {
            $("#delivery-adddress-box").slideUp();
        }
    });
    
    $("#new-shipping a").click(function() {
        $("#new-shipping a").hide();
        $("#choose-shipping").hide(0, function() {
            $("#new-shipping-form").show(0, function() {
                $("#cancel-new-shipping").show();
            });
        });
        $("#new-shipping-address").val(1);
        return false;
    });
    $("#cancel-new-shipping a").click(function() {
        $("#cancel-new-shipping").hide();
        $("#new-shipping").show();
        $("#new-shipping-form").hide(0, function() {
            $("#choose-shipping").show(0, function() {
                $("#new-shipping a").show();
            });
        });
        $("#new-shipping-address").val(0);
        return false;
    });
    
});

/** Some Functions **************************************************/
$.fn.emptyOnFocus = function() {
    return this.focus(function() {
        if( this.value == this.defaultValue ) {
            this.value = "";
            $(this).addClass('active');
        }
    }).blur(function() {
        if( !this.value.length ) {
            this.value = this.defaultValue;
            $(this).removeClass('active');
        }
    });
};

function initZoom() {
    $('#product-images > div.image-view > a.jqzoom').jqzoom({
        zoomType: 'innerzoom',
        preloadImages: false,
        alwaysOn: false,
        zoomWidth: 342,
        zoomHeight: 342,
        title: false
    });
}
