﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../js-docs/jquery-1.2.6-vsdoc.js" />
var DEFAULT_FONT_SIZE = "site-container resize12";
var FONT_SIZE_14 = "site-container resize14";
var FONT_SIZE_16 = "site-container resize16";
var FONT_SIZE_COOKIE_NAME = "ctc_fontSize";
var FONT_SIZES_STRING = "site-container resize12,site-container resize14,site-container resize16";
var FONT_SIZES = {}; //set with initialiseFontSize()

var DEFAULT_PAGE_COLOR = "default";
var PAGE_COLOR_COOKIE_NAME = "ctc_pageColor";
var PAGE_COLORS_STRING = "default,grey,black";
var PAGE_COLORS = {}; //set with initialisePageSize()

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 + "=" + 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 setFontSizeByIndex(idx) {

    if (FONT_SIZES)
        setFontSize(FONT_SIZES.sizes[idx]);
}

function setFontSize(fontSize) {
    var index = FONT_SIZES.indices[fontSize];
    if (index >= 0 && index < FONT_SIZES.sizes.length) {        
        $(".site-container").attr("class", 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(",");
    FONT_SIZES.indices = {};

    for (var i = 0; i < FONT_SIZES.sizes.length; i++) {
        FONT_SIZES.indices[FONT_SIZES.sizes[i]] = i;
    }

    var fontSizePref = getFontSize();
    setFontSizeIcons(fontSizePref);
    if (fontSizePref != DEFAULT_FONT_SIZE) {
        setFontSize(fontSizePref);
    }
}

function setFontSizeIcons(fontSizePref)
{
    $(".size12").removeClass("selected");
    $(".size14").removeClass("selected");
    $(".size16").removeClass("selected"); 

    if(fontSizePref == DEFAULT_FONT_SIZE)       
    {
        $(".size12").addClass("selected");
    }
    else if (fontSizePref == FONT_SIZE_14){
        $(".size14").addClass("selected");
    }
    else if (fontSizePref == FONT_SIZE_16){
        $(".size16").addClass("selected");
    }
}

/* Page Colors */
function getPageColor() {
    var pageColorPref = readCookie(PAGE_COLOR_COOKIE_NAME);
    if (pageColorPref) {
        return pageColorPref;
    }
    return DEFAULT_PAGE_COLOR;
}

function setPageColorByIndex(idx) {
    setPageColor(PAGE_COLORS.sizes[idx]);
}

function setPageColor(pageColor) {
    var index = PAGE_COLORS.indices[pageColor];
    if (index >= 0 && index < PAGE_COLORS.sizes.length) {
        resetPageColors(); // For IE
        $(".site-container").attr("id", pageColor);
        /* This section needed for IE */
        if(pageColor == "black") {
            $("#black .header .ctc-logo .black-logo").css({'display':'block'});
        }
        else if(pageColor == "grey") {
            $("#grey .header .ctc-logo .grey-logo").css({'display':'block'});
        } 
        else {
            $(".header .ctc-logo .default-logo").css({'display':'block'});
        }
        if (pageColor != DEFAULT_PAGE_COLOR) {
            createCookie(PAGE_COLOR_COOKIE_NAME, pageColor, 1);
            return;
        }
    }
    eraseCookie(PAGE_COLOR_COOKIE_NAME);
}
function resetPageColors()
{
        $("#black .header .ctc-logo .black-logo").css({'display':'none'});
        $("#grey .header .ctc-logo .grey-logo").css({'display':'none'});
        $(".header .ctc-logo .default-logo").css({'display':'none'});
}
function initialisePageColor() {
    PAGE_COLORS.sizes = PAGE_COLORS_STRING.split(",");
    PAGE_COLORS.indices = {};

    for (var i = 0; i < PAGE_COLORS.sizes.length; i++) {
        PAGE_COLORS.indices[PAGE_COLORS.sizes[i]] = i;
    }

    var pageColorPref = getPageColor();
    if (pageColorPref != DEFAULT_PAGE_COLOR) {
        setPageColor(pageColorPref);
    }
}


Sys.Application.add_load(Function.createDelegate(this, initialiseFontSize));
Sys.Application.add_load(Function.createDelegate(this, initialisePageColor));

