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

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



$(document).ready(function(){

			
	

	
});



function addlist(param,col)
{
    
    col = col || 1;
    
    //alert (col);


	$.ajax({
	type: "POST",
	url: "http://shop.stn.com.ua/addtocart.php?r="+Math.random(),
	data: 'code='+encodeURIComponent(param) + '&col='+encodeURIComponent(col),
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader').css('visibility','visible');},
	success: function(msg){
	
		//alert (msg);		
			
		$('#ajax-loader').css('visibility','hidden');
		if(!msg.name)
		{
			return false;
		}
		else
		{
		
			if(msg.id<48 || msg.id>53)		
			$('#myorder').append('<tr id="c' + msg.code + '"><td>'+msg.name+'</td><td>'+msg.width+'</td><td>'+msg.length+'</td><td> '+ Number(msg.length*msg.width).toFixed(2)+'</td><td>'+msg.power+'</td><td>'+msg.price+'</td><td>'+msg.col+'</td><td width="16"><a href="#" onClick="DeleteItem('+msg.code+'); return false;"><img src="/i/cross.png" border="0"></a></td></tr>');
else
			$('#myorder').append('<tr id="c' + msg.code + '"><td>'+msg.name+'</td><td></td><td></td><td></td><td></td><td>'+msg.price+'</td><td>'+msg.col+'</td><td width="16"><a href="#" onClick="DeleteItem('+msg.code+'); return false;"><img src="/i/cross.png" border="0"></a></td></tr>');
			
	


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


	$.modaldialog.success('Товар добавлен в корзину.', {
  timeout: 2,
  title: 'Ваш заказ',
  width: 400,
  height: 150,
  showClose: false
  });



				
			/*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 DeleteItem(param)
{


	$.ajax({
	type: "POST",
	url: "http://shop.stn.com.ua/deleteitem.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(msg.status!=1)
		{
			return false;
		}
		else
		{
		
					
			$('#c'+param).remove();	
			
			
	


			_price=Number(msg.price).toFixed(2);
			
			
            
            
						
			cprice=(Number($('#acprice').text())-Number(_price)).toFixed(2);
		
			
			
			$('#acprice').text(cprice);
            $('#acprice2').text(cprice);
            
            $('#accol').text(Number($('#accol').text())-1);
            
         

	

		}
		
	
	
	}
	});
}




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://tnp.uwork.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 'шт.'; }
}
