$(document).ready(function() {
	$().mousemove(function(e){
	      mousex = e.pageX;
		  mousey = e.pageY;
	   }); 


	$("form[validate]").submit(function(){
			var submit_form = true;
			var form_id = $(this).attr('id');
			if(!form_id) form_id = $(this).attr('id','form' + document.uniqueID).attr('id');
			$(this).find("div[needdiv]").remove();
			$(this).find(':input[need]').each(function(){
				if($(this).val() == '')
				{
					var need_message = show_need_message(this,$("div[@fmessage=fill]",$("#" + form_id)).html());
					$(this).bind("keydown",function(){need_message.remove()})
					submit_form = false;
					if($(this).is(":visible")) $(this).focus();
					return false;
				}
				else if($(this).is('[mail]') && !check_mail($(this).val()))
				{
					var need_message = show_need_message(this,$("div[@fmessage=mail]",$("#" + form_id)).html());
					$(this).bind("keydown",function(){need_message.remove()})
					submit_form = false;
					this.focus();
					return false;
				}
			});
			if(submit_form)
			{
				if(!$(this).is("[@direct=1]"))
				{
					var sending = show_message($("div[@fmessage=send]",$("#" + form_id)).html(),'center').show();
					if(this.method == 'get') $("#" + form_id + "_result").load($(this).attr('action'),function(){sending.remove();reload_captcha(form_id)});
					else $("#" + form_id + "_result").load($(this).attr('action'),$(this).serializeArray(),function(){sending.remove();reload_captcha(form_id)});
				}
				else return true;
			}
			return false;
	})
	
	$(".ci").mouseover(function(){
		var ctext = $(this).attr('text');
		var cart = $(this).attr('articul');
		if(!cart || cart == '0') cart = '';
		var cprice = $(this).attr('price');
		if(!cprice || cprice == '0') cprice = '';
		if(cprice) cprice = cprice;
//		 + ' руб.';
		if(!ctext)
		{ 
			$("#modeltext").text(' ');
			$("#modelarticul").text(' ');
			$("#modelprice").text(' ');
		}
		else
		{ 
			$("#modeltext").text(ctext);
			$("#modelarticul").text(cart);
			$("#modelprice").text(cprice);
		}
	});
	
});

function reload_captcha(elID)
{
	
	jq = !elID ? $("img[@src*=code.jpg]") : $("img[@src*=code.jpg]",$("#"+elID));
	jq.each(function(){
		var new_src = $(this).attr('src').replace(/\?.*$/,'') + '?' + Math.random();
		$(this).attr('src',new_src);
	})
}


function show_need_message(obj,message)
{
	return $('<div class=red needdiv=1>' + message + '</div>').insertAfter($(obj)).hide().fadeIn(600);
}

function show_message(msgText,msgPos)
{
	if(!msgPos) msgPos = '';
	var ajax_message = $("<div id=ajaxmessage>" + msgText + "</div>").appendTo('body');
	ajax_message.css('display','none');
	ajax_message.css('position','absolute');
	if(msgPos == 'center')
	{
		ajax_message.css('left',screen.width / 2)
		ajax_message.css('top',screen.height / 2);
	}
	else
	{
		ajax_message.css('left',mousex)
		ajax_message.css('top',mousey);
	}
	return ajax_message;
}

function show_menu(i)
{
	$('#submenu'+i).is(":hidden") ? $('#submenu'+i).show() : $('#submenu'+i).hide();
}

function add_to_cart(n)
{
	var str = 'lang=' + $('#lang').val() + '&id=' + n + '&q=' + Math.abs(parseInt($(":text[@name=q" + n + "]").val()));
	$("select[@name^=prop_"+n+"_]").each(function(){str += '&' + this.name + '=' + this.options.selectedIndex;});
	$("#cart_block").load('/shop_cart_block.php?' + str);
	show_message($("#cart_added_text").val(),'mouse').fadeIn(600).fadeOut(600,function(){$(this).remove()});
}

