var catHover1 = null;
var catHover2 = null;
var catHover3 = null;
var catHover4 = null;
var catHover5 = null;
var catHover6 = null;
var catHoverArr = [];

/**
 * Generates hover effect for category menu.
 *
 * Constructor fields:
 * -------------------
 * catCont  category container
 * catHoCont category hover container
 */
var CategoryHover = Class.create({
	initialize : function(catId) {
		this.catCont = $('categorymenu_' + catId);
		this.catHoCont = $('categoryHover_' + catId);
		this.catArrowImg = $('categoryArrow_' + catId);
		
		if (this.catHoCont) {
			catHoverArr.push(this);
			this.catHoLists = this.catHoCont.select('ul.categoryGroup');
			this.catLinks = this.catCont.select('span');
			this.actCat = this.catCont.select('.active span')[0];
			this.over = false;
			this.curHoCat = null;
			this.catLinks.each(function(e){
				e.observe('mouseover',this.showMenu.bind(this));
				e.observe('mouseout',this.checkVis.bind(this));	
			}.bind(this));
			this.catHoCont.observe('mouseover', this.isOver.bind(this));
			this.catHoCont.observe('mouseout', this.checkVis.bind(this));
		}
	},		
	
	isOver : function() {
		this.over = true;
	},
	
	checkVis : function (e) {
		this.over = false;
		setTimeout(function() {this.hideMenu();}.bind(this), 500);
	},
	
	showMenu : function(e) {
		catHoverArr.each(function(o) { 
			 o.over = false;
			 o.hideMenu();
		});
		
		this.over = true;
		var el = Event.element(e);
		
		this.catCont.down().addClassName('hover');
		
		var pos = 0; //this.catLinks.indexOf(e);
		this.calcPos(pos);
		this.curHoCat = el;	
		
		var maxHeight = 0;
		var submenuBoxes = this.catHoLists[pos].select('.boxSecond','.box','.boxDbl','.leftCol', '.boxSubcat');
		submenuBoxes.each(function(elem) {
		var height = elem.getDimensions().height;
			if (height > maxHeight) {
				maxHeight = height;
			}
		});
								
		var switchBoxes = this.catHoLists[pos].select('.boxSecond', '.boxSubcat');
		switchBoxes.each(function(elem) {
			elem.style.height = maxHeight + 'px';
		});	
	},
	
	hideMenu : function() {
		if(this.over == false)
		{
			if (this.catArrowImg) {
				this.catArrowImg.hide();
			}
			this.catHoCont.hide();
			this.catCont.down().removeClassName('hover');
		}
	},
	
	hideSelectFields : function(hoverElement) {
		$('main').getElementsBySelector('select').each(function(el,i){
			if(hoverElement.overlaps(hoverElement, el)){
				el.style.visibility = 'hidden';
			}	
		}); 
	},
	
	showSelectFields : function() {
		$('main').getElementsBySelector('select').each(function(el,i){
			el.style.visibility='visible';
		}); 
	},
	
	calcPos : function(position) {
		
		if (this.catHoCont.id == 'categoryHover_100000' || this.catHoCont.id == 'categoryHover_200000'
				|| this.catHoCont.id == 'categoryHover_300000' || this.catHoCont.id == 'categoryHover_400000'
				|| this.catHoCont.id == 'categoryHover_500000') {
			this.catHoCont.style.left = 0;
		}
		else if (evilBrowserInformation.isEvil) {
			this.catHoCont.style.left = this.catCont.positionedOffset().left;
		}
		
		// IE 6 fix for dynamic width
		if (evilBrowserInformation.isEvil && evilBrowserInformation.evilVersion == 6) {
			var subcatBoxes = this.catHoLists[0].select('.boxSubcat');
			var minSize = 201;
			if(this.catHoCont != null) {
				this.catHoCont.style.width = (subcatBoxes.size() * minSize) + 'px';
			}
		}
		
		this.catHoCont.show();
		this.catHoLists.each(function(e,i){
			e.hide();
		});
		
		if(this.catHoLists[position])
			this.catHoLists[position].show();
		
		// positioned offset of the category plus half width
		this.catArrowImg.style.left = this.catCont.positionedOffset().left + (Math.floor(this.catCont.getWidth() / 2)) - 4 + 'px';
		this.catArrowImg.show();
	}
});

if ('observe' in document) {
	document.observe('dom:loaded', function() {
		catHover1 = new CategoryHover('100000');
		catHover2 = new CategoryHover('200000');
		catHover3 = new CategoryHover('300000');
		catHover4 = new CategoryHover('400000');
		catHover5 = new CategoryHover('500000');
		catHover6 = new CategoryHover('600000');
		catHover7 = new CategoryHover('denim');
		catHover8 = new CategoryHover('hilfiger');
		catHover9 = new CategoryHover('peoplesplace');
	});
}