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

function checkBrowserWidth() {
    var theWidth = getBrowserWidth();
    if (theWidth == 0) {
        var resolutionCookie = document.cookie.match(/(^|;)res_layout[^;]*(;|$)/);
        if (resolutionCookie != null) {
            setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
        }
        addLoadListener(checkBrowserWidth);
        return false;
    }
    if (theWidth <= 1010) {
        setStylesheet("735");
        document.cookie = "res_layout=" + escape("735");
    }
    else if ((theWidth > 1010) && (theWidth <= 1255)) {
        setStylesheet("980");
        document.cookie = "res_layout=" + escape("980");
    }
    else if ((theWidth > 1255) && (theWidth <= 1505)) {
        setStylesheet("1225");
        document.cookie = "res_layout=" + escape("1225");
    }
    else if ((theWidth > 1505) && (theWidth <= 1745)) {
        setStylesheet("1470");
        document.cookie = "res_layout=" + escape("1470");
    }
    else if ((theWidth > 1745) && (theWidth <= 1990)) {
        setStylesheet("1715");
        document.cookie = "res_layout=" + escape("1715");
    }
    else if ((theWidth > 1990) && (theWidth <= 2235)) {
        setStylesheet("1960");
        document.cookie = "res_layout=" + escape("1960");
    }
    else if (theWidth > 2235) {
        setStylesheet("2205");
        document.cookie = "res_layout=" + escape("2205");
    }
	else {
        setStylesheet("");
        document.cookie = "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 (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title")) {
                currTag.disabled = true;
                if (currTag.getAttribute("title") == styleTitle) {
                    currTag.disabled = false;
                }
            }
        }
    }
    return true;
};


