/**
 * @author Vlad Yakovlev (scorpix@design.ru)
 * @copyright Art.Lebedev Studio (http://www.artlebedev.ru)
 */
(function()
{
	var cityIds = {};
	var typeIds = {};
	var sectorIds = {};
	var activePopup = '';
	
	$(function()
	{
		document.onkeydown = explore;

		fillCities();
		fillSectors();
		fillTypes();
		
		$('body').click(function()
		{
			closeActive();
		});
		
		$('#middle2 .popup').click(function(e)
		{
			clickCharact(e.target);
			e.stopPropagation();
		});
		
		$('#middle2 .charact span').click(function(e)
		{
			e.stopPropagation();
		});
		
		$('#middle2 .popup .close').click(function() { closeActive(); });
	
		$('#clear-cities').click(function() { clearCities(); });
		$('#clear-sectors').click(function() { clearSectors(); });
		$('#clear-types').click(function() { clearTypes(); });
	});
	
	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 clearTypes()
	{
		typeIds = {};
		updateTypes();

		$('#search-type-popup input:checked').removeAttr('checked').each(function()
		{
			$(this).next().removeClass('checked');
		});
	}
	
	function clickCharact(domEl)
	{
		if ('INPUT' == domEl.nodeName && 'checkbox' == $(domEl).attr('type'))
		{
			switch ($(domEl).attr('name'))
			{
				case   'city': clickCity(domEl);   break;
				case 'sector': clickSector(domEl); break;
				case   'type': clickType(domEl);   break;
			}
			
			return true;
		}
		
		return false;
	}
	
	function clickCity(obj)
	{
		if (obj.checked) 
		{
			cityIds[obj.value.toString()] = $(obj).next().next().text();
			$(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().next().text();
			$(obj).next().addClass('checked');
		}
		else 
		{
			delete sectorIds[obj.value.toString()];
			$(obj).next().removeClass('checked');
		}
		
		updateSectors();
	}
	
	function clickType(obj)
	{
		if (obj.checked) 
		{
			typeIds[obj.value.toString()] = $(obj).next().next().text();
			$(obj).next().addClass('checked');
		}
		else 
		{
			delete typeIds[obj.value.toString()];
			$(obj).next().removeClass('checked');
		}
		
		updateTypes();
	}
	
	function fillCities()
	{
		$('#search-city-popup input:checked').each(function()
		{
			$(this).next().addClass('checked');
			cityIds[this.value.toString()] = $(this).next().next().text();
		});
		updateCities();
	}
	
	function fillSectors()
	{
		$('#search-sector-popup input:checked').each(function()
		{
			$(this).next().addClass('checked');
			sectorIds[this.value.toString()] = $(this).next().next().text();
		});
		updateSectors();
	}
	
	function fillTypes()
	{
		$('#search-type-popup input:checked').each(function()
		{
			$(this).next().addClass('checked');
			typeIds[this.value.toString()] = $(this).next().next().text();
		});
		updateTypes();
	}
	
	function explore(event)
	{
		var el = null;

		switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
		{
			case 0x25:
				el = $('#search-prev-page');
				
				if (!el.length)
					el = $('#search-prev-vacancy');

				break;

			case 0x27:
				el = $('#search-next-page');

				if (!el.length) 
					el = $('#search-next-vacancy');

				break;
		}
		
		if (el)
			document.location = el.attr('href');
	}
	
	function updateCities()
	{
		var length = 0;
		
		for (var k in cityIds)
			length++;
			
		if (length) 
		{
			var string = 'В&#160;';
			//alert(k);
			var i = 0;
			
			for (var k in cityIds) 
			{
				if((k == 'Владивосток' || k == 'Владимир' ) && i == 0){
				string = 'Во&#160;';
			}
				if (i) string += i == length - 1 ? ' и&#160;' : ', ';
				
				string += '<' + 'span class="selected">' + cityIds[k] + '<\/span>';
				i++;
			}
			
			string += '.';
			
			$('#search-city').html(string);
			$('#search-city-popup .clear span').removeClass('disabled');
		}
		else 
		{
			$('#search-city').html('<' + 'span>Город<\/span> не указан.');
			$('#search-city-popup .clear span').addClass('disabled');
		}
		
		$('#search-city span').click(function(e)
		{
			if ('none' == $('#search-city-popup').css('display')) 
				openPopup('city');
			else 
				closeActive();
	
			$.browser.msie && carcas.ieResize();
			e.stopPropagation();
		});
	}
	
	function updateTypes()
	{
		var length = 0;
		
		for (var k in typeIds)
			length++;
			
		if (length) 
		{
			var string = 'Тип позиции<br />';
			var i = 0;
			
			for (var k in typeIds) 
			{
				if (i) string += ';<br /> ';
				
				string += '<' + 'span class="selected">' + typeIds[k] + '<\/span>';
				i++;
			}
			
			string += '.';
			
			$('#search-type').html(string);
			$('#search-type-popup .clear span').removeClass('disabled');
		}
		else 
		{
			$('#search-type').html('<' + 'span>Тип позиции<\/span> не указан.');
			$('#search-type-popup .clear span').addClass('disabled');
		}
		
		$('#search-type span').click(function(e)
		{
			if ('none' == $('#search-type-popup').css('display')) 
				openPopup('type');
			else 
				closeActive();
	
			$.browser.msie && carcas.ieResize();
			e.stopPropagation();
		});
	}
	
	function updateSectors()
	{
		var length = 0;
		
		for (var k in sectorIds)
			length++;
			
		if (length) 
		{
			var string = 'Сектор рынка<br />';
			var i = 0;
			
			for (var k in sectorIds) 
			{
				if (i)
					string += ';<br /> ';
				
				string += '<' + 'span class="selected">' + sectorIds[k] + '<\/span>';
				i++;
			}
			
			string += '.';
			
			$('#search-sector').html(string);
			$('#search-sector-popup .clear span').removeClass('disabled');
		}
		else 
		{
			$('#search-sector').html('<' + 'span>Сектор рынка<\/span> не указан.');
			$('#search-sector-popup .clear span').addClass('disabled');
		}
		
		$('#search-sector span').click(function(e)
		{
			if ('none' == $('#search-sector-popup').css('display')) 
				openPopup('sector');
			else 
				closeActive();
	
			$.browser.msie && carcas.ieResize();
			e.stopPropagation();
		});
	}
	
	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;
	}
})();