// JavaScript Document
function array_unique(arrayName)
{
	var newArray=new Array();
	label:for(var i=0; i<arrayName.length;i++ )
	{  
		for(var j=0; j<newArray.length;j++ )
		{
			if(newArray[j]==arrayName[i]) 
				continue label;
		}
		newArray[newArray.length] = arrayName[i];
	}
	return newArray;
}

function click_question(name, hwnd)
{
	text   = $(hwnd).html();
	hwnd   = '#search input[name="' + name + '"]';
	val    = $(hwnd).val();
	newval = text;
	del    = false;
			
	if(val.length > 0)
	{
		data = val.split(',');
		c    = data.length;
		tmp  = new Array();
		
		for(i = 0; i < c; ++i)
			if(text == data[i])
				del = true;
		
		if(!del)
		{
			tmp = data;
			tmp.push(text);
		}
		else
			for(i = 0; i < c; ++i)
				if(data[i] != text && data[i].length > 0)
					tmp.push(data[i]);
		
		newval = tmp.join(',');
	}
	
	$(hwnd).val(newval);
	
	return del;
}

function add_metro(id)
{
	title = $('#pp' + id).attr('title');
	$('#pp' + id).show();
	
	if(!$('#metroselect option[value="' + id + '"]').html())
		$('#metroselect').append('<option value="' + id + '">' + title + '</option>');
}

function add_metro_multi(arr)
{
	$.each(
			arr, 
			function(i, item)
			{
				id = '#pp' + item;
				if($(id).css('display') != 'none')
				{
					$(id).hide();
					$('#metroselect option[value="' + item + '"]').remove();
				}
				else	
					add_metro(item);
			}
		  );
}

function clear_metro()
{
	$('#metroselect').empty();
	$('.pp').hide();
}
			
