/* Script for popup menu's Webplatform

	getStyle(name): routine to get the style object of an element with name 'name'
	showMenu(el): routine to show the submenu
	hideMenu(el): routine to hide the submenu again
	highlight(el): routine to highlight the submenu-item 'el' (ie switch styles)
	unhighlight(el): routine to turn the submenu-item 'el' back to normal (ie switch styles)

*/


function getStyle(name) {
  if (document.getElementById) return (document.getElementById(name)? document.getElementById(name).style : new Object()); // extra check to see if object really exists!
  else if (document.all) return (document.all[name]?document.all[name].style: new Object());  // extra check to see if object really exists!
  else return new Object(); // dummy
}

function getElement(name) {
  if (document.getElementById) return document.getElementById(name);
  else if (document.all) return document.all[name];
  else return new Object(); // dummy
}


function toggleAdvancedSearch(el,visiblity)
{	
	var elObj = getElement(el);
	if(elObj!=null)
		getStyle(el).visibility = visiblity;
}

function showMenu(el,firstEl,lastEl) {
	if (getElement(firstEl).parentNode.offsetTop == getElement(lastEl).parentNode.offsetTop) {
		getStyle(el).visibility = 'visible';	
		getStyle("selectedContainer").visibility = 'hidden';
	}
}

function findElementBelowWithClass(el,classname)
{
	var root = getElement(el).getElementsByTagName("div") ;
	for (var i=0; i<root.length;i++)
	{
		if (root[i].className == classname) 
		{	
			return root[i];
		}
	}return null;
}

function myIsElder(elderName, childElement) {
	var elder = getElement(elderName);
	var found = false;
	var parent = childElement.parentNode;
	while (parent != null && !found) {
		if (parent == elder) {
			found = true;
		}
		parent = parent.parentNode;
	}
	return found;
}

function hideMenu(el, event) {
	var dst = null;
	
	if (event) {
		if (event.toElement) {
			dst = event.toElement;
		} else if (event.relatedTarget) {
			dst = event.relatedTarget;
		}
	}
        
	var found = false;
	if (dst) {
		found = myIsElder(el, dst);
	}
	if (!found) {
		getStyle(el).visibility = 'hidden';
		getStyle("selectedContainer").visibility = 'visible';
	}
}

function highlight(el) {
	el.className = "w3portalsubmenuitemhighlight";
	return true;
}


function unhighlight(el) {
	el.className = "w3portalsubmenuitem";
	return true;
}

function highlightSectionMenu(current, selected) {
  if (selected != 'si' && selected != current) {
    var SItem = getElement(selected);
    var CItem = getElement(current);
    SItem.className = "w3SBMItem";
    CItem.className = "w3SBMSelectedItem";
  }
  else if (selected == 'si') {
    var CItem = getElement(current);
    CItem.className = "w3SBMSelectedItem";
  }

  return true;
}


function unhighlightSectionMenu(current, selected) {
  if (selected != 'si' && selected != current) {
    var SItem = getElement(selected);
    var CItem = getElement(current);
    SItem.className = "w3SBMSelectedItem";
    CItem.className = "w3SBMItem";    
  }
  else if (selected == 'si') {
    var CItem = getElement(current);
    CItem.className = "w3SBMItem";    
  }
    
  return true;
}


/* form disabler */

var dis_form = null;

function disableForm() {
  if (dis_form) {
    for (var i = 0; i < dis_form.elements.length; i++) {
        dis_form.elements[i].disabled='true';
    }
  }
}

function showBusy(form) {
  dis_form = form;
  setTimeout("disableForm();", 500); 
    if (document && document.body && document.body.style) {
        document.body.style.cursor = 'wait';
    }
} 

/* Xopus calls: */

function openXopus(url, mouse_event) {
	
  if (mouse_event.ctrlKey) {
   // ctrl-mouse click, open SpyX 
    document.location = "/_definition/shared/js/spyx.html#content=" + url; 
    mouse_event.cancelBubble = true;
  }
  else {
   var __xopus_frame = window;

   while (__xopus_frame && (__xopus_frame.name != 'xopus' || 
			    (!__xopus_frame.xopus_globs))) {
     if (__xopus_frame == top) {
       __xopus_frame = null;
       break;
     } else {
       __xopus_frame = __xopus_frame.parent;
     }
   }
   
   if (__xopus_frame && __xopus_frame.name == 'xopus' && 
       __xopus_frame.xopus_globs && __xopus_frame.xopus_globs.framework) {
     // already inside xopus: don't open frame
     document.location.href = url;
   }
   else {
     // open frame
     document.location.href = "/_definition/shared/xopus/xopus.html?rnd=1.0#content=" + url;
   }
  }
}

