/*
Script: imageMenu_height.js

Authors:
	Joe Ally
	Sam Birch

url option added by wipsa (www.wipsa.net)

License:
	MIT-style license.

*/
var ImageMenuHeight = new Class({

	initialize: function(myElements,options){
		options = Object.extend({
//			onClick: Class.empty,
			start: -1,
			openHeight: 0,
			smallHeight: 0,
			itemHeight: 0,
			selected: -1,
			open: -1,
			url: null,
			transition: Fx.Transitions.bounceOut
		}, options || {});

		this.myElements = myElements;
		this.options = options;

		options.itemHeight = myElements[0].getStyle('height').toInt();
		options.smallHeight = Math.round(((options.itemHeight*myElements.length)-options.openHeight)/(myElements.length-1));

		var fx = new Fx.Elements(myElements, {wait: false, duration: 400, transition: options.transition});

		myElements.each(function(el, i){
			el.addEvent('mouseover', function(e){
				e = new Event(e).stop();
				el.show();
			});

			el.addEvent('click', function(e){
				el.select();
			});

			el.addEvent('mouseout', function(e){
				e = new Event(e).stop();
				el.hide();
			});

			el.show = function(){
				var obj = {};
				obj[i] = {'height': [el.getStyle('height').toInt(), options.openHeight]};
				myElements.each(function(other, j){
					if (other != el){
						var w = other.getStyle('height').toInt();
						if (w != options.smallHeight) obj[j] = {'height': [w, options.smallHeight]};
					}
				});
				fx.start(obj);
			};

			el.hide = function(){
				var obj = {};
				if(options.selected == -1){
					myElements.each(function(el,i){
						obj[i] = {'height': [el.getStyle('height').toInt(), options.itemHeight]};
					});
				}else{
					myElements.each(function(el,i){
						if(i != options.selected){
							var w = el.getStyle('height').toInt();
							if(w != options.smallHeight){obj[i] = {'height': [w, options.smallHeight]}};
						}else{
							obj[i] = {'height': [el.getStyle('height').toInt(), options.openHeight]};
						}
					});
				}
				fx.start(obj);
			};

			el.select = function(){
				if(options.selected == i){options.selected = -1}else{options.selected = i}

				if (options.url) {
					window.location.href = options.url;
					return;
				} else {
					options.onClick(options.selected,options.open);
				}
				options.open = options.selected;
			};
		});

		if(options.start != -1){
			myElements[options.start].show();
			myElements[options.start].select();
		}
	},

	reset: function(){
		this.options.selected = -1;
		this.options.open = -1;
		this.myElements.each(function(el, i){
			el.hide();
		});
	}

});
