var clubSearch = Class.create( );
clubSearch.prototype = {
	busy : false,
	current_state : 0,
	current_city  : 0,
	current_club  : 0,
	
	state_index   : 0,
	city_index    : 0,
	
	city_open : false,
	club_open : false,
	data      : null,
	tmp       : '',
	initialize : function( ) {
		this.data = { };
		Event.observe( window , 'load' , this.start.bind( this ) );
	},
	
	start : function( ) {
		Event.observe( $( 'club-state' ) , 'click' , this.clickState.bind( this ) );
  		Event.observe( $( 'club-state' ) , 'change' , this.clickState.bind( this ) );
  		Event.observe( $( 'club-city' ) , 'click' , this.clickCity.bind( this ) );
  		Event.observe( $( 'club-city' ) , 'change' , this.clickCity.bind( this ) );
  		Event.observe( $( 'club-club' ) , 'click' , this.clickClub.bind( this ) );
  		Event.observe( $( 'club-club' ) , 'change' , this.clickClub.bind( this ) );
  		Event.observe( $( 'club-btn' ) , 'click' , this.showClub.bind( this ) );
  		$( 'club-state' ).disabled = false;
	},
	
	hide : function( what ) {
		if( this[what+'_open'] ) {
			$( 'club-'+what ).disabled = true;
			this[what+'_open']    = false;
			this['current_'+what] = 0;
			RC( 'club-'+what );
			$( 'club-'+what ).innerHTML = '<option selected="selected"> -- wybierz -- </option>';
			if( what == 'club' ) {
				$( 'club-btn' ).hide( );
				$( 'club-btn-off' ).show( );
			}
		}
	},
	
	getState : function( ) {
		this.busy = true;
		$( 'club-state-loader' ).show( )
		if( this.data['state_'+this.current_state] ) {
			RC( $( 'club-city' ) );
			var o = DC( 'option' );
			o.value = 0;
			App( o , Txt( ' -- wybierz -- ' ) );
			App( $('club-city') , o );
			this.data['state_'+this.current_state].each( function( item ) {
				sub = item.show_substate ? ' ('+item.substate+')' : '';
				var o = DC( 'option' );
  			o.value = item.city_id;
  			App( o , Txt( item.name + sub ) );
  			App( $('club-city') , o );
			} );
			$('club-city').selectedIndex = 0;
			$( 'club-city' ).disabled = false;
			$( 'club-state-loader' ).hide( );
			this.city_open = true;
			this.busy = false;
		} else {
			ajax.addPost( 'cmd' , 'get-club-cities' );
			ajax.addPost( 'state-id' , this.current_state );
			ajax.json( '/json/klub' , function( json ) {
				if( ! json ) {
					$( 'club-state-loader' ).hide( );
					$('club-state').selectedIndex = 0;
					CS.busy = false;
					Notice.error( 'Wystąpił błąd wewnętrzny serwisu. Spróbuj za chwile ponownie.' , 3 );
					return;
				}
				CS.data['state_'+CS.current_state] = json;
				CS.getState( );
			} );
		}
	},
	
	clickState : function( ) {
		if( this.busy ) {
			return;
		}
		try {
			var s = parseInt( $F( 'club-state' ) );
		} catch( e ) { var s = 0; }
		if( s == this.current_state ) {
			return;
		}
		this.hide( 'city' );
		this.hide( 'club' );
		this.current_state = s;
		if( s == 0 ) {
			return;
		}
		this.getState( );
	},
	
	getCity : function( ) {
		this.busy = true;
		$( 'club-city-loader' ).show( )
		if( this.data['city_'+this.current_city] ) {
			RC( $( 'club-club' ) );
			var o = DC( 'option' );
			o.value = 0;
			App( o , Txt( ' -- wybierz -- ' ) );
			App( $('club-club') , o );
			this.data['city_'+this.current_city].each( function( item ) {
				var o = DC( 'option' );
  			o.value = item.club_id;
  			App( o , Txt( item.name ) );
  			App( $('club-club') , o );
			} );
			$('club-club').selectedIndex = 0;
			$( 'club-club' ).disabled = false;
			$( 'club-city-loader' ).hide( );
			this.club_open = true;
			this.busy = false;
		} else {
			ajax.addPost( 'cmd' , 'get-clubs' );
			ajax.addPost( 'city-id' , this.current_city );
			ajax.json( '/json/klub' , function( json ) {
				if( ! json ) {
					$( 'club-city-loader' ).hide( );
					$('club-city').selectedIndex = 0;
					CS.busy = false;
					Notice.error( 'Wystąpił błąd wewnętrzny serwisu. Spróbuj za chwile ponownie.' , 3 );
					return;
				}
				CS.data['city_'+CS.current_city] = json;
				CS.getCity( );
			} );
		}
	},
	
	clickCity : function( ) {
		if( this.busy || ! this.city_open ) {
			return;
		}
		try {
			var c = parseInt( $F( 'club-city' ) );
		} catch( e ) { var c = 0; }
		if( this.current_city == c ) {
			return;
		}
		this.hide( 'club' );
		this.current_city = c;
		this.getCity( );
	},
	
	clickClub : function( ) {
		if( this.busy || ! this.city_open ) {
			return;
		}
		try {
			var c = parseInt( $F( 'club-club' ) );
		} catch( e ) { var c = 0; }
		if( this.current_club == c ) {
			return;
		}
		this.current_club = c;
		if( c > 0 ) {
			$( 'club-btn-off' ).hide( );
			$( 'club-btn' ).show( );
		} else {
			$( 'club-btn' ).hide( );
			$( 'club-btn-off' ).show( );
		}
	},
	
	showClub  : function( ) {
		if( ! this.busy && this.club_open && this.current_club > 0 ) {
			window.location = '/klub/'+this.current_club;
		}
		return false;
	}
	
};
var CS = new clubSearch( );