/**************************************************************

	Script	: Image Menu
	Version	: 2.2
	Authors	: Samuel Birch
	Desc	: 
	Licence	: Open Source MIT Licence

**************************************************************/

var ImageMenu = new Class({
	Implements: Options,
	options:{
		onOpen: false,
		onClose: $empty,
		openWidth: 200,
		transition: Fx.Transitions.Quart.easeOut,
		duration: 400,
		open: null,
		border: 0
	},
	
	initialize: function(elements, options){
		this.setOptions(options);
		this.elements = $$(elements);
		this.widths = {};
		this.widths.closed = this.elements[0].getStyle('width').toInt();
		this.widths.openSelected = this.options.openWidth;
		this.widths.openOthers = 57; //Math.round(((this.widths.closed*this.elements.length) - (this.widths.openSelected+this.options.border)) / (this.elements.length-1));
		//alert(this.widths.openOthers);
		this.fx = new Fx.Elements(this.elements, {wait: false, duration: this.options.duration, transition: this.options.transition});
		this.btns = this.elements.getElements('a.tog'); 
		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				e.stop();
				if (i != this.options.open) {
					//this.hover(i);
					//this.btns.set('tween',{duration:'long'});
					this.btns[i].set('tween',{duration:250});
					this.btns[i].tween('background-color','#78164f');
				}
			}.bind(this));
			el.addEvent('mouseleave', function(e){
				e.stop();
				if (i != this.options.open) {
					//el.set('tween',{duration:'long'});
					this.btns[i].tween('background-color','#4c0325');
					//this.reset(this.options.open);
				}
			}.bind(this));
			var obj = this;
			this.btns[i].addEvent('click', function(e){
				if(obj.options.onOpen){
					e.stop();
					if(obj.options.open == i){
						//obj.options.open = null;
						//obj.options.onClose(this.href, i);
					}else{
						obj.options.onClose(obj.btns[obj.options.open], obj.options.open);
						obj.options.open = i;
						obj.reset(i);
						obj.options.onOpen(this.href, i);
					}
				}
			});
		}.bind(this));
		if($chk(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);
	},

	hover: 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 dif = Math.round(80/this.elements.length);
			if($chk(num)){
				if(i!=num){
					w = this.widths.openOthers - dif; 
					if(i==this.options.open){
						w = this.widths.openSelected - dif;
					}
				} else{
					w = this.widths.openOthers+80;
				}
			}else{
				w = this.widths.closed;
			}
			// var w = this.widths.openOthers;
			if(i == this.elements.length-1){
				w = w+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);
*/

/*************************************************************/
