//Variable used for calculating the colspan
var colSpanFactor = 4;
//Number of total buttons in the main navigation section
var numOfButtons = 6;
//The id of the button which is last clicked
var buttonLastClicked = 0;
//Hard-coded array defining the button id's and their columnwidths
var buttons = new Array();
buttons[0] = Array(52, "Home", "main.html");
buttons[1] = Array(52, "Vision", "");
buttons[2] = Array(62, "Portfolio", "");
buttons[3] = Array(72, "Think tank", "");
buttons[4] = Array(48, "Links", "");
buttons[5] = Array(52, "Contact", "");

/**
 * Function for manipulating the navigation bar.
 * @param buttonId - The id of the button clicked.
 */
 function changeNavigationBar(buttonId) {
	 //First change the side images of the previous clicked button
	 document.getElementById("begin-" + buttonLastClicked).src = "images/buttonbegin.gif";
	 document.getElementById("end-" + buttonLastClicked).src = "images/buttonend.gif";
	 //Set the side images of the last clicked button
	 document.getElementById("begin-" + buttonId).src = "images/butcornerupleft.gif";
	 document.getElementById("end-" + buttonId).src = "images/butcornerupright.gif";
	 //Set the lit images above the previously clicked button back to normal
	 document.getElementById("litnavoptionup" + buttonLastClicked).src = "images/navscanlines.gif";
	 document.getElementById("litnavoptiondown" + buttonLastClicked).src = "images/navscanlines.gif";
 	 //Adapt the colspan and images of the two navscanline columns
	 if(buttonId == 0 || buttonId == 1 || buttonId == 5) {
		 document.getElementById("litnavoptionup" + buttonId).src = "images/litnavoptionup.gif";
	 	 document.getElementById("litnavoptiondown" + buttonId).src = "images/litnavoptiondown.gif";
	 } else {
		 document.getElementById("litnavoptionup" + buttonId).src = "images/litnavoptionup" + buttonId + ".gif";
	 	 document.getElementById("litnavoptiondown" + buttonId).src = "images/litnavoptiondown" + buttonId + ".gif";
	 }
	 //Change colspan of "navmiddle" element
	 var navMiddleTD = document.getElementById("navmiddle");
	 navMiddleTD.colSpan = (buttonId * colSpanFactor) + 1;
	 //Modify the width of middleselected TD
	 var middleSelectedTD = document.getElementById("middleselected");
	 //alert("middle: " + buttons[buttonId][0]);
	 middleSelectedTD.width = buttons[buttonId][0];
	 //Modify navbar section
	 var navBarTR = document.getElementById("navbar");
	 //If the first button in the row of buttons is clicked
	 //alert("begin-" + buttonLastClicked);	 
	 if(buttonId == 0 && buttonLastClicked > 0) {
		 //Delete spacing TD and TD labeled 'navstripesleft'
		 navBarTR.deleteCell(0);
		 navBarTR.deleteCell(0);
		 //Move up the TD labeled 'navstripesright'
		 var tdNavStripesRight = document.getElementById("navstripesright");
		 if(tdNavStripesRight != null) {
			 tdNavStripesRight.colSpan = (((buttons.length - 1) - buttonId) * colSpanFactor) - 1;
		 }
	 } else if(buttonId == buttons.length - 1 && buttonLastClicked < buttons.length - 1) {
		 //If last button in the row of buttons is clicked
		 //Delete TD labeled 'navstripesright'
		 //Move up the TD labeled 'navstripesleft'
		 navBarTR.deleteCell(navBarTR.cells.length - 1);
		 if(buttonLastClicked > 0) {
			 var tdNavStripesLeft = document.getElementById("navstripesleft");
			 tdNavStripesLeft.colSpan = (buttonId * colSpanFactor) - 1;
		 }
	 }
	 //If one of the buttons is clicked other than the first or the last
	 var tdNavStripesLeft = document.getElementById("navstripesleft");
	 //If TD labeled 'navstripesleft' hasn't been added yet after clicking on the first button
	 if(tdNavStripesLeft == null && buttonId != 0) {
		 //Insert a new TD in the corresponding TR en set its properties
		 tdNavStripesLeft = navBarTR.insertCell(0);
		 tdNavStripesLeft.height = 16;
		 tdNavStripesLeft.id = "navstripesleft";
		 tdNavStripesLeft.style.background = "url(images/subnavstripes.gif)";
	 }
	 if(tdNavStripesLeft) {
		 //Set the colspan value of the 'navstripesleft' column corresponding the button clicked
		 tdNavStripesLeft.colSpan = (buttonId * colSpanFactor) - 1;
	 }
	 var tdBeforeHome = document.getElementById("beforehome");
	 //Also reinsert the TD for spacing purposes which was previously removed when clicked on 
	 //first or last button
	 if(tdBeforeHome == null && buttonId != 0) {
		 tdBeforeHome = navBarTR.insertCell(0);
		 tdBeforeHome.id = "beforehome";
	 }
	 var tdNavStripesRight = document.getElementById("navstripesright");
	 //If TD labeled 'navstripesright' hasn't been added yet after clicking on the last button
	 if(tdNavStripesRight == null && buttonId != buttons.length - 1) {
		 //Insert a 'new' 'navstripesright' TD in the corresponding TR en set its properties
		 tdNavStripesRight = navBarTR.insertCell(navBarTR.cells.length);
		 tdNavStripesRight.height = 16;
		 tdNavStripesRight.id = "navstripesright";
		 tdNavStripesRight.style.background = "url(images/subnavstripes.gif)";
	 }
	 if(tdNavStripesRight != null) {
		 //Set the colspan value of the 'navstripesright' column corresponding the button clicked
		 tdNavStripesRight.colSpan = (((buttons.length - 1) - buttonId) * colSpanFactor) - 1;
	 }
	 buttonLastClicked = buttonId;
 }
 
