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

	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: 550,
			transition: Fx.Transitions.quadOut,
			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(); ////////////// 180
		this.widths.openSelected = this.options.openWidth; //////////////////////////// 550
		this.widths.openOthers = Math.round(((this.widths.closed*this.elements.length) - (this.widths.openSelected+this.options.border)) / (this.elements.length-1)) // 88
		
		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));
		

//			if(el.id != this.options.open)
//				{
				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.href, i);
						}
					else
						{
						obj.options.open = i;
						obj.options.onOpen(this.href, i);
						}
					}
				})
			}.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) // Nel mouseenter è numerico (l'index dell'elemento), nel mouseleave è stringa con il NOME (this.options.open) da tenere aperto
		{
		if($type(num) == 'number') ////////////////////////////////// mouseenter
			{
			var width = this.widths.openOthers; // 88
			if(num+1 == this.elements.length)
				{
				width += this.options.border;
				}
			}
		else ////////////////////////////////// mouseleave
			{
			if(num == this.options.open)
				{
				var width = this.widths.closed; // 180
				}
			else
				{
				var width = this.widths.openSelected; // 550
				}
				
			}

	
			var obj = {};

			this.elements.each(function(el,i) ///////// el = link, i = element index
				{
				var w = width;


//alert("this.elements[i].id:"+this.elements[i].id + " this.options.open:" + this.options.open+ " num:" + num + " i:" + i)
			if(this.options.open)
				{
				if (this.elements[i].id == this.options.open && this.options.open == num) //// elemento di default e mouseleave nell'elemento di default della pagina
					{
					w = this.widths.openSelected;
					}
				else if(this.elements[i].id != this.options.open && this.options.open == num)  //// altri elementi e mouseleave nell'elemento di default della pagina
					{
					w = this.widths.openOthers;
					}
				}
//				if(i == this.elements.length-1) //////// elements.length = quanti elementi ci sono, se l'ultimo della fila aggiungi 5 px
//					{
//					w = width+5;
//					}
				obj[i] = {'width': w};
				}.bind(this));
			
			if($type(num) == 'number')
				{
				obj[num] = {'width': this.widths.openSelected}; // 550
				}
				
		this.fx.start(obj);
		}
	
});

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


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

