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

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

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

var ImageMenu = new Class({
	
	getOptions: function(){
		return {
			OnOpen: $lambda(false),
			OnClose: $lambda(false),
			OnClickOpen: false,
			OnClickClose: false,
			openWidth: 200,
			transition: Fx.Transitions.Quad.easeOut,
			duration: 400,
			open: null,
			border: 0
		};
	},

	initialize: function(elements, options){
		this.setOptions(this.getOptions(), options);
		
		this.elements = $$(elements);
			
		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});
		
		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();
				this.reset(i);
				if(this.options.OnOpen){
					this.options.OnOpen(el, i);
				}
				$clear(this.hintFX);
				$clear(this.hintChain);
			}.bind(this));
			
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();
				this.reset(this.options.open);
				this.makeHints();
				if(this.options.OnClose){
					this.options.OnClose(el, i);
				}
			}.bind(this));
			
			var obj = this;
			
			el.addEvent('click', function(e){
				
				if(obj.options.OnClickOpen){
					new Event(e).stop();
					if(obj.options.open == i){
						obj.options.open = null;
						obj.options.OnClickClose(this.href, i);
					}else{
						obj.options.open = i;
						obj.options.OnClickOpen(this.href, i);
					}
					
				}
				
			})
			
		}.bind(this));
		
		if(this.options.open != null){
			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);
			}
		}
		
		this.makeHints();
		
//		this.elements[0].getParent('div').addEvent('mouseenter', function(e){ $clear(  iM.hintFX ); } );
//		this.elements[0].getParent('div').addEvent('mouseleave', function(e){ iM.makeHints(); } );
		
	},
	
	makeHints: function(){
	
		t1 = function(){ iM.hint(2); };
		t2 = function(){ iM.hint(3); };
		t3 = function(){ iM.hint(4); };
		t4 = function(){ iM.reset(1); };

		hint = function(){
			this.hintChain = new Chain();
			this.hintChain.chain(t1,t2,t3,t2,t1,t4);
			this.hintChain.callChain();
			this.hintChain.callChain.delay(50,this.hintChain);
			this.hintChain.callChain.delay(150,this.hintChain);
			this.hintChain.callChain.delay(400,this.hintChain);
			this.hintChain.callChain.delay(450,this.hintChain);
			this.hintChain.callChain.delay(500,this.hintChain);
		}
		
		this.hintFX = hint.periodical( 3000 );	
	},
	
	
	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);
	},
	
	hint: function(num){
		
		var obj = {};
		width = 140;
		this.elements.each(function(el,i){
			if(i==1){
				var w = 630;
				if(i == this.elements.length-1){
					w = width+5
				}
				obj[i] = {'width': w};
			} else if(i>0){
				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': 210};
		}
				
		this.fx.start(obj);
	}
	
});

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


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