// :mode=js:tabSize=4:indentSize=4:noTabs=true:

/**
 * All your copyrights are belong to [ i ] motion design
 * One day we are going to have a big license block here
 * that is going to kick your ass!
*/

linkTags = document.getElementsByTagName("link");
onloadFunction();

function onloadFunction() {
    var layoutCookie = readCookie("layout");
    var layoutTitle = (layoutCookie && layoutCookie != "null") ? layoutCookie : getPreferredStyleSheet("title", "layout");
    var windowWidth, windowHeight;
    if (self.innerWidth) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    } else {
        windowWidth = 620;
        windowHeight = 460;
    }
    if (windowWidth < 790) {
        layoutTitle = "no watermark";
    }
    setActiveStyleSheet(layoutTitle);
}

function changeImage(imageId, fileName)
{
    document.getElementById(imageId).src = fileName;
    return true;
}

function openWindow(uri)
{
    //(uri,windowWidth,windowHeight,showScrollbars,showChrome)
    var windowFeatureList;
    var windowWidth = "";
    var windowHeight = "";
    var windowScrollbars = "";
    var windowChrome = "";
    var showScrollbars = 1;
    var showChrome = 1;
    
    if (arguments[1] != null) windowWidth = "width=" + arguments[1] + ",";
    if (arguments[2] != null) windowHeight = "height=" + arguments[2] + ",";
    if (arguments[3] != null) showScrollbars = arguments[3];
    windowScrollbars = "scrollbars=" + showScrollbars + ",";
    if (arguments[4] != null) showChrome = arguments[4];
    windowChrome = "location=" + showChrome + ",menubar=" + showChrome + ",status=" + showChrome + ",titlebar=" + showChrome + ",toolbar=" + showChrome;
    
    windowFeatureList = windowWidth + windowHeight + windowScrollbars + windowChrome;
    newWindow = window.open(uri, "External", windowFeatureList);
    newWindow.focus();
    return true;
}


function setActiveStyleSheet()
{
    var i, j, linkTag;
    var enabledCount = 0;
    for (i = 0; i < linkTags.length; i++) {
        linkTag = linkTags[i];
        if (linkTag.getAttribute("rel").indexOf("style") != -1 && linkTag.getAttribute("title")) {
            linkTag.disabled = true;
            for (j = 0; j < arguments.length; j++) {
                if (linkTag.getAttribute("title") == arguments[j]) {
                    linkTag.disabled = false;
                    enabledCount++
                    break;
                }
            }
        }
    }
}

function getActiveStyleSheet(attribute, type)
{
    var i, linkTag, title;
    for (i = 0; i < linkTags.length; i++) {
        linkTag = linkTags[i];
        if (linkTag.getAttribute("rel").indexOf("style") != -1 && linkTag.getAttribute("title") && !linkTag.disabled) {
            if (type != null && linkTag.getAttribute("title").indexOf(type) != -1) {
                return linkTag.getAttribute(attribute);
            } else if (type == null) {
                return linkTag.getAttribute(attribute);
            }
        }
    }
    return null;
}

function getPreferredStyleSheet(attribute, type)
{
    var i, linkTag;
    for (i = 0; i < linkTags.length; i++) {
        linkTag = linkTags[i];
        if (linkTag.getAttribute("rel").indexOf("style") != -1
                && linkTag.getAttribute("rel").indexOf("alt") == -1
                && linkTag.getAttribute("title")
        ) {
            if (type != null && linkTag.getAttribute("title").indexOf(type) != -1) {
                return linkTag.getAttribute(attribute);
            } else if (type == null) {
                return linkTag.getAttribute(attribute);
            }
        }
    }
    return null;
}


/*========== cookie utlity functions ==========*/

function createCookie(name, value, day) {
    if (day) {
        var newDate = new Date();
        newDate.setTime(newDate.getTime() + (day*24*60*60*1000));
        var expires = "; expires=" + newDate.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name)
{
    var nameEQ = name + "=";
    var cookieArray = document.cookie.split(";");
    for (var i=0; i < cookieArray.length; i++) {
        var cookieSingle = cookieArray[i];
        while (cookieSingle.charAt(0) == " ") {
            cookieSingle = cookieSingle.substring(1, cookieSingle.length);
        }
        if (cookieSingle.indexOf(nameEQ) == 0) {
            return cookieSingle.substring(nameEQ.length, cookieSingle.length);
        }
    }
    return null;
}

