/* 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) {
	var element = getElement(el);
	var subElement = getElement(el + '_submenu');
	var docWidth = getDocumentSize()[0];
	var compensation = 0;

	var rightPosition = element.parentNode.offsetLeft + subElement.offsetWidth;
	
	if (rightPosition > docWidth) {
		compensation = rightPosition - docWidth;
	}
	
	var rightStyle = (0 - ((subElement.offsetWidth - element.offsetWidth) - compensation)) + 'px';
	if (rightStyle != subElement.style.right)
	{
		subElement.style.right = rightStyle;
	}
	
	if (getElement(firstEl).parentNode.offsetTop == getElement(lastEl).parentNode.offsetTop) {
		getStyle(el).visibility = 'visible';	
		getStyle("selectedContainer").visibility = 'hidden';
	}
}

function precalculateSubmenuPosition( i, docWidth )
{
	var element = getElement( 'pm' + i );
	if (element)
	{
		var subElement = getElement('pm' + i + '_submenu');
		var compensation = 0;
		
		subElement.parentNode.style.width = element.offsetWidth + 'px';

		var rightPosition = element.parentNode.offsetLeft + subElement.offsetWidth;
		if ((rightPosition) > docWidth) {
			compensation = (rightPosition) - docWidth;
		}
		if (subElement.offsetWidth > element.offsetWidth)
		{
			subElement.style.right = '-' + ((subElement.offsetWidth - element.offsetWidth) - compensation) + 'px';
		}
		else
		{
			subElement.style.right = element.offsetWidth - subElement.offsetWidth + 'px';	
		}
		
		precalculateSubmenuPosition( i+1 );
	}
}

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/xopus10e1.html?rnd=1.0#content=" + url;
   }
  }
}

// HTTPS onclick handler (dirty hack :)
if (document.location && document.location.href.substring(0,6) == "index%21normal.html") {
  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) == "../index.html" ) {
    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 getInnerWindowSize () {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  var size = new Array();
  size[0] = myWidth;
  size[1] = myHeight;
  
  return size;
}

function getDocumentSize () {
  var x = 0;
  var y = 0;
  
  var test1 = document.body.scrollHeight;
  var test2 = document.body.offsetHeight
  if (test1 > test2) {
    // all but Explorer Mac
	x = document.body.scrollWidth;
	y = document.body.scrollHeight;
  } else {
    // Explorer Mac;
    //would also work in Explorer 6 Strict, Mozilla and Safari
	x = document.body.offsetWidth;
	y = document.body.offsetHeight;
  }
  
  var size = new Array();
  size[0] = x;
  size[1] = y;
  
  return size;
}

function autoAdjustSize () {
  var innerSize = getInnerWindowSize();
  var documentSize = getDocumentSize();
  
  // This is a popup window: Set the overflow to 'auto', so scrollbars
  // in IE are only shown when needed.
  if (document.getElementsByTagName) {
    document.getElementsByTagName('HTML')[0].style.overflow = 'auto';
  }
  
  /* Select a decent height for the popup window */
  var newHeight = 239; // Default height (won't be used)

  if (documentSize[1] <= 100) {
    // Don't do anything if the document size is very small.
    // That should not happen.
    return true;
  } else if (documentSize[1] > 480) {
    // If the document is very big, make the window a little bigger,
    // and add scrollbars.
    newHeight = 480;
    
  } else {
    newHeight = documentSize[1];
  }

  // + 30 for scrollbars for scrollbars (30 is just enough for IE)
  window.resizeBy(0, newHeight - innerSize[1] + 30); 
}

function popupWindow(theUrl) {
  var properties = new Array();
  properties['toolbar'] = 0;
  properties['location'] = 0;
  properties['directories'] = 0;
  properties['status'] = 0;
  properties['menubar'] = 0;
  properties['scrollbars'] = 1;
  properties['resizable'] = 1;
  properties['width'] = 600;
  properties['height'] = 239;
  properties['left'] = 150;
  
  var propString = '';
  for (var i in properties) {
    propString = propString + i + '=' + properties[i] + ',';
  }
  if (propString.length > 0) {
    propString = propString.substr(0, propString.length - 2);
  }
  
  nw = window.open(theUrl,'popup',propString);
  if (!nw) {
    // popup blocked...
    return false;
  }
  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 == "")) {
		style2.display = "block";
		style2.visibility = "visible";
	} else if ((style2.visibility == "hidden") || (style2.visibility == "")) {
		style2.visibility = "visible";
	} else {
		style2.visibility = "hidden";
	}
}

function expand(id)
{
	var style2 = getElement(id).style;
	if (style2.display == "none") {
		style2.display = "block";
		style2.visibility = "visible";
	} else if (style2.visibility == "hidden") {
		style2.visibility = "visible";
		style2.display = "block";
	} else {
		style2.visibility = "hidden";
		style2.display = "none";
	}
}

function toggleClass(elm, clazz1, clazz2, id)
{
	var style2 = getElement(id).style;
	if (style2.display == "none") {
		elm.className = clazz1;
	} else if (style2.visibility == "hidden") {
		elm.className = clazz1
	} else {
		elm.className = clazz2;
	}
}

worldmapWindow = null;
function openWorldMap(reqTab) {
    if (reqTab == undefined) {
     reqTab = "";
    }
	// set a default screen resolution in case we can't find out what the real one is
	var screenWidth = 800;
	var screenHeight = 600;
	var windowWidth = 800;
	var windowHeight = 550;
	worldWindow = null;
	if (screen.width) { screenWidth = screen.width; }
	if (screen.height) { screenHeight = screen.height; }
	var topPosition = Math.round((screenWidth-windowWidth)/2);
	var leftPosition = Math.round((screenHeight-windowHeight)/2);
    var myURL = "international/worldmap/worldmap.html";
    if (reqTab != "") {
     myURL += "?reqTab=" + reqTab;
    }
	worldWindow = window.open(myURL,worldmapWindow,"location=0,menubar=0,resizable=0,width=" + windowWidth + ",height=" + windowHeight);
	worldWindow.moveTo(topPosition,leftPosition);
}