/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("ND");

ND.PageTools = function(element) {
    ND.PageTools.initializeBase(this, [element]);

    this._includeIncreaseFontSize = false;
    this._includeDecreaseFontSize = false;
    this._includePrint = false;
    this._includeEmailFriend = false;
    this._includeContactUs = false;

    this._incEl = null;
    this._decEl = null;
    this._printEl = null;
    this._emailEl = null;
    this._contEl = null;

    this._defaultFontSize = "62.5%";
    this._fontSizeCookieName = "fontSize";
    this._fontSizesList = "42.5%,52.5%,62.5%,72.5%,82.5%";
    this._fontSizes = {}; //set with initialiseFontSize()
}

ND.PageTools.prototype = {
    get_includeIncreaseFontSize: function() {
        return this._includeIncreaseFontSize;
    },
    set_includeIncreaseFontSize: function(val) {
        this._includeIncreaseFontSize = val;
    },
    get_includeDecreaseFontSize: function() {
        return this._includeDecreaseFontSize;
    },
    set_includeDecreaseFontSize: function(val) {
        this._includeDecreaseFontSize = val;
    },
    get_includePrint: function() {
        return this._includePrint;
    },
    set_includePrint: function(val) {
        this._includePrint = val;
    },
    get_includeEmailFriend: function() {
        return this._includeEmailFriend;
    },
    set_includeEmailFriend: function(val) {
        this._includeEmailFriend = val;
    },
    get_includeContactUs: function() {
        return this._includeContactUs;
    },
    set_includeContactUs: function(val) {
        this._includeContactUs = val;
    },
    printPage: function() {
        window.print();
    },
    createCookie: function(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());

    },
    readCookie: function(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;

    },
    eraseCookie: function(name) {
        this.createCookie(name, "", -1);
    },
    get_fontSize: function() {
        var fontSizePref = this.readCookie(this._fontSizeCookieName);
        if (fontSizePref) {
            return fontSizePref;
        }
        return this._defaultFontSize;
    },
    set_fontSize: function(fontSize) {
        var index = this._fontSizes.indices[fontSize];
        if (index >= 0 && index < this._fontSizes.sizes.length) {
            document.body.style.fontSize = fontSize;
            if (fontSize != this._defaultFontSize) {
                this.createCookie(this._fontSizeCookieName, fontSize, 1);
                return;
            }
        }
        this.eraseCookie(this._fontSizeCookieName);
    },
    increaseFontSize: function() {
        var fontSize = this.get_fontSize();
        var newIndex = this._fontSizes.indices[fontSize] + 1;
        if (newIndex > -1 && newIndex < this._fontSizes.sizes.length) {
            this.set_fontSize(this._fontSizes.sizes[newIndex]);
        }
    },
    decreaseFontSize: function() {
        var fontSize = this.get_fontSize();
        var newIndex = this._fontSizes.indices[fontSize] - 1;
        if (newIndex > -1 && newIndex < this._fontSizes.sizes.length) {
            this.set_fontSize(this._fontSizes.sizes[newIndex]);
        }
    },
    initialiseFontSize: function() {
        this._fontSizes.sizes = this._fontSizesList.split(",").sort();
        this._fontSizes.indices = {};

        for (var i = 0; i < this._fontSizes.sizes.length; i++) {
            this._fontSizes.indices[this._fontSizes.sizes[i]] = i;
        }

        var fontSizePref = this.get_fontSize();
        if (fontSizePref != this._defaultFontSize) {
            this.set_fontSize(fontSizePref);
        }
    },
    contactUs: function() {
        window.location.href = "/Pages/Contact-Us.aspx";
    },
    emailFriend: function() {
        window.location.href = "/Pages/Email-A-Friend.aspx?url=" + window.location.pathname + '&title=' + document.title.replace(' - VECCI', '');
    },
    initialize: function() {
        ND.PageTools.callBaseMethod(this, 'initialize');

        this.initialiseFontSize();

        if (this.get_includeIncreaseFontSize()) {
            this._incEl = document.createElement("a");
            this._incEl.id = this.get_element().id + "_increase";
            this._incEl.className = "increase-font-size";
            this._incEl.alt = "Increase Font Size";
            $addHandler(this._incEl, "click", Function.createDelegate(this, this.increaseFontSize));
            this.get_element().appendChild(this._incEl);
        }

        if (this.get_includeDecreaseFontSize()) {
            this._decEl = document.createElement("a");
            this._decEl.id = this.get_element().id + "_decrease";
            this._decEl.className = "decrease-font-size";
            this._decEl.alt = "Decrease Font Size";
            $addHandler(this._decEl, "click", Function.createDelegate(this, this.decreaseFontSize));
            this.get_element().appendChild(this._decEl);
        }

        if (this.get_includeContactUs()) {
            this._contEl = document.createElement("a");
            this._contEl.id = this.get_element().id + "_contact";
            this._contEl.className = "contact-us";
            this._contEl.alt = "Contact Us";
            $addHandler(this._contEl, "click", Function.createDelegate(this, this.contactUs));
            this.get_element().appendChild(this._contEl);
        }

        if (this.get_includeEmailFriend()) {
            this._emailEl = document.createElement("a");
            this._emailEl.id = this.get_element().id + "_email";
            this._emailEl.className = "email-friend";
            this._emailEl.alt = "Email a Friend";
            $addHandler(this._emailEl, "click", Function.createDelegate(this, this.emailFriend));
            this.get_element().appendChild(this._emailEl);
        }

        if (this.get_includePrint()) {
            this._printEl = document.createElement("a");
            this._printEl.id = this.get_element().id + "_print";
            this._printEl.className = "print";
            this._printEl.alt = "Print";
            $addHandler(this._printEl, "click", Function.createDelegate(this, this.printPage));
            this.get_element().appendChild(this._printEl);
        }
        // Add custom initialization here
    },
    dispose: function() {
        //Add custom dispose actions here
        $clearHandlers(this._incEl);
        $clearHandlers(this._decEl);
        $clearHandlers(this._contEl);
        $clearHandlers(this._emailEl);
        $clearHandlers(this._printEl);
        ND.PageTools.callBaseMethod(this, 'dispose');
    }
}
ND.PageTools.registerClass('ND.PageTools', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
