<!-- TODOA: add javascript if required ->

/*** Hooks ********************************************************************/
/**
  * The container of hooks.
  */
var eventHooks = new Object();

/**
  * Add a function to be called on a given event
  *
  * @param where The event the function is to be added to
  * @param fn The function reference to use
  */
function registerHook(where, fn) {
  if (!eventHooks[where]) {
    eventHooks[where] = new Array()
  }
  eventHooks[where][eventHooks[where].length] = fn;
}
 
/**
  * Call each hook found for the given event
  *
  * @param where the event type
  */
function callHooks(where) {
  if (eventHooks[where]) {
    for (var i = 0; i < eventHooks[where].length; i++) {
      var fn = eventHooks[where][i];
      result = fn();
      if (! result) {
        return false;
      }
    }
  }
  else {
    //Empty by design
  }
  return true;
}

/**
  * Add 'onLoad' hooks to onload.
  */
window.onload=function(e) {
  callHooks('onLoad');
}

/**
  * Add 'onUnLoad' hooks to onunload.
  */
window.onunload=function(e) {
  callHooks('onUnLoad');
}
/******************************************************************** Hooks ***/




/*** Cookies ******************************************************************/
/**
  * Creates a named cookie
  *
  * @param name the name of the cookie
  * @param value the value of the cookie
  * @param days The time for the cookie to live
  */
function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    var expires = ";expires=" + date.toGMTString();
  }
  else {
    expires = "";
  }
  document.cookie = name + "=" + value + expires + ";domain=www.idg.no;path=/";
}

/**
  * Read a named cookie
  *
  * @param name the name of the cookie to reed.
  */
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
/****************************************************************** Cookies ***/




/*** CSS Switcher *************************************************************/
function setActiveStyleSheet(title) {
  var i, link, main;
  for(i=0; (link = document.getElementsByTagName("link")[i]); i++) {
    if(link.getAttribute("rel").indexOf("style") != -1 && link.getAttribute("title")) {
      link.disabled = true;
      if(link.getAttribute("title") == title) {
        link.disabled = false;
      }
    }
  } 
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) {
      return a.getAttribute("title");
    }
  }
  return null;
}

function checkForCookie(){
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}


function loadActiveStyleSheet() {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  
  return true;
} 

function saveActiveStyleSheet() {
  var title = getActiveStyleSheet();
  createCookie("style", title, 100);
  
  return true;
}

registerHook('onLoad', loadActiveStyleSheet);
registerHook('onUnLoad', saveActiveStyleSheet);
/************************************************************* CSS Switcher ***/



/*** Utils ********************************************************************/
/**
  *
  */
function hasClass(element, className) {
	if (!element.className) return false;
	return (element.className.search('(^|\\s)' + className + '(\\s|$)') != -1);
}

/**
  * Elements with the given name, and className.
  *
  * @param pElementName The name or type of element to find
  * @param pClassName The name of the class this element should have
  * @return an array of elements of given name with class
  */
function getElementsWithClassName(pElementName, pClassName) {
	var allElements = document.getElementsByTagName(pElementName);
	var elemColl = new Array();
	for (var i = 0; i< allElements.length; i++) {
		if (hasClass(allElements[i], pClassName)) {
			elemColl[elemColl.length] = allElements[i];
		}
	}
	return elemColl;
}
/******************************************************************** Utils ***/


/* (#) file: commonScripts.js */

/*********************************************************************/
/* Setter lik høyde på div-tagger med samme ID */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");

	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function setTall(searchClass,node,tag) {

	if (document.getElementById) {
		// the divs array contains references to each column's div element.
		// Replace 'center' 'right' and 'left' with your own.
		// Or remove the last one entirely if you've got 2 columns.  Or add another if you've got 4!

		var divs = new getElementsByClass(searchClass,node,tag);

		// Let's determine the maximum height out of all columns specified
		var maxHeight = 0;
		for (var i = 0; i < divs.length; i++) {
			if (divs[i].offsetHeight > maxHeight) maxHeight = divs[i].offsetHeight;
		}

		// Let's set all columns to that maximum height

		for (var i = 0; i < divs.length; i++) {
			divs[i].style.height = maxHeight + 'px';

			// Now, if the browser's in standards-compliant mode, the height property
			// sets the height excluding padding, so we figure the padding out by subtracting the
			// old maxHeight from the new offsetHeight, and compensate!  So it works in Safari AND in IE 5.x
			if (divs[i].offsetHeight > maxHeight) {
				divs[i].style.height = (maxHeight - (divs[i].offsetHeight - maxHeight)) + 'px';
			}
		}
	}
}

window.onload = function() {
	setTall('equalheight', document, '*');
}

window.onresize = function() {
	setTall('equalheight', document, '*');
}
/*********************************************************************/

/* end (#) file: commonScripts.js */

function findHighest(name) {
  var height = 0;
  var elements = getElementsWithClassName('div', name);
  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    if (height < element.offsetHeight) {
      height = element.offsetHeight;
    }
  }
  return height;
}

function initStuff() {
  var height = findHighest('row-1');
  var elements = getElementsWithClassName('div', 'row-2');
  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    element.style.top = (height + 10) + 'px';
  }
}

registerHook('onLoad', initStuff);
