/**
 * Location
 * @version 1.0.1
 */
var Location = function(options)
{
	// Public
	Location.prototype.init		= location_init;
	Location.prototype.hide		= location_hide;
	Location.prototype.update	= location_update;
	Location.prototype.choose	= location_choose;
	Location.prototype.country	= location_country;
	Location.prototype.region	= location_region;
	Location.prototype.town		= location_town;

	// Private
	Location.prototype.timer	= location_timer;
	Location.prototype.fill		= location_fill;
	Location.prototype.process	= location_process;
	Location.prototype.load_country	= location_load_country;
	Location.prototype.load_region	= location_load_region;
	Location.prototype.search	= location_search;
	Location.prototype.regions	= location_regions;
	Location.prototype.towns	= location_towns;
	Location.prototype.action	= location_action;
	Location.prototype.clear	= location_clear;

	this.init(options);
}

function location_init(options)
{
	this.options = options;
	this.text = "";

	this.elements = {};
	this.elements['country'] = $("#country");
	this.elements['region'] = $("#region");
	this.elements['town'] = $("#town");

	if (typeof this.options['actions'] == "undefined")
		this.actions = {};
	else
		this.actions = this.options['actions'];

	if (this.elements['town'][0].tagName == "INPUT")
	{
		if (typeof this.options['search_url'] == "undefined")
			throw "Need search_url option";
		this.options['search_auto'] = true;
	}
	else
	{
		if (typeof this.options['towns_url'] == "undefined")
			throw "Need towns_url option";
		this.options['search_auto'] = false;
	}

	var init = function(instance)
	{
		instance.elements['country'].bind("change", function()
		{
			instance.load_country();

			instance.region("");
			instance.town("");
		});

		instance.elements['region'].bind("change", function()
		{
			instance.load_region();

			instance.town("");
		});

		instance.elements['town'].bind("change", function()
		{
			instance.action("town_change", $(this).val());
		});

		var country = instance.country();
		if (country != "")
			instance.action("country_change", country);
		else
			instance.elements['region'].attr("disabled", "disabled");

		var region = instance.region();
		if (region != "")
			instance.action("region_change", region);
		else
			instance.elements['town'].attr("disabled", "disabled");

		if (instance.options['search_auto'])
		{
			$("#location_close a").bind("click", function()
			{
				instance.hide();
				return false;
			});

			instance.elements['town'].attr("autocomplete", "off");
			instance.timer();
		}
		else
		{
			var town = instance.town();
			if (town != "")
				instance.action("town_change", town);
		}
	};

	init(this);
}

function location_timer()
{
	var set = function(instance)
	{
		setTimeout(function() {instance.process();}, 100);
	};

	set(this);
}

function location_action(action, param)
{
	if (typeof this.actions[action] != "function")
		return;

	this.actions[action](param);
}

function location_fill(data)
{
	data = trim(data);
	if (data == "")
	{
		this.hide();
		return;
	}

	$("#location_list").html(data);

	var init = function(instance)
	{
		$("#location_list a").bind("click", function()
		{
			var country = $(this).attr("country");
			var region = $(this).attr("region");
			var name = $(this).text();

			instance.choose(country, region, name);
			return false;
		});
	};

	init(this);

	this.update();
}

function location_process()
{
	var text = this.town();
	if (text == "" || text.length < 2)
	{
		this.text = text;
		this.timer();
		this.hide();
		return;
	}

	if (text != this.text)
	{
		this.text = text;
		this.search(text);
	}

	this.timer();
}

function location_hide()
{
	$("#location_form").hide();
}

function location_update()
{
	var location_form = $("#location_form");
	var location_close = $("#location_close");
	var town_field = this.elements['town'];

	location_form.show();
	location_form.css("top", town_field.offset().top + town_field[0].offsetHeight - 1 - location_close.height());
	location_form.css("left", town_field.offset().left);
}

function location_load_country()
{
	var region = this.elements['region'];
	this.clear(region);

	var country = this.country();
	if (country != "")
	{
		var xml = this.regions(country);
		inputs_fill_options(region, xml);
	}

	this.action("country_change", country);
}

function location_load_region()
{
	var region = this.region();

	if (this.options['search_auto'])
	{
		this.action("region_change", region);
		return;
	}

	var town = this.elements['town'];
	this.clear(town);

	if (region != "")
	{
		var xml = this.towns(region);
		inputs_fill_options(town, xml);
	}

	this.action("region_change", region);
}

function location_choose(country, region, town)
{
	this.country(country);

	if (typeof region == "undefined" || region == "")
		return;
	this.region(region);

	if (typeof town == "undefined" || town == "")
		return;
	this.town(town);

	this.text = town;
	this.hide();
}

function location_country(country)
{
	if (typeof country == "undefined")
		return this.elements['country'].val();

	this.elements['country'].val(country);

	this.load_country();
	return true;
}

function location_region(region)
{
	if (typeof region == "undefined")
		return this.elements['region'].val();

	this.elements['region'].val(region);

	this.load_region();
	return true;
}

function location_town(town)
{
	if (typeof town == "undefined")
		return this.elements['town'].val();

	this.elements['town'].val(town);

	this.action("town_change", town);
	return true;
}

function location_clear(element)
{
	element.children(":gt(0)").remove();
	element.attr("disabled", "disabled");
}

function location_search(town)
{
	var send = function(instance)
	{
		$.post(instance.options['search_url'],
			{
				'country':	instance.country(),
				'region':	instance.region(),
				'town':		town
			},
			function(data)
			{
				instance.fill(data);
			},
			"html"
		);
	};

	send(this);
}

function location_regions(country)
{
	return ajax_send_post(this.options['regions_url'], {'country_id': country});
}

function location_towns(region)
{
	return ajax_send_post(this.options['towns_url'], {'region_id': region});
}
