﻿
    // Object Prototyping
    // ******************
    String.prototype.trim = function ()
    {
        var _this = this;
        try
        {
            return _this.replace(/^\s+|\s+$/g, "");
        }
        finally
        {
            _this = null;
        }
    };

    String.prototype.escape = function ()
    {
        return escape(this);
    };

    String.prototype.unEscape = function ()
    {
        return unescape(this);
    };

    String.prototype.urlEncode = function ()
    {
        return this.replace(new RegExp("&amp;", "g"), "&").escape().replace(new RegExp("[+]", "g"), "%2B");
    };

    String.prototype.urlDecode = function ()
    {
        return this.replace(new RegExp("%2B", "g"), "+").unEscape();
    };

    // Common
    // ******
    var isIE6 = (document.all && !window.opera && !window.XMLHttpRequest ? true : false);

    function attachEventHandler(element, eventName, fn)
    {
        if (window.attachEvent)
            element.attachEvent("on" + eventName, function (ev) { return fn(element, typeof event != "undefined" ? event : ev); });
        else if (window.addEventListener)
            element.addEventListener(eventName, function (ev) { return fn(element, ev); }, false);
    }

    function clickButton(btn, event)
    {
        if ((event.keyCode && event.keyCode == 13) || (event.which && event.which == 13))
        {
            var oBtn = document.getElementById(btn);
            if (oBtn)
            {
                event.returnValue = false;
                event.cancel = true;
                if (event.preventDefault) event.preventDefault();
                oBtn.click();
            }
        }
    }

    // DOM Helper
    var DOMHelper = {};
    DOMHelper.left = function (element)
    {
        if (!element) return 0;
        var _curleft = 0;
        if (element.offsetParent)
            while (true)
            {
                _curleft += element.offsetLeft;
                if (!element.offsetParent)
                    break;
                element = element.offsetParent;
            }
        else if (element.x)
            _curleft += element.x;
        return _curleft;
    };
    DOMHelper.top = function (element)
    {
        if (!element) return 0;
        var _curleft = 0;
        if (element.offsetParent)
            while (true)
            {
                _curleft += element.offsetTop;
                if (!element.offsetParent)
                    break;
                element = element.offsetParent;
            }
        else if (element.y)
            _curleft += element.y;
        return _curleft;
    };
    DOMHelper.viewportWidth = function ()
    {
        if (typeof window.innerWidth != 'undefined')
            return window.innerWidth;
        else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
            return document.documentElement.clientWidth;
        else
            return document.getElementsByTagName('body')[0].clientWidth;
    };
    DOMHelper.viewportHeight = function ()
    {
        if (typeof window.innerWidth != 'undefined')
            return window.innerHeight;
        else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
            return document.documentElement.clientHeight;
        else
            return document.getElementsByTagName('body')[0].clientHeight;
    };
    DOMHelper.viewportScrollTop = function ()
    {
        if (document.body.scrollTop > 0)
            return document.body.scrollTop;
        else
        {
            if (window.pageYOffset)
                return window.pageYOffset;
            else
                return document.body.parentNode ? document.body.parentNode.scrollTop : 0;
        }
    };
    DOMHelper.addClass = function (el, className)
    {
        this.removeClass(el, className);
        el.className += " " + className;
    };
    DOMHelper.removeClass = function (el, className)
    {
        var 
            _classes = el.className.split(" "),
            _newClassString = "";

        for (var i = 0; i < _classes.length; i++)
        {
            if (_classes[i] != className)
                _newClassString += _classes[i] + " ";
        }

        el.className = _newClassString.trim();
    };
    DOMHelper.preventSelect = function(element)
    {
        if (element.style.MozUserSelect !== undefined)
            element.style.MozUserSelect = "none";
        else if (element.style.webkitUserSelect !== undefined)
            element.style.webkitUserSelect = "none";

        element.setAttribute("unselectable", "on");
    }

    // Tweener
    // *******
    function tweener(stepChangedCallBack, easing)
    {
        this.timeoutID = 0;
        this.stepChangedCallBack = stepChangedCallBack;
        this.easingFunction = typeof easing != "undefined" ? easing : tweener.easing.quinticOut;
        this.fps = 20;
        this.msPerFrame = 1000 / this.fps;
        this.tweenInProgress = false;
    }
    tweener.prototype.tween = function (startValue, offsetValue, ms, objectContext)
    {
        var 
            _this = this,
            _step = 0;

        _this.tweenInProgress = true;

        function _nextStep()
        {
            if (_step <= ms / _this.msPerFrame)
            {
                var _stepChangedCallBackResult = undefined;

                if (_this.stepChangedCallBack)
                    _stepChangedCallBackResult = _this.stepChangedCallBack(_this.easingFunction(_step, startValue, offsetValue, ms / _this.msPerFrame), objectContext);

                if (_stepChangedCallBackResult == undefined || _stepChangedCallBackResult)
                {
                    _step += 1;
                    _this.timeoutID = setTimeout(_nextStep, _this.msPerFrame);
                }
                else
                    _this.tweenInProgress = false;
            }
            else
                _this.tweenInProgress = false;
        }
        _this.timeoutID = setTimeout(_nextStep, 1);
    };
    tweener.prototype.cancel = function ()
    {
        clearTimeout(this.timeoutID);
    };
    tweener.easing = {};
    tweener.easing._reverse = function (eq, t, b, c, d)
    {
        return -eq(d - t, b, c, d) + c + 2 * b;
    };
    tweener.easing._inToInOut = function (easeIn, t, b, c, d)
    {
        t = t * 2;
        return (t < d ? easeIn(t, b, c / 2, d) : tweener.easing._reverse(easeIn, t - d, b + c / 2, c / 2, d));
    };
    tweener.easing._outToInOut = function (easeOut, t)
    {
        t = 2 * t;
        return 0.5 * (t < 1 ? 1 - easeOut(1 - t) : 1 + easeOut(t - 1));
    };
    tweener.easing._inOutPairToInOut = function (easeIn, easeOut, t)
    {
        t = 2 * t;
        return 0.5 * (t < 1 ? easeIn(t) : 1 + easeOut(t - 1));
    };
    tweener.easing.quinticIn = function (t, b, c, d)
    {
        return c * (t /= d) * t * t * t * t + b;
    };
    tweener.easing.quinticOut = function (t, b, c, d)
    {
        return tweener.easing._reverse(tweener.easing.quinticIn, t, b, c, d);
    };
    tweener.easing.quinticInOut = function (t, b, c, d)
    {
        return tweener.easing._inToInOut(tweener.easing.quinticIn, t, b, c, d);
    };

    // Left Navigation
    // ***************
    function leftNavigation()
    {
        this.element = null;
        this.expandCollapsers = [];
        this.tweener = null;
        this.ulHeight = 0;
        this.tweenDirection = 0;
        this.initialise();
    }
    leftNavigation.prototype.initialise = function ()
    {
        var _this = this;
        _this.element = document.getElementById("LeftNavigation");
        _this.getExpandCollapsers(_this.element);
        _this.tweener = new tweener(function (value, objectContext) { return _this.tweenerStepChanged(value, objectContext); });
    };
    leftNavigation.prototype.getExpandCollapsers = function (startNode)
    {
        var _this = this;
        if (startNode)
        {
            for (var i = 0; i < startNode.childNodes.length; i++)
            {
                if (startNode.childNodes[i].nodeName == "A" && startNode.childNodes[i].className.substr(0, 15) == "expand-collapse")
                {
                    _this.expandCollapsers.push(startNode.childNodes[i]);
                    startNode.childNodes[i].href = "#";
                    attachEventHandler(startNode.childNodes[i], "click", function (sender, ev)
                    {
                        _this.toggleExpandCollapse(sender, ev);
                        if (ev.preventDefault)
                            ev.preventDefault();
                        ev.returnValue = false;
                        return false;
                    });
                }
                else
                    _this.getExpandCollapsers(startNode.childNodes[i]);
            }
        }
    };
    leftNavigation.prototype.tweenerStepChanged = function (value, objectContext)
    {
        var 
            _newHeight = 0,
            _percentHeight = 0;

        if (this.tweenDirection == 0)
            _newHeight = Math.ceil(value * this.ulHeight);
        else
            _newHeight = this.ulHeight - Math.ceil(value * this.ulHeight);

        _percentHeight = _newHeight / this.ulHeight;

        objectContext.style.height = _newHeight + "px";
        objectContext.style.opacity = _percentHeight;
        objectContext.style.filter = 'alpha(opacity = ' + (_percentHeight * 100) + ')';

        if (_newHeight == 0)
            objectContext.style.display == "none";
        else
            objectContext.style.display == "";

        if (this.tweenDirection == 0 && (value == 1 || _newHeight == this.ulHeight))
        {
            objectContext.style.height = "";
            return false; // Stop the tween
        }
        else if (this.tweenDirection == 1 && (value == 1 || _newHeight == 0))
            return false;
    };
    leftNavigation.prototype.toggleExpandCollapse = function (sender, ev)
    {
        var subNav = null;
        try
        {
            if (!this.tweener.tweenInProgress)
            {
                subNav = sender;
                while (subNav)
                {
                    subNav = subNav.nextSibling;
                    if (subNav.nodeName == "UL")
                        break;
                }

                if (subNav.nodeName == "UL")
                {
                    if (subNav.style.height == "0px")
                    {
                        this.tweenDirection = 0;

                        subNav.style.height = "";
                        subNav.style.display = "";
                        this.ulHeight = parseInt(subNav.offsetHeight);
                        subNav.style.height = "0px";

                        this.tweener.tween(0, 1, 750, subNav);
                        sender.className = "expand-collapse expand-collapse-expanded";
                    }
                    else
                    {
                        this.tweenDirection = 1;

                        this.ulHeight = parseInt(subNav.offsetHeight);

                        this.tweener.tween(0, 1, 750, subNav);
                        sender.className = "expand-collapse expand-collapse-collapsed";
                    }
                }
            }
        }
        finally
        {
            subNav = null;
        }
    };

    attachEventHandler(window, "load", function ()
    {
        new leftNavigation();
    });

    /* Image slide show manager
    *********************************************************************************************************************************/
    function _imageSlideManager()
    {
        this.pods = [];
        this.modalPopUp = null;
        this.initialise();
    }
    _imageSlideManager.prototype.initialise = function ()
    {
        var _this = this;
        attachEventHandler(window, "resize", function (sender, event)
        {
            _this.hideAll();
        });
        attachEventHandler(document, "mousedown", function (sender, event)
        {
            _this.hideAll();
        });
    };
    _imageSlideManager.prototype.add = function (tag, id, titleLinkId, viewLinkId, title, rolloverImage, mainImageFileID, mainImagePath, description)
    {
        var cp = new _imageSlide(tag, id, titleLinkId, viewLinkId, title, rolloverImage, mainImageFileID, mainImagePath, description);
        cp.manager = this;
        this.pods.push(cp);
        cp = null;
    };
    _imageSlideManager.prototype.showByTag = function (tag, args)
    {
        for (var i = 0; i < this.pods.length; i++)
        {
            if (this.pods[i].tag == tag)
            {
                this.hideAll();
                this.showModalPopUp(this.pods[i], args);
                break;
            }
        }
    };
    _imageSlideManager.prototype.podIndexOf = function (pod)
    {
        for (var i = 0; i < this.pods.length; i++)
        {
            if (this.pods[i] == pod)
                return i;
        }
        return -1;
    };
    _imageSlideManager.prototype.indexByTag = function (pod)
    {
        var 
            count = 0,
            tag = pod.tag;

        for (var i = 0; i < this.pods.length; i++)
        {
            if (this.pods[i].tag == tag)
            {
                if (this.pods[i] == pod)
                    return count;
                count++;
            }
        }
        return -1;
    };
    _imageSlideManager.prototype.countByTag = function (tag)
    {
        var count = 0;
        for (var i = 0; i < this.pods.length; i++)
        {
            if (this.pods[i].tag == tag)
                count++;
        }
        return count;
    };
    _imageSlideManager.prototype.nextByTag = function (current)
    {
        var 
            index = this.podIndexOf(current),
            tag = current.tag;

        do
        {
            index++;
            if (index == this.pods.length)
                index = 0;

            if (this.pods[index].tag == tag)
                return this.pods[index]
        }
        while (true);

        return null;
    };
    _imageSlideManager.prototype.previousByTag = function (current)
    {
        var 
            index = this.podIndexOf(current),
            tag = current.tag;

        do
        {
            index--;
            if (index == -1)
                index = this.pods.length - 1;

            if (this.pods[index].tag == tag)
                return this.pods[index]
        }
        while (true);

        return null;
    };
    _imageSlideManager.prototype.hideAll = function ()
    {
        for (var i = 0; i < this.pods.length; i++)
            this.pods[i].hide();
    };
    _imageSlideManager.prototype.showModalPopUp = function (pod, args)
    {
        this.modalPopUp = new _imageSlidePopUp(args && args.showSlideShow, args && args.showSlideCount, (args && args.fixedWidth) ? args.fixedWidth : null);
        this.modalPopUp.manager = this;
        this.modalPopUp.show(pod);
    };

    /* Image slide
    *********************************************************************************************************************************/
    function _imageSlide(tag, id, titleLinkId, viewLinkId, title, rolloverImage, mainImageFileID, mainImagePath, description)
    {
        this.tag = tag;
        this.id = id;
        this.titleLinkId = titleLinkId;
        this.viewLinkId = viewLinkId;
        this.triggerElement = this.id != "" ? document.getElementById(this.id) : null;
        this.titleLink = this.titleLinkId != "" ? document.getElementById(this.titleLinkId) : null;
        this.viewLink = this.viewLinkId != "" ? document.getElementById(this.viewLinkId) : null;
        this.title = title;
        this.rolloverImage = rolloverImage;
        this.mainImageFileID = mainImageFileID;
        this.mainImagePath = mainImagePath;
        this.description = description;
        this.visible = false;
        this.tweener = null;
        this.initialise();
    }
    _imageSlide.prototype.initialise = function ()
    {
        var _this = this;
        _this.element = document.createElement("div");
        _this.element.className = "popup-choice";

        _this.divTop = document.createElement("div");
        _this.divTop.className = "top";
        _this.element.appendChild(_this.divTop);

        _this.divMiddle = document.createElement("div");
        _this.divMiddle.className = "middle";

        _this.element.appendChild(_this.divMiddle);

        _this.divContent = document.createElement("div");
        _this.divContent.className = "popup-choice-content";

        _this.divMiddle.appendChild(_this.divContent);

        _this.pTitle = document.createElement("p");
        _this.pTitle.className = "title";
        _this.pTitle.appendChild(document.createTextNode(_this.title));

        _this.divContent.appendChild(_this.pTitle);

        _this.a = document.createElement("a");
        _this.a.href = "#";

        attachEventHandler(_this.a, "click", function (sender, event)
        {
            if (event.preventDefault)
                event.preventDefault();
            event.returnValue = false;
            return false;
        });

        if (_this.triggerElement)
        {
            attachEventHandler(_this.triggerElement, "click", function (sender, event)
            {
                _this.showModalPopUp();
                if (event.preventDefault)
                    event.preventDefault();
                event.returnValue = false;
                return false;
            });
        }

        if (_this.titleLink)
        {
            attachEventHandler(_this.titleLink, "click", function (sender, event)
            {
                _this.showModalPopUp();
                if (event.preventDefault)
                    event.preventDefault();
                event.returnValue = false;
                return false;
            });
        }

        if (_this.viewLink)
        {
            attachEventHandler(_this.viewLink, "click", function (sender, event)
            {
                _this.showModalPopUp();
                if (event.preventDefault)
                    event.preventDefault();
                event.returnValue = false;
                return false;
            });
        }

        _this.divContent.appendChild(_this.a);

        _this.img = document.createElement("img");
        _this.img.alt = "";
        _this.img.src = _this.rolloverImage;

        _this.a.appendChild(_this.img);

        _this.pDescription = document.createElement("p");
        _this.pDescription.appendChild(document.createTextNode("Click on image to see larger size"));

        _this.divContent.appendChild(_this.pDescription);

        _this.divBottom = document.createElement("div");
        _this.divBottom.className = "bottom";

        _this.element.appendChild(_this.divBottom);

        _this.tweener = new tweener(function (value, objectContext) { return _this.tweenerStepChanged(value, objectContext); });

        if (_this.triggerElement)
        {
            var lightBoxFunction = function (sender, event)
            {
                if (!_this.visible && _this.manager)
                {
                    _this.manager.hideAll();
                    _this.visible = true;

                    // Bring to front !
                    document.body.appendChild(_this.element);

                    _this.element.style.display = "block";
                    _this.element.style.left = (DOMHelper.left(_this.triggerElement) - _this.element.offsetWidth + (_this.triggerElement.offsetWidth / 4)) + "px";
                    _this.element.style.top = (DOMHelper.top(_this.triggerElement) - _this.element.offsetHeight + (_this.triggerElement.offsetHeight / 4)) + "px";

                    if (!window.attachEvent)
                    {
                        _this.element.style.opacity = 0;
                        _this.element.style.filter = "alpha(opacity = 0)";
                        _this.tweener.tween(0, 1, 500, _this);
                    }
                }
            }

            attachEventHandler(_this.triggerElement, "mousemove", lightBoxFunction);
            if (_this.titleLink)
                attachEventHandler(_this.titleLink, "mousemove", lightBoxFunction);
            if (_this.viewLink)
                attachEventHandler(_this.viewLink, "mousemove", lightBoxFunction);
        }

        attachEventHandler(_this.element, "mousedown", function (sender, event)
        {
            event.cancelBubble = true;
            if (event.stopPropagation)
                event.stopPropagation();
        });

        attachEventHandler(_this.a, "click", function (sender, event)
        {
            _this.showModalPopUp();
            if (event.preventDefault)
                event.preventDefault();
            return false;
        });
    };
    _imageSlide.prototype.showModalPopUp = function ()
    {
        if (this.manager)
        {
            this.hide();
            this.manager.showModalPopUp(this);
        }
    };
    _imageSlide.prototype.hide = function ()
    {
        this.element.style.display = "";
        this.visible = false;
    };
    _imageSlide.prototype.tweenerStepChanged = function (value, objectContext)
    {
        var _percentComplete = Math.round(value * 100);

        objectContext.element.style.opacity = _percentComplete / 100;
        objectContext.element.style.filter = "alpha(opacity = " + _percentComplete + ")";
    };


    /* Image slide popup
    *********************************************************************************************************************************/
    function _imageSlidePopUp(showSlideShow, showSlideCount, fixedWidth)
    {
        this.visible = false;
        this.tweener = null;
        this.lastCurrentPod = null;
        this.currentPod = null;
        this.manager = null;
        this.slideShow = null;
        this.slideDirectionOverride = 0;
        this.showSlideShow = typeof showSlideShow != "undefined" ? showSlideShow : false;
        this.showSlideCount = typeof showSlideCount != "undefined" ? showSlideCount : false;

        if (fixedWidth)
            this.fixedWidth = fixedWidth;
        else
            this.fixedWidth = -1;

        this.initialise();
    }
    _imageSlidePopUp.prototype.initialise = function ()
    {
        var _this = this;
        _this.element = document.createElement("div");
        _this.element.className = "popup-modal hello";

        _this.element.style.height = document.body.offsetHeight + "px";
        _this.element.style.opacity = 0.51;
        _this.element.style.filter = "alpha(opacity = 51)";

        _this.windowContainer = document.createElement("div");
        _this.windowContainer.className = "popup-window-container";

        _this.window = document.createElement("div");
        _this.window.className = "popup-window";
        _this.windowContainer.appendChild(_this.window);

        _this.divTopLeft = document.createElement("div");
        _this.divTopLeft.className = "top-left";
        _this.window.appendChild(_this.divTopLeft);

        _this.divTop = document.createElement("div");
        _this.divTop.className = "top";
        _this.window.appendChild(_this.divTop);

        _this.divTopRight = document.createElement("div");
        _this.divTopRight.className = "top-right";
        _this.window.appendChild(_this.divTopRight);

        _this.divLeft = document.createElement("div");
        _this.divLeft.className = "left";
        _this.window.appendChild(_this.divLeft);

        _this.divMiddle = document.createElement("div");
        _this.divMiddle.className = "middle";
        _this.window.appendChild(_this.divMiddle);

        _this.pTitle = document.createElement("p");
        _this.pTitle.className = "popup-window-title";
        _this.divMiddle.appendChild(_this.pTitle);

        _this.tnTitle = document.createTextNode("");
        _this.pTitle.appendChild(_this.tnTitle);

        _this.img = document.createElement("img");
        _this.divMiddle.appendChild(_this.img);

        _this.imgTemp = document.createElement("img");
        _this.imgTemp.style.display = "none";
        _this.divMiddle.appendChild(_this.imgTemp);

        _this.divSlidePanel = document.createElement("div");
        _this.divSlidePanel.className = "popup-window-slide-panel";
        if (!_this.showSlideShow)
            _this.divSlidePanel.style.display = "none";
        _this.divMiddle.appendChild(_this.divSlidePanel);

        _this.divSlidePanelViewPort = document.createElement("div");
        _this.divSlidePanelViewPort.className = "viewport";
        _this.divSlidePanel.appendChild(_this.divSlidePanelViewPort);

        _this.divSlides = document.createElement("div");
        _this.divSlides.className = "slides";
        _this.divSlidePanelViewPort.appendChild(_this.divSlides);

        _this.aLeftSmall = document.createElement("a");
        _this.aLeftSmall.href = "#";
        _this.aLeftSmall.className = "arrow-block-left-pink-small";
        _this.divSlidePanel.appendChild(_this.aLeftSmall);

        attachEventHandler(_this.aLeftSmall, "click", function (sender, event)
        {
            _this.slideShow.slideToIndex(_this.slideShow.startIndex - Math.round(_this.slideShow.visibleSlides()), _this.slideShow.directionEnum.left);
            if (event.preventDefault)
                event.preventDefault();
            return false;
        });

        _this.aRightSmall = document.createElement("a");
        _this.aRightSmall.href = "#";
        _this.aRightSmall.className = "arrow-block-right-pink-small";
        _this.divSlidePanel.appendChild(_this.aRightSmall);

        attachEventHandler(_this.aRightSmall, "click", function (sender, event)
        {
            _this.slideShow.slideToIndex(_this.slideShow.startIndex + Math.round(_this.slideShow.visibleSlides()), _this.slideShow.directionEnum.right);
            if (event.preventDefault)
                event.preventDefault();
            return false;
        });

        _this.pDescription = document.createElement("p");
        _this.divMiddle.appendChild(_this.pDescription);

        _this.tnDescription = document.createTextNode("");
        _this.pDescription.appendChild(_this.tnDescription);

        _this.pFooter = document.createElement("p");
        _this.pFooter.className = "popup-window-footer";
        if (!_this.showSlideCount)
            _this.pFooter.style.display = "none";
        _this.divMiddle.appendChild(_this.pFooter);

        _this.tnFooter = document.createTextNode("");
        _this.pFooter.appendChild(_this.tnFooter);

        _this.divRight = document.createElement("div");
        _this.divRight.className = "right";
        _this.window.appendChild(_this.divRight);

        _this.divBottomLeft = document.createElement("div");
        _this.divBottomLeft.className = "bottom-left";
        _this.window.appendChild(_this.divBottomLeft);

        _this.divBottom = document.createElement("div");
        _this.divBottom.className = "bottom";
        _this.window.appendChild(_this.divBottom);

        _this.divBottomRight = document.createElement("div");
        _this.divBottomRight.className = "bottom-right";
        _this.window.appendChild(_this.divBottomRight);

        _this.aClose = document.createElement("a");
        _this.aClose.href = "#";
        _this.aClose.className = "close";
        _this.window.appendChild(_this.aClose);

        attachEventHandler(_this.aClose, "click", function (sender, event)
        {
            _this.hide();
            if (event.preventDefault)
                event.preventDefault();
            return false;
        });

        _this.aLeft = document.createElement("a");
        _this.aLeft.href = "#";
        _this.aLeft.className = "arrow-block-left-pink";
        _this.window.appendChild(_this.aLeft);

        attachEventHandler(_this.aLeft, "click", function (sender, event)
        {
            _this.previous();
            if (event.preventDefault)
                event.preventDefault();
            return false;
        });

        _this.aRight = document.createElement("a");
        _this.aRight.href = "#";
        _this.aRight.className = "arrow-block-right-pink";
        _this.window.appendChild(_this.aRight);

        attachEventHandler(_this.aRight, "click", function (sender, event)
        {
            _this.next();
            if (event.preventDefault)
                event.preventDefault();
            return false;
        });

        _this.tweener = new tweener(function (value, objectContext) { return _this.tweenerStepChanged(value, objectContext); });

        _this._scrollPosition = 0;
        _this._scrollCheckTimeoutID = 0;

        function _scrollCheck()
        {
            if (_this._scrollPosition == DOMHelper.viewportScrollTop())
            {
                _this.tweener.cancel();

                var 
                    scrollTop = DOMHelper.viewportScrollTop(),
                    start = parseInt(_this.window.style.marginTop),
                    end = ((DOMHelper.viewportHeight() - _this.window.offsetHeight) / 2 + scrollTop);

                if (end < scrollTop)
                    end = scrollTop;

                _this.tweener.tween(start, end - start, 750, _this);
                _this._scrollCheckTimeoutID = 0;
            }
            else
            {
                _this._scrollPosition = DOMHelper.viewportScrollTop();
                _this._scrollCheckTimeoutID = setTimeout(_scrollCheck, 100);
            }
        }

        attachEventHandler(window, "scroll", function (sender, event)
        {
            if (_this.visible && _this._scrollCheckTimeoutID == 0)
            {
                _this._scrollPosition = DOMHelper.viewportScrollTop();
                _this._scrollCheckTimeoutID = setTimeout(_scrollCheck, 1);
            }
        });

        attachEventHandler(_this.imgTemp, "load", function ()
        {
            if (_this.imgTemp.src != "")
            {
                _this.img.style.display = "none";
                _this.imgTemp.style.display = "";

                document.body.appendChild(_this.element);
                document.body.appendChild(_this.windowContainer);

                var 
                    _podID = _this.manager.podIndexOf(_this.currentPod),
                    _podIDByTag = _this.manager.indexByTag(_this.currentPod);

                if (_this.currentPod.tag == "choice-pod")
                {
                    _this.aLeft.style.display = "none";
                    _this.aRight.style.display = "none";
                    _this.pFooter.style.display = "none";
                }
                else
                {
                    if (_this.manager.countByTag(_this.currentPod.tag) > 1)
                    {
                        _this.aLeft.style.display = "";
                        _this.aRight.style.display = "";
                    }
                    else
                    {
                        _this.aLeft.style.display = "none";
                        _this.aRight.style.display = "none";
                    }
                    //_this.pFooter.style.display = "";
                }

                _this.img.src = _this.imgTemp.src;
                _this.tnTitle.data = _this.currentPod.title;
                _this.tnDescription.data = _this.currentPod.description;
                _this.tnFooter.data = (_this.manager.indexByTag(_this.currentPod) + 1) + " / " + _this.manager.countByTag(_this.currentPod.tag);

                if (_this.fixedWidth > 0)
                {
                    _this.divMiddle.style.width = _this.fixedWidth + "px";
                }
                else
                    _this.divMiddle.style.width = (_this.imgTemp.offsetWidth) + "px";

                _this.window.style.width = _this.divLeft.offsetWidth + _this.divMiddle.offsetWidth + _this.divRight.offsetWidth + "px";
                _this.window.style.height = _this.divTop.offsetHeight + _this.divMiddle.offsetHeight + _this.divBottom.offsetHeight + "px";
                _this.divTop.style.width = _this.divMiddle.offsetWidth + "px";
                _this.divLeft.style.height = _this.divMiddle.offsetHeight + "px";
                _this.divRight.style.height = _this.divMiddle.offsetHeight + "px";
                _this.divBottom.style.width = _this.divMiddle.offsetWidth + "px";

                _this.aLeft.style.top = (_this.imgTemp.offsetTop + (_this.imgTemp.offsetHeight / 2)) + "px";
                _this.aRight.style.top = (_this.imgTemp.offsetTop + (_this.imgTemp.offsetHeight / 2)) + "px";

                var 
                    _scrollTop = DOMHelper.viewportScrollTop(),
                    _marginTop = ((DOMHelper.viewportHeight() - _this.window.offsetHeight) / 2 + _scrollTop);

                if (_marginTop < _scrollTop)
                    _marginTop = _scrollTop;

                _this.window.style.marginTop = _marginTop + "px";

                if (!_this.slideShow)
                    _this.slideShow = new hammonds.slideshow(_this.divSlides, 3000, false, 500);

                for (var i = 0; i < _this.divSlides.childNodes.length; i++)
                {
                    if (_this.divSlides.childNodes[i].childNodes[0]._podID == _podID)
                    {
                        _this.divSlides.childNodes[i].childNodes[0].style.border = "solid 2px #E11D5F";

                        if (_this.lastCurrentPod)
                        {
                            var 
                                _lastPodIDByTag = _this.manager.indexByTag(_this.lastCurrentPod),
                                _leftNumber = 0,
                                _rightNumber = 0;

                            if (_podIDByTag <= _lastPodIDByTag)
                                _leftNumber = _lastPodIDByTag - _podIDByTag;
                            else
                                _leftNumber = _lastPodIDByTag + _this.slideShow.panelCount - _podIDByTag;

                            if (_podIDByTag >= _lastPodIDByTag)
                                _rightNumber = _podIDByTag - _lastPodIDByTag;
                            else
                                _rightNumber = _podIDByTag + _this.slideShow.panelCount - _lastPodIDByTag;

                            _this.slideShow.slideToIndex(_podIDByTag, (_leftNumber <= _rightNumber && _this.slideDirectionOverride == 0 ? _this.slideShow.directionEnum.left : _this.slideShow.directionEnum.right));
                            _this.slideDirectionOverride = 0;
                        }
                    }
                    else
                    {
                        _this.divSlides.childNodes[i].childNodes[0].style.borderStyle = "";
                        _this.divSlides.childNodes[i].childNodes[0].style.borderWidth = "";
                        _this.divSlides.childNodes[i].childNodes[0].style.borderColor = "";
                    }
                }
            }

            _this.img.style.display = "";
            _this.imgTemp.style.display = "none";
        });
    };
    _imageSlidePopUp.prototype.show = function (pod)
    {
        var 
            _this = this,
            _slide = null,
            _img = null,
            _count = 0;

        try
        {
            // Setup index panel
            if (_this.manager)
            {
                while (_this.divSlides.childNodes.length)
                    _this.divSlides.removeChild(_this.divSlides.childNodes[0]);

                for (var i = 0; i < _this.manager.pods.length; i++)
                {
                    if (_this.manager.pods[i].tag == pod.tag)
                    {
                        _slide = document.createElement("div");
                        _slide._podID = i;
                        _slide.className = "slide";
                        _this.divSlides.appendChild(_slide);

                        _img = document.createElement("img");
                        _img._podID = i;
                        _img.style.width = "63px";
                        _img.style.height = "48px";
                        _img.src = WEBSITE_BASE_URL + "thumbnailgenerator.aspx?fileid=" + _this.manager.pods[i].mainImageFileID + "&width=63&height=48";
                        _slide.appendChild(_img);

                        attachEventHandler(_img, "click", function (sender, event)
                        {
                            _this.slideDirectionOverride = 1;
                            _this._show(_this.manager.pods[sender._podID]);
                        });

                        _count++;
                    }
                }

                _this.divSlides.style.width = (96 * _count) + "px";
            }

            _this._show(pod);
        }
        finally
        {
            _slide = null;
            _img = null;
        }

    };
    _imageSlidePopUp.prototype._show = function (pod)
    {
        this.visible = true;
        this.lastCurrentPod = this.currentPod;
        this.currentPod = pod;
        this.imgTemp.src = "";
        this.imgTemp.src = pod.mainImagePath;
    };
    _imageSlidePopUp.prototype.hide = function ()
    {
        try
        {
            document.body.removeChild(this.element);
            document.body.removeChild(this.windowContainer);
        }
        catch (e) { }
        finally
        {
            this.visible = false;
        }
    };
    _imageSlidePopUp.prototype.previous = function ()
    {
        if (this.manager)
        {
            var previous = this.manager.previousByTag(this.currentPod);
            if (previous)
                this._show(previous);
        }
    };
    _imageSlidePopUp.prototype.next = function ()
    {
        if (this.manager)
        {
            var next = this.manager.nextByTag(this.currentPod);
            if (next)
                this._show(next);
        }
    };
    _imageSlidePopUp.prototype.tweenerStepChanged = function (value, objectContext)
    {
        objectContext.window.style.marginTop = Math.round(value) + "px";
    };

    var imageSlideManager = new _imageSlideManager();

    function showGalleryImages(evt, fixedWidth)
    {
        if (_gaq)
            _gaq.push(["_trackPageview", "features-slideshow/open/" + window.location.href.substr(WEBSITE_BASE_URL.length)]);
        imageSlideManager.showByTag("gallery-image", { showSlideShow: false, showSlideCount: true, fixedWidth: fixedWidth });
        if (evt.preventDefault)
            evt.preventDefault();
        return false;
    }

    function podClick(evt, id, type, tag)
    {
        if (_gaq)
            _gaq.push(["_trackPageview", id + "/" + window.location.href.substr(WEBSITE_BASE_URL.length)]);
        
        if (type == "Light Box")
        {
            imageSlideManager.showByTag(tag, { showSlideShow: true });
            if (evt.preventDefault)
                evt.preventDefault();
            return false;
        }
    }


    /* Video popup
    *********************************************************************************************************************************/
    function _videoPopUp()
    {
        this.initialised = false;
        this.visible = false;
        this.currentVideoID = "";
    }
    _videoPopUp.prototype.initialise = function ()
    {
        var _this = this;
        _this.initialised = true;

        // Create the popup elements
        _this.element = document.createElement("div");
        _this.element.className = "popup-modal";

        _this.element.style.height = document.body.offsetHeight + "px";
        _this.element.style.opacity = 0.51;
        _this.element.style.filter = "alpha(opacity = 51)";

        _this.windowContainer = document.createElement("div");
        _this.windowContainer.className = "popup-window-container";

        _this.window = document.createElement("div");
        _this.window.className = "popup-window";
        _this.windowContainer.appendChild(_this.window);

        _this.divTopLeft = document.createElement("div");
        _this.divTopLeft.className = "top-left";
        _this.window.appendChild(_this.divTopLeft);

        _this.divTop = document.createElement("div");
        _this.divTop.className = "top";
        _this.window.appendChild(_this.divTop);

        _this.divTopRight = document.createElement("div");
        _this.divTopRight.className = "top-right";
        _this.window.appendChild(_this.divTopRight);

        _this.divLeft = document.createElement("div");
        _this.divLeft.className = "left";
        _this.window.appendChild(_this.divLeft);

        _this.divMiddle = document.createElement("div");
        _this.divMiddle.className = "middle";
        _this.divMiddle.style.padding = 25 + "px";
        _this.window.appendChild(_this.divMiddle);

        _this.divRight = document.createElement("div");
        _this.divRight.className = "right";
        _this.window.appendChild(_this.divRight);

        _this.divBottomLeft = document.createElement("div");
        _this.divBottomLeft.className = "bottom-left";
        _this.window.appendChild(_this.divBottomLeft);

        _this.divBottom = document.createElement("div");
        _this.divBottom.className = "bottom";
        _this.window.appendChild(_this.divBottom);

        _this.divBottomRight = document.createElement("div");
        _this.divBottomRight.className = "bottom-right";
        _this.window.appendChild(_this.divBottomRight);

        // Close button and event
        _this.aClose = document.createElement("a");
        _this.aClose.href = "#";
        _this.aClose.className = "close";
        _this.window.appendChild(_this.aClose);

        attachEventHandler(_this.aClose, "click", function (sender, event)
        {
            _this.hide();
            if (event.preventDefault)
                event.preventDefault();
            return false;
        });

        // Add an event to check that whether the user has scrolled the page.
        attachEventHandler(window, "scroll", function (sender, event)
        {
            if (_this.visible && _this._scrollCheckTimeoutID == 0)
            {
                _this._scrollPosition = DOMHelper.viewportScrollTop();
                _this._scrollCheckTimeoutID = setTimeout(_scrollCheck, 1);
            }
        });

        // Attach to the body.
        document.body.appendChild(_this.element);
        document.body.appendChild(_this.windowContainer);
    };
    _videoPopUp.prototype.show = function (videoID, width, height)
    {
        var _this = this;
        _this.visible = true;

        if (!_this.initialised)
            _this.initialise();

        //_this.window.style.marginTop = _marginTop + "px";

        // Embed the flash
        if (_this.currentVideoID != videoID)
        {
            _this.currentVideoID = videoID;

            // Clear all elements
            while (_this.divMiddle.firstChild)
                _this.divMiddle.removeChild(_this.divMiddle.firstChild);

            // Create an element for the embed - this gets consumed by swfobject. Nom nom nom.
            _this.divVideo = document.createElement("div");
            _this.divVideo.id = "video-popup-element";
            _this.divVideo.className = "video-element";
            _this.divMiddle.appendChild(_this.divVideo);

            _this.addText();

            // Set the dimensions
            _this.divMiddle.style.width = width + "px";
            _this.divMiddle.style.height = (_this.divMiddle.offsetHeight + height - 50) + "px"; // (height) + "px";

            _this.window.style.width = _this.divLeft.offsetWidth + _this.divMiddle.offsetWidth + _this.divRight.offsetWidth + "px";
            _this.window.style.height = _this.divTop.offsetHeight + _this.divMiddle.offsetHeight + _this.divBottom.offsetHeight + "px";
            _this.divTop.style.width = _this.divMiddle.offsetWidth + "px";
            _this.divLeft.style.height = _this.divMiddle.offsetHeight + "px";
            _this.divRight.style.height = _this.divMiddle.offsetHeight + "px";
            _this.divBottom.style.width = _this.divMiddle.offsetWidth + "px";

            var parameters =
                {
                    allowScriptAccess: "always",
                    allowFullScreen: "true",
                    wmode: "opaque"
                },
                attributes =
                {
                    id: _this.divVideo.id + "Object"
                };

            swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "?enablejsapi=1&rel=0&playerapiid=" + _this.divVideo.id + "Object", _this.divVideo.id, width, height, "8", null, null, parameters, attributes);

        }

        var 
            _scrollTop = DOMHelper.viewportScrollTop(),
            _marginTop = ((DOMHelper.viewportHeight() - _this.window.offsetHeight) / 2 + _scrollTop);

        if (_marginTop < _scrollTop)
            _marginTop = _scrollTop;

        // Reveal the goodness.
        _this.element.style.display = "block";
        _this.windowContainer.style.display = "block";
    };
    _videoPopUp.prototype.hide = function ()
    {
        var _this = this;

        _this.visible = false;
        _this.element.style.display = "none";
        _this.windowContainer.style.display = "none";
    };
    _videoPopUp.prototype.addText = function ()
    {
        var _this = this;
        if (_this.divMiddle)
        {
            _this.aLink = document.createElement("a");
            _this.aLink.href = WEBSITE_CONTENT_URL + "design-visit.aspx";
            _this.aLink.className = "design-visit-button";
            _this.aLink.innerHTML = "Book a design visit";
            _this.divMiddle.appendChild(_this.aLink);

            _this.h3Title = document.createElement("h3");
            _this.h3Title.className = "title-normal";
            _this.h3Title.innerHTML = "See what it's like to have your";
            _this.divMiddle.appendChild(_this.h3Title);

            _this.h3TitleBold = document.createElement("h3");
            _this.h3TitleBold.className = "title-bold";
            _this.h3TitleBold.innerHTML = "room transformed";
            _this.divMiddle.appendChild(_this.h3TitleBold);

            _this.pText = document.createElement("p");
            _this.pText.className = "video-pop-up-text";
            _this.pText.innerHTML = "Our designers are friendly and always willing to accomodate your requirements. Watch this short video to see what to expect.";
            _this.divMiddle.appendChild(_this.pText);
        }
    };

    var videoPopUp = new _videoPopUp();

    // Brochure Request Popup
    // **********************
    function brochureRequestPopup(elementID, timeout, TrackingTagPrefix)
    {
        var _this = this;
        _this._element = document.getElementById(elementID);
        _this._timeout = timeout;
        _this._scrollPosition = 0;
        _this._scrollCheckTimeoutID = 0;
        _this._tweener = null;
        _this.visible = false;
        _this._TrackingTagPrefix = TrackingTagPrefix;

        if (this._element)
            attachEventHandler(window, "load", function () { _this.initialise(); });
    }
    brochureRequestPopup.prototype.initialise = function ()
    {
        if (this._element)
        {
            var 
                _this = this,
                _aTags = _this._element.getElementsByTagName("a");

            for (var i = 0; i < _aTags.length; i++)
            {
                switch (_aTags[i].className)
                {
                    case "close":
                        attachEventHandler(_aTags[i], "click", function (sender, event) { return _this.close(sender, event); });
                        break;
                    case "already-ordered":
                        attachEventHandler(_aTags[i], "click", function (sender, event) { return _this.closePermanently(sender, event); });
                        break;
                    case "link":
                        attachEventHandler(_aTags[i], "click", function (sender, event)
                        {
                            if (_gaq)
                                _gaq.push(["_trackPageview", _this._TrackingTagPrefix + "minor_conversion"]);
                        });
                        break;
                }
            }

            _this.tweener = new tweener(function (value, objectContext) { return _this.tweenerStepChanged(value, objectContext); });

            function _scrollCheck()
            {
                if (_this._scrollPosition == DOMHelper.viewportScrollTop())
                {
                    _this.tweener.cancel();

                    var 
                        scrollTop = DOMHelper.viewportScrollTop(),
                        start = parseInt(_this._element.style.marginTop),
                        end = ((DOMHelper.viewportHeight() - _this._element.offsetHeight) / 2 + scrollTop);

                    if (end < scrollTop)
                        end = scrollTop;

                    _this.tweener.tween(start, end - start, 750, _this);
                    _this._scrollCheckTimeoutID = 0;
                }
                else
                {
                    _this._scrollPosition = DOMHelper.viewportScrollTop();
                    _this._scrollCheckTimeoutID = setTimeout(_scrollCheck, 100);
                }
            }


            attachEventHandler(window, "scroll", function (sender, event)
            {
                if (_this.visible && _this._scrollCheckTimeoutID == 0)
                {
                    _this._scrollPosition = DOMHelper.viewportScrollTop();
                    _this._scrollCheckTimeoutID = setTimeout(_scrollCheck, 1);
                }
            });

            setTimeout(function () { _this.show(); }, _this._timeout);
        }
    };
    brochureRequestPopup.prototype.show = function (sender, event)
    {
        if (this._element)
        {
            document.body.appendChild(this._element);
            this._scrollPosition = DOMHelper.viewportScrollTop();
            this._element.style.display = "";
            this._element.style.marginTop = (this._scrollPosition + (DOMHelper.viewportHeight() - this._element.offsetHeight) / 2) + "px";
            this.visible = true;

            if (_gaq)
                _gaq.push(["_trackPageview", this._TrackingTagPrefix + "loaded"]);
        }
    };
    brochureRequestPopup.prototype.close = function (sender, event)
    {
        if (this._element)
        {
            if (_gaq)
                _gaq.push(["_trackPageview", this._TrackingTagPrefix + "close"]);

            this._element.style.display = "none";
            document.cookie = "BrochureRequestPopup=off; path=/";
            this.visible = false;
        }
        if (event.preventDefault)
            event.preventDefault();
        event.returnValue = false;
        return false;
    };
    brochureRequestPopup.prototype.closePermanently = function (sender, event)
    {
        if (this._element)
        {
            if (_gaq)
                _gaq.push(["_trackPageview", this._TrackingTagPrefix + "I-have-brochure"]);

            this._element.style.display = "none";
            var expiryDate = new Date();
            expiryDate.setDate(expiryDate.getDate() + 180);
            document.cookie = "BrochureRequestPopup=off; expires=" + expiryDate.toGMTString() + "; path=/";
            this.visible = false;
        }
        if (event.preventDefault)
            event.preventDefault();
        event.returnValue = false;
        return false;
    };
    brochureRequestPopup.prototype.tweenerStepChanged = function (value, objectContext)
    {
        objectContext._element.style.marginTop = Math.round(value) + "px";
    };
