
checkBrowserWidth();

attachEventListener(window, "resize", checkBrowserWidth, false);




function checkBrowserWidth()
{
	var theWidth = getBrowserWidth();
	
	if (theWidth == 0)
	{
		var resolutionCookie = document.cookie.match(/(^|;)tmib_res_layout[^;]*(;|$)/);

		if (resolutionCookie != null)
		{
			setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
		}
		
		addLoadListener(checkBrowserWidth);
		
		return false;
	}

	if (theWidth < 1000)
	{
		setStylesheet("800");
		document.cookie = "tmib_res_layout=" + escape("800");
	}
	else
	{
		// The parameter is currently not defined as the 1024.css
		// style sheets do not have the title attribute set.
		setStylesheet("");
		document.cookie = "tmib_res_layout=";
	}
	
	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 (isWidthStyle(currTag))
			{

				// alert("Disable css: " + currTag.getAttribute("href"));
				currTag.disabled = true;
				// if(currTag.getAttribute("title") == styleTitle)
				if(matchStyle(currTag, styleTitle))
				{
				       // alert("Enable css: " + currTag.getAttribute("href"));
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
};

function isWidthStyle(a) {
    if (!a) return false;
    // DO not attempt to mtch on the title attribute as not all pages
    // have the title attribute set for width based style sheets for the
    // default 1024 style sheets..
    // if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
     if (a.getAttribute("rel").indexOf("style") != -1 )
 {

        var re=new RegExp("(1024|800)\.css$", "i");
        if (a.getAttribute("href").match(re)) {
            return true;
        }
    }
    return false;
}

function matchStyle(a, mysize) {
    if (!a) return false;
    // The default is 1024 if the size is not defined.
    if (mysize == "") {
	mysize = "1024";
    }
    if (a.getAttribute("rel").indexOf("style") != -1 )
    {

        var re=new RegExp("-"+mysize+"\.css$", "i");
        if (a.getAttribute("href").match(re)) {
            return true;
        }
    }
    return false;

}

