//
// Ostoskori
//

function showCart() {
	$.ajax({
		type:      "POST",
		url:       domain+"/docs/ajax/ajax.cart.php",   
        dataType:  "json",
		success:    function (data) {
			$("#jQCart").html(data.cart);
			$("#jQCart").dialog({
				title: "Ostoskori",
				modal: true,
				resizable: false,
				draggable: false,
				width: 700,
				buttons: {
					"Sulje ostoskori": function() {
						$(this).dialog('destroy');
					},
					"Kassalle": function() {
						window.location = domain + "/webshop/checkout/"
					},
					"Tyhjennä ostoskori": function() {
						emptyCart();
					}
				},
				close: function() {
					$(this).dialog('destroy');
				}
			});
			$("a.lisaa").button({
				icons: { primary:'ui-icon-plus' },
				text:false
			});
			$("a.vahemman").button({
				icons: { primary:'ui-icon-minus' },
				text:false
			});
		},
		error: 	   function ( _response )
		{
			alert('Virhe!');
		}
	});
}

function addToCart() {
	var options = { 
		dataType:  'json',
		success:    function (data) {
			switch (data.type) {
				case 'error' :
					msgDialog(data.message, 'Virhe!');
				break;
				case 'allok' :
					$("#webshop_header_cart").html(data.updateData);
					if(data.message) {
						msgDialog(data.message, 'HUOM!');
					}	
				break;
			}
		},
		error: 		function () {
			msgDialog('Virhe lisäyksessä!', 'Virhe!');
		}
	}; 
	// pass options to ajaxForm 
	$('#form_tuote').ajaxForm(options);
	$('#form_tuote').submit();
}

function moreToCart(id) {
	$.ajax({
		type:      "POST",
		url:       domain+"/docs/ajax/ajax.moreToCart.php",   
        dataType:  "json",
		data: 	   "tuote="+ id,
		success:    function (data) {
			switch (data.type) {
				case 'error' :
					msgDialog(data.message, 'Virhe!');
				break;
				case 'allok' :
					$("#jQCart").html(data.updateData);
					$("#webshop_header_cart").html(data.cartData);
				break;
			}
			$("a.lisaa").button({
				icons: { primary:'ui-icon-plus' },
				text:false
			});
			$("a.vahemman").button({
				icons: { primary:'ui-icon-minus' },
				text:false
			});
		},
		error: 	   function ( _response )
		{
			msgDialog('Virhe lisäyksessä!', 'Virhe!');
		}
	});
}

function removeFromCart(id) {
	$.ajax({
		type:      "POST",
		url:       domain+"/docs/ajax/ajax.removeFromCart.php",   
        dataType:  "json",
		data: 	   "tuote="+ id,
		success:    function (data) {
			switch (data.type) {
				case 'error' :
					msgDialog(data.message, 'Virhe!');
				break;
				case 'allok' :
					$("#jQCart").html(data.updateData);
					$("#webshop_header_cart").html(data.cartData);
				break;
			}
			$("a.lisaa").button({
				icons: { primary:'ui-icon-plus' },
				text:false
			});
			$("a.vahemman").button({
				icons: { primary:'ui-icon-minus' },
				text:false
			});
		},
		error: 	   function ( _response )
		{
			msgDialog('Virhe lisäyksessä!', 'Virhe!');
		}
	});
}

function emptyCart() {
	$.ajax({
		type:      "POST",
		url:       domain+"/docs/ajax/ajax.emptyCart.php",   
        dataType:  "json",
		success:    function (data) {
			switch (data.type) {
				case 'error' :
					msgDialog(data.message, 'Virhe!');
				break;
				case 'allok' :
					$("#jQCart").html(data.cart);
					$("#webshop_header_cart").html(data.cartData);
				break;
			}
			$("a.lisaa").button({
				icons: { primary:'ui-icon-plus' },
				text:false
			});
			$("a.vahemman").button({
				icons: { primary:'ui-icon-minus' },
				text:false
			});
		},
		error: 	   function ( _response )
		{
			alert('Virhe!');
		}
	});
}

function msgDialog(msg, title) {
	$("#dialog").html(msg);
	$("#dialog").dialog({
		modal: true,
		title: title,
		resizable: false,
		buttons: {
			Ok: function() {
				$(this).dialog('destroy');
			}
		},
		close: function() {
			$(this).dialog('destroy');
		}
	});
}