//This file contains all the functions to set and read a cookie that switches our CSS to make the
//layout either fixed-width or liquid.

//setup initialisation function, wires onPageLoad to fire once content has been loaded
//.. gecko, safari, konqueror and generic
if(typeof window.addEventListener != 'undefined') {
   document.addEventListener('DOMContentLoaded', onPageLoad, false); 
   }
//.. opera 7
else if(typeof document.addEventListener != 'undefined') {
   document.addEventListener('load', onPageLoad, false); 
   }
//.. win/ie
else if(typeof window.attachEvent != 'undefined') {
   window.attachEvent('onload', onPageLoad); 
   }

//This function gets called as soon as the page has loaded
function onPageLoad() {
   checkForCookie(); 
   //Wire up events to our display switching button
   var displaySwitcher_btn = document.getElementById("switchButton"); 
   if (displaySwitcher_btn != null) {
      displaySwitcher_btn.onclick = getStyle; 
      displaySwitcher_btn.onkeyboardpress = getStyle; 
      }
   }

//Checks for a layout cookie, applies relevant style
function checkForCookie() {
   var cookie = readCookie("style"); 
   var title = cookie; 
   setActiveStyleSheet(title); 
   }

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; 
   }

function setActiveStyleSheet(title) {
   var i, a, main; 
   for(i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
      if(a.getAttribute("rel").indexOf("style") != - 1 && a.getAttribute("title")) {
         a.disabled = true; 
         if(a.getAttribute("title") == title && title != "Default") {
            a.disabled = false; 
            createCookie("style", title); 
            //We need to check for IE 7 and feed it a stylesheet with some tweaks
            if (navigator.userAgent.toLowerCase().indexOf("ie 7") != - 1) {
               var head = document.getElementsByTagName("head")[0]; 
               var ie7link = document.createElement('link'); 
               ie7link.setAttribute('rel', 'stylesheet'); 
               ie7link.setAttribute('type', 'text/css'); 
               ie7link.setAttribute('href', '/CSS/ie7liquid.css'); 
               ie7link.setAttribute('media', 'screen'); 
               ie7link.setAttribute('id', 'ie7Link'); 
               head.appendChild(ie7link); 
               }
            else if (navigator.userAgent.toLowerCase().indexOf("ie 6") != - 1) {
               var head = document.getElementsByTagName("head")[0]; 
               var ie7link = document.createElement('link'); 
               ie7link.setAttribute('rel', 'stylesheet'); 
               ie7link.setAttribute('type', 'text/css'); 
               ie7link.setAttribute('href', '/CSS/ie6liquid.css'); 
               ie7link.setAttribute('media', 'screen'); 
               ie7link.setAttribute('id', 'ie6Link'); 
               head.appendChild(ie7link); 
               }
            }
         }
      else if(title == "Default") {
         eraseCookie("style"); 
         var ie7Node = document.getElementById("ie7Link"); 
         if (ie7Node != null) {
            document.getElementsByTagName("head")[0].removeChild(ie7Node); 
            }
         var ie6Node = document.getElementById("ie6Link"); 
         if (ie6Node != null) {
            document.getElementsByTagName("head")[0].removeChild(ie6Node); 
            }
         }
      }
   }

function createCookie(name, value) {
   document.cookie = name + "=" + value + "; path=/"; 
   }

//Gets the selected style from the style switcher drop down
function getStyle() {
   var styleSelectorDropDown = document.getElementById("navDesignSelector"); 
   if (styleSelectorDropDown != null) {
      var styleName = styleSelectorDropDown.options[styleSelectorDropDown.selectedIndex].value; 
      if (styleName != "choose") {
         setActiveStyleSheet(styleName); 
         }
      }
   }

function eraseCookie(name) {
   createCookie(name, "", - 1); 
   }
