$(document).ready(function() {
	$().mousemove(function(e){
	      mousex = e.pageX;
		  mousey = e.pageY;
	   }); 


	$('body').bind('contextmenu',function(){
		return false;
	})
	
	$("*[@id^=addtocart]").click(function(){
		add_to_cart($(this).attr('id').replace(/\D/g,''));
		return false;
	})
	
	$("#order_recount").click(reload_cart)

	function reload_cart()
	{
		$("#cart_table").load("/shop_cart_table.php?lang=" + $("body").attr("lang"),$("#order_form").serializeArray(),function(){
			$("#cart_block").load("/shop_cart_block.php?lang=" + $("body").attr("lang"));
			$("#cart_sum").load("/shop_cart_sum.php?lang=" + $("body").attr("lang"));
			$(":text[@name^=q_]").keyup(function(){if(new RegExp('^[0-9]+$','g').test($(this).val())) reload_cart()})
		});

		if($(":checkbox[@name^=d]").length == $(":checkbox[@name^=d]:checked").length)
		{
			$("#cart_options").hide();
		}
	}
	
	$(":text[@name^=q_]").keyup(function(){if(new RegExp('^[0-9]+$','g').test($(this).val())) reload_cart()})
	
	$('#orderphys').click(function(){
		$('#ordercontent').load('/shop_order_phys.php?lang=' + $("body").attr("lang"),function(){$(this).show()});
	})

	$('#orderjur').click(function(){
		$('#ordercontent').load('/shop_order_jur.php?lang=' + $("body").attr("lang"),function(){$(this).show()});
	})

	$.get('/forms_messages.php',function(data){
		$(data).appendTo('body');
	})
	
	$("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() == '' && !$("div[needdiv]").length)
				{
					var need_message = show_need_message(this,$("div[@fmessage=fill]").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()) && !$("div[needdiv]").length)
				{
					var need_message = show_need_message(this,$("div[@fmessage=mail]").html());
					$(this).bind("keydown",function(){need_message.remove()})
					submit_form = false;
					this.focus();
					return false;
				}
			});
			$(this).find(':input[minlength],:password[minlength]').each(function(){
				var val_length = $(this).val().length;
				var min_length = parseInt($(this).attr('minlength'));
				if(val_length < min_length && !$("div[needdiv]").length)
				{
					var need_message = show_need_message(this,$("div[@fmessage=min]").html().replace(/#n#/g,min_length));
					$(this).bind("keydown",function(){need_message.remove()})
					submit_form = false;
					this.focus();
					return false;
				}
			});
			$(this).find(':file[exts]').each(function(){
				var exts = $(this).attr('exts').split(' ');
				var file_name = $(this).val();
				if(file_name)
				{
					var ext = '';
					var valid_ext = false;
					if(preg = new RegExp('\.([a-zA-Z]+)$','g').exec(file_name)) ext = preg[1];
					for(i = 0;i < exts.length; i++)
					{
						if(ext == exts[i])
						{
							valid_ext = true;
							break;
						}
					}
					if(!valid_ext && !$("div[needdiv]").length)
					{
						var message = $("div[@fmessage=fileext]").html();
						message = message.replace('#formats#',exts.join(','));
						var need_message = show_need_message(this,message);
						$(this).bind("keydown",function(){need_message.remove()})
						submit_form = false;
						if($(this).is(":visible")) $(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 && !$("div[needdiv]").length)
					{
						var need_message = show_need_message(this,$("div[@fmessage=pwds]").html());
						$(this).bind("keydown",function(){need_message.remove()})
						submit_form = false;
						this.focus();
						return false;
					}
				})
			}
			if(submit_form)
			{
				$(this).find(':file').each(function(){
					if($(this).val())
					{
						$("#" + form_id).attr('direct','1');
						if($("#" + form_id).attr('action').indexOf('forms_handler.php') != -1) $("#" + form_id).attr('action','');
						return;
					}
				})
				if(!$(this).is("[@direct=1]"))
				{
					var sending = show_message($("div[@fmessage=send]").html(),'center');
					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);
						reevent_comments();
						$('#comments textarea').val('');
						if(typeof(tinyMCE) == 'object') tinyMCE.execInstanceCommand('comment','mceSetContent',false,'');
						if($(this).find('#form_message_ok').length) $("#" + form_id).remove();
						if(form_id == 'order_form') close_order_form();
					});
				}
				else return true;
			}
			return false;
	})
	
	$("form.pollform").submit(function(){
		url_str = "id=" + $(this).attr('pollid') + "&variant=" + $(":checked",this).val() + "&lang=" + $('body').attr('lang');
		$("#poll" + $(this).attr('pollid')).load("/poll.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;
	})


	
	$('a[id^=sbma]').click(function(){
		$("div[id^=sbm]:visible").hide();
		var sid = $(this).attr('id').replace(/a/g,'');
		var sm = $('#' + sid);
		if(sm.is(':hidden')) sm.slideDown(250);
		else sm.slideUp(250);
		if($(this).is('.shr'))
		{
			$('#shr').load('/show_rooms.php?id=' + $(this).attr('id').replace(/\D/g,''),oksana);
		}

		return false;
	})

	$('a.ggg').click(function(){
		$('#ggg').load('/gallery2.php?id=' + $(this).attr('id').replace(/\D/g,''),oksana);
	})

	oksana();
	reevent_comments();
});

