var countryHover = null;
/**
 * Generates hover effect for change country link.
 *
 */
var CountryHover = Class.create({
	initialize : function() {
		this.countryLink = $('countryLink');
		if( this.countryLink )
		{
			this.countryLink.observe('mouseover',this.showMenu.bind(this));
			this.countryLink.observe('mouseout',this.checkVis.bind(this));	
		}
		
		this.countryHover = $('countryHover');
		if( this.countryHover )
		{
			this.countryHover.observe('mouseover', this.isOver.bind(this));
			this.countryHover.observe('mouseout', this.checkVis.bind(this));
		}

		
		this.over = false;

		this.IE6 = false;
		if (navigator.userAgent.indexOf('IE 6') != -1) {
			this.isIE6 = true;
		}			
	},		
	
	isOver : function() {
		this.over = true;
	},
	
	checkVis : function (e) {
		this.over = false;
		setTimeout('countryHover.hideMenu()',10);			
	},
	
	showMenu : function(e) {
		if( this.countryHover )
		{
			this.over = true;
			
			var coord = this.countryLink.viewportOffset();
			
			coord.top += this.countryLink.cumulativeScrollOffset()[1]  || 0;
	        coord.left += this.countryLink.cumulativeScrollOffset()[0] || 0;
	        
			coord.top += this.countryLink.getDimensions().height;
			coord.top -= 6;
			coord.left -= 8;
			//coord.left -= this.countryLink.getDimensions().width;  
	        
			this.countryHover.style.left =  coord.left+'px';
			this.countryHover.style.top = coord.top+'px';
			this.countryHover.show();
		}
	},
	
	hideMenu : function() {
		if(this.over == false)
		{
			this.countryHover.hide();			
		}
	}
	
});


var catHover = null;
/**
 * Generates hover effect for category menu.
 *
 * Constructor fields:
 * -------------------
 * catCont  category container
 * catHoCont category hover container
 */
var CategoryHover = Class.create({
	initialize : function(catCont, catHoCont) {
		this.catCont = $(catCont);
		this.catHoCont = $(catHoCont);
		this.IE6 = false;
		if (this.catHoCont) {
			this.catHoLists = this.catHoCont.select('ul.categoryGroup');
			this.catLinks = this.catCont.select('span');
			this.actCat = this.catCont.down('.active span');
			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));
			if (navigator.userAgent.indexOf('IE 6') != -1) {
				this.isIE6 = true;
			}			
			this.hideMenu();
		
		}
	},		
	
	isOver : function() {
		this.over = true;
	},
	
	checkVis : function (e) {
		this.over = false;
		setTimeout('catHover.hideMenu()',1000);			
	},
	
	showMenu : function(e) {
		this.over = true;
		var el = Event.element(e);

		if(this.curHoCat)
		{
			this.curHoCat.up().style.background = "";
		}
		
		var pos = this.catLinks.indexOf(el);
		this.calcPos(pos);
		if (this.actCat) {
			this.actCat.up().style.background = "";
		}
		
		el.up().style.background = "url("+Constants.url.staticRoot + "images/arr_up_light.gif) no-repeat center 20px";
		
		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';
		});	
		if (this.isIE6) {

			this.hideSelectFields(el);
			
		}
	},
	
	hideMenu : function() {
		if(this.over == false)
		{
			this.catHoCont.hide();			
			if(this.curHoCat)
			{
				this.curHoCat.up().style.background = "";
			}
			if (this.actCat) {
				this.actCat.up().style.background = "url("+Constants.url.staticRoot + "images/arr_up_light.gif) no-repeat center 20px";
			}
			if (this.isIE6) {
				this.showSelectFields();
			}			
		}
	},
	
	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) {
		var coord = this.catLinks[position].up().viewportOffset();
		
		coord.top += this.catLinks[position].up().cumulativeScrollOffset()[1]  || 0;
        coord.left += this.catLinks[position].up().cumulativeScrollOffset()[0] || 0;
		coord.left -= 1;
		coord.top += 24;	
        
		if (!evilBrowserInformation.isEvil || (evilBrowserInformation.isEvil && evilBrowserInformation.evilVersion > 6)) {
			this.catHoCont.style.left =  coord.left+'px';
		} else {
			this.catHoCont.style.left = (this.catLinks[position].viewportOffset().left - this.catLinks[0].viewportOffset().left + (position == 0 ? 0 : 1))+ 'px';
		}
		this.catHoCont.style.top = coord.top+'px';
		this.catHoCont.show();
		var catNavi = $('categoryNavigation');
		var rightLimit = catNavi.viewportOffset()[0] + catNavi.getWidth() + 2;
		this.catHoLists.each(function(e,i){
			e.hide();
		});
		if(this.catHoLists[position])
			this.catHoLists[position].show();
		
		// Displays flyouthover inside the main page area when the flyouthover overflows the right border of the page area  
		var hoverWidth = this.catHoCont.getWidth();
		if((hoverWidth + this.catHoCont.viewportOffset()[0]) > rightLimit) {
			this.catHoCont.style.left = (rightLimit - hoverWidth) + 'px';
		}
					
	}
});

