var head=document.getElementsByTagName("head");
var pageWidthLink=document.getElementById("pageWidthLink");

function updateWidth(){
	var wW;

	if(window.innerWidth){
		//for non-IE
		wW=window.innerWidth;
		} else if (document.documentElement.clientWidth){
		//for IE 6 strict 
		wW=document.documentElement.clientWidth;
		} else if (document.body.clientWidth){
		//for IE 5-7 quirks and IE 4
		wW=document.body.clientWidth;
		} else {
		wW=0;
		}

	if(wW>1270){ //if the window is wide enough
		if(browser!="Chrome" && browser!="Safari"){
			//if the browser is not Chrome nor Safari,
			//it's okay to change the page width and show the pageWidth link
			
			if(getCookie("c_pageWidth")=="narrow"){ //if cookie says narrow
				//take out the widerPage link
				removeWiderPageCss();
				} else {
				//if window is wide enough AND the cookie says wide or there's no cookie
				//add the widerPage <link>
				createWiderPageCss();
				}
				
			//if window is wide enough, show the option to go wide and narrow
			if(document.getElementById("widerPageCss")){
				updatePageWidthLinkText("narrow");
				} else {
				updatePageWidthLinkText("wide");
				}
			}
		} else {
		//if the window is not wide enough
		//take out the pageWidthLink option
		updatePageWidthLinkText("none");
		//take out the widerPage <link>
		removeWiderPageCss();
		}
	
	window.onresize=function(){
		updateWidth();
		}
	}

function createWiderPageCss(){
	setCookie("c_pageWidth","wide",1);
	if(!document.getElementById("widerPageCss")){
		var widerPageCss=document.createElement("link");
		widerPageCss.setAttribute("id","widerPageCss");
		widerPageCss.setAttribute("href","http://ivanwlam.com/blog/flush/wp-content/themes/ivanwlam/styles/widerPage.css");
		widerPageCss.setAttribute("rel","stylesheet");
		widerPageCss.setAttribute("type","text/css");
		head[0].appendChild(widerPageCss);
		}
	}

function removeWiderPageCss(){
	setCookie("c_pageWidth","narrow",1);
	if(document.getElementById("widerPageCss")){
		var widerPageCss=document.getElementById("widerPageCss");
		head[0].removeChild(widerPageCss);
		//as of 5/5/09 chrome and safari doesn't revert the stylesheet back,
		//even though it removes the widerPageCss <link>
		}
	}

function updatePageWidthLinkText(opt){
	if(opt=="narrow"){
		pageWidthLink.innerHTML='<a onclick="widerPageSwitch(\'narrow\');">Narrow</a> | ';
		} else if (opt=="wide"){
		pageWidthLink.innerHTML='<a onclick="widerPageSwitch(\'wide\');">Wide</a> | ';
		} else {
		pageWidthLink.innerHTML="";
		}
	}

function widerPageSwitch(opt){
	if(opt=="narrow"){
		removeWiderPageCss();
		updatePageWidthLinkText("wide");
		} else if (opt=="wide"){
		createWiderPageCss();
		updatePageWidthLinkText("narrow");
		}
	}
	
window.onload=updateWidth();


