/*
Class: MainNavigation
 
*/
try{
var MainNavigation = Class.create({
	initialize: function(container) {
		// set variables
		this.container = $(container);
		this.timeout =  null; // timeout used for delayed hide
		this.elements = this.container.childElements(); // conatains the list elements
		this.dropdowns = []; // conatains the dropdown elements
		this.current = -1;  // conatains the currently opened tab
		
		// setup events
		var oThis = this; // create collapsible this variable
		// loop through all tabs
		this.elements.each(
			function(li,i) {
				var dropdown = li.down('.dropDown');
				oThis.matchHeight(dropdown);
				// store the dropdown element for easy access later on
				oThis.dropdowns.push(dropdown);
				
				// add a hover state
				li.observe('mouseover', function(event){
					//var el = Event.element(event);
					oThis.hover(i);
				});
			}
		);
		this.container.observe('mouseout', function(event){
			//var el = Event.element(event);
			oThis.unhover();
		});
		
		this.setupRoundedCorners();
	},
	setupRoundedCorners: function(){
		// TODO feature check for border radius instead of browser sniffing
		if(Prototype.Browser.IE){
			this.dropdowns.each(function(element,index){
				var tpl = '<div class="beak"></div><div class="dropdown-top-left"></div><div class="dropdown-top-center"></div><div class="dropdown-top-right"></div>';
				tpl += '<div class="dropdown-middle"><div class="dropdown-middle-left"></div><div class="dropdown-middle-center">'+element.innerHTML+'</div><div class="dropdown-middle-right"></div></div>';
				tpl += '<div class="dropdown-bottom-left"></div><div class="dropdown-bottom-center"></div><div class="dropdown-bottom-right"></div>';
				
				element.innerHTML = tpl;
			})
		}
	},
	open: function(index){
		var oThis = this;
		if(index != -1){
			if(oThis.elements[index] != null)
				oThis.elements[index].addClassName('sfhover');
		}
		// set classname for drop
		this.current = index;
	},
	close: function (index){
		var oThis = this;
		var curr = this.current;
		
		if(this.current != -1){
			if(oThis.elements[curr] != null)
				oThis.elements[curr].removeClassName('sfhover');
		}
		
		this.current = -1;
	},
	hover: function(index){
		// clear a possible running timeout
		try{clearTimeout(this.timeout)}catch(e){}
		// if the called dropdown is different than the opened one
		if(index != this.current) {
			
			if(this.current != -1){
				// hide current dropdown
				this.close(this.current);
			}
			
			// show called dropdown
			this.open(index);
		}
	},
	unhover: function(){
		try{clearTimeout(this.timeout)}catch(e){}
		
		var oThis = this;
		this.timeout = setTimeout(function(){
			if(this.current != -1){
					oThis.close(oThis.current);
			}
		},250);
	},
	matchHeight: function(parent){
		var boxes = parent.select('ul[class^=grid_]');
		
		var length = boxes.length;
		var i = 0;
		
		while ( i < length) {
			var list = [];
			var top = boxes[i].cumulativeOffset().top;
			var height = 0;
			do{
				list.push(boxes[i]);
				var myHeight = boxes[i].getHeight();
				height = (height > myHeight)? height : myHeight;
			   //console.log(i + "-" + boxes[i].positionedOffset().top);
				i++;
			}
			while(i < length && top == boxes[i].cumulativeOffset().top );
			
			if(list.length > 1){
				for(var j=0;j < list.length;j++){
					list[j].style.height = height+'px';
				}
			}
			
		};
	}
});

}catch(e){}
	
