var CMToggle = new Class({
	Implements : Options,
	
	options : {
		root : null, //element racine (conteneur des items)
		checkBeforeToggle : function(currentItem, clickedItem) {return true}
	},
	
	items : [],
	currentItem : null, //reference sur l'item en cours
		
	initialize : function(options) {
		this.setOptions(options);
		this.root = $(this.options.root);
		if(this.root.hasClass('noToggle')) {
			this.noToggle = true;
		}
		this.items = this.root.getElements('li.item');
		this._initItems();
	},
	
	_initItems : function() {
		this.items = this.items.map(function(domItem, index) {
			var item = new CMToggle.Item({root:domItem, index:index});
			if (item.opened) 
				this.currentItem = item;
			item.title.addEvent('click', this._itemClickHandler.bindWithEvent(this, [item]));
			return item;
		}.bind(this));
	},
	
	_itemClickHandler : function(event,item) {
		new Event(event).stop();
		if(!this.noToggle) {
			if (item == this.currentItem) return;
			if(!this.options.checkBeforeToggle(this.currentItem, item))
				return;
		}
		this.toggle(item);
	},
	
	toggle : function(item){
		if(this.noToggle) {
			if(item.opened)
				item.close();
			else 
				item.open();
		} else {
			if (this.currentItem) this.currentItem.close();
			item.open();
			this.currentItem = item;
		}
	}
})

CMToggle.Item = new Class({
	Implements : Options,
	options : {
		root : null,
		duration : 600,
		index : 0 //item index in the toggle
	},
	
	fx : null, //effet
	root : null,
	title : null,
	content : null,
	index : 0,
	opened : false,
	
	initialize : function(options) {
		this.setOptions(options);
		this.root = $(this.options.root);
		this.index = this.options.index;
		this.title = this.root.getElement('.item-title');
		this.content = this.root.getElement('.item-content');
		if (this.root.hasClass('itemOpened')) {
			this.opened = true;
		}
		this._initFx();
	},
	
	_initFx : function() {
		this.fxDiv = new Element('div', {
			styles : {
				overflow : 'hidden',
				zoom : 1,
				position : 'relative'
			}
		}).inject(this.content, 'after');
		this.fxDiv.adopt(this.content);
		//this.fxDiv.setStyle('margin', this.content.getStyle('margin'));
		this.content.setStyle('display', 'block');
		this.content.setStyles({
				'margin-top' : this.opened?0:-this.content.scrollHeight,
				'opacity' : this.opened?1:0
		});
		
		this.fx = new Fx.Morph(this.content, {
			duration : this.options.duration,
			onComplete : this._fxCompleteHandler.bind(this),
			onStart : this._fxStartHandler.bind(this)
		});
		
	},
	
	_fxStartHandler : function() {
		if (this.opened) {
			this.root.addClass('itemOpened');
		} else {
			this.root.removeClass('itemOpened');
		}
	},
	
	_fxCompleteHandler : function() {
		Notifier.fireEvent("onLayoutChanged");
	},
	
	open : function() {
		this.fx.cancel();
		this.opened = true;
		this.fx.start({
			'margin-top': 0,
			'opacity' : 1
		});
	},
	
	close : function() {
		this.fx.cancel();
		this.opened = false;
		this.fx.start({
			'margin-top':-this.content.offsetHeight,
			'opacity':0
		})
	}
})

function addLocationFilter(region, country, lower, upper) {
    /*
	
	// trouve sur clubmed.fr
	
	if(!loaded) {
        connect(document, "loaded_document", addLocationFilter);
        document.locationFilter = {
            "region": region,
            "country": country,
            "lower": lower,
            "upper": upper
        };
        return;
    }
    // MSIE fires the flash before the page is loaded...
    var f = new Filter(recent_filter);

    location_filter = FilterBy.location(region, country, parseInt(lower, 10), parseInt(upper,10));

    if(current_village_list) { // (0 != searchform.departure.day.value) && (0 != searchform.departure.month.value)) {
        //window.loader.style.display = 'block';
        $('chargement').style.displat='block';
        f.addPredicate(location_filter);

        var v = f.apply(villages);
        if(getCurrentDate()) {
            prices_collection.load({'DATEDEP':getIsoCurrentDate(), 'duration':0, 'delta':0, 'transport': ($('with-transport').checked?"true":"false")}, displayVillages, v);
        } else {
            prices_collection.load({'transport': ($('with-transport').checked?"true":"false")}, displayVillages, v);
        }
        //displayVillages(v);
    }
	*/
}
