/**************************************************************

	Script	: Image Menu
	Version	: 2.2
	Authors	: Samuel Birch
	Desc	: 
	Licence	: Open Source MIT Licence

**************************************************************/

var ImageMenu = new Class({
	
	getOptions: function(){
		return {
			onOpen: false,
			onClose: Class.empty,
			openWidth: 200,
			transition: Fx.Transitions.quadOut,
			duration: 200,
			open: null,
			border: 0
		};
	},

	initialize: function(elements, elements2, options){
		this.setOptions(this.getOptions(), options);
		
		this.elements = $$(elements);
		this.elements2 = $$(elements2);
		
		//this.options.timer2 = {};		
		this.widths = {};
		this.widths.closed = this.elements[0].getStyle('width').toInt();
		this.widths.openSelected = this.options.openWidth;
		this.widths.openOthers = Math.round(((this.widths.closed*this.elements.length) - (this.widths.openSelected+this.options.border)) / (this.elements.length-1))
		
		
		this.fx = new Fx.Elements(this.elements, {wait: false, duration: this.options.duration, transition: this.options.transition});

			var IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)  
 && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);
		
		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();								
				this.options.open = i;
				this.reset(i);
				
				this.elements2.each(function(el,i){ el.setStyle('display', 'none'); }.bind(this));
				
				this.elements2[i].setStyle('display', 'block');
				$clear(this.options.timer);
				
			}.bind(this));
			
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();				
				this.options.timer = (function(i){ this.elements2[i].setStyle('display', 'none'); this.options.open = null; this.reset(this.options.open); }).delay(1, this, i);
				
			}.bind(this));

		}.bind(this));
		
		this.elements2.each(function(el,i){
		
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();
				this.reset(i);
				$clear(this.options.timer);				

			}.bind(this));
			
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();
				this.options.timer = (function(i){ this.elements2[i].setStyle('display', 'none'); this.options.open = null; this.reset(this.options.open); }).delay(5, this, i);
				
			}.bind(this));
			if (IE6==false)
				el.setStyle('margin-left', this.widths.closed*.65*i+32);
			else
				el.setStyle('margin-left', this.widths.closed*.325*i+16);
			
			el.setStyle('width', this.widths.openSelected-60);
			
		}.bind(this));
	
		
		if(this.options.open){
			if($type(this.options.open) == 'number'){
				this.reset(this.options.open);
			}else{
				this.elements.each(function(el,i){
					if(el.id == this.options.open){
						this.reset(i);
					}
				},this);
			}
		}
		
	},
	
	reset: function(num){
		if($type(num) == 'number'){
			var width = this.widths.openOthers;
			if(num+1 == this.elements.length){
				width += this.options.border;
			}
		}else{
			var width = this.widths.closed;
		}
		
		var obj = {};
		this.elements.each(function(el,i){
			var w = width;
			if(i == this.elements.length-1){
				w = width+5
			}
			obj[i] = {'width': w};
		}.bind(this));
		
		if($type(num) == 'number'){
			obj[num] = {'width': this.widths.openSelected};
		}
				
		this.fx.start(obj);
	}
	
});

ImageMenu.implement(new Options);
ImageMenu.implement(new Events);


/*************************************************************/
