/*
Script: imageMenu.js

Authors:
	Sam Birch

License:
	MIT-style license.

*/
var ImageMenu = new Class({
	
	initialize: function(myElements,options){
		options = Object.extend({
			onClick: Class.empty,
			start: -1,
			openWidth: 0,
			smallWidth: 0,
			itemWidth: 0,
			selected: -1,
			open: -1,
			duration: 400,
			transition: Fx.Transitions.quadOut
		}, options || {});
		
		this.myElements = myElements;
		this.options = options;
		
		options.itemWidth = myElements[0].getStyle('width').toInt();
		options.smallWidth = Math.round(((options.itemWidth*myElements.length)-options.openWidth)/(myElements.length-1));
		
		this.resetSpeed = function(){ 
			fx.options.duration = options.duration;// pixelchutes - ok to animate again 
		}// pixelchutes - restoration
		
		this.openLink = function(){ 
			if( typeof fadeFx.href != 'undefined' ){
				//if( location.href != fadeFx.href )
				location.href = fadeFx.href;
			}
		}// pixelchutes - openLink (link handling)
		
		var fx = new Fx.Elements(myElements, {wait: false, duration: options.duration, transition: options.transition, onComplete:this.resetSpeed });
		var fadeFx = new Fx.Style( myElements[0], 'opacity', { onComplete:this.openLink } );
		
		myElements.each(function(el, i){
			el.addEvents({
				mouseover: function(e){
					e = new Event(e).stop();
					el.show();
				},			
				click: function(e){
					el.select();
				},
				mouseout: function(e){
					e = new Event(e).stop();
					el.hide();
				}
			}); // pixelchutes - addEvents (plural)
			
			el.show = function(){
				var obj = {};
				obj[i] = {'width': [el.getStyle('width').toInt(), options.openWidth]};
				myElements.each(function(other, j){
					if (other != el){
						var w = other.getStyle('width').toInt();
						if (w != options.smallWidth) obj[j] = {'width': [w, options.smallWidth]};
					}
				});
				fx.start(obj);
			};
			
			el.hide = function(){
				var obj = {};
				if(options.selected == -1 ){					
					myElements.each(function(el,i){
						obj[i] = {'width': [el.getStyle('width').toInt(), options.itemWidth]};	
					});
				}else{
					myElements.each(function(el,i){
						if(i != options.selected){
							var w = el.getStyle('width').toInt();
							if(w != options.smallWidth){obj[i] = {'width': [w, options.smallWidth]}};
						}else{
							obj[i] = {'width': [el.getStyle('width').toInt(), options.openWidth]};
						}
					});
				}
				fx.start(obj);
			};
			
			el.select = function(start){
				if(options.selected == i){options.selected = -1}else{options.selected = i}
				options.onClick(options.selected,options.open);					
				options.open = options.selected;
				if( !start ) fadeFx.href = el.getChildren()[0]; // pixelchutes - Make slide clickable					
				if( options.selected > -1 ) fadeFx.element = myElements[options.selected]; // Map current item to fadeFx
				if( options.selected > -1 && !start ) fadeFx.start(0,1); // pixelchutes - fade in flicker

			};
		});

		if(options.start != -1){
			options.start = map2modx( options.start );
			fx.options.duration = 1;// pixelchutes - don't animate on start
			myElements[ options.start ].show();			
			myElements[ options.start ].select(1);// pixelchutes - don't redirect on start			
		}
	},
	
	reset: function(){
		this.options.selected = -1;
		this.options.open = -1;
		this.myElements.each(function(el, i){
			el.hide();
		});
	}
	
});

function map2modx( doc_id ){	
	return_var = 0;
	switch( doc_id )
	{
		 case 1: return_var = 0; break;
		 case 7: return_var = 1; break;
		case 19: return_var = 2; break;
		 case 2: return_var = 3; break;		
	}
	
	return return_var;
}

window.addEvent('load',function(){
	setTimeout(function(){
		var fadeIn = new Fx.Style($('nav'),'opacity');
		fadeIn.hide();
		$('nav').setStyle('display','block');
		fadeIn.start(0,1);
	}, 50 );
});