function validate_order_form()
{
	if($('#order_address').length && !$('#order_address').val())
	{
		alert($('#order_error_address').val());
		$('#order_address').get(0).focus();
		return false;
	}
	if($('#order_person').length && !$('#order_person').val())
	{
		alert($('#order_error_person').val());
		$('#order_person').get(0).focus();
		return false;
	}
	if($('#order_mail').length && !$('#order_mail').val())
	{
		alert($('#order_error_mail').val());
		$('#order_mail').get(0).focus();
		return false;
	}
	if($('#order_mail').length && $('#order_mail').val() && !check_mail($('#order_mail').val()))
	{
		alert($('#order_error_mail_correct').val());
		$('#order_mail').get(0).focus();
		return false;
	}
	if($('#order_phones').length && !$('#order_phones').val())
	{
		alert($('#order_error_phones').val());
		$('#order_phones').get(0).focus();
		return false;
	}
	if($('#order_company').length && !$('#order_company').val())
	{
		alert($('#order_error_company').val());
		$('#order_company').get(0).focus();
		return false;
	}
	if($('#order_juraddress').length && !$('#order_juraddress').val())
	{
		alert($('#order_error_juraddress').val());
		$('#order_juraddress').get(0).focus();
		return false;
	}
	$("#order_done").load('/shop_order_send.php?lang=' + $("#lang").val(),$("#order_form").serializeArray());
	$("#order_tabs").remove();
	$("#cart_table").remove();
	$("#cart_buttons").remove();
	return false;
}

function validate_remember_form(obj)
{
	if(obj.logmail && !obj.logmail.value)
	{
		alert(obj.error_fill.value);
		obj.logmail.focus();
		return false;
	}
	else if(obj.human && !obj.human2.value)
	{
		alert(obj.error_fill.value);
		obj.human2.focus();
		return false;
	}
	return true;
}

function validate_registration_form()
{
	if($("#registration_name").length && !$("#registration_name").val())
	{
		alert($("#error_registration_name").val());
		$("#registration_name").get(0).focus();
		return false;
	}
	if($("#registration_mail").length && !$("#registration_mail").val())
	{
		alert($("#error_registration_mail").val());
		$("#registration_mail").get(0).focus();
		return false;
	}
	if($("#registration_mail").length && $("#registration_mail").val() && !check_mail($("#registration_mail").val()))
	{
		alert($("#error_registration_mail_correct").val());
		$("#registration_mail").get(0).focus();
		return false;
	}
	if($("#registration_login").length && !$("#registration_login").val())
	{
		alert($("#error_registration_login").val());
		$("#registration_login").get(0).focus();
		return false;
	}
	if($("#registration_password").length && !$("#registration_password").val())
	{
		alert($("#error_registration_password").val());
		$("#registration_password").get(0).focus();
		return false;
	}
	if($("#registration_password2").length && !$("#registration_password2").val())
	{
		alert($("#error_registration_repassword").val());
		$("#registration_password2").get(0).focus();
		return false;
	}
	if(
		$("#registration_password").length && 
		$("#registration_password2").length && 
		$("#registration_password").val() && 
		$("#registration_password2").val() && 
		$("#registration_password").val() != $("#registration_password2").val()
	 )	
	{
		alert($("#error_registration_passwords").val());
		$("#registration_password2").get(0).focus();
		return false;
	}
	if($("#registration_human").length && !$("#registration_human").val())
	{
		alert($("#error_registration_human").val());
		$("#registration_human").get(0).focus();
		return false;
	}
	return true;
}


function SwitchTab(tabID,url,color)
{
	$('#inlaycontent').css('background-color',color).load(url);
}

function scroll_up(dir)
{
	if(dir == null) dir = true;
	
	if(dir) asd = {"scrollTop": 2000};
	else asd = {"scrollTop": 0};
	
	$('.scrollup').animate(
		asd
	,3000
	);
}
 