/**
 * @author Vlad Yakovlev (scorpix@design.ru)
 * @copyright Art.Lebedev Studio (http://www.artlebedev.ru)
 */
(function()
{
	var cityIds = {};
	var sectorIds = {};
	var activePopup = '';
	
	$(function()
	{
		document.onkeydown = explore;

		fillCities();
		fillSectors();
		
		$('#middle2 .popup .close').click(function() { closeActive(); });

		$('#clear-cities').click(function() { clearCities(); });
		$('#clear-sectors').click(function() { clearSectors(); });

		$('#search-city-popup input').click(function() { clickCity(this); });
		$('#search-sector-popup input').click(function() { clickSector(this); });
	});
	
	function clearCities()
	{
		cityIds = {};
		updateCities();

		$('#search-city-popup input:checked').removeAttr('checked').each(function()
		{
			$(this).next().removeClass('checked');
		});
	}
	
	function clearSectors()
	{
		sectorIds = {};
		updateSectors();

		$('#search-sector-popup input:checked').removeAttr('checked').each(function()
		{
			$(this).next().removeClass('checked');
		});
	}
	
	function clickCity(obj)
	{
		if (obj.checked) 
		{
			cityIds[obj.value.toString()] = $(obj).next().text().substr(1);
			$(obj).next().addClass('checked');
		}
		else 
		{
			delete cityIds[obj.value.toString()];
			$(obj).next().removeClass('checked');
		}
		
		updateCities();
	}
	
	function clickSector(obj)
	{
		if (obj.checked) 
		{
			sectorIds[obj.value.toString()] = $(obj).next().text().substr(1);
			$(obj).next().addClass('checked');
		}
		else 
		{
			delete sectorIds[obj.value.toString()];
			$(obj).next().removeClass('checked');
		}
		
		updateSectors();
	}
	
	function fillCities()
	{
		$('#search-city-popup input:checked').each(function()
		{
			$(this).next().addClass('checked');
			cityIds[this.value.toString()] = $(this).next().text().substr(1);
		});
		updateCities();
	}
	
	function fillSectors()
	{
		$('#search-sector-popup input:checked').each(function()
		{
			$(this).next().addClass('checked');
			sectorIds[this.value.toString()] = $(this).next().text().substr(1);
		});
		
		updateSectors();
	}
	
	function explore(event)
	{
		var el = null;

		switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
		{
			case 0x25:
				el = $('#search-prev-page');

				break;

			case 0x27:
				el = $('#search-next-page');

				break;
		}
		
		if (el && el.length)
			document.location = el.attr('href');
	}
	
	function updateCities()
	{
		var length = 0;
		
		for (var k in cityIds)
			length++;
			
		if (length)
		{
			var string = '<' + 'div class="name"><' + 'span>Офисы АНКОРа:<\/span><\/div><' + 'div class="values">';
			var i = 0;
			
			for (var k in cityIds) 
			{
				string += i ? ', ' : '';
				string += cityIds[k];
				i++;
			}
			
			string += '.<\/div>';
		
			$('#search-city').html(string);
		}
		else
			$('#search-city').html('<' + 'span>Уточнить офис АНКОРа<\/span>');
		
		$('#search-city span').click(function()
		{
			if ('none' == $('#search-city-popup').css('display')) 
				openPopup('city');
			else 
				closeActive();
	
			$.browser.msie && carcas.ieResize();
		});
	}
	
	function updateSectors()
	{
		var length = 0;
		
		for (var k in sectorIds)
			length++;
			
		if (length)
		{
			var string = '<' + 'div class="name"><' + 'span>Сектора рынка:<\/span><\/div><' + 'div class="values">';
			var i = 0;
			
			for (var k in sectorIds) 
			{
				string += i ? '; ' : '';
				string += sectorIds[k];
				i++;
			}
			
			string += '.<\/div>';
		
			$('#search-sector').html(string);
		}
		else
			$('#search-sector').html('<' + 'span>Выбрать сектор рынка<\/span>');
		
		$('#search-sector span').click(function()
		{
			if ('none' == $('#search-sector-popup').css('display')) 
				openPopup('sector');
			else 
				closeActive();
	
			$.browser.msie && carcas.ieResize();
		});
	}
	
	function openPopup(type)
	{
		closeActive();

		var wrap = $('#wrap');
		var obj = $('#search-' + type + '-popup');
		wrap.css('height', '1px');
		obj.show();
		var top = document.getElementById('search-' + type).offsetTop / carcas.getEmK();
		obj.css('margin-top', top + 'em');
		wrap.css('height', '100%');
		$(window).resize();

		activePopup = type;
	
		return false;
	}
	
	function closeActive()
	{
		if (activePopup) 
		{
			var wrap = $('#wrap');
			wrap.css('height', '1px');
			$('#search-' + activePopup + '-popup').hide();
			wrap.css('height', '100%');
			$(window).resize();
			activePopup = '';
		}
		
		return false;
	}
})();