/*	
 *		jQuery cityBox plugin
 *	
 *		Author: Andrew Savchenko (http://free-lance.ru/users/homebwoi)
 *
 */
(function($){
	$.fn.cityBox = function(inputObject, inputType){
	
		var settings = {
			url: "/cityBox.php?"
		};
	
		$("body").append("<div id='city-box-wrapper'></div><div id='cityBox'><div id='cityBoxTitle'>ВАШ РЕГИОН</div><div id='cityBoxPopular'><a href='#' id='cityBoxKiev'>Киев</a></div><div id='cityBoxClose'></div><div id='city-box-select'></div></div>");
		
		var sectorType = "city";
		var sectorID = 1;
		
		$("#city-box-close").click(function(){
			_go_back_cityBox();
			return false;
		});

		$("#cityBoxClose").click(function(){_hide_cityBox();});
		$("#city-box-wrapper").click(function(){_hide_cityBox();});

		$("#city-box-select > select").children("option").click(function(){
			alert("test");
		});

		$("#cityBoxKiev").click(function(){return false;});
		$("#cityBoxKiev").dblclick(function(){
			inputObject.html("Украина, Киев");
			WriteCity(inputObject.html());
			_hide_cityBox();
			return false;
		});
	
		function _hide_cityBox(){
			$("#city-box-wrapper").hide();
			$("#cityBox").animate({"top": "-500px"});
			sectorType = "region";
			sectorID = 1;
		}
	
		function _show_cityBox(){
			_load_current_sector();
			$("#city-box-wrapper").show();
			$("#cityBox").animate({"top": "+=600px"});
			return false;
		}
		
		function _load_current_sector(){
			$.ajax({
				url: settings.url,
				type: "get",
				data: "type="+sectorType+"&id="+sectorID,
				success: function(data){
					$("#city-box-select").html(data);
					_bind_next_button();
				}
			});
		}
		
		function _bind_next_button(){
			$("#city-box-select > select").children("option").unbind("dblclick").dblclick(function(){
				if ($("#city-box-select > select").children("option:selected").size())
				switch (sectorType){
					case "city": 
						if (inputType == "val")
							inputObject.val($("#city-box-select > select").children("option:selected").html());
						else {
							inputObject.html("Украина, "+$("#city-box-select > select").children("option:selected").html());
							WriteCity(inputObject.html());
							$("#search-city").val($("#city-box-select > select").children("option:selected").html());
						}
						_hide_cityBox();
						break;
					case "region": 
						sectorType = "city";
						sectorID = $("#city-box-select > select").val();	
						_load_current_sector();						
						break;
					case "country": 
						sectorType = "region";
						sectorID = $("#city-box-select > select").val();
						_load_current_sector();
						break;
				}
				return false;
			});
		}
		
		function WriteCity(CityName){
			$.ajax({
				url: "/index.php",
				type: "get",
				data: "cityname="+CityName
			});
		}
	
		function _go_back_cityBox(){
				if (sectorType == "city"){
					sectorType = "region";
					sectorID = 1;
					_load_current_sector();
				} else _hide_cityBox();
		}
	
		return this.unbind("click").click(_show_cityBox);
	};
	
	$.fn.cityBoxMoscow = function(inputObject){
		
		function _set_value(){
			$(inputObject).val('Киев');
			return false
		}
		
		return this.unbind("click").click(_set_value);
	};
	
})(jQuery);

