﻿/*Globals*/
function Globals(){}
Globals.getPageScroll = function () {
    var yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {
        yScroll = document.body.scrollTop;
    }
    var xScroll;
    if (self.pageXOffset) {
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollLeft) {
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {
        xScroll = document.body.scrollLeft;
    }
    return new Array(xScroll, yScroll);
}
Globals.getCacheBuster = function(){
    var f = Math.random();
    return "cb=" + encodeURIComponent(f);
}
Globals.getPageSize = function () {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    if (!window.webkit)
    {
        /*modified by PM to prioritise clientWidth*/
        if (document.documentElement && document.documentElement.clientHeight) {
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        }  
        /*modified by PM to prioritise body element over documentElement element*/
        if (document.body) {
            bodywindowWidth = document.body.clientWidth;
            bodywindowHeight = document.body.clientHeight;
            if (bodywindowWidth < windowWidth)
                windowWidth = bodywindowWidth;
            if (bodywindowHeight < windowHeight)
                windowHeight = bodywindowHeight;
        }
    }
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }
    return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
}
Globals.cancelEvent = function (e) {
    if (e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.cancelBubble = true;
    }
}
var Glamour = {};
Glamour.init = function() {
    var iSites = $get("InternationalSites");
	if (iSites)
	{
	    $addHandler(iSites, "change", Glamour.internationalSites_Change);
	}
};
Glamour.internationalSites_Change = function()
{
    if (this.options[this.selectedIndex].value.length > 0)
    {
        document.location = this.options[this.selectedIndex].value;
    }
};
Glamour.setUpEnterKeyAction = function(inputElement, button) {
    if (inputElement && button) {
        $addHandler(inputElement, "keypress", Glamour.checkForEnterKeyPress);
        inputElement.defaultButton = button;
    }
};
Glamour.checkForEnterKeyPress = function(e) {
    var key = e.keyCode ? e.keyCode : e.rawEvent.keyCode;
    if (key == Sys.UI.Key.enter && this.defaultButton) {
        if (typeof (this.defaultButton) === "function") {
            this.defaultButton();
        }
        else {
            this.defaultButton.click(e);
        }
        try { e.preventDefault(); e.cancelBubble(); }
        catch (eerror) { e.returnValue = false; }
    }
};
Glamour.scrollToErrorMessageInit = function(validationTriggerElementID, validationMessageElementID)
{
    var validationTriggerElement = $get(validationTriggerElementID);
    if (validationTriggerElement)
    {
        var validationMessageElement = $get(validationMessageElementID);
        if (validationMessageElement)
        {
            validationTriggerElement.validationMessageElement = validationMessageElement;
            $addHandler(validationTriggerElement, "click", Glamour.scrollToErrorMessageActivate);
        }
    }
};
Glamour.scrollToErrorMessageActivate = function()
{
    if (this.validationMessageElement && this.validationMessageElement.style.display.length == 0)
    {
        // get the position of the element
        var elementBounds = Sys.UI.DomElement.getBounds(this.validationMessageElement);
        // scroll to the validation summary
        window.scrollTo(0, elementBounds.y);
    }
};

Glamour.resetMaxWidth = function()
{
    var elementsToResize = Glamour.getElementsByClass("fullWidth", document.body);
    var count = 0;
    for (; count < elementsToResize.length; count++)
    {
        elementsToResize[count].style.width = Glamour.getMaxWidth();
    }
};
/* See http://ejohn.org/blog/getelementsbyclassname-speed-comparison/ */
Glamour.getElementsByClass = function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
            node = document;
    if ( tag == null )
            tag = '*';
            
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;    
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
                classElements[j] = els[i];
                j++;
        }
    }
    return classElements;
}