var ServiceDropDown = Class.create({

	idx : null,
	element : null,
	dropDownList : null,
	visible : null,
	isClosing: null,
	isOver: null,
	forcedClose: null,
	isIE6 : false,

	initialize : function(element, idx) {
		this.idx = idx;
		if (element) {
			this.element = element;
			var linkElements = element.select('a.dropdownLink');
			if (linkElements.length > 0) {
				linkElements[0].serviceDropDownHandler = this;
				linkElements[0].onclick = this.clickHandler;
				linkElements[0].onmouseover = this.mouseOverHandler.bind(this);
				linkElements[0].onmouseout = this.mouseOutHandler.bind(this);
				this.dropDownList = $(linkElements[0].href.substring(linkElements[0].href.indexOf('#') + 1));
				this.dropDownList.onmouseover = this.mouseOverHandler.bind(this);
				this.dropDownList.onmouseout = this.mouseOutHandler.bind(this);
				Event.observe(window, 'resize', this.resizeHandler.bind(this));
				if (navigator.userAgent.indexOf('IE 6') != -1) {
					this.isIE6 = true;
				}				
			}
		}		
	},
	
	show : function() {
		serviceDropDowns.each(function(el) {
			if (el.dropDownList.visible()) {
				el.hide();
			}
		});
	
		this.element.addClassName('activeService');
		this.visible = true;
		this.dropDownList.show();
				
		this.calculatePosition();
		
		if (this.isIE6) {
			this.hideSelectFields(this.element);			
		}		
		
	},
	
	hide : function() {
		if (this.visible && this.isClosing && !this.isOver) {
			this.element.removeClassName('activeService');
			this.visible = false;
			this.dropDownList.hide();
		}
		if (this.isIE6) {
			this.showSelectFields();
		}		
	},
	
	calculatePosition : function() {
		if (this.visible) {
			if (this.element.viewportOffset().left != this.dropDownList.viewportOffset().left) {
				this.dropDownList.style.position = 'absolute';
				if (!evilBrowserInformation.isEvil || (evilBrowserInformation.isEvil && evilBrowserInformation.evilVersion > 6)) {
					this.dropDownList.style.left = this.element.viewportOffset().left - 2 + 'px';
				} else {
					var theHeader = $('header');
					this.dropDownList.style.left = (this.element.viewportOffset().left - (theHeader ? theHeader.viewportOffset().left : 0) - 2) + 'px';
				}
				

			}
		}
	},
	
	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';
		}); 
	},
	
	resizeHandler : function() {
		this.calculatePosition();
	},
	
	clickHandler : function() {
		var serviceDropDownHandler = this.serviceDropDownHandler;
		
		if (serviceDropDownHandler.dropDownList.visible()) {
			serviceDropDownHandler.hide();
		} else {
			serviceDropDownHandler.show();
		}

		return false;
	},
	
	mouseOverHandler: function() {
		if (this.visible && !this.forcedClose) {
			this.isOver = true;
			this.isClosing = false;
		}
	},
	
	mouseOutHandler: function() {
		if (this.visible) {
			setTimeout('serviceDropDowns[' + this.idx + '].hide()',3000);
			
			this.isOver = false;
			this.isClosing = true;
		}
	}
	
});

