function addToCart(pid, options, transfer_el, quantity){
	$.post("?mod=store&action=cart", "do=ajaxAddToCart&product_id="+pid+"&options="+options+"&quantity="+quantity, function(data){
			$("#cart").html(data);
			initCartButtons();
			cl_hide();
		}
	);
	
	if($("#cart").css("display") == "none"){
		$("#cart").slideToggle(function(){
			bindDocumentClick();
	      	transfer(transfer_el);
		});				
	}
	else{
		transfer(transfer_el);
	}
}

function transfer(from_el){
	$('html, body').animate({scrollTop:0}, 'slow');
	from_el.effect("transfer", { to: $("#cart") }, 1000);
//	var html = '<table width="100%" height="100%" style="background:#fff;"><tr><td text-align="center" valign="center"><div>';
	var html = '';
	html += from_el.html();
	html += '</div></td></tr></table>';
	$(".ui-effects-transfer").html(html);
}

function initCartButtons(){	
	$(".cart_minus").click(function(){
		cl_show();
		$.post("?mod=store&action=cart", "do=cartminus&cid="+$(this).attr("cart_id"), function(data){
			$("#cart").html(data);
			initCartButtons();
			cl_hide();
		})
	}).css("cursor","pointer");
	
	$(".cart_plus").click(function(){
		cl_show();
		$.post("?mod=store&action=cart", "do=cartplus&cid="+$(this).attr("cart_id"), function(data){
			$("#cart").html(data);
			initCartButtons();
			cl_hide();
		})		
	}).css("cursor","pointer");
	
	$(".empty_cart").click(function(e){
		e.preventDefault();
		if(confirm("Izpraznim vsebino vozička?"))
		{
			$.post("?mod=store&action=cart", "do=ajaxAddToCart&empty=1", function(data){
//				$("#cart").html(data);
//				initCartButtons();
//				cl_hide();
				window.location.reload();
			});
		}
	});
}

function bindDocumentClick(){
	$(document).bind('click.ccc', function(e){
		$("#cart").fadeOut();
		unBindDocumentClick();
	});
	
	$('#cart, #ajax_addtocart, select.options_select, .kupi').click(function(e) {
	  e.stopPropagation();
	});


}

function cl_show(){
	//cart ajax loader
	$("#cl").fadeIn();
}

function cl_hide(){
	$("#cl").fadeOut();
}

function unBindDocumentClick(){
	$(document).unbind('.ccc');
}

$(function(){	
	$("#ajax_addtocart").click(function(e){
		cl_show();
		e.preventDefault();
		var options='';
		$("select.options_select").each(function(){
			options += $(this).val() + ",";
		});
		addToCart($("#product_id").val(),options);
	});
	
	

    $(".cart_toggle").click(function(e){
    	e.preventDefault();
		$("#cart").slideToggle(function(){
			if($("#cart").is(":visible")){
				bindDocumentClick();
			}
			else {
				unBindDocumentClick();
			}
		});		
		return false;
	});
	   
	initCartButtons();
});
