var errorfields = {};

var ww = {
    order : function()
    {
    	
    	var user_message = $('#user_message').val();
    	
    	$.post("/megrendeles",'&user_message='+user_message, function(data)
	     {
	        if (data.error)
	        {
	        		        	
	        	if(data.errortype == 'nouser')
	        	{
	        		window.location.href = '/bejelentkezes';
	        	}
	        	
	        	
	        }
	        else
	        {
	        	window.location.href = data.url;
	        }
	     }, "json");
    	
    },
	addBasket : function(obj,product_id)
	{
		
		$.post("/add-basket", $(obj).serialize(), function(data)
	     {
	        if (data.error)
	        {
	        	
	        }
	        else
	        {
	        	
	        	window.location.href = data.url;
	        }
	     }, "json");
		
	},
	deleteBasketItem : function(product_id)
	{
		
		$.post("/delete-basket-item", 'product_id='+product_id, function(data)
	     {
	        if (data.error)
	        {
	        	
	        }
	        else
	        {
	        	
	        	window.location.href = data.url;
	        }
	     }, "json");
		
	},
	deleteBasket : function()
	{
		$.post("/delete-basket", '', function(data)
	     {
	        if (data.error)
	        {
	        	
	        }
	        else
	        {
	        	window.location.href = data.url;
	        }
	     }, "json");
	},
	modifyBasket : function(obj)
	{
		$.post("/modify-basket", $(obj).serialize(), function(data)
	     {
	        if (data.error)
	        {
	        	
	        }
	        else
	        {
	        	
	        	window.location.href = data.url;
	        }
	     }, "json");
	},
	calculatePrice : function(product_id)
	{

		 var modifiers = new Array();
		 var checkselected = false;
		 jQuery.each($('.modifierselect'),function(i,val) 
		 {
		 	 modifiers[i] = $(val).val();
		 	 if($(val).val() != 'x')
		 	 {
		 		 checkselected = true;
		 	 }
		 	
		 });
		
		 var qty = $('.qty.p_'+product_id).val();
		
		 $.post("/calculateprice", 'modifiers='+modifiers+'&product_id='+product_id+'&qty='+qty, function(data)
	     {
	        if (data.error)
	        {
	           $('.p_'+product_id).val(1);
	           ww.calculatePrice(product_id);
	        }
	        else
	        {
	        	if(qty == '1' && !checkselected)
	        	{
	        		$('.p_'+product_id+' span.unit').show();
	        	}
	        	else
	        	{
	        		$('.p_'+product_id+' span.unit').hide();
	        	}
	            $('.p_'+product_id+' .price_big').html(data.price + ' Ft');
	        }
	     }, "json");
		
	},
	
    closeErrors: function(){
        $('.errors').hide();
    },
    
    ptab : function(tc) {
    	
    	$('#bgTab').removeClass();
    	$('#bgTab').addClass('hoverTab'+tc);
    	
    	$('.pimage').hide();
    	$('#img'+tc).show();
    	
    }
	
}

$(document).ready(function(){

    jQuery.each(errorfields, function(i, val){
        $('#' + errorfields[i].field).addClass('error');
        $('#' + errorfields[i].field + '-error').html('<a class="south" href="#" title="' + errorfields[i].message + '"></a>').show();
    });
    
    $('.south').tipsy({
        gravity: 's'
    });
	
});
