﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../js-docs/jquery-1.2.6-vsdoc.js" />
var DEFAULT_FONT_SIZE = "72.5%";
var FONT_SIZE_COOKIE_NAME = "fontSize";
var FONT_SIZES_STRING = "42.5%,52.5%,62.5%,72.5%,82.5%";
var FONT_SIZES = {}; //set with initialiseFontSize()
var TEXT_COOKIE_NAME = "textMode";


function initialisePageTools() {
    /// <summary>
    /// sets up the tools toolbar for the page
    /// </summary>
    if (Sys.Application) {
        var pageTools = $get("fontTools");
        pageTools.style.display = "block";
    }
}

function printPage() {
    window.print();
}

function createCookie(name, value, days) {
    /// <summary>
    /// Creates a cookie within the browser
    /// </summary>
    /// <param name="name" type="String">Name of the cookie</param>
    /// <param name="value" type="Object">Value of the cookie</param>
    /// <param name="days" type="Integer">Number of days the cookie to to be active for</param>
    var exdate = new Date();
    exdate.setTime(exdate.getTime() + (days * 24 * 60 * 60 * 1000));
    document.cookie = name + "=" + escape(value) + ((days == null) ? "" : ";expires=" + exdate.toGMTString());

}

function readCookie(searchName) {
    /// <summary>
    /// retrieves the cookie from the browser context
    /// </summary>
    /// <param name="name" type="String">Name of the cookie to locate</param>

    // note: document.cookie only returns name=value, not the other components
    var cookiesArray = document.cookie.split(';');
    for (var i = 0; i < cookiesArray.length; i++) {

        // now we'll split apart each name=value pair
        var cookieTmp = cookiesArray[i].split('=');

        // and trim left/right whitespace while we're at it
        var cookieName = cookieTmp[0].replace(/^\s+|\s+$/g, '');
        // if the extracted name matches passed search_name
        if (cookieName == searchName) {
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (cookieTmp.length > 1) {
                return unescape(cookieTmp[1].replace(/^\s+|\s+$/g, ''));
            }
            // cookie is initialized but no value => result = null
            return null;
        }
    }
    return null;

}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function getFontSize() {
    var fontSizePref = readCookie(FONT_SIZE_COOKIE_NAME);
    if (fontSizePref) {
        return fontSizePref;
    }
    return DEFAULT_FONT_SIZE;
}

function setFontSize(fontSize) {
    var index = FONT_SIZES.indices[fontSize];
    if (index >= 0 && index < FONT_SIZES.sizes.length) {
        $('body').css('font-size', fontSize);
        if (fontSize != DEFAULT_FONT_SIZE) {
            createCookie(FONT_SIZE_COOKIE_NAME, fontSize, 1);
            return;
        }
    }
    eraseCookie(FONT_SIZE_COOKIE_NAME);
}

function increaseFontSize() {
    var fontSize = getFontSize();
    var newIndex = FONT_SIZES.indices[fontSize] + 1;
    if (newIndex > -1 && newIndex < FONT_SIZES.sizes.length) {
        setFontSize(FONT_SIZES.sizes[newIndex]);
    }
}

function decreaseFontSize() {
    var fontSize = getFontSize();
    var newIndex = FONT_SIZES.indices[fontSize] - 1;
    if (newIndex > -1 && newIndex < FONT_SIZES.sizes.length) {
        setFontSize(FONT_SIZES.sizes[newIndex]);
    }
}

function initialiseFontSize() {
    FONT_SIZES.sizes = FONT_SIZES_STRING.split(",").sort();
    FONT_SIZES.indices = {};

    for (var i = 0; i < FONT_SIZES.sizes.length; i++) {
        FONT_SIZES.indices[FONT_SIZES.sizes[i]] = i;
    }

    var fontSizePref = getFontSize();
    if (fontSizePref != DEFAULT_FONT_SIZE) {
        setFontSize(fontSizePref);
    }
}

function textOnly() {
    var cookie = getTextMode();
    if (cookie) {
        if (cookie === "on") {
            toTextMode();
            makeOffCookie();
        } else {
            toStandardMode();
            makeOnCookie();
        }
    } else {
        makeOnCookie();
    }
}

function toStandardMode() {
    $('img').each(function() {
        $(this).css("display", "inline");
    });
    
    $("#headerBanner").css("visibility","visible");
    $("#eureka_header").css("height","377px");
}

function toTextMode() {
    $('img').each(function() {
        $(this).css("display", "none");
    });
    
    $("#headerBanner").css("visibility","hidden");
    $("#eureka_header").css("height","102px");
}

function makeOnCookie() {
    createCookie(TEXT_COOKIE_NAME, "on", 1);
}

function makeOffCookie() {
    createCookie(TEXT_COOKIE_NAME, "off", 1);
}

function getTextMode() {
    return readCookie(TEXT_COOKIE_NAME);
}

function initTextMode() {
    var cookie = getTextMode();
    if (cookie) {
        if (cookie === "off") {
            toTextMode();
        }
    } else {
        makeOnCookie();
    }
}

Sys.Application.add_load(Function.createDelegate(this, initialiseFontSize));
Sys.Application.add_load(Function.createDelegate(this, initialisePageTools));
Sys.Application.add_init(Function.createDelegate(this, initTextMode));
