/* Resolution Dependant Style Switcher

   This javascript by Cameron Adams
   http://www.themaninblue.com/writing/perspective/2006/01/19/
   ------------------------------------------------------------------------------------------------------------------------*/

checkBrowserWidth();
//attachEventListener(window, "load", checkTextSize, false);
attachEventListener(window, "resize", checkBrowserWidth, false);


function checkBrowserWidth()
{
	var theWidth = getBrowserWidth();
	
	if (theWidth == 0) {
		var resolutionCookie = document.cookie.match(/(^|;)tmib_res_layout[^;]*(;|$)/);
				
		if (resolutionCookie != null)	{
			alert("tmib_res_layout cookie present");
			setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
		}
		
		addLoadListener(checkBrowserWidth);
		
		return false;
	}

	if (theWidth > 1000) {
		setStylesheet("high resolution");
		document.cookie = "tmib_res_layout=" + escape("high resolution");
	}
	else {
		setStylesheet("default");
		document.cookie = "tmib_res_layout=";
	}
	
	return true;
};

function checkTextSize()
{		
	var textSizeCookieVal = getCookie("kf_text_size");
	
	if (textSizeCookieVal != null)	
	{		
	
		var textSize = unescape(textSizeCookieVal);		
		if(textSize!="")
		{
			setTextStylesheet(textSize);
		}
	}
	else {
			
			setTextStylesheet("textsizesmall");
			setCookie("kf_text_size",escape("textsizesmall"));
	}
	
	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 setStylesheet(styleTitle)
{
	var currTag;

	if (document.getElementsByTagName) {
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++) {
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title")) {
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)	{
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
};


function setTextStylesheet(styleTitle)
{
	var currTag;
	
	if (document.getElementsByTagName) {
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++) {
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title") && currTag.getAttribute("title").indexOf("textsize") != -1) 
			{
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)	
				{
					currTag.disabled = false;
					setCookie("kf_text_size",escape(styleTitle));
				}
				
			}
		}
	}
	
	return true;
};


function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
};

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
};

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
};