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

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

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

var ImageMenu = new Class({
	
	getOptions: function(){
		return {
			onOver: false,
			onOpen: false,
			onClose: false,
			onLeave: false,
			openWidth: 15,
			transition: Fx.Transitions.quadOut,
			duration: 400,
			open: null,
			border: 0,
			unit: 'em'
		};
	},

	initialize: function(elements, options){
		this.setOptions(this.getOptions(), options);
		this.elements = $$("#"+elements+" a");
		this.panel = $(elements);
		this.widths = {};
		//this.widths.closed = this.elements[0].getStyle('width').toFloat();
		this.widths.closed = 6;
		this.widths.openSelected = this.options.openWidth;
		this.widths.openOthers = ((this.widths.closed*this.elements.length) - this.widths.openSelected) / (this.elements.length-1);
		this.fx = new Fx.Elements(this.elements, {wait: false, duration: 0, transition: this.options.transition, unit: this.options.unit});
		this.reset();
		this.fx = new Fx.Elements(this.elements, {wait: false, duration: this.options.duration, transition: this.options.transition, unit: this.options.unit});

		this.panel.addEvent('mouseleave', function(e){
			new Event(e).stop();
			if(this.options.onLeave){
				if(this.options.open!=null){
					this.options.onLeave(this.elements[this.options.open].title, this.options.open);
				}else{
					this.options.onLeave("", "");
				}
			}
		}.bind(this));
		
		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();
				this.reset(i);
				if(obj.options.onOver){
					obj.options.onOver(this.elements[i].title, i);
				}
				
			}.bind(this));
			
			el.addEvent('mouseleave', function(e){
				//new Event(e).stop();
				this.reset(this.options.open);
			}.bind(this));
			
			var obj = this;
			
			el.addEvent('click', function(e){
				
				if(obj.options.onOpen){
					new Event(e).stop();
					if(obj.options.open == i){
						obj.options.open = null;
						obj.options.onClose(this.tagName, i);
					}else{
						obj.options.open = i;
						obj.options.onOpen(this.elements[i].title, i);
					}
					
					
				}
				
			}.bind(this));
			
		}.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;
		}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+0.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);


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