function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
	else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	}
	else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else {
		return false;
	}
}

function gotoAnswer(objGotaq) {
	var objSelectList = document.getElementById('gourl');
	if (objSelectList && (objSelectList.selectedIndex != 0)) {
		document.location.href = objSelectList.options[objSelectList.selectedIndex].value;
	}
}
function addHandlers(){
	var objSelectList = document.getElementById('gourl');
	if (objSelectList) addEvent(objSelectList, 'change', gotoAnswer);
}
addLoadEvent(addHandlers);

function firstCommonAncestor(elm1, elm2){
	var p = elm1.parentNode;
	while( !elm2.descendantOf(p) ){
		p = p.parentNode;
	}
	return p;
}
function stopEvent(e){
	try{
		e.stop();
	}catch(ex){}
}

document.observe('mouseout', function(e){
	var from = e.element();
	var to = e.relatedTarget;
	if( to && !from.descendantOf(to)){
		/* bubble up only to the child elements */
		var stopOn = null;
		if( to.descendantOf(from)){
			stopOn = from.childElements();
		}else{
			// bubble up to first common ancestor's children, see below.
			parent = firstCommonAncestor(to, from);
			stopOn = parent.childElements();
		}
		if( stopOn ){
			stopOn.invoke('observe', 'custom:mouseenter', stopEvent);
		}
		to.fire('custom:mouseenter');
		if( stopOn ){
			stopOn.invoke('stopObserving', 'custom:mouseenter', stopEvent);
		}
	}
	if ( !to || (from !== to && !to.descendantOf(from))) {
		/* mouseleave should bubble up until the to element because we have left all elements up to that one */
		var stopOn = null;
		if( to ){
			if( from.descendantOf(to) ){
				stopOn = to.childElements();
			}else{
				parent = firstCommonAncestor(from, to);
				if( parent && to.descendantOf(parent) ){
					stopOn = parent.childElements();
				}
			}
		}
		if( stopOn ){
			stopOn.invoke('observe', 'custom:mouseleave', stopEvent);
		}
		from.fire('custom:mouseleave');
		if( stopOn ){
			stopOn.invoke('stopObserving', 'custom:mouseleave', stopEvent);
		}
	}
});

function search(containerId) {
	if(!containerId) {
		containerId = "search";
	}
	var _container = $(containerId);
	if(_container) {
		var _advancedOption = _container.select(".advanced-option")[0];
		var _advancedContainer = _container.select(".advanced")[0];

		function toggle() {
			$("search-programme").value = "";
			_advancedContainer.toggle();
			if(_advancedContainer.style.display !== "") {
				_container.removeClassName("show");
			} else {
				_container.addClassName("show");
			}
		}

		function init() {
			_advancedContainer.hide();
			_container.addClassName("option");
			_advancedOption.tabIndex = 0;
			_advancedOption.observe("click", toggle);
			_advancedOption.observe("keydown",
				function(e) {
					switch(e.keyCode) {
						case 13:
						case 32:
							toggle();
							break;
					}
				}
			);
		}

		init();
	}
}
