$(document).ready(function() {
	$().mousemove(function(e){
	      mousex = e.pageX;
		  mousey = e.pageY;
	   }); 

	/*$('a[cid]').mouseover(function(){
		$(this).parent('td').animate({
		width: '30%'
		},'fast')
	})
	
	$('a[cid]').mouseout(function(){
		$(this).parent('td').animate({
		width: '25%'
		},'fast')
	})
*/
	
	$('a[cid]').click(function(){
		var cid = $(this).attr('cid');
		var ac = $(this).attr('ac');
		$(this).parent('td').parent('tr').find('td a[cid]').css('font-weight','400');
		$(this).css('font-weight','900');
		$(this).parent('td').parent('tr').find('td.a1,td.a2').removeClass().addClass('a1');
		$(this).parent('td').removeClass().addClass('a2');
		$('#acontent' + ac).load('ajax.php@id=' + cid);
		return false;
	})


	$("#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;
				}
			});
			$(this).find(':input[mail]').each(function(){
				if($(this).val() && !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;
				}
			});

			var pss = $(this).find(':password[password]');
			if(pss.length && submit_form)
			{
				var pss_val = $(this).find(':password[password]:first').val();
				pss.each(function(){
					if($(this).val() != pss_val)
					{
						var need_message = show_need_message(this,$("div[@fmessage=pwds]",$("#" + 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;
	});
	
	$("a[@id^=th_]").click(function(){
		var preg_array = /^th_(\d+)_(\d+)_(\d+)$/.exec($(this).attr('id'));
		if(preg_array)
		{
			var res_id = preg_array[1];
			var res_width = preg_array[2];
			var res_height = preg_array[3];
			open_modal_window('open_image.php@file=' + res_id,res_width,res_height);
		}
		return false;
	})
	
});

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 f = $("select[_40name^=prop_"+n+"_],:text[_40name=q_" + n + "]").serializeArray();
	$("#cart_block").load('shop_cart_block.php@lang=' + $('body').attr('lang'),f,function(){
		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=' + $("body").attr("lang"),$("#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 check_mail(str)
{
	if(str == '') return true;
	var reg = /\s/g;
	str = str.replace(reg,'');
	var mails = str.split(",");
	var reg2 = /^[a-zA-Z0-9][a-zA-Z0-9\._-]+@[a-zA-Z0-9_\.-]+\.[a-zA-Z]{2,4}$/;
	for(i = 0;i < mails.length;i++) if(!reg2.test(mails[i])) return false;
	return true;
} 

function open_image(imgID,imgWidth,imgHeight,lang)
{
   if(!lang) lang = 'ru'
   winTop=(screen.height-imgHeight)/2;
   winLeft=(screen.width-imgWidth)/2;
   if(imgHeight > screen.height) imgHeight = screen.height;
   if(imgWidth > screen.width) imgWidth = screen.width;
   window.open("open_image.php@lang="+lang+"&file="+imgID,"new_image","toolbar=0,menubar=0,location=0,status=0,statusbar=0,scrollbars=auto,directories=0,resizable=auto,copyhistory=0,top="+winTop+",left="+winLeft+",width="+imgWidth+",height="+imgHeight);
}

function open_modal_window(file,modal_width,modal_height,func)
{
	$("#modal").remove();
	var modal = $('<div id=modal style="display: none">Загрузка...</div>').appendTo('body');
	modal.css('position','absolute');
	modal.css('width',modal_width);
	modal.css('height',modal_height);
	var left_coord = $(document).scrollLeft() + ($(window).width() - modal_width) / 2;
	var top_coord = $(document).scrollTop() + ($(window).height() - modal_height) / 2;
	modal.css('left',left_coord);
	modal.css('top',top_coord);
	var reg = new RegExp('^/','g');
	modal.load(file,function(){
		$("#modalimage").parents('a').click(function(){return false})
		$("body,#modalimage").click(function(){$("#modal").hide(250,function(){$(this).remove()});})
		modal.show(250);
	});
}
