///////////////////////////////////////
// Main.js - Globally used functions
///////////////////////////////////////

/** 
 * Switchy McLayout - Dynamically
 * Code borrowed from http://www.alistapart.com/articles/switchymclayout
 * Modified to set a cookie instead of change the page class
 */
function setScreenClass(){
  // If mobi hasn't been set and no cookie has been set, set the screen class
  if(getParameter('mobi') == 'true') {
    setCookie("screenClass", 'screen_ultralow', 1000);
  } else if(!(foo = getCookie('screenClass'))) {
    var fmt = document.documentElement.clientWidth;
    var cls = (fmt<=240)?'pda_ver':(fmt>240&&fmt<=320)?'pda_hor':(fmt>320&&fmt<=640)?'screen_ultralow':(fmt>640&&fmt<=800)?'screen_low':(fmt>800&&fmt<=1024)?'screen_med':(fmt>1024&&fmt<=1280)?'screen_high':'screen_wide';
    setCookie("screenClass", cls, 1000);
  }
}   
function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=")
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1 
      c_end=document.cookie.indexOf(";",c_start)
      if (c_end==-1) c_end=document.cookie.length
      return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
  return "";
}
function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value) +  ";path=/" + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
} 
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function addUnloadEvent(func) {
  var oldunload = window.unload;
  if (typeof window.unload != 'function') {
    window.unload = func;
  } else {
    window.unload = function() {
      if (oldunload) {
        oldunload();
      }
      func();
    }
  }
}

function getParameter(parameterName ) {
  var queryString = window.location.toString();
  // Add "=" to the parameter name (i.e. parameterName=value)
  parameterName += "=";
  if ( queryString.length > 0 ) {
  
    // Find the beginning of the string
    begin = queryString.indexOf(parameterName);
    // If the parameter name is not found, skip it, otherwise return the value
    if ( begin != -1 ) {
      // Add the length (integer) to the beginning
      begin += parameterName.length;
      // Multiple parameters are separated by the "&" sign
      end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
        end = queryString.length
      }
      // Return the string
      return unescape ( queryString.substring ( begin, end ) );
    }
    // Return "null" if no parameter has been found
    return "null";
  }
} 

/**
 * Add js events to the menu for hover
 */
function setmenu() {
	var menu = document.getElementById('menu');
	
	if(menu) {
	  var menuItems = menu.getElementsByTagName('li');
		for(i=0; i<menuItems.length;i++) {
			if(i == 0) { 
	      menuItems[i].onmouseout = function() {
	        this.className = this.className.replace(" homehover", '');       
	      }
	      menuItems[i].onmouseover = function() {
	        this.className += " homehover";       
	      }				 
			} else{
	      menuItems[i].onmouseout = function() {
	        this.className = this.className.replace("hover",'');        
	      } 			
	      menuItems[i].onmouseover = function() {
	        this.className += " hover";        
	      } 				
			}		
		}
	}
}

addLoadEvent(setmenu)
addLoadEvent(setScreenClass);