/**
 * This script generate an animated Image Menu from a few elements (list elements by default)
 * You have to initialize it with "expandmenu.init(obj,options)"
 *
 * @param mixed obj A dom object or the id of a dom object including the menu list elements
 * @param object options The list of options within an object. The following options you can change:
 *        list: an array within all menu list elements as dom objects or string of object id
 *        opensize: an integer which define the opened size of menu list element in pixels
 *        origin: The origin of menu list elements as a string ('left', 'right', 'top', 'bottom')
 *        duration: The duration of the animation in milliseconds as an integer
 *        transition: A mootools transition as a function or as a string (for example: transitions.quadOut or only 'quadOut')
 *
 * you have to insert required scripts before this script.
 *
 * @requirements prototype 1.6
 * @requirements transitions 0.beta
 * @requirements motion 0.beta
 *
 * @author Helmut Wandl <helmut@wandls.net>
 * @version 0.beta
 *
 * @copy STERNWERK 2008
 */
var expandmenu=
{
	init:function(obj,options)
	{
		var obj=$(obj),a,o,size;

		// initialize event function
		obj.expandmenu=
		{
			set:function(event)
			{
				var o=obj.expandmenu.options;
				var size=(o.origin == ('top' || 'bottom') ? obj.offsetHeight : obj.offsetWidth);
				var from,to,add=0,a;

				var styles={};

				for (a=0; a < o.list.length; a++)
				{
					styles[a]={};
					styles[a][o.origin]=(event.type == 'mouseover' || event.type == 'mouseenter'
							? (Math.floor((a-add)*(size-o.opensize)/(o.list.length-1))+(add*o.opensize))
							: Math.floor(a*size/o.list.length)
							)+'px';
					if (this == o.list[a]) add=1;
				}

				obj.expandmenu.fx.start(styles);
			}
		};

		// initialize settings
		o=obj.expandmenu.options=
		{
			list:obj.getElementsByTagName('li'),
			opensize:Math.floor(obj.offsetWidth/1.5),
			origin:'left',
			duration:400,
			transition:transitions.quadOut
		};
		Object.extend(o, (options ? options : {}));

		// initialize list objects
		size=(o.origin == ('top' || 'bottom') ? obj.offsetHeight : obj.offsetWidth);
		for (a=0; o.list[a]; a++)
		{
			if (typeof o.list[a] == 'string') o.list[a]=$(o.list[a]);
			Event.observe(o.list[a], 'mouseenter', obj.expandmenu.set);
			Event.observe(o.list[a], 'mouseleave', obj.expandmenu.set);
			//o.list[a].expandmenu_fx = new FX(o.list[a], {origin: o.origin, wait: false, duration: o.duration, transition: (typeof o.transition == 'string' ? Fx.Transitions[o.transition] : o.transition)});

			/*o.list[a].expandmenu_fx.set(
				Math.floor(a*size/o.list.length)
			);*/
		}

		obj.expandmenu.fx=new motion(o.list, {origin: o.origin, wait: false, duration: o.duration, transition: (typeof o.transition == 'string' ? transitions[o.transition] : o.transition)});

		var s={};
		for (a=0; o.list[a]; a++)
		{
			s[a]={};
			s[a][o.origin]=Math.floor(a*size/o.list.length)+'px';
		}
		obj.expandmenu.fx.set(s);
	}
};
