var ProductMenu = new Class({
	Implements: [Events, Options],
	options: {
		onChangeComplete: Class.empty
	},
	initialize: function(container, options){
		this.setOptions(options);
		
		this.container = $(container);
		
		// array of nested lists
		this.nestedLists = this.container.getElements('ul ul');
		// array of all lists
		this.lists = this.container.getElements('ul');
		this.nestedLists.each(function(item){
			// remove nested lists from array of all lists to find the non nested lists
			this.lists.erase(item);
		}.bind(this));
		
		// create required accordions
		var selectedIndex = this.findSelected();
		this.nestedListAccordion = new Accordion(this.container.getElements('h4'), this.nestedLists, { show: selectedIndex.nested });
		this.listAccordion = new Accordion(this.container.getElements('h3'), this.lists, { show: selectedIndex.primary });

		// add coming soon hover element to dom
		this.comingSoon = new Element('div', {'id':'hover_coming_soon'}).inject(document.body);					
		
		// attach events
		this.attach();
		
		// fire change complete event because menu has been reconfigured
		this.fireEvent('onChangeComplete');
	},
	attach: function(){		
		// attach events to accordions
		this.listAccordion.addEvents({
			onActive: function(toggler, element){
				// is animating is set to true while the menu is animating
				this.isAnimating = true;
				
				// highlight the toggler
				toggler.addClass('highlight');
				// set a fixed height on the element so the slide effect works
				element.setStyle('height', element.offsetHeight);
				
				// wait until animation is complete
				(function(){
					this.fireEvent('onChangeComplete');
					this.isAnimating = false;
				}.bind(this)).delay(500);
			}.bind(this),
			onBackground: function(toggler, element){
				// remove highlighting from toggler
				toggler.removeClass('highlight');
				// set a fixed height on the element so the slide effect works
				element.setStyle('height', element.offsetHeight);
			}
		});
		this.nestedListAccordion.addEvents({
			onActive: function(toggler, element){
				// is animating is set to true while the menu is animating
				this.isAnimating = true;
				
				// highlight the toggler
				toggler.addClass('highlight');
				// apply auto height on the parent list so that it will expand properly
				// with a change in size of the inner contents
				element.getParent().getParent().setStyle('height', 'auto');
				
				// wait until animation is complete
				(function(){
					this.fireEvent('onChangeComplete');
					this.isAnimating = false;
				}.bind(this)).delay(500);
			}.bind(this),
			onBackground: function(toggler, element){
				// remove highlighting from toggler
				toggler.removeClass('highlight');
			}
		});
		
		// add coming mouseover events
		this.container.getElements('li.coming_soon a').addEvents({
			click: function(e){
				// disable default click behaviour
				e.preventDefault();	
			},
			mouseenter: function(e){
				// only show mouseover if the menu is not animating
				// prevents flikering of coming son as menu animates below mouse cursor
				if(!this.isAnimating){
					// position and show coming soon element
					var elementCoordinates = $(e.target).getCoordinates();
					this.comingSoon.setStyles({
						'top': elementCoordinates.top -3,
						'left': elementCoordinates.right + 5,
						'display': 'block'
					});
				}
			}.bind(this),
			mouseleave: function(){
				// hide coming soon element
				this.comingSoon.setStyle('display', 'none');
			}.bind(this)
		});
	},
	findSelected: function(){
		// look for a selected item
		var selected = this.container.getElement('li.selected');
		// if a selected item is found
		if(selected != null){
			// default -1 for closed
			var index = {
				nested: -1,
				primary: -1
			}
			// get the ul of the selected li
			var ul = selected.getParent();
			// check to see if the selected item is in a nested list
			index.nested = this.nestedLists.indexOf(ul);
			if(index.nested != -1){
				// we know the list is nested so now we need to find its parent list
				index.primary = this.lists.indexOf(ul.getParent().getParent());
			}
			else{
				// we know the list is not nested so lets look for it in the first level of lists
				index.primary = this.lists.indexOf(ul);
			}
		}
		else{
			// default for no selection is to open the first nested and first primary lists
			var index = {
				nested: 0,
				primary: 0
			}
		}
		return index;
	}
});