jQuery(function($) {
    $(".buy_button").click(function(e){
        e.preventDefault();
        var form = $(this).parents("form:first");
        addOrderItem(form);
    });
    $(".buy_form").submit(function(e){
        e.preventDefault();
        var form = $(this);
        addOrderItem(form);
    });

});

function addOrderItem(form){
    var fields = form.serialize();
    var path   = form.attr("action").split("?");
    var url    = path[0]+".json";
    var lang   = path[1] ? "?"+path[1] : "";
    path   = url+lang;
    $.ajax({
        url:      path,
        type:     'post',
        data:     fields,
        dataType: 'json',
        success:  function(response) {
            $("#cart_total_price").html(InSales.formatMoney(response.total_price, response.currency_format));
            $("#cart_items_count").html(response.items_count);
        },
        error:  function(response) {  }
    });
}

jQuery(function(){
    $('body').bind("ajaxSend", function(){
        own_preloader(true);
    }).bind("ajaxComplete", function(){
        own_preloader(false);
    });
});



function changeCss(OBJ){
    var imageHeight = OBJ.height();
    var imageWidth = OBJ.width();
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    OBJ.css({
        "position" : "absolute",
        "left" : windowWidth / 2 - imageWidth / 2,
        "top" : getPageScroll()[1] + (getPageHeight() / 2)
    });
};


// getPageScroll() by quirksmode.com
function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    return new Array(xScroll,yScroll)
}

// Adapted from getPageSize() by quirksmode.com
function getPageHeight() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowHeight = document.body.clientHeight;
    }
    return windowHeight
}

function own_preloader(event){
    if (event==true) {
        if (!($("#own_preloader").attr("id"))){
            preloader='<div id="own_preloader" style="z-index:1000; width:32px; height:32px;"><img src="/images/loading.gif"/></div>';
            $("body").append(preloader);
        }
        preloader = $("#own_preloader");
        preloader.show();

        changeCss(preloader);
        $(window).bind("resize", function(){
            changeCss(preloader);
        });
        $(window).bind("scroll", function(){
            changeCss(preloader);
        });
    }
    else{
        $(window).unbind("resize");
        $(window).unbind("scroll");
        if (typeof(preloader) != 'undefined') preloader.hide();
    }
}

