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

	Script	: Image Menu
	Version	: 2.2
	Authors	: Samuel Birch
	Desc	: 
	Licence	: Open Source MIT Licence
	
	Modified, bugfixed and extended by Jan Wichmann, jones-design, Jan 2010

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




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

	initialize: function(elements, options){
		this.setOptions(this.getOptions(), options);
		
		this.elements = $$(elements);
			
		// jones: wandle id in mummer um
		if(this.options.open) {
			this.elements.each(function(el,i){
				if(el.id == this.options.open){
					this.openNumber=i;
				}						
			}.bind(this));
		}
		//end jones
		
		
		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.elements.length-1))
		
		
		this.fx = new Fx.Elements(this.elements, {wait: false, duration: this.options.duration, transition: this.options.transition});
		
		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();
				this.reset(i);
				
			}.bind(this));
			
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();
				this.reset(this.openNumber);
				
			}.bind(this));
			
			var obj = this;
			
			
			
		}.bind(this));
		
		
		//by jones 10.01.2010, sorgt für den auf geklappten Zustand (ohne Animation) beim Laden der Seite (wenn options.open gesetzt ist)
		if(this.options.open){
				this.elements.each(function(el,i){
					if(i == this.openNumber){
						el.setStyle('width',this.widths.openSelected);
					} else {
						el.setStyle('width',this.widths.openOthers);
					}
				},this);
		}
		//end jones
		
	},
	
	reset: function(num){
		//jones: id-support added für das open-Option beim mouseout
		if(num) {
			this.elements.each(function(el,i){
				if(el.id == num){
					num=i;
				}						
			});
		}
		
		//end jones
		
		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+5
			}
			obj[i] = {'width': w};
		}.bind(this));
		
		if($type(num) == 'number'){
			obj[num] = {'width': this.widths.openSelected};
		} 
		
	//jones begin
		if (num==this.openNumber) {
			obj[this.openNumber].opacity =[1];
		} else {
			obj[this.openNumber].opacity =[0];
		}
		//jones end
	
		this.fx.start(obj);
	}
	
});

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


