var lang = '';

$(document).ready(function() {
	$().mousemove(function(e){mousex = e.pageX;mousey = e.pageY;});

	lang = $('body').attr('lang');
	
	$.get('/general/ajax_vars.php?lang=' + lang,function(data){
		$(data).appendTo('body');
	})

	$('*[@class*=toggle]').click(function(){
		var oid = (preg = new RegExp('toggle([a-zA-Z0-9_]+)','g').exec($(this).attr('class'))) ? preg[1] : '';
		if(oid)
		{
			var oblock = $('#' + oid); 
			oblock.is(':hidden') ? $('#' + oid).show() : $('#' + oid).hide();
		}
	})

	$('*[@class*=open]').click(function(){
		var oid = (preg = new RegExp('open([a-zA-Z0-9_]+)','g').exec($(this).attr('class'))) ? preg[1] : '';
		if(oid) $('#' + oid).show()
	})

	$('*[@class*=close]').click(function(){
		var oid = (preg = new RegExp('close([a-zA-Z0-9_]+)','g').exec($(this).attr('class'))) ? preg[1] : '';
		if(oid) $('#' + oid).hide();
	})

	$("a[@id^=th_]").click(function(){
		if(preg_array = /^th_(\d+).*$/.exec($(this).attr('id')))
		{
			open_image($(this).find('img'),preg_array[1]);
		}
		return false;
	})

	$('#print').click(function(){
		var new_win = self.open('/print' + $(this).attr('title'),'print_version','');
		return false;
	})
	
	init();
});

function show_message(msgText,msgPos,hide)
{
	if(!msgPos) msgPos = 'center';
	var ajax_message = $("<div id=ajaxmessage style='display:none'>" + msgText + "</div>").appendTo('body').css('position','absolute');
	ajax_message.css('left',($(document).scrollLeft() + ($(window).width() - ajax_message.width()) / 2));
	ajax_message.css('top',($(document).scrollTop() + ($(window).height() - ajax_message.height()) / 2));
	ajax_message.fadeIn(200);
	if(!hide) setTimeout(function(){ajax_message.fadeOut(600,function(){$(this).remove()})},1000);
	else return ajax_message;
}

function open_image(obj,file)
{
	var modal_width = modal_height = 0;
	var modal = $('<div id=openimage style="display: none"></div>').css('position','absolute').appendTo('body');
	aleft =  parseInt($(obj).offset({scroll:false}).left) - (!$.browser.safari && !$.browser.opera ? 1 : 0);
	atop = parseInt($(obj).offset({scroll:false}).top) - (!$.browser.safari && !$.browser.opera ? 1 : 0);
	awidth = $(obj).width();
	aheight = $(obj).height();
	mborder = modal.css('borderWidth');
	modal.css('left',aleft).css('top',atop).width(awidth).height(aheight);
	$.getJSON('/general/open_image.php?json=1&file=' + file + '&lang=' + lang,function(data)
	{
		path = data.path;
		modal_width = data.width;
		modal_height = data.height;
		alt = data.alt;
		left_coord = parseInt($(document).scrollLeft() + ($(window).width() - modal_width) / 2);
		top_coord = parseInt($(document).scrollTop() + ($(window).height() - modal_height) / 2);
		if(left_coord < 0) left_coord = 0;
		if(top_coord < 0) top_coord = 0;
		
		function animate_image()
		{
			obj.css('visibility','hidden');
			modal.animate(
			{
				width: modal_width,
				height: modal_height,
				left: left_coord,
				top: top_coord
			},'fast','linear').click(function()
				{
					$(this).animate(
					{
						width: awidth,
						height: aheight,
						left: aleft,
						top: atop
					},'fast','linear',function(){obj.css('visibility','visible');$(this).hide().remove()})
				}
			)
		}
		
		modal.load('/general/open_image.php?file=' + file,function()
		{
			if(!$.browser.opera) modal.find('img').load(animate_image);
			else animate_image();
		})	
	})
}

function init()
{
	lang = $("body").attr("lang");
	
	$(":checkbox[@class^=chunch]").click(function(){
		if(this.checked)
		{
			cls = $(this).attr('class');
			th = $(this);
			$(":checkbox." + cls).not(th).each(function(){this.checked = false})
		}
	})
}




