function toggleSearch(wrapper_id,button){
	var wrapper = $(wrapper_id);
	if(wrapper.hasClass('opened')){
		$$('.accordion').each(function(element){
			element.removeClass('opened');
			element.removeClass('closed');
		});
		wrapper.addClass('closed');
	} else {
		$$('.accordion').each(function(element){
			element.removeClass('opened');
			element.removeClass('closed');
		});
		wrapper.addClass('opened');
	}
}


/**
 * A Class containing a list of SearchBoxes 
 */
var SearchBar = new Class({
	Implements: [Options,Events],
	options: {
		baseUrl:"/clubs/ajax_load_list/"
	},
	initialize: function(options){
		this.setOptions(options);
		this.boxes = [new SearchBox('search_france',0,this,this.options.baseUrl+'france'),new SearchBox('search_foreign',1,this,this.options.baseUrl+'foreign'),new SearchBox('search_location',2,this,this.options.baseUrl+'location')];
	},
	open : function(index){
		this.closeAll();
		this.boxes[index].open();
	},
	close : function(index){
		this.closeAll();
	},
	closeAll : function(){
		this.boxes.each(function(box){
			box.close();
		});
	}
});


var SearchBox = new Class({
	Implements: [Events],
	initialize: function(id,index,parent,request_url){
		this.element = $(id);
		this.button = this.element.getChildren('input.open');
		this.parent = parent;
		this.index = index;
		this.firstopened = true;
		//event to close
		this.button.addEvent('click',function(E){
			this.fireClick();
		}.bind(this));
		
		//request the form on open
		this.request = new Request({
			'method': 'get',
			'url': request_url,
			'async': false,
			'onRequest':this.showLoader.bind(this),
			'onSuccess': function(textResponse){
				this.setForm(textResponse);
			}.bind(this)
		});
		
		//request for updating filters
		this.updateRequest = new Request({
			'method': 'post',
			'url': request_url,
			'async': false,
			'onRequest':this.showLoader.bind(this),
			'onSuccess': function(textResponse){
				this.setForm(textResponse);
			}.bind(this)
		}); 
		
	},
	open : function(){
		this.element.removeClass('closed');
		this.element.addClass('opened');
		this.button.set('value',"REFERMER");
		if(this.firstopened){
			this.request.send();
		}
		this.firstopened = false;
	},
	close : function(){
		this.element.removeClass('opened');
		this.element.addClass('closed');
		this.button.set('value',"RECHERCHER");
	},
	fireClick : function(){
		if(this.element.hasClass('opened')){
			this.parent.close(this.index);
		} else {
			this.parent.open(this.index);
		}
	},
	setForm : function(formHtml){
		this.hideLoader();
		this.element.getElement('.filters_wrapper').innerHTML = formHtml;
		this.addSelectEvents();
	},
	showLoader : function(){
		this.element.getElement('.loader').setStyle('display','block');
	},
	hideLoader : function(){
		this.element.getElement('.loader').setStyle('display','none');
	},
	addSelectEvents : function (){
		this.element.getElements('select').each(function(select){
			select.addEvent('change',function(E){
				this.updateRequest.post(this.element.getElement('form'));
			}.bind(this));
		}.bind(this));
	},
	reload : function(){
		
	}
});

var ClubMap = new Class({
	Implements: [Options, Events],
	options: {
		mapId:"map",
		lat:48.884191,
		lng:2.294045,
		zoom:18
	},
	initialize: function(options){
		this.setOptions(options);
		this.mapDiv = $(this.options.mapId);
		var latlng = new google.maps.LatLng(this.options.lat,this.options.lng);
		var myOptions = {
			scrollwheel: false,
			zoom: this.options.zoom,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.HYBRID
		};
		this.map = new google.maps.Map(this.mapDiv,myOptions);
	}
});



