var initDOM = false;

$(document).ready(function()
{
	//Enable all functions
    initDOM = true;
});

function country_control($country, $city, country_id)
{
	if (!initDOM)
		return;
	
	if (country_id > 0)
	{
		classBlue($country);
		get_input($country).val($country.data('temp_value'));
	}
	else
	{
		classGrey($country);
		get_input($country).val($country.data('default_value'));
	}
	
	get_hidden_input($country).val(country_id);
	get_sub_div($country)
		.bind("mouseenter", {obj: $country}, input_mouseenter)
		.bind("mouseleave", {obj: $country}, input_mouseleave);
	get_input($country)
		.bind("click", {obj: $country}, input_focus_dropdown)
		.autocomplete(stat_data.countries, 
		{
			minChars: 0, 
			isDropDown: true,
			$addField: get_hidden_input($country),
			$arrow: get_arrow($country),
			parse: parse_object,
			highlight: no_highlight,
			formatItem: function(item) 
			{
				return item.name;
			}
		})
		.result(function(e, item)
		{
			$country.data('is_input_modifed', false);
			if (item.id == 0)
			{
				classGrey($country);
				get_input($country).val($country.data('default_value'));
				
				classGrey($city);
				get_input($city).val($city.data('default_value'));
				
				disable_children($country);
			}
			else
			{
				classBlue($country);
				classGrey($city);
				get_input($city)
					.val($city.data('default_value'))
					.setOptions({
						extraParams: {
							'page': 'Geo',
							'action': 'get_city',
							'country_id': get_hidden_input($country).val()
						}
					})
					.flushCache();
				enable_children($country);
			}
		});
}

function city_control($city, options)
{
	if (!initDOM)
		return;
		
	if (options.city_id > 0)
	{
		classBlue($city);
		get_input($city).val($city.data('temp_value'));
	}
	else
	{
		classGrey($city);
		get_arrow($city).hide();
		get_input($city)
			.val($city.data('default_value'))
			.attr("disabled", "true");
	}
	
	get_hidden_input($city).val(options.city_id);
	get_sub_div($city)
		.bind("mouseenter", {obj: $city}, input_mouseenter)
		.bind("mouseleave", {obj: $city}, input_mouseleave);
	get_input($city)
		.bind("focus", {obj: $city}, input_focus)
		.bind("blur", {obj: $city}, input_blur)
		.autocomplete("processing.php", 
		{
			minChars: 0, 
			isDropDown: true,
			$addField: get_hidden_input($city),
			$arrow: get_arrow($city),
			$ajaxLoader: options.popup_ajax,
			dataType: "json",
			extraParams:
			{
				'page': 'Geo',
				'action': 'get_city',
				'country_id': get_hidden_input($city.data('country')).val()
			},
			parse: parse_object,
			//highlight: no_highlight,
			formatItem: function(item) 
			{
				var result = item.name;
				if (item.short_name)
					result += "<br/><small>"+item.short_name+"</small>";
				return result;
			}
		})
		.result(function(e, item)
		{
			$city.data('is_input_modifed', false);
		});
}

function dropdown_control($obj, options)
{
	if (!initDOM)
		return;
	
	// default options
	var defaults = {
		elem_id: 0,
		disable_if_zero: false,
		autocomplete: {
			data: null,
			url: '',
			dataType: 'json',
			parse: parse_object,
			minChars: 0,
			isDropDown: true,
			ajax_loader: null,
			extraParams: {}
		}
	};
	//merge
	var settings = $.extend({}, defaults, options);
	
	if (settings.elem_id > 0)
	{
		classBlue($obj);
		get_input($obj).val($obj.data('temp_value'));
	}
	else
	{
		classGrey($obj);
		get_arrow($obj).hide();
		get_input($obj)
			.val($obj.data('default_value'));
		if (settings.disable_if_zero)
			get_input($obj).attr("disabled", "true");
	}
	
	get_hidden_input($obj).val(settings.elem_id);
	get_sub_div($obj)
		.bind("mouseenter", {obj: $obj}, input_mouseenter)
		.bind("mouseleave", {obj: $obj}, input_mouseleave);
	get_input($obj)
		.bind("click", {obj: $obj}, input_focus_dropdown)
		.autocomplete("processing.php", 
		{
			minChars: settings.autocomplete.minChars, 
			isDropDown: settings.autocomplete.isDropDown,
			$addField: get_hidden_input($obj),
			$arrow: get_arrow($obj),
			$ajaxLoader: settings.autocomplete.ajax_loader,
			dataType: settings.autocomplete.dataType,
			extraParams: settings.autocomplete.extraParams,
			parse: parse_object,
			formatItem: function(item) 
			{
				var result = item.name;
				if (item.short_name)
					result += "<br/><small>"+item.short_name+"</small>";
				return result;
			}
		})
		.result(function(e, item)
		{
			$home_city.data('is_input_modifed', false);
		});
}