/**
 * Function for swapping the image of the banner in the navigationbar.
 * @param newImage - The relative url of the image to swap to.
 */
 function swapBannerImage(newImage) {
	var bannerImage = document.getElementById("banner");
	if(bannerImage.src != newImage) {
		bannerImage.src = newImage;
	}
 }

/**
 * Function for generating the navigation of
 * the M4 site dynamically.
 */
 function generateNavigation() {
	var navCode = generateGeneralBegin();
	if(areCookiesEnabled()) {
		//alert("Cookies enabled");
		navCode += generateAdvancedNavigation();
	} else {
		alert("Cookies disabled");
		navCode += generateBasicNavigation();
	}
	navCode += generateGeneralEnd();
	document.write(navCode);
 }
 
 /**
  * Function for generating the advanced version of the M4 navigation.
  */
  function generateAdvancedNavigation() {	
  	  var navCode = "";
	  //var buttonId = parseFloat(getCookieVariable("buttonId"));
	  var navMiddleColSpan = 0;
	  var navStripesColSpan = 0;
	  if(buttonLastClicked > 0) {
		  navMiddleColSpan = (buttonLastClicked * colSpanFactor) + 1;
		  //alert("navMiddleColSpan = " + navMiddleColSpan);
		  navStripesLeftColSpan = (buttonLastClicked * colSpanFactor) - 1;
	  } else {
		  buttonLastClicked = 0;
		  navMiddleColSpan = 1;
		  //alert("navMiddleColSpan = " + navMiddleColSpan);
	  }
	  //alert("buttonId: " + buttonId);
		  navCode += "		<td id=\"navmiddle\" colspan=\"" + navMiddleColSpan + "\" height=\"10\"></td>\n";
		  navCode += " 		<td colspan=\"3\" height=\"10\" bgcolor=\"#FFFFFF\"></td>\n";
		  navCode += "	</tr>\n";
		  navCode += "	<tr>\n";
	  
	  if(navMiddleColSpan > 1) {
		  navCode += "		<td id=\"beforehome\"></td>\n";
		  navCode += "		<td id=\"navstripesleft\" colspan=\"" + navStripesLeftColSpan + "\" background=\"images/subnavstripes.gif\" height=\"16\"></td>\n";
	  } else {
		  //The extra columns aren't necessary
	  }
	  
	  	  navCode += "		<td></td>\n";
		  navCode += "		<td width=\"5\"><img src=\"images/butcornerdownleft.gif\" /></td>\n";
		  navCode += "		<td id=\"middleselected\" bgcolor=\"#FFFFFF\" width=\"" + buttons[buttonLastClicked] + "\"></td>\n";
		  navCode += "		<td width=\"5\"><img src=\"images/butcornerdownright.gif\" /></td>\n";
		  navCode += "		<td></td>\n";
	  
	  if(buttonLastClicked < numOfButtons - 1) {
		  var navStripesRightColSpan = (((numOfButtons - 1) - buttonLastClicked) * colSpanFactor) - 1;
		  navCode += " 		<td id=\"navstripesright\" colspan=\"" + navStripesRightColSpan + "\" background=\"images/subnavstripes.gif\" height=\"16\"></td>\n";
	  } else {
		  //The extra column isn't necessary
	  }
	  
	  return navCode;
  }
  
 /**
  * Function which generates the basic menu, when cookies aren't enabled.
  */
  function generateBasicNavigation() {
	  	  var navCode = "";
	  	  navCode += "		<td id=\"navmiddle\" colspan=\"25\" height=\"10\"></td>\n";
		  navCode += "	</tr>\n";
		  navCode += "	<tr>\n";
		  navCode += "		<td id=\"beforehome\"></td>\n";
		  navCode += "		<td id=\"navstripesleft\" colspan=\"23\" background=\"images/subnavstripes.gif\" height=\"16\"></td>\n";
		  
		  return navCode;
  }
  
 /**
  * Function which generates the general begin of the navigation.
  */
  function generateGeneralBegin() {
	  var navCode = "";
	  navCode += "<table id=\"navigation\" cellpadding=\"0\" cellspacing=\"0\">\n";
	  navCode += "	<tr>\n";
	  navCode += "		<td height=\"13\" colspan=\"31\"></td>\n";
	  navCode += "	</tr>\n";
	  navCode += "	<tr>\n";
	  navCode += "		<td colspan=\"31\"><img src=\"images/navscanlines.gif\" /></td>\n";
	  navCode += "	</tr>\n";
	  navCode += "	<tr>\n";
	  navCode += "		<td height=\"6\" colspan=\"31\"></td>\n";
	  navCode += "	</tr>\n";
	  navCode += "	<tr>\n";
	  navCode += "		<td width=\"6\"></td>\n";
	  //if(areCookiesEnabled()) {
		  //var buttonId = parseFloat(getCookieVariable("buttonId"));
		  /*if(!buttonLastClicked) {
			  buttonLastClicked = 0;
		  }*/
		  for(i=0; i<buttons.length; i++) {
			  if(buttonLastClicked == i) {
	  navCode += "		<td width=\"6\"><img src=\"images/butcornerupleft.gif\" /></td>\n";
	  navCode += "		<td width=\"" + buttons[i][0] + "\" bgcolor=\"#FFFFFF\"><center><a href=\"" + buttons[i][2] + "\" onClick=\"setCookieVariable('buttonId', " + i + ");\">" + buttons[i][1] + "</a></center></td>\n";
	  navCode += "		<td width=\"5\"><img src=\"images/butcornerupright.gif\" /></td>\n";
	  navCode += "		<td width=\"7\"></td>\n";
			  } else {
	  navCode += "		<td width=\"6\"><img src=\"images/buttonbegin.gif\" /></td>\n";
	  navCode += "		<td width=\"" + buttons[i][0] + "\" bgcolor=\"#FFFFFF\"><center><a href=\"" + buttons[i][2] + "\" onClick=\"setCookieVariable('buttonId', " + i + ");\">" + buttons[i][1] + "</a></center></td>\n";
	  navCode += "		<td width=\"5\"><img src=\"images/buttonend.gif\" /></td>\n";
	  navCode += "		<td width=\"7\"></td>\n";
			  }
		  }
	  /*} else {
		  for(i=0; i<buttons.length; i++) {
	  navCode += "		<td width=\"6\"><img src=\"images/buttonbegin.gif\" /></td>\n";
	  navCode += "		<td width=\"" + buttons[i][0] + "\" bgcolor=\"#FFFFFF\"><center><a href=\"" + buttons[i][2] + "\" onClick=\"setCookieVariable(\"buttonId\", " + i + ");\">" + buttons[i][1] + "</a></center></td>\n";
	  navCode += "		<td width=\"5\"><img src=\"images/buttonbegin.gif\" /></td>\n";
	  navCode += "		<td width=\"7\"></td>\n";
		  }
	  }*/
	  /*navCode += "		<td width=\"6\"><img src=\"images/buttonbegin.gif\" /></td>\n";
	  navCode += "		<td id=\"2\" width=\"52\" bgcolor=\"#FFFFFF\"><center><a href=\"\">Vision</a></center></td>\n";
	  navCode += "		<td width=\"5\"><img src=\"images/buttonend.gif\" /></td>\n";
	  navCode += "		<td width=\"7\"></td>\n";
	  navCode += "		<td width=\"6\"><img src=\"images/butcornerupleft.gif\" /></td>\n";
	  navCode += "		<td id=\"3\" width=\"62\" bgcolor=\"#FFFFFF\"><center><a href=\"\">Portfolio</a></center></td>\n";
	  navCode += "		<td width=\"5\"><img src=\"images/butcornerupright.gif\" /></td>\n";
	  navCode += "		<td width=\"7\"></td>\n";
	  navCode += "		<td width=\"6\"><img src=\"images/buttonbegin.gif\" /></td>\n";
	  navCode += "		<td id=\"4\" width=\"72\" bgcolor=\"#FFFFFF\"><center><a href=\"\">Think tank</a></center></td>\n";
	  navCode += "		<td width=\"5\"><img src=\"images/buttonend.gif\" /></td>\n";
	  navCode += "		<td width=\"7\"></td>\n";
	  navCode += "		<td width=\"6\"><img src=\"images/buttonbegin.gif\" /></td>\n";
	  navCode += "		<td id=\"5\" width=\"48\" bgcolor=\"#FFFFFF\"><center><a href=\"\">Links</a></center></td>\n";
	  navCode += "		<td width=\"5\"><img src=\"images/buttonend.gif\" /></td>\n";
	  navCode += "		<td width=\"7\"></td>\n";
	  navCode += "		<td width=\"6\"><img src=\"images/buttonbegin.gif\" /></td>\n";
	  navCode += "		<td id=\"6\" width=\"52\" bgcolor=\"#FFFFFF\"><center><a href=\"\">Contact</a></center></td>\n";
	  navCode += "		<td width=\"5\"><img src=\"images/buttonend.gif\" /></td>\n";
	  navCode += "		<td width=\"6\"></td>\n";*/
	  navCode += "	</tr>\n";
	  navCode += "	<tr>\n";
	  
	  return navCode;
  }
  
 /**
  * Function which generates the general end of the navigation.
  */
  function generateGeneralEnd() {
	  var navCode = "";
	  navCode += "	</tr>\n";
	  navCode += "	<tr>\n";
	  navCode += "		<td colspan=\"31\"></td>\n";
	  navCode += "	</tr>\n";
	  navCode += "	<tr>\n";
	  navCode += "		<td colspan=\"31\"><img src=\"images/navscanlines.gif\" /></td>\n";
	  navCode += "	</tr>\n";
	  navCode += "</table>\n";
	  
	  return navCode;
  }
 
 /**
  * Function which checks if cookies are enabled in clients browser.
  * @return boolean - True when cookies are enabled, false otherwise.
  */
  function areCookiesEnabled() {
	  var cookieEnabled = (navigator.cookieEnabled)? true : false

	  //if not IE4+ nor NS6+
	  if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled){ 
		  document.cookie = "testcookie";
		  cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
  	  }
  	  return cookieEnabled;
  }
  
 /**
  * Function which sets the id of the last clicked button.
  * @param buttonId - Id of the button which was clicked as last.
  */
  function setLastClickedButton(buttonId) {
	  buttonLastClicked = buttonId;
  }
  
 /**
  * Function for getting the value of a corresponding key out of
  * the cookie string.
  * @param key - Key of the value to return.
  * @return value - The value which corresponds with the key.
  */
  function getCookieVariable(key) {
	  key += "=";
	  var value = "";
	  if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(key)
		// if cookie exists
		if (offset != -1) { 
		  offset += key.length
		  // set index of beginning of value
		  end = document.cookie.indexOf(";", offset);
		  // set index of end of cookie value
		  if (end == -1) {
		  	end = document.cookie.length;
		  }
		  value = unescape(document.cookie.substring(offset, end))
		  }
	   }
	   return value;
  }
  
  /**
   * Function which sets a value under a corresponding key
   * in the cookie.
   * @param key - The key of the cookie entry.
   * @param value - The value of the cookie entry.
   */
  function setCookieVariable(key, value) {
	  document.cookie = key + "=" + value;
  }
  
  /**
   * This function opens the theatre window of M4 to showcase media.
   * Utilizing a helper function per type of media (JPG, Flash, etc.) to write
   * the specific html code to the theatre window.
   */
  function openTheatreWindow(titleOfMedia, urlOfMedia) {
	  var fullWindow = window.open('', 'Theatre', 'scrollbars=0,menubar=0,width=670,height=648,resizable=0,toolbar=0,location=0,directories=0,status=0,copyhistory=1,screenX='+(screen.width / 6)+',screenY='+(screen.height / 20)+'');
	  var htmlPage = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">\n';
	  htmlPage += '<html>\n';
	  htmlPage += '<head>\n';
	  htmlPage += '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n';
	  htmlPage += '<title>M4 Theatre</title>\n';
	  htmlPage += '<link href="m4style.css" rel="stylesheet" type="text/css">\n';
	  htmlPage += '</head>\n';
	  htmlPage += '<body>\n';
	  htmlPage += '<table cellpadding="0" cellspacing="0">\n';
	  htmlPage += '\t<tr>\n';
	  htmlPage += '\t\t<td colspan="3"><img src="theatre/theatretop.gif" /></td>\n';
	  htmlPage += '\t</tr>\n';
	  htmlPage += '\t<tr>\n';
	  htmlPage += '\t\t<td><img src="theatre/theatresideleft.gif" /></td>\n';
	  htmlPage += '\t\t<td width="640" height="480">\n';
	  
	  if(urlOfMedia.indexOf(".swf") > -1) {
		  htmlPage += openFlash(urlOfMedia);
	  } else if(urlOfMedia.indexOf(".jpg") > -1 || urlOfMedia.indexOf(".gif") > -1) {
		  htmlPage += openImage(titleOfMedia, urlOfMedia);
	  }
	  
	  htmlPage += '\t\t</td>\n';
	  htmlPage += '\t\t<td><img src="theatre/theatresideright.gif" /></td>\n';
	  htmlPage += '\t</tr>\n';
	  htmlPage += '\t<tr valign="top">\n';
	  htmlPage += '\t\t<td height="75" colspan="3" style="background-image:url(theatre/theatrebottom.gif);">\n';
	  htmlPage += '\t\t\t<table cellpadding="0" cellspacing="0">\n';
	  htmlPage += '\t\t\t\t<tr>\n';
	  htmlPage += '\t\t\t\t\t<td height="12"></td>\n';
	  htmlPage += '\t\t\t\t</tr>\n';
	  htmlPage += '\t\t\t\t<tr>\n';
	  htmlPage += '\t\t\t\t\t<td width="125" height="19"></td>\n';
	  htmlPage += '\t\t\t\t\t<td width="423">\n';
	  htmlPage += '\t\t\t\t\t\t<div style="position: relative">\n';
	  htmlPage += '\t\t\t\t\t\t\t<span class="featuretexttitleshadow">' + titleOfMedia + '</span>\n';
	  htmlPage += '\t\t\t\t\t\t\t<span class="featuretexttitle">' + titleOfMedia + '</span>\n';
	  htmlPage += '\t\t\t\t\t\t</div>\n';
	  htmlPage += '\t\t\t\t\t</td>\n';
	  htmlPage += '\t\t\t\t</tr>\n';
	  htmlPage += '\t\t\t</table>\n';
	  htmlPage += '\t\t</td>\n';
	  htmlPage += '\t</tr>\n';
	  htmlPage += '</table>\n';
	  //htmlPage += '<br />\n';
	  htmlPage += '</body>\n';
	  htmlPage += '</html>';
	  
  	  fullWindow.document.open();
	  fullWindow.document.write(htmlPage);
	  fullWindow.document.close();
  }
  
  /**
   * This function opens a new browser window without any bars and at the size
   * of the image to show. A new html page is written with the image to be shown.
   * @param urlToShow - Address of the image to be shown
   */
  function openImage(titleOfMedia, urlOfMedia) {
	  var htmlPage = '\t\t\t<img src="' + urlOfMedia + '" alt="' + titleOfMedia + '" />\n';
	  return htmlPage;
  }
  
  /**
   * This function opens a new browser window without any bars and at the size
   * of the flash movie to show. A new html page is written with the flash movie to be shown.
   * @param urlToShow - Address of the flash movie to be shown
   */
  function openFlash(urlOfMedia) {
	  var htmlPage = '\t\t\t<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n';
		  htmlPage += '\t\t\tcodebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"\n';
		  htmlPage += '\t\t\tWIDTH="640" HEIGHT="480" ALIGN="">\n';
		  htmlPage += '\t\t\t<PARAM NAME=movie VALUE="' + urlOfMedia + '"><PARAM NAME=quality VALUE=best><EMBED src="' + urlOfMedia + '" quality=best WIDTH="640" HEIGHT="480" NAME="Flashmovie" ALIGN=""\n';
 		  htmlPage += '\t\t\tTYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>\n';
		  htmlPage += '\t\t\t</OBJECT>\n';
	  return htmlPage
  }