function isOption(list,value)
{
	var i;
	if(list.options)
	{
		for(i=0; i<list.options.length; i++)
		{
			if(list.options[i].value==value)
				return true;
		}
	}
	return false;
}

function addOptions(all)
{
	var list=document.getElementById('subway_zones');
	var list2=document.getElementById('selected_zones');
	for(i=0; i<list.options.length; i++)
	{
		if(list.options[i].selected || all)
		{
			if(!isOption(list2,list.options[i].value))
			{
				list2.options[list2.options.length] = new Option(list.options[i].text, list.options[i].value);
			}
			
		}
	}
}

function setArea(id, value,show)
{
	var list=document.getElementById('selected_zones');
	if(show)
	{
		for(i=0; i<list.options.length; i++)
		{
			if(list.options[i].value==id)
				list.options[i]=null;
		}
	}
	else if(!isOption(list,id))
		list.options[list.options.length] = new Option(value, id);	
}

function removeOptions(all)
{
	var list=document.getElementById('selected_zones');
	var i;
	for(i=(list.options.length-1); i>-1; i--)
	{
		if(list.options[i].selected || all)
		{
			list.options[i]=null;
		}
	}
}

function addZones()
{
	var list=document.getElementById('selected_zones');
	var zones=document.getElementById('zones_hidden');
	for(i=0; i<list.options.length; i++)
	{
		zones.value+=list.options[i].value+',';
	}
	
	return true;
}



var if_main_objects_map={
	Hide:function()
	{
		$('#if_main_objects_map_Bgr').fadeOut('fast');
		$('#if_main_objects_map_Box').fadeOut('fast');
		$('#subway_zones').show();
		$('#selected_zones').show();
		$('select[name=class]').show();
		$('select[name=type]').show();
		$('select[name=pl]').show();
		if (!show_type)
			$('select[name=metro]').show();
			
	},
	
	Show:function()
	{
			$('#subway_zones').hide();
			$('#selected_zones').hide();
			if (show_type)
			{
				$('#map_type').val($('#type').val());
			}
			$('#map_class').val($('#class').val());
			if (show_pl)
				$('#map_pl').val($('#pl').val());
	        
			$('select[name=class]').hide();
			$('select[name=type]').hide();
			$('select[name=pl]').hide();
			if (!show_type)
				$('select[name=metro]').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 (show_type)
        {
			$('#type').val($('#map_type').val());
        }
        $('#class').val($('#map_class').val());
        $('#pl').val($('#map_pl').val());
        $('#metro').val('');
        $('#search_form').submit();
        
        if_main_objects_map.ShowNumber(i);
    },
	
	ShowNumber:function(Number)
	{
		if(Number)
		{
			$('#choosedStations strong').html(Number);
		}
		else
		{
		$('#choosedStations strong').html(0);
		}
	},
	
	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);
		if (show_type == false)
		{
			$('#typelabel').hide();
			$('#map_type').hide();
		}
		if (show_pl == false)
		{
			$('#pllabel').hide();
			$('#map_pl').hide();
		}
	},
	
	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;
		}
	)
		if_main_objects_map.Prepare();
		if_main_objects_map.beforeShow();
	
})

