$(document).ready(function() {
	$().mousemove(function(e){
	      mousex = e.pageX;
		  mousey = e.pageY;
	   }); 

	$("#mf").flash({src: '/flash/mainflash.swf',width: 1004,height: 434});
	
	$("#tf").flash({src: '/flash/logo.swf',width: 142,height: 49})
	
	$("div.smallimg").bind("mouseover",function(){
		$("#mainimage").attr('src',"/image.php?file=" + $(this).attr('big'));
	})
	
	$("form[@validate=1]").submit(function(){
			var submit_form = true;
			$(this).find("div[needdiv]").remove();
			$(this).find(':input[need]').each(function(){
				if($(this).val() == '')
				{
					var need_message = show_need_message(this,$("#formerrorfill").html());
					$(this).bind("keydown",function(){need_message.remove()})
					submit_form = false;
					this.focus();
					return false;
				}
				else if($(this).is('[mail]') && !check_mail($(this).val()))
				{
					var need_message = show_need_message(this,$("#formcorrectmail").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($("#formsending").html(),'center').show();
					if(this.method == 'get') $("#"+$(this).attr('id')+"_result").load($(this).attr('action'),function(){sending.remove();});
					else $("#"+$(this).attr('id')+"_result").load($(this).attr('action'),$(this).serializeArray(),function(){sending.remove();});
				}
				else return true;
			}
			return false;
	})
	
	$(":input[@name=text]",document.forms['guest']).bind('keydown',recount_chars);
	$(":input[@name=text]",document.forms['guest']).bind('keyup',recount_chars);
	
	$("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;
	});
	
});

function recount_chars()
{
	var char_limit = parseInt($("#char_limit",document.forms['guest']).val());
	if(!char_limit) return true;
	var chars =  char_limit - $(":input[@name=text]",document.forms['guest']).val().length;
	if(isNaN(chars)) return true;
	if(chars < 0)
	{
		var val = $(":input[@name=text]",document.forms['guest']).val().substring(0,char_limit);
		$(":input[@name=text]",document.forms['guest']).val(val);
		chars = 0;
	}
	$("#chars",document.forms['guest']).text(chars);
}

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 openCalendar(name1,name2)
{
	winL = ( screen.width - 210 ) / 2;
	winT = ( screen.height - 210 ) / 2;
	open("/calendar.php?name1=" + name1 + "&name2=" + name2 + "&date=" + document.forms['train_form'].elements[name1].value,'calWin',"width=210,height=210,resizable=0,status=0,menubar=0,toolbar=0,scrollbars=0,left="+winL+",top="+winT+",screenX="+winL+",screenY="+winT);
}

function put_stamp(obj,f)
{
	var str = obj.value.split("-");
	y = str[0];
	m = str[1] - 1;
	d = str[2];
	h = "00";
	mi = "00";
	s = "00";
	if(d && d.indexOf(":") != -1)
	{
		shit = d.split(" ");
		d = shit[0];
		ti = shit[1];
		ti = ti.split(":");
		h = ti[0]; 
		mi = ti[1]; 
		s = ti[2]; 
		var dt = new Date(y,m,d,h,mi,s);
		obj.form.elements[f].value = dt.getTime()/1000;
	}
	else obj.form.elements[f].value = 0;
}