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

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



$(document).ready(function(){

			menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
			
			lmenuh=$('.cart').height()+300;
				
			$('.lblock').height(lmenuh);
			
			
			$(window).scroll(function () { 
				offset = menuYloc+$(document).scrollTop();
				
				//$(name).height()
				
				//alert($(name).height());
				//alert($(window).height());
				
				if(offset<1200)
				{
					$(name).animate({top:'820px'},{duration:1500,queue:false});
					return;
				}
				
				if($(name).height()+400>$('.mblock').height())
				{
					
					return;
				}
				
				offset=offset-120-($(name).height());
				offset=offset+"px";
				
				
				$(name).animate({top:offset},{duration:1500,queue:false});
			});
 
	
	/*$('.prod .pict').simpletip({
		
		offset:[40,0],
		content:'<img src="img/ajax_load.gif" alt="loading" style="margin:10px;" />',
		onShow: function(){
			
			var param = this.getParent().find('img').attr('src');
			
			if($.browser.msie && $.browser.version=='6.0')
			{
				param = this.getParent().find('img').attr('style').match(/src=\"([^\"]+)\"/);
				param = param[1];
			}
			
			//this.load('ajax/tips.php',{img:param}); 
		} 

	});*/
	
	$(".product img").draggable({
	
	containment: 'document',
	opacity: 0.6,
	revert: 'invalid',
	helper: 'clone',
	zIndex: 100
	
	});
	
	$(".pd02 img").draggable({
	
	containment: 'document',
	opacity: 0.6,
	revert: 'valid',
	helper: 'clone',
	zIndex: 110
	
	});

	$("div.drop-here").droppable({
	
			//over: function(event, ui) {alert("hhh"); },

	
			drop:
					function(e, ui)
					{
					
						var param = $(ui.draggable).attr('src');
						
						if($.browser.msie && $.browser.version=='6.0')
						{
							param = $(ui.draggable).attr('style').match(/src=\"([^\"]+)\"/);
							param = param[1];
						}

						addlist(param);
					},
					
			out:
					function(e, ui)
					{
					
						var param = $(ui.draggable).attr('src');
						
						if($.browser.msie && $.browser.version=='6.0')
						{
							param = $(ui.draggable).attr('style').match(/src=\"([^\"]+)\"/);
							param = param[1];
						}

						$(ui.draggable).remove();
						removelist(param);

					}
	
	
	});
	
});


function addlist(param)
{
	$.ajax({
	type: "POST",
	url: "http://d-a.dp.ua/addtocart.php",
	data: 'img='+encodeURIComponent(param),
	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
		{
		
			if($('#cinfo'))
			{
				$('#cinfo').remove()
			};
			
		
			$('#cart-list').append('<img class="png" src="http://d-a.dp.ua/i/upload/ss/'+msg.txt+'"> ');
				
			lmenuh=$('.cart').height()+300;
				
			$('.lblock').height(lmenuh);
				
			/*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();*/

		}
		
		offset = menuYloc+$(document).scrollTop();
				
		//alert($(name).height());
		//alert($(window).height());
				
					if(offset<1200)
				{
					$(name).animate({top:'685px'},{duration:1500,queue:false});
					return;
				}
				
				if($(name).height()+400>$('.mblock').height())
				{
					
					return;
				}
				
				offset=offset-350-($(name).height());
				offset=offset+"px";
				
				
				$(name).animate({top:offset},{duration:1500,queue:false});
		
		//$('.tooltip').hide();
	
	}
	});
}

function removelist(param)
{


	$.ajax({
	type: "POST",
	url: "http://d-a.dp.ua/deletetocart.php",
	data: 'img='+encodeURIComponent(param),
	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
		{
			
		
			

		}
		
		offset = menuYloc+$(document).scrollTop();
				
		//alert($(name).height());
		//alert($(window).height());
				
					if(offset<1200)
				{
					$(name).animate({top:'625px'},{duration:1500,queue:false});
					return;
				}
				
				if($(name).height()+400>$('.mblock').height())
				{
					
					return;
				}
				
				offset=offset-350-($(name).height());
				offset=offset+"px";
				
				
				$(name).animate({top:offset},{duration:1500,queue:false});
		
		//$('.tooltip').hide();
	
	}
	});
}


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://d-a.dp.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 'порций'; }
}