function oksana()
{
		
	$('img[image2]').mouseover(function(){
		if(!$(this).attr('image2')) return true;
		$(this).removeAttr('height');
		var tem = $(this).attr('image2');
		var src = $(this).attr('src');
		var nh = $(this).attr('nh');
		$(this).attr('nh',$(this).attr('height'));
		$(this).attr('image2',src);
		$(this).attr('src',tem).attr('height',nh);
		return false;
	})
	
	$('img[image2]').mouseout(function(){
		if(!$(this).attr('image2')) return true;
		$(this).removeAttr('height');
		var tem = $(this).attr('image2');
		var src = $(this).attr('src');
		var nh = $(this).attr('nh');
		$(this).attr('nh',$(this).attr('height'));
		$(this).attr('image2',src);
		$(this).attr('src',tem).attr('height',nh);
		return false;
	})
	
		$('*[chimage]').mouseover(function(){
		var src = $(this).attr('chimage');
		$('#mainimg').attr('src',src);
		return false;
	})
	
		$('*[chimage4]').click(function(){
		var src = $(this).attr('chimage4');
		$('#mainimg').attr('src',src);
		return false;
	})



	$('*[chimage]').click(function(){return false})

	$('img[chimage2]').mouseover(function(){
		var src = $(this).attr('chimage2');
		$('#mainimg').css('background','url("'+src+'") no-repeat');
		$('#mainimg').attr('src','/images/trans.gif');
		return false;
	})


	$('img[chimage3]').click(function(){
		var src = $(this).attr('chimage3');
//		alert(src)
//		$('#nimage').removeAttr('width').removeAttr('height').
	$('#nimage').attr('src',src);
		return false;
	})

	
}


function reevent_comments()
{
	$('#comments *[@id^=quote]').click(function(){
		var cid = 'comment' + $(this).attr('id').replace(/\D/g,'');
		var str = $('#' + cid).html();
		str = str.replace(/<(blockquote)>.*<\/\1>/ig,'');
		str = '[QUOTE]' + str + '[/QUOTE]';
 		if(typeof(tinyMCE) == 'object') tinyMCE.execInstanceCommand('comment','mceInsertContent',false,str);
		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(/\?.*$/g,'') + '?' + Math.random();
		$(this).attr('src',new_src);
	})
}


function show_need_message(obj,message)
{
	return $('<div class=needmessage needdiv=1>' + message + '</div>').insertAfter($(obj)).hide().fadeIn(600);
}

function show_message(msgText,msgPos)
{
	if(!msgPos) msgPos = '';
	var ajax_message = $("<div id=ajaxmessage style='display:none'>" + msgText + "</div>").appendTo('body');
	ajax_message.css('position','absolute');
	if(msgPos == 'center')
	{
		ajax_message.css('left',($(document).scrollLeft() + ($(window).width() - ajax_message.width()) / 2))
		ajax_message.css('top',($(document).scrollTop() + ($(window).height() - ajax_message.height()) / 2));
	}
	else
	{
		ajax_message.css('left',mousex)
		ajax_message.css('top',mousey);
	}
	ajax_message.show();
	return ajax_message;
}

function show_menu(i)
{
	$('#submenu'+i).is(":hidden") ? $('#submenu'+i).show() : $('#submenu'+i).hide();
}

function add_to_cart(n)
{
	if(!$(":text[@name=q_" + n + "]").size()) $("<input type=hidden name=q_" + n + " value=1>").appendTo('body');
	var f = $("select[@name^=prop_"+n+"_],:text[@name=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 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_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 = parseInt($(document).scrollLeft() + ($(window).width() - modal_width) / 2);
	var top_coord = parseInt($(document).scrollTop() + ($(window).height() - modal_height) / 2);
	if(left_coord < 0) left_coord = 0;
	if(top_coord < 0) top_coord = 0;
	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);
	});
}

function close_order_form()
{
	$('#cart_table,#cart_options').remove();
}

$(document).ready(function(){
	$('a[@id^=asub]').mouseover(function(){
		var id = $(this).attr('id').replace(/\D/g,'');
		var pos = $(this).offset({scroll:true});
		var l = pos.left;
		var t = pos.top + 20;
		alert(pos.top)
		$('#submenu' + id).css('left',l);
		$('#submenu' + id).css('top',t);
		$('#submenu' + id).show(500);
		return false;
	})	
	
	var to;
	$('a[@id^=asub]').mouseout(function(){
		var id = $(this).attr('id').replace(/\D/g,'');
		to = setTimeout(function(){
			if(!$('#submenu' + id).is(':hidden')) $('#submenu' + id).hide();
		},1000)
	})
	$('table.submenu').mouseover(function(){
		clearTimeout(to);
	})

	$('#scroll_left').mouseover(function(){
		scr(false);
	})
	$('#scroll_left').mouseout(function(){
		$('#scr').stop();
	})
	
	
	$('#scroll_right').mouseover(function(){
		scr();
	})
	$('#scroll_right').mouseout(function(){
		$('#scr').stop();
	})
	
})

function scr(dir)
{
	if(dir == null) dir = true;
	
	if(dir) asd = {"scrollLeft": 10000};
	else asd = {"scrollLeft": 0};
	
	$('#scr').animate(asd,3000);
}