var purchased=new Array();
var totalprice=0;

var name = ".cart";
var menuYloc = null;
var dY=0;
	



$(document).ready(function(){

			
	

	
});


function showitem(param)
{


	$.ajax({
	type: "POST",
	url: "http://atlant-holod.com.ua/showitem.php?r="+Math.random(),
	data: 'code='+encodeURIComponent(param),
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader').css('display','block');},
	success: function(msg){
	
			
		$('#ajax-loader').css('display','none');
		if(parseInt(msg.status)!=1)
		{
			return false;
		}
		else
		{
		
		
			var name=msg.producer + ' ' + msg.name;
			
						
			var $dialog = $('<div></div>')
			.html('<div class="full"><div class="photo"><img src="'+ msg.photo +'"></div><div class="descr"><div class="ftitle">' + name+ '</div><p><a href="#" onclick="addlist(' + msg.code + '); return false;"><img src="/i/bbuy.gif" border="0"></a></p><div class="complete">' + msg.complete + '</div><div class="price">'+ msg.price +' <span>грн.</span></div><p><a href="#" onclick="addlist(' + msg.code + '); return false;"><img src="/i/bbuy.gif" border="0"></a></p><p><a href="https://siteheart.com/webconsultation/30586?byhref=1&s=1" target="siteheart_sitewindow_30586" onclick="o=window.open;o(\'https://siteheart.com/webconsultation/30586?s=1\', \'siteheart_sitewindow_30586\', \'width=550,height=400,top=30,left=30,resizable=yes\'); return false;">Задать вопрос онлайн</a></p></div><div class="clear"></div></div>')
			.dialog({
				autoOpen: true,
				title: name,
				width: 750,
				height: 570
			});

		}
		
	
	
	}
	});
}


function addlist(param)
{


	$.ajax({
	type: "POST",
	url: "http://atlant-holod.com.ua/addtocart.php?r="+Math.random(),
	data: 'code='+encodeURIComponent(param),
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader').css('visibility','visible');},
	success: function(msg){
	
		//alert (msg);		
			
		$('#ajax-loader').css('visibility','hidden');
		if(parseInt(msg.status)!=1)
		{
			return false;
		}
		else
		{
		
			if($('#cinfo'))
			{
				$('#cinfo').remove()
			};
			
		
			$('#cart-list').append('<div class="prod" ><div class="photo"><img src="http://atlant-holod.com.ua/i/upload/ss/'+msg.code+'.jpg"></div><div class="descr"><b>' + msg.name + '</b><br><b>' + (msg.price*8.0).toFixed(2) + '</b> грн.</div><div class="clear"></div></div>	');	
			


			_price=Number((msg.col)*(msg.price*8.0)).toFixed(2);
			
			ccol=Number($('#ccol').text())+Number(msg.col);
			
			$('#ccol').text(ccol);
			
			cprice=(Number($('#cprice').text())+Number(_price)).toFixed(2);
			
			$('#cprice').text(cprice);

				
			/*if(!check)
			{
				purchased.push({id:msg.id,cnt:1,price:msg.price});
			}
			else
			{
				if(cnt>=3) return false;
				
				purchased[i].cnt++;
				$('#'+msg.id+'_cnt').val(purchased[i].cnt);
			}
			
			totalprice+=msg.price;
			update_total();*/

		}
		
	
	
	}
	});
}
function findpos(id)
{
	for(var i=0; i<purchased.length;i++)
	{
		if(purchased[i].id==id)
			return i;
	}
	
	return false;
}

function remove(id)
{
	var i=findpos(id);

	totalprice-=purchased[i].price*purchased[i].cnt;
	purchased[i].cnt = 0;

	$('#table_'+id).remove();
	update_total();
}

function change(id)
{
	var i=findpos(id);
	
	totalprice+=(parseInt($('#'+id+'_cnt').val())-purchased[i].cnt)*purchased[i].price;
	
	purchased[i].cnt=parseInt($('#'+id+'_cnt').val());
	update_total();
}

function update_total()
{
	if(totalprice)
	{
		$('#total').html('total: $'+totalprice);
		$('a.button').css('display','block');
	}
	else
	{
		$('#total').html('');
		$('a.button').hide();
	}
}

function changeCount(col, name)
{

			_col=$('#c'+name).val();
			_price=Number($('#pr'+name).text());
			_sum=Number($('#s'+name).text());
			_totprice=Number($('#totprice').text());
	
	
			_ncol=Number(_col)+Number(col);
	

$.ajax({
	type: "POST",
	url: "http://atlant-holod.com.ua/updatetocart.php",
	data: 'id='+encodeURIComponent(name)+'&col='+encodeURIComponent(_ncol),
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader').css('visibility','visible');},
	success: function(msg){
	
			
		$('#ajax-loader').css('visibility','hidden');
		
		if(parseInt(msg.status)!=1)
		{
			return false;
		}
		else
		{
			
			
			//alert(_ncol);
	
			if(_ncol<1 || _ncol>99)
			return;
		
			_nsum=Number(_sum+_price*col).toFixed(2);
			_ntotprice=Number(_totprice+_price*col).toFixed(2);
	
			$('#c'+name).val(_ncol);
	
			$('#p'+name).text(pluralize(_ncol));
	
			$('#s'+name).text(_nsum);
			$('#totprice').text(_ntotprice);
			

		}
		
	
	
	}
	});

		
			
	




	
	
	
}

function pluralize(number)
{
  if(number > 20) { strval = number.toString(); number = strval[strval.length - 1]; }
  
  
  if (number == 0) { return 'шт.'; }
  if (number == 1) { return 'шт.'; }
  if (number > 1 && number <= 4) { return 'шт.'; }
  if (number > 4 && number < 21) { return 'шт.'; }
}
