//cookie functions
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+"; path=/";
}

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;
}

//create toggle link and tell it what to do	
	function toggleLink(layoutType) {
		var resize = document.createElement('a');
		resize.id = 'resize';
		resize.href = '#';
		var header = document.getElementById('header');
		var child2 = header.firstChild.firstChild;
		child2.appendChild(resize);
		resizeText = document.createTextNode('');
		resize.appendChild(resizeText);
		
			if(layoutType == 'fluid'){
				fluid = true;
				fixed = false;
				resize.title = "Switch to Fixed layout";
				resize.firstChild.nodeValue = "Switch to Fixed layout";
			}
			else {
				fluid = false; 
				fixed = true;
				resize.title = "Switch to Fluid layout";
				resize.firstChild.nodeValue = "Switch to Fluid layout";
			}
	
	resize.onclick = function(){
		if(fluid == true){
			setLayout('fixed');
			fixed = true;
			fluid = false;
			this.title = "Switch to Fluid layout";
			this.firstChild.nodeValue = "Switch to Fluid layout";
		}
		else{
			setLayout('fluid');
			fixed = true;
			fluid = true;
			this.title = "Switch to Fixed layout";
			this.firstChild.nodeValue = "Switch to Fixed layout";
		}
		return false;
	}
	
}

//set body id
function setLayout(layoutType) {
	var body = document.getElementsByTagName('body')[0];
	if(layoutType){
		body.className = layoutType;
		createCookie('style', layoutType, 365);
	}
	//else return null;
}


//get the current body id
function getCurrentStyleSheet() {
	var body = document.getElementsByTagName('body')[0];
    if(body.id>0){
	    var layoutType = body.className;
    }
  else var layoutType = 'fixed';
  return layoutType;
}




function loadLayoutType(){
var cookie = readCookie("style");
var layoutType = cookie ? cookie : getCurrentStyleSheet();
toggleLink(layoutType);
setLayout(layoutType);
}



addDOMLoadEvent(loadLayoutType);
