var updateCart = function(url, onlyFloat) {
	$.getJSON(url, function (data) {
		$(".body .items", ".korzina-gl").html(data.content);
        if (!onlyFloat) {
            $(".korzina-inline").replaceWith(data.inline);
        }
	});
	return false;
};

$(function() {

	$(".head, .kor-close b", ".korzina-gl").bind("click", function() {
        with ($(".body", ".korzina-gl")) {
            if (hasClass("hidden")) {
                css("visibility", "hidden").removeClass("hidden");
                data("width", width());
                addClass("hidden").css("visibility", "visible");
                show().animate({width: data("width")}, "slow", function() {
                    $(this).removeClass("hidden").css("width", "auto");
                });
            } else {
                $(".body", ".korzina-gl").animate({width: "1px"}, "fast", function() {
                    $(this).addClass("hidden").css("width", "");
                });
            }
        }
	    return false;
	});

	updateCart("/?r=cart", true);
	
	$(".to-cart").live("click", function() {
		$.ajax({
		    type: "GET",
		    url: "/",
		    dataType: "json",
		    data: {
			    r : "cart/add",
			    id : this.id,
			    cnt : $(this.rel).val()
			},
		    beforeSend: function (XMLHttpRequest) {
		    	//this; // the options for this ajax request
                $(".korzina-process").fadeIn("fast");
			    $(".img-kart").removeClass("working success error").addClass("working");
		    },
		    success: function (data, textStatus) {
			    // data could be xmlDoc, jsonObj, html, text, etc...
			    //this; // the options for this ajax request
			    if (data.result) {
			    	$(".body .items", ".korzina-gl").html(data.content);
                    $(".korzina-inline").replaceWith(data.inline);
			    	$(".img-kart").removeClass("working success error").addClass("success");
			    } else {
			    	$(".img-kart").attr("title", textStatus).removeClass("working success error").addClass("error");
			    }
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				// typically only one of textStatus or errorThrown will have info
				//this; // the options for this ajax request
				$(".img-kart").attr("title", errorThrown || textStatus).removeClass("working success error").addClass("error");;
			},
		    complete: function (XMLHttpRequest, textStatus) {
		    	//this; // the options for this ajax request
                $(".korzina-process").fadeOut(2000);
                if ($(".body", ".korzina-gl").hasClass("hidden")) {
                    $(".head", ".korzina-gl").trigger("click");
                    setTimeout(function() {
                        $(".head", ".korzina-gl").trigger("click");
                    }, 2000);
                }
		    }
		});
	    return false;
	});
	
	$(".off-cart").live("click", function() {
		updateCart("/?r=cart/del&id=" + this.id);
	});
    $(".clr-cart").live("click", function() {
    	updateCart("/?r=cart/clear");
    });
    
    
	$("td.td-five a").bind("mouseover", function() {
		$("img", this).attr("src", "/img/kor-act.gif");
	});
    $("td.td-five a").bind("mouseout", function() {
        !$(this).data("opened") && $("img", this).attr("src", "/img/kor-pas.gif");
    });
    $("td.td-five a").bind("click", function() {
        $(this).data("opened", $(this).data("opened") ? false : true);
        $(this).parent().next().toggleClass("hidden");
        return false;
    });

    $(document).bind("mousemove", function(e) {
        $(".korzina-process").css("top", e.pageY+25).css("left", e.pageX+15);
    });
});