Glamour.hideEmptyAds = function(containerId) {
    var container = document.getElementById(containerId);
    if (!container) return;
    for (var i = 0; i < container.childNodes.length; i++) {
        if (container.childNodes[i].nodeType == 1 && container.childNodes[i].getElementsByTagName("div").length > 0) {
            var adContainer = container.childNodes[i].getElementsByTagName("div")[0];
            if (adContainer &&
                adContainer.getElementsByTagName("a").length == 1 &&
                adContainer.getElementsByTagName("a")[0].getElementsByTagName("img").length == 1 &&
                adContainer.getElementsByTagName("a")[0].getElementsByTagName("img")[0].getAttribute("alt") == "Click here to find out more!") {
                adContainer.parentNode.style.display = "none";
            }
        }
    }
};
Glamour.displayElementOnRolloverMouseOver = function() {
    var activeClassName = "active";
    var sourceElement = this;
    // get all the elements in this collection
    var sourceElementAndSiblings = sourceElement.parentNode.getElementsByTagName(sourceElement.tagName);
    // hide all the elements
    for (var i = 0; i < sourceElementAndSiblings.length; i++) {
        if (sourceElementAndSiblings[i].className && sourceElementAndSiblings[i].className.indexOf(activeClassName) != -1) {
            sourceElementAndSiblings[i].className = sourceElementAndSiblings[i].className.replace(/active/gi, "");
        }
    }
    // display the current element
    if (sourceElement.className && sourceElement.className.length > 0) {
        sourceElement.className += " " + activeClassName;
    }
    else {
        sourceElement.className = activeClassName;
    }
};
Glamour.wireUpDisplayElementOnRollover = function(containerId) {
    var container = $("#" + containerId)[0];
    if (!container) {
        if (console && console.log) console.log("Container with ID: " + containerId + " not found (Glamour.wireUpDisplayElementOnRollover)");
        return;
    }
    // get the items that will trigger the rollover
    var items = new Array();
    for (var i = 0; i < container.childNodes.length; i++) {
        if (container.childNodes[i].nodeType === 1) {
            items.push(container.childNodes[i]);
        }
    }
    if (items.length == 0) {
        if (console && console.log) console.log("Could not find any items within container. containerId: " + containerId + " itemTagName: " + itemTagName + " (Glamour.wireUpDisplayElementOnRollover)");
        return;
    }
    // set up the rollover
    for (var i = 0; i < items.length; i++) {
        $addHandler(items[i], "mouseover", Glamour.displayElementOnRolloverMouseOver);
    }
};
/*Glamour.wireUpDisplayElementOnRollover = function(containerId, itemTagName, toggleDisplayTagName) {
    // get the container element
    var container = $("#" + containerId);
    if (!container) {
        if (console && console.log) console.log("Container with ID: " + containerId + " not found (Glamour.wireUpDisplayElementOnRollover)");
        return;
    }
    // get the items that will trigger the rollover
    var items = $("#" + containerId + " " + itemTagName);
    if (!items || items.length == 0) {
        if (console && console.log) console.log("Could not find any items within container. containerId: " + containerId + " itemTagName: " + itemTagName + " (Glamour.wireUpDisplayElementOnRollover)");
        return;
    }
    // set up the rollover
    for (var i = 0; i < items.length; i++) {
        $addHandler(items[i], "mouseover", Glamour.displayElementOnRolloverMouseOver);
    }
    // configure the rollover items
    for (var i = 0; i < items.length; i++) {
        var toggleElement = items[i].getElementsByTagName(toggleDisplayTagName);
        if (toggleElement && toggleElement.length > 0)
        {
            toggleElement = toggleElement[0];
            items[i].toggleElement = toggleElement;
        }
        else if (console && console.log)
        {
            console.log("Could not find the toggleElement for an item. containerId: " + containerId + " itemTagName: " + itemTagName + " (Glamour.wireUpDisplayElementOnRollover)");
        }
    }
};*/
Glamour.setUpTreeMenu = function(elementId) {
    if (!$("#" + elementId)) {
        return;
    }
    $("#" + "CategoryTreeMenu span").click(Glamour.treeMenu_ItemClick);
};
Glamour.treeMenu_ItemClick = function() {
    var list = $(this).siblings()[0];
    if (list.style.display === "none") {
        this.className = "active";
        list.style.display = "block";
    }
    else {
        this.className = "";
        list.style.display = "none";
    }
}
if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7)
{
    $addHandler(window, 'load', Glamour.init); 
	$addHandler(window, 'resize', Glamour.resetMaxWidth);
}
function fireEvent(obj, evt) {

    var fireOnThis = obj;
    if (document.createEvent) {
        var evObj = document.createEvent('MouseEvents');
        evObj.initEvent(evt, true, false);
        fireOnThis.dispatchEvent(evObj);
    } else if (document.createEventObject) {
        fireOnThis.fireEvent('on' + evt);
    }
}