
// global "enter" and "leave" checks for elements without the usual event-bubbling over/out-repetition problem
function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}

function checkMouseEnter (element, evt) {
  if (element.contains && evt.fromElement) {
    return !element.contains(evt.fromElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

function checkMouseLeave (element, evt) {
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}



// replace searchbox focus/blur functionality, since the native one won't work with the hidden searchbox in place
searchboxFieldObserver = Class.create({
 	initialize: function(ele) {
 		if (!ele) return;
 		this.ele = ele;
 		this.defaultValue = ele.value;
 		
 		Event.observe(this.ele, 'focus', this.focus.bindAsEventListener(this));
 		Event.observe(this.ele, 'blur', this.blur.bindAsEventListener(this));
 	},
 	
	focus: function() {
		if (this.ele.value == this.defaultValue) this.ele.value = '';
	},
	blur: function() {
		if (this.ele.value == '') this.ele.value = this.defaultValue;
	}
});



// open/close search and login boxes on mouseover
headerLinkObserver = Class.create({
	initialize: function(ele, box) {
		if (!box) return;
		this.ele = ele;
		this.box = box;
		
		if (this.box.visible()) this.showBox();
		
		Event.observe(this.ele, 'mouseover', this.over.bindAsEventListener(this));
		Event.observe(this.ele, 'mouseout', this.outEle.bindAsEventListener(this));
		Event.observe(this.box, 'mouseover', this.overBox.bindAsEventListener(this));
		Event.observe(this.box, 'mouseout', this.outBox.bindAsEventListener(this));
		
		if (this.ele.down('a'))
			Event.observe(this.ele.down('a'), 'click', this.clickStop.bindAsEventListener());
	},
	
	over: function(evt) {
		if (!checkMouseEnter(this.ele, evt)) return;
		this.showBox();
	},
	outEle: function(evt) {
		if (!checkMouseLeave(this.ele, evt)) return;
		this.hideBox();
	},
	overBox: function(evt) {
		if (!checkMouseEnter(this.box, evt)) return;
		this.showBox();
	},
	outBox: function(evt) {
		if (!checkMouseLeave(this.box, evt)) return;
		this.hideBox();
	},
	
	showBox: function() {
		this.ele.addClassName('hl_item_over');
		if (this.ele.hasClassName('hl_item5')) this.ele.down('a').addClassName('over');
		this.box.show();
	},
	hideBox: function() {
		this.ele.removeClassName('hl_item_over');
		if (this.ele.hasClassName('hl_item5')) this.ele.down('a').removeClassName('over');
		this.box.hide();
	},
	
	clickStop: function(evt) {
		evt.stop();
	}
});


headerLoggedLinkObserver = Class.create({
	initialize: function(ele) {
		this.ele = ele;
		Event.observe(this.ele, 'mouseover', this.over.bindAsEventListener(this));
		Event.observe(this.ele, 'mouseout', this.out.bindAsEventListener(this));
		Event.observe(this.ele, 'click', this.clickDown.bindAsEventListener(this));
	},
	
	over: function() {
		this.ele.addClassName('hl_item_over');
	},
	out: function() {
		this.ele.removeClassName('hl_item_over');
	},
	clickDown: function(evt) {
		evt.stop();
		if (this.ele.down('a')) location.href = this.ele.down('a').href;
	}
});


document.observe("dom:loaded", function(){
	if ($('searchBox')) new searchboxFieldObserver($('searchBox').down('fieldset.fs_searchField').down('input'));
	
	if ($('hdrLinks') && $('hdrLinks').down('li.hl_item4')) new headerLinkObserver($('hdrLinks').down('li.hl_item4'), $('searchBox'));
	if ($('hdrLinks') && $('hdrLinks').down('li.hl_item5')) new headerLinkObserver($('hdrLinks').down('li.hl_item5'), $('headerLoginbox'));
	
	if ($('hdrLoggedLinks')) {
		var LIs = $('hdrLoggedLinks').select('li');
		LIs.each(function(li) {
			new headerLoggedLinkObserver(li);
		});
	}
});

