$(document).ready(function()
{
	var subNavTimer; 
	var submit_img = $('.order-payment .submit').find('img');
	var submit_input = $('.order-payment .submit').find('input');
	
	$('.menu-item').bind('mouseenter', function()
	{
		clearTimeout(subNavTimer);
		$('.menu-item').not(this).find('.menu-panel').hide(300);
		$(this).find('.menu-panel').show(300);
		
		
	});
	
	$('.menu-item').bind('mouseleave', function()
	{
		subNavTimer = setTimeout('$(".menu-panel:visible").hide(200);', 1000);
	});
	
	// Показывает и скрывает прогресс для ajxa
	$("#loader").ajaxStart(function(){
   		$(this).show();
	});				
	$("#loader").ajaxStop(function(a){
		$(this).hide();
	});	
	
	$("#ajax-form").ajaxForm( {success: successFunc, dataType: 'json'} );
	
	function successFunc(data)
	{
		if(data.status == 1)
		{
			//alert(data.msg);
		}
		
		if(data.msg)
			alert('Сервер говорит: «' + data.msg + '»');
		
		if(data.js)
			eval(data.js);
	}	
	
	function switch_submit (flag)
	{
		if (flag)
		{
			submit_input.css('display','block');
			submit_img.css('display','none');
		}
		else
		{
			submit_img.css('display','block');
			submit_input.css('display','none');
		}
	}
	
	function check_item (item,src)
	{
		$(item).keydown(function(){
			if ($(this).attr('value') != '') {
				$(this).parent().prev().removeClass('error');
				var errors = $(src).find('.error');
				if ($(errors).length == 0) 
				{
					switch_submit(true);
				}					
			}
			else 
			{
				switch_submit(false);
				$(this).parent().prev().addClass('error');
			}
		});
		$(item).change(function(){
			if ($(this).attr('value') != '') {
				$(this).parent().prev().removeClass('error');
				var errors = $(src).find('.error');
				if ($(errors).length == 0) 
				{
					switch_submit(true);
				}					
			}
			else 
			{
				switch_submit(false);
				$(this).parent().prev().addClass('error');
			}
		});
	}
	
	$('.order-payment .jur-info').find('input').each(function(){
	 	check_item($(this),'.order-payment .jur-info');	
	});
	$('.order-payment .fiz-info').find('input').each(function(){
	 	check_item($(this),'.order-payment .fiz-info');	
	});
	$('.order-payment .jur-info').find('textarea').each(function(){
	 	check_item($(this),'.order-payment .jur-info');	
	});
	 
	
	if ($('.order-payment .jur-info').css('display') == 'block') {
		var errors = $('.order-payment .jur-info').find('.error');
		if ($(errors).length == 0) {
			switch_submit(true);
		}
		else {
			switch_submit(false);
		}
	}
	
	$('.order-payment .select-info').find('label').click(function(){
		
		
		switch ($('input',this).attr('value'))
		{
			case 'cash': switch_submit(true);
						 $('.order-payment .jur-info').slideUp(100);
						 $('.order-payment .fiz-info').slideUp(100);
						 $('.order-payment .select-info').find('.notice:visible').hide();
						 break;
						 
			case 'fiz' : var errors = $('.order-payment .fiz-info').find('.error');
						 if ($(errors).length == 0) {
						 	switch_submit(true);
						 }
						 else {
								switch_submit(false);
						 } 
						 $('.order-payment .jur-info').slideUp(100); 
						 $('.order-payment .fiz-info').slideDown(100);
						 //$('.order-payment .select-info').find('.notice:visible').hide();
						 //$('.order-payment .select-info').find('.notice').eq(0).show(100);
						 $('.order-payment .select-info').find('.notice').eq(0).show(100);
						 break;
						 
			case 'jur' : var errors = $('.order-payment .jur-info').find('.error');
						 if ($(errors).length == 0) {
						 	switch_submit(true);
						 }
						 else {
								switch_submit(false);
						 } 
						 $('.order-payment .jur-info').slideDown(100);
						 $('.order-payment .fiz-info').slideUp(100);
						 //$('.order-payment .select-info').find('.notice:visible').hide();
						 //$('.order-payment .select-info').find('.notice').eq(1).show(100);
						 $('.order-payment .select-info').find('.notice').eq(0).show(100);
						 break;
		} 
	});
});

$(document).ready(function(){
	$('.fancybox').fancybox({
		'zoomSpeedIn': 200,
		'zoomSpeedOut': 200,
		'frameWidth': 100,
		'frameHeight': 100,
		'overlayShow': true
	});
	$('.thickbox').fancybox({
		'zoomSpeedIn': 200,
		'zoomSpeedOut': 200,
		'frameWidth': 100,
		'frameHeight': 100,
		'overlayShow': true
	});
});

function lookup(inputString) {
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#suggestions').hide(100);
	} else {
		$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
			if(data.length >0) {
				$('#suggestions').show(100);
				$('#autoSuggestionsList').html(data);
			}
		});
	}
} // lookup

function fill(thisValue) {
	$('#inputString').val(thisValue);
	setTimeout("$('#suggestions').hide(100);", 200);
}

function addInBasket(id_item)
{
	$.post('/shop/add_in_basket', {id_item: id_item }, function(data) {
		//alert($("div.menu-panel").html());
		//$("div.menu-panel").replaceWith(data);
	});//, "json");	
}

function deleteItem(id_item)
{
	$.post('/shop/delete_item', {id_item: id_item }, function(data) {
		//alert($("div.menu-panel").html());
		//$("div.menu-panel").replaceWith(data);
		if(data) {
			//$("div.menu-panel").replaceWith(data);
			$("#i_" + id_item).remove();
		}
		//alert(data);
	});//, "json");	
}