var Change = {
	
	change: function (id) {
		$('#text-' + id).html('<textarea id="content-' + id + '" cols="60" rows="4" style="	background:#efefe4; border:1px solid #d1c8b0; font-family:Arial, Helvetica, sans-serif; font-size:12px;">' + $('#text-' + id).html() + '</textarea>')		
		$('#label-' + id).html('Salvesta');
		
		$('#label-' + id).attr('href', 'javascript:Change.save(' + id + ')');
	},

	save: function (id) {
		$.post('?show=change', {
			id: id,
			content: $('#content-' + id).val()
		}, function () {
			$('#text-' + id).html($('#content-' + id).val());
			$('#label-' + id).attr('href', 'javascript:Change.change(' + id + ')').html('Muuda');
		});
	}
};


 var Cart = {
	add: function (id, type) {
		$.ajax({
		   type: "POST",
		   url: "/ostukorv?action=add&id=" + id + '&amount=1&type=' + type + '&sum=' + $('#productSum-' + id).text() + '&code=' + $('#productCode-' + id).text(),
		   data: "productId=" + id,
		   success: function(msg){
		   	$.getJSON("/json_data/index.php?action=getCartData", function(json){
			  $('.korv P').html('<a href="/ostukorv?action=show">Ostukorvis on ' + json[0] + ' toodet. Kokku: ' + json[1] + ' EUR</a><br><a href="/ostukorv?action=show">VAATA OSTUKORVI</a>');
			  $('#productSum').html(json[0]);
			  $('#productPrice').html(json[1]);
			});
		   	$('#popup-lisatud').show(); 
		   	
		   	setTimeout(function() {
		   			$('#popup-lisatud').fadeOut('slow'); 
		   	}, 1000);
		   }
		 });
	},
	
	del: function(id) {
		$.ajax({
		   type: "POST",
		   url: "/ostukorv?action=delete&id=" + id,
		   data: "productId=" + id,
		   success: function(msg){
		   	$('#product-row-id-' + id).hide();
		   	Cart.getDetails();
		   }
		 });
	},
	
	reload: function(id) {
		$.ajax({
		   type: "POST",
		   url: "/ostukorv?action=add&id=" + id + '&amount=' + $('#amount-' + id).val() + '&sum=' + $('#productSum-' + id).val() + '&reload=1',
		   data: "productId=" + id,
		   success: function(msg){
		   	$.getJSON("/json_data/index.php?action=getRowPrice&id=" + id, function(json){
			  $('#productPrice-' + id).html(json[0]);
			  Cart.getDetails();
			});
		   	
		   }
		 });
	},
	
	getDetails: function () {
	
		$.getJSON("/json_data/index.php?action=getProductsDetails", function(json){
			
		  $('#price').html(json[0]);
		  $('#tax').html(json[1]);
		  $('#sum').html(json[2]);
		  $('#sum-eur').html(json[3]);
		});
	}
};
jQuery(document).ready(function() {
	$('#searchBox').click(function() {
		$(this).val('');
	});
	jQuery('#productMessage').show();
   jQuery('#productMessage').hide();
   $.getJSON("/json_data/index.php?action=getCartData", function(json){
			  $('.korv P').html('<a href="/ostukorv?action=show">Ostukorvis on ' + json[0] + ' toodet. Kokku: ' + json[1] + ' EUR</a>');
			  $('#productSum').html(json[0]);
			  $('#productPrice').html(json[1]);
			});
	$("#searchBox").autocomplete("/otsing&autocomplete=1", { minChars:2,width: '200px',delay: 400 });
 });

