$(document).ready(function() {
var show=0;
var id_el=0;
var w = $(window);
$('#w_order').css("top",(w.height()-$('#w_order').height())/2+w.scrollTop()+"px");
$('#w_order').css("left",(w.width()-$('#w_order').width())/2+"px");	

$(window).scroll(function() {
var w = $(window);
if(show==1)
{
	$('#w_order').css("top",(w.height()-$('#w_order').height())/2+w.scrollTop()+"px");
	$('#w_order').css("left",(w.width()-$('#w_order').width())/2+"px");	
}
});
	
	$('.butt').click(function(){
		showord(this);
	});
	
	$('#head_win').click(function(){
		hideord();
	});
	
	$('#canc_win').click(function(){
		hideord();
	});
	
function showord(elem)
	{
	var w = $(window);
	$('#w_order').css("top",(w.height()-$('#w_order').height())/2+w.scrollTop()+"px");
	$('#w_order').css("left",(w.width()-$('#w_order').width())/2+"px");
	$('#w_order').show();
	id_el=$(elem).parent().find('.id_el').val();
	$('#pic_ajax').attr('src',$(elem).parent().find('.bigpic').val());
	$('#art_ajax').text($(elem).parent().find('.art').text());
	$('#weight_ajax').text($(elem).parent().find('.weig').text());
	show=1;
	}
$('.sizes_ajax select').change(function(){
		if($(this).val()!="0")	$(this).prev().prev().attr('checked','checked'); else $(this).prev().prev().attr('checked',''); 
	});	
	
	$('.chb').click(function(){
		if($(this).attr('checked'))
			{
				$(this).next().next().find("option[value='1']").attr("selected", "selected");
			}
			else
			{
				$(this).next().next().find("option[value='0']").attr("selected", "selected");
			}
	});
	
	$('#add_tov').click(function(){
		js=globpath+'addbacket.php?'+'art='+encodeURI($('#art_ajax').text());
		js+='&col='+encodeURI($('#col_ord').val());
		js+='&vst='+encodeURI($('#ajax_vst').val());
		
		$('.size_el select').each(function(index) {
			if($(this).val()!="0")
				{
					js+="&sizes["+encodeURI($(this).prev().text())+"]="+encodeURI($(this).val());
				}
		});
			js+='&comm='+encodeURI($('#ajax_comment').val());	
			js+='&id_el='+id_el;			
		
		
		//$('#ajax_json').html(js);
		//alert(js)
		
		$.getJSON(js,
        function(json){
		alert('Товар добавлен в корзину');
		});
		
	});

	
function hideord()
	{
	$('#w_order').hide();
	show=0;
	}	
	

$('#close_win').click(function(){
	$('#w_order').hide();
	show=0;
});	

$('#main_order_but').click(function(){	

	if($('#fio').val()!=""&&$('#addr').val()!="")
		{
			$('#sendord').submit();
		}
		else
		{
			alert("Заполните обязательные поля!");
		}
	});


$('.delete_ord').click(function(){	
op=$(this).parent();
id_el_ord=$(this).parent().find('.id_el_ord').val();
alert("!");
	js=globpath+'delbacket.php?id_el='+id_el_ord;
	$.getJSON(js,
        function(json){
			op.remove();
		});
	});	
	
	
$('.order_num_tab').keyup(function(e){	
	
	if($(this).val()=='') $(this).val('0');		
	js=globpath+'updbacket.php?el='+$(this).attr('id').substr(3)+'-'+$(this).val();
	opa=$(this).parent().parent().parent().parent().parent().find('.srves');
	$.getJSON(js,
        function(json){
		
		opa.text(json.elem);
		
			});
	});		



 $(".num_only").keypress(function (e)  
    { 
      //Если символ - не цифра, ввыодится сообщение об ошибке, другие символы не пишутся
      if($(this).val()==0) $(this).val('');
	  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
      {
        return false;
      }    
    });

$(".pic").hover(
  function () {
    $(this).find('.zoompic').show();
  }, 
  function () {
    $(this).find('.zoompic').hide();
  }
);	
	
});
