// JavaScript Document

var MenuBresse = new Class({
	Implements: Options,
	page:null,
	options: {
		elements:{
			particuliers:{
				header:null,
				content:null
			},
			entreprises:{
				header:null,
				content:null
			}
		},
		enableHeaderClick:false
	},
	openedTab:null,
	closedTab:null,
	
	initialize: function(page, options) {
		if(page!="general") this.page = page;
			this.setOptions(options);
	   	this.setDefaultOptions();
	   	this.init();
   	},
	
	setDefaultOptions: function() {
		if(!this.options.elements.particuliers.header) this.options.elements.particuliers.header = $('tab_header_particuliers');
		if(!this.options.elements.particuliers.content) this.options.elements.particuliers.content = $('tab_content_particuliers');
		if(!this.options.elements.entreprises.header) this.options.elements.entreprises.header = $('tab_header_entreprises');
		if(!this.options.elements.entreprises.content) this.options.elements.entreprises.content = $('tab_content_entreprises');		
	},
	
	init:function () {
		for(var category in this.options.elements) {
			this.options.elements[category].header.addEvents({
				'mouseenter': this.headerMouseEnter.bind(this)
			});
			
			if(!this.options.enableHeaderClick) this.options.elements[category].header.getFirst().removeProperty('href');
			this.options.elements[category].header.addClass('off')
			this.options.elements[category].content.addClass(['hidden', 'off']);
			this.options.elements[category].content.store('tweenHeight', new Fx.Tween(this.options.elements[category].content, {duration:300, transition:Fx.Transitions.Expo.easeOut}));
			
		}
		
		if(this.page) {
			this.options.elements[this.page].header.fireEvent('mouseenter', {target:this.options.elements[this.page].header.getFirst(), skipTween:true});
			
		}
	},
	
	headerMouseLeave:function(event) {
		this.openedTab.header.removeEvents().addEvent('mouseenter', this.headerMouseEnter.bind(this));
		if(this.page) {
			this.options.elements[this.page].header.fireEvent('mouseenter', {target:this.options.elements[this.page].header.getFirst(), fired:true});
		} else {
			//this.openedTab.content.retrieve('tweenHeight').cancel().start('height', 0);
			this.openedTab.header.addClass('off');	
			//this.openedTab.content.addClass('off');	
		}
		
		$('menu').removeEvents();
		
	},
	
	
	headerMouseEnter:function(event) {
		
			var skipTween = false;
			if(this.openedTab) {
				if(this.openedTab.content.getStyle('height').toInt()==150) skipTween = true;
				if(this.openedTab.content.getStyle('height').toInt()>0) {
					this.closedTab.content.setStyle('height', this.openedTab.content.getStyle('height').toInt());
					this.openedTab.content.setStyles({
						display	: 'none',
						height	: 0
					});
					this.openedTab.content.retrieve('tweenHeight').cancel();
				}
			}
			
			if($(event.target).getParent() == this.options.elements.particuliers.header) {
				this.openedTab = this.options.elements.particuliers;
				this.closedTab = this.options.elements.entreprises;
			} else {
				this.openedTab = this.options.elements.entreprises;
				this.closedTab = this.options.elements.particuliers;
			}
			
			if(this.page) {
				this.openedTab.content.setStyles({
					display	: 'block',
					height	: 150
				});
			} else {
				/*
				if(skipTween) {
					this.closedTab.content.retrieve('tweenHeight').cancel();
					this.closedTab.content.setStyle('display', 'none');
					this.openedTab.content.setStyles({
						display	: 'block',
						height	: 126
					});
				} else {
					this.openedTab.content.setStyle('display', 'block').retrieve('tweenHeight').cancel().start('height', 126);
					this.closedTab.content.retrieve('tweenHeight').cancel().start('height', 0);
				}
				*/
			}
			
			this.openedTab.content.removeClass('off');
			this.openedTab.header.removeEvents().removeClass('off');
			
			this.closedTab.header.addClass('off').removeEvents().addEvent('mouseenter', this.headerMouseEnter.bind(this));
			//this.closedTab.content.addClass('off');
			
			$('menu').removeEvents().addEvent('mouseleave', this.headerMouseLeave.bind(this));
	
		
	}
	
});