/*!
 * AMteam (c) 2008 - Interstate Greenhouse Company
 */

var AnimatedMenu = new Class({
	Implements: [Options, Events],
	
	options: {
		container: 'animatedmenu', // Container to apply this class to
		elements: '#animatedmenu a', // Elements to use
		activeClass: 'active', // Detect active class
		mode: 'horizontal', // Slidingmode
		containerPosition: { x: 0, y: 0 }, // Background position for container
		containerOffset: { x: 0, y: 0 }, // Offset for background position
		elementOffset: { x: 0, y: 0 }, // Offset for background-position (calculated relative to container)
		containerOptions: {}, // Tween options for container (background position)
		elementOptions: {} // Tween options for element (fade)
	},
	
	initialize: function(options){
		// Set options
		this.setOptions(options);
		// Get container and set tween options
		this.container = $(this.options.container).set('tween', this.options.containerOptions);
		// Get elements
		this.elements = $$(this.options.elements);
		// Get element offset (for element wrapper position)
		this.elementsOffset = this.elements.getPosition(this.container).map(function(item, index){
			return {
				x: item.x + this.options.elementOffset.x,
				y: item.y + this.options.elementOffset.y
			};
		}, this);
		// Get element position (for container position)
		this.elementsPosition = this.elementsOffset.map(function(item, index){
			return {
				x: item.x + this.options.containerOffset.x + (this.options.mode == 'horizontal' ? (this.elements[index].getSize().x / 2).toInt() : 0) + ((this.options.containerOffset[index] && this.options.containerOffset[index].x) ? this.options.containerOffset[index].x : 0),
				y: item.y + this.options.containerOffset.y + (this.options.mode == 'horizontal' ? 0 : (this.elements[index].getSize().y / 2).toInt()) + ((this.options.containerOffset[index] && this.options.containerOffset[index].y) ? this.options.containerOffset[index].y : 0)
			};
		}, this);
		// Remove background image from elements (except active item) and set default position for container position)
		this.elements.filter(function(item, index){
			if (item.hasClass(this.options.activeClass)){
				// Set default position for container
				if (this.options.mode == 'horizontal') this.options.containerPosition.x = this.elementsPosition[index].x;
				else this.options.containerPosition.y = this.elementsPosition[index].y;
				// Tween container position to active element
				this.toPosition(this.container, this.options.containerPosition, this.options.containerPosition);
				return false;
			} else return true;
		}, this).setStyle('background-image', 'none');
		// Add wrapper to element
		this.elements.each(function(item, index){
			// Create wrapper, set styles and inject
			var wrapper = new Element('div').setStyles({
				'opacity': 0,
				'background-position': [-this.elementsOffset[index].x, -this.elementsOffset[index].y]
			}).set('tween', this.options.elementOptions).inject(item, 'top');
			// Add events to elements
			item.addEvents({
				'mouseenter': function(){
					// Fade element in
					wrapper.fade('in');
					// Tween container position to hovered element
					if (this.options.mode == 'horizontal') this.toPosition(this.container, [this.elementsPosition[index].x, this.options.containerPosition.y]);
					else this.toPosition(this.container, [this.options.containerPosition.x, this.elementsPosition[index].y]);
				}.bind(this),
				'mouseleave': function(){
					// Fade element out
					wrapper.fade('out');
					// Tween container to default position
					this.toPosition(this.container, this.options.containerPosition);
				}.bind(this)
			});
		}, this);
	},
	
	toPosition: function(item, from, to){
		// Convert types
		if ($type(from) == 'string') from = from.split(' ');
		if ($type(from) == 'object') from = [from.x, from.y];
		if ($type(to) == 'string') to = to.split(' ');
		if ($type(to) == 'object') to = [to.x, to.y];
		// Tween background-position
		if (window.ie){
			if (this.options.mode == 'horizontal' && to){
				return item.tween('background-position-x', from[0], to[0]);
			} else if (this.options.mode == 'horizontal') {
				return item.tween('background-position-x', from[0]);
			} else if (to) {
				return item.tween('background-position-y', from[1]);
			} else {
				return item.tween('background-position-y', from[1]);
			}
		} else {
			if (to){
				return item.tween('background-position', from[0] + ' ' + from[1], to[0] + ' ' + to[1]);
			} else {
				return item.tween('background-position', from[0] + ' ' + from[1]);
			}
		}
	}
});

window.addEvent('domready', function(){
	// Disable menu for the following pages
	var disableMenuFor = ['sitemap', 'disclaimer'];
	
	// Check is menu must be enabled
	var enableMenu = !disableMenuFor.some(function(value){
		return window.location.href.contains(value);
	});
	
	// Set active menuitem
	if (enableMenu){
		var menuItems = new Hash($$('#menu ul li a').associate($$('#menu ul li').get('class')));
		if (!menuItems.some(function(value, key){
			if (window.location.href.contains(key)) return value.addClass('active');
		})) menuItems['home'].addClass('active');
	}
	
	// Create animated menu
	new AnimatedMenu({
		container: 'menu',
		elements: '#menu ul li a',
		containerPosition: { x: 0, y: 38 },
		containerOffset: { x: 16, y: 0, 0: { x: -5 }, 8: { x: 5 } },
		elementOffset: { x: 0, y: 53 },
		containerOptions: { transition: 'quart:out' },
		elementOptions: { transition: 'quart:out' }
	});
});
