$(document).ready(function() {
	$().mousemove(function(e){
	      mousex = e.pageX;
		  mousey = e.pageY;
	   }); 

	$("#order_form").bind("submit",function(){return validate_order_form()})
	
	$("a[@id^=addtocart]").each(function(){
		this.onmouseover = function(){return true}
		this.onclick = function(){add_to_cart($(this).attr('id').replace("addtocart",""));return false;};
	})

	$("#order_recount").bind("click",function(){
//		$("#order_form").submit();
		$("#cart_table").load("/shop_cart_table.php?lang=" + $("#lang").val(),$("#order_form").serializeArray());
		$("#cart_block").load("/shop_cart_block.php?lang=" + $("#lang").val());
	$("#order_sum").load("/shop_cart_sum.php?lang=" + $("#lang").val());
		if($(":checkbox[@name^=d]").length == $(":checkbox[@name^=d]:checked").length)
		{
			$("#cart_buttons").remove();
			$("#order_tabs").text("");
		}
	});

	$("#order_open_form").bind("click",function(){
		$("#order_tabs").load("/shop_order_tabs.php?lang=" + $("#lang").val());
	});
	
	$("#order_submit").bind("click",function(){
		$("#order_form").submit(function(){return validate_order_form()})
	})
	
	$("#cart_block").load("/shop_cart_block.php?lang=" + $("#lang").val());
	$("#cart_table").load("/shop_cart_table.php?lang=" + $("#lang").val());
	$("#order_sum").load("/shop_cart_sum.php?lang=" + $("#lang").val());
	

	$("#registration_form").bind("submit",function(){
		return validate_registration_form();
	})
	
	$("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;
	})
	
	$("form.voteform").submit(function(){
		url_str = "id=" + $(this).attr('voteid') + "&variant=" + $(":checked",this).val();
		$("#vote" + $(this).attr('voteid')).load("/vote.php?" + url_str);
		return false;
	});
	
	$("img[src*=777]").mouseover(function(){
		$(this).css('cursor','hand');
	})
	
	$("img[src*=777]").click(function(e){
		$("div.or").remove();
		var w = 640;
		var h = 480;
		var srcb = $(this).attr('src').replace("777","777g");
		var d = $('<div class=or style="display: none"></div>').appendTo('body');

		d.load("/ajax_open_res.php?file="+srcb,function(){
			var xtop = $(window).scrollTop() + ($(window).height() - $(this).height()) / 2;
			var xleft = $(document).scrollLeft() + ($(window).width() - $(this).width()) / 2;
			$(this).css('position','absolute').css('top',xtop).css('left',xleft).show();
			$(this).css('cursor','hand');
			$(this).click(function(){$('.or').remove()})
		});
		
	})
});

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);
}