// HTTPS onclick handler (dirty hack :)
if (document.location && document.location.href.substring(0,6) == "https:") {
  if (document.attachEvent) { //IE
    document.attachEvent('onclick', removeLinks);
  }
  else if (document.addEventListener) { //Mozilla
    document.addEventListener('click', removeLinks, false);
  }
}

function removeLinks(e) {
  // removes http://www.rug.nl from "a href"'s, so the user stays on https
  var element = null;
  if (e && e.srcElement && e.srcElement.tagName && e.srcElement.tagName == "A") {
    /*IE*/
    element = e.srcElement;
  }
  else if (e && e.target && e.target.parentNode && e.target.parentNode.nodeName == "A") {
    element = e.target.parentNode;
  }

  if (element && element.href.substring(0,17) == "http://www.rug.nl" ) {
    e.returnValue = false;
    e.cancelBubble = true;
    if (e.stopPropagation) 
      e.stopPropagation();
    if (e.cancelable) {
      e.preventDefault();
    }
    document.location = element.href.substring(17,element.href.length);
  }
}

function getLang(){
  if (document.location.search) { 
    var queryString = document.location.search;
    var data = queryString.slice(1,queryString.length);
    dataArray = data.split("&");         
    for (j=0;j<dataArray.length;j++){
      data2 = dataArray[j].split("=");
      if (data2[0] && data2[1] && data2[0] == "lang")
	return (data2[1]);
    }
  }
  return("");
}


function popupWindow(theUrl) {
	/*
  var lang = getLang();
  if (lang && lang != "") {
    theUrl += "?lang=" + lang;
  }
  if (document.location.href.indexOf("!") > -1)
  {
  	theUrl = document.location.href.substring(0, document.location.href.indexOf("!")) + theUrl; 
  }
  else if (document.location.href.indexOf("?") > -1)
  {
  	theUrl = document.location.href.substring(0, document.location.href.indexOf("?")) + theUrl; 
  }
  else
  {	
  	theUrl = document.location.href + theUrl; 
  }
	*/
  nw = window.open(theUrl,'popup','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=600,height=239,left=150');
  //nw = window.open(theUrl,'popup','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=600,height=239,left=150');
  nw.focus();
}

function relocate(url) {
	//alert(url);
	if (window.opener) {
		window.opener.location=url;
		window.opener.focus();
	}
	window.close();
	
}

function showMaintenance() {
  var menu = getElement("w3portalmenuobject");
  if (menu) {
    if (getLang() != "" && getLang() != "nl")
      menu.innerHTML = menu.innerHTML + "<br><a style='color:white' href='http://www.rug.nl/webplatform/techniek/onderhoud/20041028_02'>Today maintenance from 17:00 until 20:00</a>";
    else
      menu.innerHTML = menu.innerHTML + "<br><a style='color:white' href='http://www.rug.nl/webplatform/techniek/onderhoud/20041028_02'>Vandaag onderhoud van 17:00 tot 20:00</a>";
  }
}

var date = new Date();

if (date.getDate() == 29 && date.getMonth() == 9 && (date.getYear() == 2004 || date.getYear() == 104) && date.getHours() < 17) { 
  if (window.addEventListener) 
    window.addEventListener("load",  showMaintenance, false);
  else if (window.attachEvent)
    window.attachEvent("onload", showMaintenance);
}

function redir(d, u)
{
	ml = 'm'+'a'+'i'+'l'+'t'+'o';
	window.location=ml+":"+u+"@"+d;
}

/* Floating div for media object display */

function toggleLayer(whichLayer)
{
	var style2 = getElement(whichLayer).style;
	if (style2.display == "none") {
		style2.display = "block";
		style2.visibility = "visible";
	} else if (style2.visibility == "hidden") {
		style2.visibility = "visible";
	} else {
		style2.visibility = "hidden";
	}
}

