var if_main_objects_map={
	Hide:function()
	{
		$('#if_main_objects_map_Bgr').fadeOut('fast');
		$('#if_main_objects_map_Box').fadeOut('fast');
		$('#map_swf').show();
		$('#subway_zones').show();
		$('#selected_zones').show();
		$('select[name=price]').show();
	},
	
	Show:function()
	{
		if(!$('#if_main_objects_map_Box').attr('id'))
		{
			$('#map').load('?mod=main&act=map',false,function(){
				$('#map_swf').hide();
				$('#subway_zones').hide();
				$('#selected_zones').hide();
				$('select[name=price]').hide();
				if_main_objects_map.Prepare();
				if_main_objects_map.beforeShow();
				$('#if_main_objects_map_Bgr').fadeIn('fast');
				$('#if_main_objects_map_Box').fadeIn('fast');
				
			});
		}
		else
		{
			$('#map_swf').hide();
			$('#subway_zones').hide();
			$('#selected_zones').hide();
			$('select[name=price]').hide();
			if_main_objects_map.Prepare();
			$('#if_main_objects_map_Bgr').fadeIn('fast');
			$('#if_main_objects_map_Box').fadeIn('fast');
		}
		
		
	},
	
	SetSubway:function()
	{
		var Stations='';
		var i=0;
		$('div.if_main_objects_map_PointActive input').each(
			function()
			{
				Stations+=$(this).val()+',';
				i++;
			}
		);
		Stations=Stations.substring(0,Stations.length-1);
		$('#stations').val(Stations);
		if_main_objects_map.ShowNumber(i);
	},
	
	ShowNumber:function(Number)
	{
		if(Number)
		{
			$('#choosedStations span').html(Number);
			$('#choosedStations').show();
			$('#notChoose').hide();
		}
		else
		{
			$('#choosedStations').hide();
			$('#notChoose').show();
		}
	},
	
	GetSubway:function()
	{ 
		var Text=$('#stations').val();
		if(Text)
		{
			eval("var Stations=["+Text+"]");
			if_main_objects_map.ShowNumber(Stations.length);
			$(Stations).each(
				function()
				{
					var Station=$('div.if_main_objects_map_Point input[value='+this+']').parent().attr('title');
					if_main_objects_map.SelectStation(Station);
				}
			);
			
		}
	},
	
	ChangeStation:function(Title)
	{
		var Points=$('div.if_main_objects_map_Point[title='+Title+']');
		if(Points.hasClass('if_main_objects_map_PointActive'))
			if_main_objects_map.DeselectStation(Title);
		else
			if_main_objects_map.SelectStation(Title);
	},
	
	SelectStation:function(Title)
	{
		var Points=$('div.if_main_objects_map_Point[title='+Title+']');
		var Boxes=$('#if_main_objects_map_Zones input[title='+Title+']');
		Points.addClass('if_main_objects_map_PointActive');
		Points.css('opacity','1');
		Boxes.attr('checked','checked');
		$('<li title="'+Title+'"><a title="'+Title+'" href="#"><b>X</b></a> '+Title+'</li>').appendTo($('#if_main_objects_map_Checked'));
	},
	
	DeselectStation:function(Title)
	{
		var Points=$('div.if_main_objects_map_Point[title='+Title+']');
		var Boxes=$('#if_main_objects_map_Zones input[title='+Title+']');
		Points.removeClass('if_main_objects_map_PointActive');
		Points.css('opacity','0');
		Boxes.removeAttr('checked');
		$('#if_main_objects_map_Checked li[title='+Title+']').remove();
		
	},
	
	Over:function(Title)
	{
		var Texts=$('div.if_main_objects_map_Text[title='+Title+']');
		Texts.css('opacity','0.5');
	},
	
	Out:function(Title)
	{
		var Texts=$('div.if_main_objects_map_Text[title='+Title+']');
		Texts.css('opacity','0');
	},
	
	selectZone:function(zoneId)
	{
		var root=$('#if_main_objects_map_Zones>ul>li>input[value='+zoneId+']');
		root.attr('checked',1);
		$('#if_main_objects_map_Zones ul[title='+root.attr('title')+'] input[name^=station]').each(
			function()
			{
				if($(this).attr('checked')!=root.attr('checked'))
				{
					$(this).attr('checked',root.attr('checked'));
					if($(this).attr('checked'))
						if_main_objects_map.SelectStation($(this).attr('title'));
					else
						if_main_objects_map.DeselectStation($(this).attr('title'));
				}	
			}
		);
	},
	
	deselectZone:function(zoneId)
	{
		var root=$('#if_main_objects_map_Zones>ul>li>input[value='+zoneId+']');
		root.removeAttr('checked');
		$('#if_main_objects_map_Zones ul[title='+root.attr('title')+'] input[name^=station]').each(
			function()
			{
				if($(this).attr('checked')!=root.attr('checked'))
				{
					$(this).attr('checked',root.attr('checked'));
					if($(this).attr('checked'))
						if_main_objects_map.SelectStation($(this).attr('title'));
					else
						if_main_objects_map.DeselectStation($(this).attr('title'));
				}	
			}
		);
	},
	
	Prepare:function()
	{
		var Bgr=$('#if_main_objects_map_Bgr');
		var Box=$('#if_main_objects_map_Box');
		Bgr.height($(document).height()+'px');
		Bgr.width($(document).width()+'px');
		var Top=$(window).height()/2-Box.height()/2+$(window).scrollTop();
		if(Top<$(window).scrollTop())
			Top=$(window).scrollTop()+10;
		Box.css('top',Top);
		Box.css('left',$(window).width()/2-Box.width()/2);
	},
	
	Clear:function()
	{
		$('#if_main_objects_map_Checked li').each(
			function()
			{
				if_main_objects_map.DeselectStation($(this).attr('title'));
			}
		);
	},
	
	beforeShow:function()
	{
		var Bgr=$('#if_main_objects_map_Bgr');
		var Box=$('#if_main_objects_map_Box');
		Bgr.click(
			function()
			{
				if_main_objects_map.Hide();
			}
		);
		Bgr.css('opacity','0.5');
		if_main_objects_map.Prepare();
		
		$('div.if_main_objects_map_Point').css('opacity','0');
		$('div.if_main_objects_map_Text').css('opacity','0');
		$('div.if_main_objects_map_Text').hover(
			function()
			{	
				if_main_objects_map.Over($(this).attr('title'));
			},
			function()
			{
				if_main_objects_map.Out($(this).attr('title'));
			}
		);
		
		$('div.if_main_objects_map_Point').hover(
			function()
			{	
				if_main_objects_map.Over($(this).attr('title'));
			},
			function()
			{
				if_main_objects_map.Out($(this).attr('title'));
			}
		);
		
		$('div.if_main_objects_map_Point').click(
			function()
			{
				if_main_objects_map.ChangeStation($(this).attr('title'));
				return false;
			}
		);
		
		$('div.if_main_objects_map_Text').click(
			function()
			{
				if_main_objects_map.ChangeStation($(this).attr('title'));
				return false;
			}
		);
		
		$('#if_main_objects_map_Zones input[name^=station]').click(
			function()
			{
				if_main_objects_map.ChangeStation($(this).attr('title'));
			}
		);
		
		$('#if_main_objects_map_Zones input[name^=zones]').click(
			function()
			{	
				var root=$(this);
				$('#if_main_objects_map_Zones ul[title='+root.attr('title')+'] input[name^=station]').each(
					function()
					{
						if($(this).attr('checked')!=root.attr('checked'))
						{
							$(this).attr('checked',root.attr('checked'));
							if($(this).attr('checked'))
								if_main_objects_map.SelectStation($(this).attr('title'));
							else
								if_main_objects_map.DeselectStation($(this).attr('title'));
						}	
					}
				);
			}
		);
		
		$('#if_main_objects_map_Zones a').click(
			function()
			{
				var List=$(this).parents('li').children('ul');
				if(List.css('display')!='none')
					List.hide();
				else
					List.show();
				return false;
			}
		);
		
		$('#if_main_objects_map_Checked').html('');
		
		$('#if_main_objects_map_Checked a').livequery('click',
			function()
			{
				if_main_objects_map.DeselectStation($(this).attr('title'));
				return false;
			}
		);
		
		$('div.if_main_objects_map_Buttons input[name=clear]').click(
			function()
			{
				if_main_objects_map.Clear();
			}
		);
		
		$('div.if_main_objects_map_Buttons input[name=apply]').click(
			function()
			{
				if_main_objects_map.SetSubway();
				if_main_objects_map.Hide();
			}
		);
		
		$('div.if_main_objects_map_Buttons input[name=close]').click(
			function()
			{
				if_main_objects_map.Hide();
			}
		);
		
		
		$('#if_main_objects_map_Zones ul ul').hide();
		if_main_objects_map.GetSubway();
	}
};

$(function(){
	$('#btn_subway').click(
		function()
		{
			if_main_objects_map.Show();
			return false;
		}
	)
	$('#map').load('?mod=main&act=map',false,function(){
		if_main_objects_map.Prepare();
		if_main_objects_map.beforeShow();
	});
})
