function max(a, b, c)
{
	var tmp = a > b ? a : b;
	return tmp > c ? tmp : c;
}

FxStyles = Class.create();
FxStyles.prototype = Object.extend(new Fx.Styles(), 
{
	addStyle: function(style)
	{
		if(!this.styles)
		{
			this.styles = style;
		}
		else
		{
			for(s in style)
			{
				this.styles[s] = style[s];
			}
		}
	},
	
	run: function()
	{
		if (this.timer && this.options.wait) return;
		var from = {};
		var to = {};
		for (p in this.styles)
		{
			from[p] = this.styles[p][0];
			to[p] = this.styles[p][1];
		}
		return this._start(from, to);
	}
});

var Menu = Class.create();
Menu.prototype = {
	initialize: function(menu, path)
	{
		if(!menu)
		{
			return null;
		}
		
		this.element = $('menu');
		this.menu = menu;	
		this.tree = null;
		this.levels = new Array();
		this.current_path = new Array();
		this.fxs = new Array();
		
		this._makeElement('', this.menu, 0, 'menu');
				
		for(var n = 0; n < this.levels.length; n++)
		{
			var el = $(document.createElement('div'));
			el.id = 'menuLevel_'+n;
			el.className = 'menuLevel';
			if(n>0)
				el.setStyle({zIndex: -100});
			this.element.appendChild(el);
			for(var o = 0; o < this.levels[n].length; o++)
			{
				var el1 = $(this.levels[n][o]);
				el.appendChild(el1);
				var width = max($(el).getWidth(), $(el1).getWidth(), 100);
				if(n > 0)
					width += 5; // this makes a nice sliding effect :)
				$(el).setStyle({width: width+'px'});
				width += 5; // this makes a nice sliding effect :)
				$(el1).setStyle({width: width+'px'});
				if(n>0)
				{
					var width = el1.getWidth();
					el1.setStyle({left: '-' + width + 'px'});
				}
			}
		}
		
		var _path = path.split('/');
		var id='menu';
		for(var n = 0; n < _path.length; n++)
		{
			var pos = _path[n];
			id += '_'+pos;
			var el = $(document.getElementById(id));
			if(!el)
			{
				break;
			}
			el.className += ' selected';
			el.parentNode.setStyle({left: 0});
			$(el.parentNode.parentNode).setStyle({zIndex: 100});
			if(n > 0)
			{
				this.current_path[n] = el.parentNode;
			}
		}
		
	},
	
	_makeElement: function (name, node, level, path)
	{
		var self = this;
		switch(typeof node)
		{
			case 'string':
				var el = document.createElement('a');
				el.className = 'element';
				el.innerHTML = name;
				el.href = node;
				return el;
				break;
			case 'object':
				var el = document.createElement('div');
				el.className = 'submenu';
				var bg = document.createElement('div');
				bg.className = 'background';
				el.appendChild(bg);
				var cnt = 0;
				for(var k in node)
				{
					var subel = this._makeElement(k, node[k], level+1, path+'_'+cnt);
					subel.id = path+'_'+cnt;
					el.appendChild(subel);
					cnt++;
				}
				if(typeof this.levels[level] == 'undefined')
					this.levels[level] = new Array();
				this.levels[level].push(el);
				if(level > -1)
				{
					var el1 = document.createElement('a');
					el1.className = 'element';
					el1.innerHTML = name;
					el1.href = '#';
					el1.onclick = function(){self.showSubmenu($(el1), $(el)); return false;};
					return el1;
				}
				break;
			default:
				break;			
		}
	},
	
	showSubmenu: function(me, submenu)
	{
		var self  =this;
		var matches = /^menuLevel_(\d*)$/.exec(me.parentNode.parentNode.id);
		if(matches && matches.length == 2 && !/selected/.exec(me.className))
		{
			var level = parseInt(matches[1]);
			
			var lastFx = null;
			var firstFx = null;
			var fx = null;
			for(var n = this.current_path.length-1; n > level; n--)
			{
				var el = this.current_path[n];
				if(n == level+1)
				{
					fx = new FxStyles(el, {duration: 900, wait: false, transition: Fx.Transitions.quintOut, onStart: function(){self.runNextFx();} });
				}
				else
				{
					fx = new FxStyles(el, {duration: 200, wait: false, transition: Fx.Transitions.linear, onComplete: function(){$(this.element.parentNode).setStyle({zIndex: -100}); self.runNextFx();} });
				}
				fx.addStyle({left: [0, (el.getWidth()*-1)]});
				
				for(var o = 0; o < el.childNodes.length; o++)
				{
					el.childNodes[o].className = el.childNodes[o].className.replace(/\ selected/g, '');
				}
				
				this.fxs.push(fx);
				
				this.current_path.pop();
			}			
			
			$(submenu.parentNode).setStyle({zIndex: 100});			
			fx = new FxStyles(submenu, {duration: 900, wait: false, transition: Fx.Transitions.quadOut});
			fx.addStyle({left: [(submenu.getWidth()*-1), 0]});
			this.fxs.push(fx);
			
			for(var o = 0; o < me.parentNode.childNodes.length; o++)
			{
				me.parentNode.childNodes[o].className = me.parentNode.childNodes[o].className.replace(/\ selected/g, '');
			}
			
			me.className = me.className + ' selected';
			
			this.runNextFx();
			
			this.current_path[level+1] = submenu;
		}
	},
	
	runNextFx: function()
	{
		var fx = this.fxs.shift();
		fx.run();
	}	
}

var menuAnimation = Class.create();
menuAnimation.prototype = 
{
	initialize: function(images)
	{
		var el = document.createElement('div');
		el.id = 'menuAnimation';
		this.element = el;
		
		el = document.createElement('div');
		el.id = 'canvas1';
		el.style.width = '100%';
		el.style.height = '100%';
		el.style.top = '0';
		el.style.left = '0';
		el.style.display = 'none';
		this.canvas1 = el;
		this.element.appendChild(el);
		
		el = document.createElement('div');
		el.id = 'canvas2';
		el.style.width = '100%';
		el.style.height = '100%';
		el.style.top = '0';
		el.style.left = '0';
		el.style.display = 'none';
		this.canvas2 = el;
		this.element.appendChild(el);
		
		
	},
	
	run: function()
	{
		this.anim1.run();
	}
}