$
(
	function()
	{
		$("#dialog").dialog({
								autoOpen: false, 
								width: 730, 
								height: 300,
								show: 'drop',
								modal: true
							});
		
		$('#metromap').load(
								'/ajaxmetro',
						   		null,
								function()
								{
									$('#metromap area').each
									(
									 	function(i)
										{
											$(this).attr('href', '#');
										}
									);
									
									$('#metromap area').click
									(
									 	function()
										{
											id  = $(this).attr('alt');
											add_metro(id);
											return false;
										}
									);
									
									$('#metromap img[alt!="no"]').click
									(
									 	function()
										{
											id = $(this).attr('alt');
											$('#metroselect option[value="' + id + '"]').remove();
											$(this).hide();
											return false;
										}
									);
									
									$('#metrocentr').click
									(
										function()
										{
											arr = [7, 134, 136, 35, 117, 48, 137, 75, 87, 12, 132, 122, 100, 28, 52, 133, 71, 8, 118, 175, 174, 33, 103, 34, 74, 32, 73, 49, 10, 31, 9, 119, 72, 53, 30, 120, 159, 158, 161, 160, 29, 121, 88, 11];
											add_metro_multi(arr);
										}
									);
									
									$('#metrocol').click
									(
										function()
										{
											arr = [133, 52, 28, 100, 132, 12, 75, 48, 35, 136, 134, 7];
											add_metro_multi(arr);
										}
									);
									
									$('#metroclear').click
									(
									 	function()
										{
											clear_metro();
											return false;
										}
									);
								}
						   );
		
		$('#pickmap').click
		(
		 	function()
			{
				$('#metromap').toggle();
				return false;
			}
		);
		
		$('#help_naz').click
		(
		 	function()
			{
				$('#blocknaz').toggle();
				return false;
			}
		);
		
		
		$('#help_place').click
		(
		 	function()
			{
				$('#blockplace').toggle();
				return false;
			}
		);
		
		$('.submit').click
		(
		 	function()
			{
				$('#metroselect option').attr('selected', 'selected');
				$(this).parents('form').submit();
				return false;
			}
		);
		
		$('body').click
		(
		 	function()
			{
				$('#metromap').hide();
				//$('#dialog').dialog('close');
			}
		);
		
		$('#metromap').click
		(
		 	function()
			{
				return false;
			}
		);
		
		$('.stype').click
		(
		 	function()
			{
				id = $(this).attr('rel');
				$('.search-type').val(id);
				$('.stype').each
				(
				 	function(i)
					{
						$(this).toggleClass('sel');
					}
				);
			}
		);
		
		$('.more').click
		(
		 	function()
			{
				$('#dialog').empty();
				$('#dialog').load($(this).attr('href')).dialog('open');	
				
				rez = $(this).attr('alt');
				if(rez != undefined)
				{
					if(rez.indexOf('x'))
					{
						rez    = rez.split('x');
						rez[0] = parseInt(rez[0]);
						rez[1] = parseInt(rez[1]);
						$('.ui-dialog').css({width: rez[0] + 'px', height: rez[1] + 'px'});
					}
				}
				
				return false;
			}
		);
		
		$('.dialog-close').live
		(
		 	'click',
			function()
			{
				$('#dialog').dialog('close');
				return false;
			}
		);
		
		$('.img-mini img').live
		(
		 	'mouseover',
			function()
			{
				$(this).addClass('sel');
			}
		);
		
		$('.img-mini img').live
		(
		 	'mouseout',
			function()
			{
				$(this).removeClass('sel');
			}
		);
		
		$('.img-mini img').live
		(
		 	'click',
			function()
			{
				hwnd = $('.img-preview img:first');
				w    = $(hwnd).css('width');
				img  = $(hwnd).attr('src');
				$(hwnd).css({width: w}).attr('src', $(this).attr('src'));
				$(this).attr('src', img);
				
				id  = $('#printlink').attr('alt');
				alt = $(this).attr('alt');
				$('#printlink').attr('href', '/catalog/' + id + '?print=1&foto=' + alt);
				
				return false;
			}
		);
		
		$('.ui-widget-overlay').live
		(
		 	'click',
		 	function()
			{
				$('#dialog').dialog('close');
			}
		);
		
		//
		$('.save-favorite').live
		(
		 	'click',
			function()
			{
				id     = $(this).attr('href').substr(1);
				cookie = $.cookie('favorite');
				
				if(cookie != null && cookie.length != 0)
				{
					cookie = cookie.split(',');
					cookie.push(id);
					id     = array_unique(cookie).join(',');
				}
				$.cookie('favorite', id);
				alert('Сохранено в избранном');
			}
		);
		
		$('.delete-favorite').live
		(
		 	'click',
			function()
			{
				id     = $(this).attr('href').substr(1);
				cookie = $.cookie('favorite');
				
				if(cookie != null)
				{
					cookie = cookie.split(',');
					
					for(i = 0; i < cookie.length; ++i)
						if(id == cookie[i])
							cookie.splice(i, 2);
							
					cookie = array_unique(cookie).join(',');
					
					$.cookie('favorite', cookie);
				}
			
				alert('Удалено из избранного');
			}
		);
		
		$('#blocknaz a[class!=close]').live
		(
		 	'click',
			function()
			{
				del = click_question('naz', $(this));
				if(!del)
					$(this).addClass('selected');
				else
					$(this).removeClass('selected');
				return false;
			}
		);
		
		$('#blockplace a[class!=close]').live
		(
		 	'click',
			function()
			{
				del = click_question('point', $(this));
				alt = $(this).attr('alt');
				if(!del)
					$(this).addClass('selected');
				else
					$(this).removeClass('selected');
					
				$.getJSON(
						  	'/metroget',
							{now : alt},
							function(data)
							{
								$.each(
									   	data, 
										function(i, item)
										{
											if(del > 0)
												$('#metroselect option[value=' + i + ']').remove();
											else
												$('#metroselect').append('<option value="' + i + '">' + item + '</option>');
										}
									  );
							}
						 );
				return false;
			}
		);
		
		$('.blockhelp .close').live
		(
		 	'click',
			function()
			{
				$(this).parent().hide();
			}
		);
		
		$('#metroselect').keydown
		(
		 	function(event)
			{
				if(event.keyCode == 46)
				{
					$(this).find('option:selected').remove();
				}
				return false;
			}
		);
		
		$('#clearmetromap').click
		(
		 	function()
			{
				clear_metro();
				return false;
			}
		);
		
		$('#img-big').live
		(
		 	'click',
		 	function()
			{
				href = $(this).find('img').attr('src');
				window.open(href);
				return false;
			}
		);
	}
)

//swfobject.embedSWF("/swf/metro.swf", "metroflash", "600", "670", "9.0.0");