var serviceDropDowns = new Array();

function initServiceDropDown() {
	var dropDowns = $$('.serviceDropDown');
	dropDowns.each(function(el, i) {
		var dropDown = new ServiceDropDown(el, i);
		serviceDropDowns.push(dropDown);
	});
	$(document.body).observe('ajaxLogin:succeeded', function() {
		initServiceDropDown();
	});
}

if ('observe' in document) {
	document.observe('soli:topnavselection:markedactive', function() {
		catHover = new CategoryHover('categorymenu','categoryHover');
		initServiceDropDown();
	});
}
















var refHover = null;
/**
 * Generates hover effect for search refinements.
 *
 * Constructor fields:
 * -------------------
 * refCont  refinement container
 * refHoCont refinement hover container
 */
var RefinementHover = Class.create({
	initialize : function(refCont, refHoCont) {
		this.refCont = $(refCont);
		this.refHoCont = $(refHoCont);
		this.IE6 = false;
		if (this.refHoCont) {
			this.refHoLists = this.refHoCont.getElementsBySelector('ul');
			this.refLinks = this.refCont.getElementsBySelector('span');
			this.actRef = this.refCont.getElementsBySelector('.active span')[0];
			this.over = false;
			this.curHoRef = null;
			this.refLinks.each(function(e,i){
				Event.observe(e,'click',this.showMenu.bind(this));
				Event.observe(e,'mouseout',this.checkVis.bind(this));	
			}.bind(this));		
			Event.observe(this.refHoCont,'mouseover',this.isOver.bind(this));
			Event.observe(this.refHoCont,'mouseout',this.checkVis.bind(this));
			if (navigator.userAgent.indexOf('IE 6') != -1) {
				this.isIE6 = true;
			}			
		
		}
	},		
	
	isOver : function() {
		this.over = true;
	},
	
	checkVis : function (e) {		
		this.over = false;
		setTimeout('refHover.hideMenu()',1000);			
	},
	
	showMenu : function(e) {
		this.over = true;
		var el = Event.element(e);
		
		if(!(el.up().hasClassName('sale') || el.up().hasClassName('new')))
		{
			if(this.curHoRef)
			{
				this.curHoRef.style.backgroundColor = 'transparent';
				this.curHoRef.up().style.backgroundColor = 'transparent';
			}
			
			var pos = this.refLinks.indexOf(el);
			if(pos==-1){
				pos=0;
			}
			this.calcPos(pos);
			if (this.actRef) {
				this.actRef.style.backgroundColor = 'transparent';
				this.actRef.up().style.backgroundColor = 'transparent';
			}
			el.style.backgroundColor = 'white';		
			el.up().style.backgroundColor = 'white';
			this.curHoRef = el;	
				
		}
	},
	
	hideMenu : function() {
		if(this.over == false)
		{
			this.refHoCont.hide();			
			if(this.curHoRef)
			{
				this.curHoRef.style.backgroundColor = 'transparent';
				this.curHoRef.up().style.backgroundColor = 'transparent';
			}
			if (this.actRef) {
				this.actRef.style.backgroundColor = 'white';		
				this.actRef.up().style.backgroundColor = 'white';
			}			
		}
	},
	
	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) {
		var coord = this.refLinks[position].positionedOffset();
		
		coord.left = coord.left - 1;
		coord.top += 19;	
        
		this.refHoCont.style.left =  coord.left+'px';
		this.refHoCont.style.top = coord.top+'px';
		
		this.refHoCont.show();
		this.refHoLists.each(function(e,i){
			e.hide();
		});
		if(this.refHoLists[position])
			this.refHoLists[position].show();
	}
});



if ('observe' in document) {
	document.observe('dom:loaded', function() {
		countryHover = new CountryHover();
		refHover = new RefinementHover('refinementmenu','refinementHover');
		
	});
}
