function attachEventListener(target, eventType, functionRef, capture)
{
	if (typeof target.addEventListener != "undefined") target.addEventListener(eventType, functionRef, capture);
	else if (typeof target.attachEvent != "undefined") target.attachEvent("on" + eventType, functionRef);
	else return false;
	return true;
}

function checkBrowserWidth()
{
	var w = getBrowserWidth();
	if(w>0 && this.lastWidth!=w)
	{
		if(w <= 1124)
		{
			setActiveStyleSheet("LowRes","Res");
			createCookie('nug_res_layout', 'LowRes', 1825); 
		}
		else if(w > 0)
		{
			setActiveStyleSheet("HiRes","Res");
			createCookie('nug_res_layout', 'HiRes', 1825); 
		}
		this.lastWidth=w;
	}
	else
		return false;
	return true;
}

function getBrowserWidth()
{
	if(window.innerWidth) return window.innerWidth;
	else if(document.documentElement && document.documentElement.clientWidth != 0) return document.documentElement.clientWidth;
	else if(document.body) return document.body.clientWidth;
	return 0;
}

function setActiveStyleSheet(title,part)
{
//	alert('called set, t:'+title+' p:'+part);
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("title")
			&& (!part || a.getAttribute("title").indexOf(part) != -1)
			)
		{
//			alert('checkin t:'+a.getAttribute("title")+' h:'+a.getAttribute("href"));
			a.disabled = true;
			if(a.getAttribute("title") == title)
				a.disabled = false;
		}
	}
}

function getActiveStyleSheet(part)
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("title")
			&& (!part || a.getAttribute("title").indexOf(part) != -1)
			&& !a.disabled) return a.getAttribute("title");
	}
	return null;
}

function getPreferredStyleSheet(part)
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
		if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("rel").indexOf("alt") == -1
			&& a.getAttribute("title")
			&& (!part || a.getAttribute("title").indexOf(part) != -1)
			) return a.getAttribute("title");
	return null;
}

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;
}

function setAllStylesheets()
{
	var c;
	c = readCookie("style");
	if(!c || c == null || c == 'null') c = getPreferredStyleSheet("Font");
	if(!c || c == null || c == 'null') c = "MediumFont";
	setActiveStyleSheet(c,"Font");

//	c = readCookie("nug_res_layout");
//	if(!c || c == null || c == 'null') c = getPreferredStyleSheet("Res");
//	if(!c || c == null || c == 'null') c = "LowRes";
//	setActiveStyleSheet(c,"Res");
}

window.onload = function(e)
{
	setAllStylesheets();
//	checkBrowserWidth();
//	attachEventListener(window, "resize", checkBrowserWidth, false);
}

window.onunload = function(e)
{
	createCookie("style", getActiveStyleSheet("Font"), 1825);
	createCookie("nug_res_layout", getActiveStyleSheet("Res"), 1825);
}

var lastWidth = -1;
setAllStylesheets();
