var errorfields = {};

var ww = {
	
	closeErrors: function(){
        $('.errors').hide();
    },
    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+' span.price').html(data.price);
	        }
	     }, "json");
		
	},
	tab : function(tabid)
	{
		
		$('.tab').removeClass('active');
		$('.t_'+tabid).addClass('active');
		
		$('.tabcontent').addClass('ns');
		$('.tc_'+tabid).removeClass('ns');
		
	},
        set_image : function(id, imgname, imgname2)
        {
            $('#' + id).attr('src', imgname);
            $('#' + id + '_link').attr('href', imgname2);
        }
}

$(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'
    });
    
});
