RegionSelector = function(woj_id,pow_id,gmn_id){
	this.woj_id=parseInt(woj_id);
	this.pow_id=parseInt(pow_id);
	this.gmn_id=parseInt(gmn_id);
	
	this.woj_tab={
		1:'dolnośląskie',
		2:'kujawsko-pomorskie',
		3:'lubelskie',
		4:'lubuskie',
		5:'łódzkie',
		6:'małopolskie',
		7:'mazowieckie',
		8:'opolskie',
		9:'podkarpackie',
		10:'podlaskie',
		11:'pomorskie',
		12:'śląskie',
		13:'świętokrzyskie',
		14:'warmińsko-mazurskie',
		15:'wielkopolskie',
		16:'zachodniopomorskie'
	}	
	this.pow_tab=new Array();
	this.gmn_tab=new Array();
	
	this.init=function(){
		this.addOptions('woj',this.woj_tab,this.woj_id,'-- Wybierz województwo --');
		if(parseInt($('#woj_id').val())>0){
			is_pow=this.updatePow(parseInt($('#woj_id').val()));
			if(is_pow && parseInt($('#pow_id').val())>0){
				this.updateGmn(parseInt($('#pow_id').val()));
			}
		}		
	}
	this.getTable=function(typ,id){
		if(typ=='pow'){
			if(this.pow_tab[id]==undefined){
				this.addLoading('pow');
				this.pow_tab[id]=this.getAjaxTable(typ,id);
			}
			return this.pow_tab[id];						
		}else if(typ=='gmn'){
			if(this.gmn_tab[id]==undefined){
				this.addLoading('gmn');
				this.gmn_tab[id]=this.getAjaxTable(typ,id);
			}
			return this.gmn_tab[id];			
		}
		return undefined;
	}
	this.getAjaxTable=function(typ,id){
		$.ajaxSetup({async:false,cache: false,global:false});
 		$.getScript( '/region,js.html?typ='+typ+'&id='+id);
		return tmp;
	}	
	this.onWojChange=function(woj_id){
		this.hide('gmn');
		if (woj_id == 0) {
			this.hide('pow');
		} else {
			this.updatePow(woj_id);
		}
	}
	this.onPowChange=function(pow_id){
		if (pow_id == 0) {
			this.hide('gmn');
		} else {
			this.updateGmn(pow_id);
		}
	}
	this.updatePow=function(woj_id){		
		var options=this.getTable('pow',woj_id);
		if (options == undefined) {
			this.hide('pow');
			return false;
		} else {
			this.addOptions('pow', options, this.pow_id, '-- Wybierz powiat --');
			return true;					
		}
	}
	this.updateGmn=function(pow_id){		
		var options=this.getTable('gmn',pow_id);
		if(options==-1){
			this.hide('gmn');
			$('#gmn_id').val('-1');
		}else if (options == undefined) {
			this.hide('gmn');
		} else {
			this.addOptions('gmn', options, this.gmn_id, '-- Wybierz gminę --');					
		}
	}
	this.hide=function(typ){
		$('#'+typ+'_block').children('select').children().remove();
		$('#'+typ+'_block').hide();
	}
	this.show=function(typ){
		$('#'+typ+'_block').show();
	}
	this.addLoading=function(typ){
		var selector=$('#'+typ+'_id');
		selector.children().remove();
		selector.append('<option value="0" selected="selected">Loading...</option>');
		this.show(typ);
	}
	this.addOptions = function(typ, options, selected, null_info){
		var selector=$('#'+typ+'_id');
		selector.children().remove();
		if(null_info!='') selector.append('<option value="0">' + null_info + '</option>');
		for (i in options) {
			if (i == selected) {
				selector.append('<option value="' + i + '" selected="selected">' + options[i] + '</option>');
			}
			else {
				selector.append('<option value="' + i + '">' + options[i] + '</option>');
			}
		}	
		this.show(typ);	
	}
}
