﻿Cwo.RegisterNamespace("Cwo.Page");

Cwo.Page.BindPrintButtonClicks = function (PrintButtonCssClass) {
    $("." + PrintButtonCssClass).bind("click", function (e) { window.print(); });
};

Cwo.Page.PreloadImage = function (URL) {
    var img = new Image();
    img.src = URL;
};

Cwo.Page.Browser = function () {
    var browserInfo = this;
    this.IsIE6 = false;
    jQuery.each(jQuery.browser, function (i, val) {
        if ((i === "msie") && (jQuery.browser.version.substr(0, 1) === "6")) {
            browserInfo.IsIE6 = true;
        }
    });
};

Cwo.Page.NavigateWithReferrer = function (URL) {
    // This allows IE to get a referrer from a Javascript redirect
    var referrerLink = document.createElement("a");

    if (typeof (referrerLink.click) === "undefined") {
        window.location.href = URL;
    } else {
        referrerLink.href = URL;
        document.body.appendChild(referrerLink);
        referrerLink.click();
    }
};

Cwo.Page.AjajCalls = new function () {
    // Register is an associative array used to store call types and the current SequenceIndex for each.
    var SequenceIndexes = {};

    // Returns true if the Index parameter matches the current SequenceIndex for the appropriate CallType
    this.IsCurrentSequenceIndex = function (CallType, Index) {
        if (parseInt(SequenceIndexes[CallType], 10) === parseInt(Index, 10)) {
            return true;
        }
        return false;
    };

    this.CreateRequest = function (CallType) {
        if (SequenceIndexes[CallType] === undefined) {
            // We haven't previously had a call of this type during this page lifecycle.
            SequenceIndexes[CallType] = 1;
        } else {
            // We have previously had a call of this type, we need to increment the count.
            var temp = SequenceIndexes[CallType];
            SequenceIndexes[CallType] = temp + 1;
        }
        return parseInt(SequenceIndexes[CallType], 10);
    };
};

Cwo.RegisterNamespace("Cwo.Page.Footer");

Cwo.Page.Footer.Current = "Closed";

Cwo.Page.Footer.Toggle = function (location) {
    var glossary = $("#LocationGlossary"),
        newClass = "Closed";

    glossary.removeClass();
    if (Cwo.Page.Footer.Current !== location) {
        // Click is a new request to show, turn appropriate one on.
        newClass = location;
    }
    Cwo.Page.Footer.Current = newClass;
    glossary.addClass(newClass);
};

Cwo.Page.EventController = new function () {
    // Private Methods
    var eventList = new Cwo.Collection(),
        EventItem = function (eventName, method) {
            this.EventName = eventName;
            this.Method = method;
        };
    return {
        // Public methods
        Subscribe: function (eventName, method) {
            eventList.Add(new EventItem(eventName, method));
        },
        FireEvent: function (eventName) {
            var i;
            for (i = 0; i < eventList.Count(); i += 1) {
                if (eventList.Items[i].EventName === eventName) {
                    // Fire the event
                    eventList.Items[i].Method();
                }
            }
        }
    };
};

$(document).ready(function () {
    Cwo.Page.OnStart = function () {
        $("#FooterLondonLink").bind("click", function () { Cwo.Page.Footer.Toggle("London"); });
        $("#FooterUKLink").bind("click", function () { Cwo.Page.Footer.Toggle("UK"); });

        $("a.Print").bind("click", function (e) {
            window.print();
            Cwo.LogMIActivity('_trackPageview', '/print/' + window.location.href);
        });
    };

    // Exception handling code to log exceptions to the event viewer
    if (window.navigator.userAgent.indexOf("MSIE") > -1) {
        Cwo.Page.OnStart();
        return;
    }

    try {
        Cwo.Page.OnStart();
    } catch (ex) {
        Cwo.Error.TrackError(ex); // re-throw ex if error should propergate
    }
});
