function Location(){
	this.items = [];
	
	this.ptr = [];
	this.root = [];
}

Location.prototype.init = function(){
	for (var i=0,len=this.items.length;i<len;i++){
		var a = this.items[i];
		this.ptr[a.id] = a;
	}
}

Location.prototype.getName = function(id){
	if (this.ptr[id]) return this.ptr[id].name;
	return null;
}

Location.prototype.getCities = function(parent){
	if (!parent) parent = 0;
	
	var arr = new Array();
	for (var i=0,len=this.items.length;i<len;i++){
		var a = this.items[i];
		if (a.type==2&&(a.parent==parent||parent==0)) arr[arr.length] = a;
	}
	return arr;
}

Location.prototype.getChildren = function(parent,online){
	if (!parent) parent = 0;
	if (!online) online = false;
	
	var arr = new Array();
	for (var i=0,len=this.items.length;i<len;i++){
		var a = this.items[i];
		if (parent==a.parent&&(!online||(online&&a.active>0))) arr[arr.length] = a;
	}
	return arr;
}

Location.prototype.findCities = function(str,country,state){
	if (!country) country = 0;
	if (!state) state = 0;
	
	var arr = new Array();
	for (var i=0,len=this.items.length;i<len;i++){
		var a = this.items[i];
		if (a.type==2&&(country==0||country==a.root)&&(state==0||state==a.parent)
			&&(str.toLowerCase()==a.name.substring(0,str.length).toLowerCase())) arr[arr.length] = a;
	}
	return arr;
}

var Location = new Location();

(function($){
	
	$.fn.fillLocations = function(parent,online){
		if (!parent) parent = 0;
		if (!online) online = false;
		
		return this.each(function(){
			var $this = $(this);
			if ($this.get(0).tagName!='SELECT') return false;
			var shift = $this.get(0).options[0]&&!parseInt($this.get(0).options[0].value)?1:0;
			for (var i=$this.get(0).options.length;i>=0+shift;i--) $this.get(0).options[i] = null;
			var arr = Location.getChildren(parent,online);
			for (var i=0;i<arr.length;i++) $this.get(0).options[i+shift] = new Option(arr[i].name,arr[i].id);
		})		
	}
	
	$.fn.fillCities = function(){
		return this.each(function(){
			var $this = $(this);
			if ($this.get(0).tagName!='SELECT') return false;
			var shift = $this.get(0).options[0]&&!parseInt($this.get(0).options[0].value)?1:0;
			for (var i=$this.get(0).options.length;i>=0+shift;i--) $this.get(0).options[i] = null;
			
			var arr = Location.getCities();
			for (var i=0;i<arr.length;i++) 
				$this.get(0).options[i+shift] = new Option(arr[i].name+' ('+Location.getName(arr[i].root)+')',arr[i].id);
		})
	}
	
	$.fn.clearLocations = function(){
		return this.each(function(){
			var $this = $(this);
			if ($this.get(0).tagName!='SELECT') return false;
			var shift = $this.get(0).options[0]&&!parseInt($this.get(0).options[0].value)?1:0;
			for (var i=$this.get(0).options.length;i>=0+shift;i--) $this.get(0).options[i] = null;
		})		
	}
	
	$.fn.findCities = function(c,s,fn){
		if (!c) c = 0;
		if (!s) s = 0;
		
		return this.each(function(){
			$this = $(this);
			$('#cityList').remove();
			var arr = Location.findCities($this.val(),c,s);
			if (!arr.length||$this.val()=='') return false;
			$('body').append('<div id="cityList" class="input-list"></div>')
			var $list = $('#cityList').css({'left':$this.offset().left+'px',
				'top':($this.offset().top+$this.height()+2)+'px'});
			var h = '<ul>';
			for (var i=0,len=arr.length;i<len;i++){
				var b = arr[i];
				var _c = Location.ptr[b.root].name;
				var _s = Location.ptr[b.parent].name;
				h += '<li value="'+b.id+'"><input type="hidden" name="state" value="'+b.parent+'" />';
				h += '<input type="hidden" name="country" value="'+b.root+'" /><b>'+b.name+'</b>';
				h += '<em>'+_c+'</em><small>'+_s+'</small><div class="clear"></div></li>';
			}
			h += '</ul>';
			$list.html(h).show();
			$list.hover(function(){
				
			},function(){
				$list.remove();
			})
			$list.find('li').click(function(){
				var c = $(this).find('input[name="country"]').val();
				var s = $(this).find('input[name="state"]').val();
				$list.remove();
				if (fn&&typeof fn=='function') fn.call(this, {country:c, state:s, 
					id:$(this).attr('value'), name:$(this).find('b').text()});
			})
		})
	}
	
})(jQuery);

