﻿
    // Global
    String.prototype.trim = function () {
        var _this = this;
        try {
            return _this.replace(/^\s+|\s+$/g, "");
        }
        finally {
            _this = null;
        }
    };

    var popupmanager = null;

    var yorkshirewater = {};
    yorkshirewater._construct = function ()
    {
        // DOM Helper
        // Used for common DOM tasks
        function _domHelper()
        {
        }
        _domHelper.prototype.addClass = function (el, className)
        {
            this.removeClass(el, className);
            el.className += " " + className;
        };
        _domHelper.prototype.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.prototype.attachEvent = function (event, el, fn)
        {

            if (window.attachEvent)
                el.attachEvent("on" + event, fn);
            else if (window.addEventListener)
                el.addEventListener(event, fn, false);
        };
        _domHelper.prototype.setFloat = function (el, styleFloat)
        {
            el.style.styleFloat = styleFloat;
        };

        var domHelper = new _domHelper();
        this.domHelper = domHelper;

        var easing = {};
        easing._reverse = function (eq, t, b, c, d)
        {
            return -eq(d - t, b, c, d) + c + 2 * b;
        };
        easing._inToInOut = function (easeIn, t, b, c, d)
        {
            t = t * 2;
            return (t < d ? easeIn(t, b, c / 2, d) : this._reverse(easeIn, t - d, b + c / 2, c / 2, d));
        };
        easing._outToInOut = function (easeOut, t)
        {
            t = 2 * t;
            return 0.5 * (t < 1 ? 1 - easeOut(1 - t) : 1 + easeOut(t - 1));
        };
        easing._inOutPairToInOut = function (easeIn, easeOut, t)
        {
            t = 2 * t;
            return 0.5 * (t < 1 ? easeIn(t) : 1 + easeOut(t - 1));
        };
        easing.quinticIn = function (t, b, c, d)
        {
            return c * (t /= d) * t * t * t * t + b;
        };
        easing.quinticOut = function (t, b, c, d)
        {
            return this._reverse(this.quinticIn, t, b, c, d);
        };
        easing.quinticInOut = function (t, b, c, d)
        {
            return this._inToInOut(this.quinticIn, t, b, c, d);
        };




        // Slideshow Control
        function slideShow(divSlider, interval, autoStart, animationLength)
        {
            var _this = this;
            _this.divSlider = divSlider;

            for (var i = 0; i < _this.divSlider.childNodes.length; i++)
            {
                if (_this.divSlider.childNodes[i].nodeType != 1)
                {
                    _this.divSlider.removeChild(_this.divSlider.childNodes[i]);
                    i--;
                }
            }

            _this.interval = interval;
            _this.autoStart = autoStart;
            _this.animationLength = (typeof animationLength != "undefined" ? animationLength : 1000);

            _this.beforeIndexChanged = null;
            _this.afterIndexChanged = null;
            _this.slidingStart = null;
            _this.stepChanged = null;

            _this.panelCount = _this.divSlider.childNodes.length;
            _this.panelWidth = (_this.divSlider.childNodes ? _this.divSlider.childNodes[0].offsetWidth : 0);
            _this.originalStartIndex = 0;
            _this.startIndex = 0;
            _this.autoTimeoutID = 0;
            _this.timeoutID = 0;
            _this._step = 0;
            _this._startPosition = 0;
            _this._desiredPosition = 0;
            _this._queueIndex = 0;

            function _slideToNextRight()
            {
                var
                    _panelsToMove = Math.round(_this.divSlider.parentNode.offsetWidth / _this.panelWidth, 0),
                    _next = _this.startIndex + _panelsToMove;

                if (_next > _this.panelCount - 1)
                    _next = _next - _this.panelCount;

                function callback()
                {
                    _this.autoTimeoutID = setTimeout(_slideToNextRight, interval);
                }
                _this._slideToIndex(_next, _this.directionEnum.right, callback);
            }

            if (_this.autoStart)
                _this.autoTimeoutID = setTimeout(_slideToNextRight, interval);
        }
        slideShow.prototype.directionEnum = { left: 0, right: 1, leftRight: 2 };
        slideShow.prototype.slideLeft = function (completedCallback)
        {
            var _index = this.startIndex - 1;
            if (_index < 0)
                _index = this.panelCount - 1;
            this.slideToIndex(_index, this.directionEnum.left, completedCallback);
        };
        slideShow.prototype.slideRight = function (completedCallback)
        {
            var _index = this.startIndex + 1;
            if (_index > this.panelCount - 1)
                _index = 0;
            this.slideToIndex(_index, this.directionEnum.right, completedCallback);
        };
        slideShow.prototype.jumpToIndex = function (index)
        {
            if (this.beforeIndexChanged)
                this.beforeIndexChanged(index);

            clearTimeout(this.autoTimeoutID);
            if (index >= 0 && index < this.panelCount)
            {
                while (this.startIndex != index)
                {
                    this.divSlider.appendChild(this.divSlider.childNodes[0]);
                    this.startIndex++;
                    if (this.startIndex > this.panelCount - 1)
                        this.startIndex = 0;
                }
            }

            if (this.afterIndexChanged)
                this.afterIndexChanged(index);
        };
        slideShow.prototype.stopAutoSlideShow = function ()
        {
            if (this.autoTimeoutID > 0)
                clearTimeout(this.autoTimeoutID);
        };
        slideShow.prototype.slideToIndex = function (index, direction, completedCallback)
        {
            if (this.timeoutID > 0)
            {
                this._queueIndex = index;
                return;
            }
            if (this.autoTimeoutID > 0)
                clearTimeout(this.autoTimeoutID);
            this._slideToIndex(index, direction, completedCallback);
        }
        slideShow.prototype._slideToIndex = function (index, direction, completedCallback, f)
        {
            var 
                    _this = this,
                    _steps = _this.animationLength / 20,
                    _newPos = 0;

            if (_this.beforeIndexChanged)
                _this.beforeIndexChanged(index);

            _this.originalStartIndex = _this.startIndex;

            _this._startPosition = isNaN(parseInt(_this.divSlider.style.left)) ? 0 : -parseInt(_this.divSlider.style.left);

            if (direction == _this.directionEnum.right && index < _this.startIndex)
                _this._desiredPosition = (_this.panelCount - _this.startIndex + index) * _this.panelWidth;
            else if (direction == _this.directionEnum.left && index > _this.startIndex)
                _this._desiredPosition = (-_this.panelCount - _this.startIndex + index) * _this.panelWidth;
            else
                _this._desiredPosition = (index - _this.startIndex) * _this.panelWidth;

            function _nextStep()
            {
                _newPos = Math.round(easing.quinticInOut(_this._step, _this._startPosition, _this._desiredPosition - _this._startPosition, _steps), 0);
                if (_newPos >= _this.panelWidth)
                {
                    _this.divSlider.appendChild(_this.divSlider.childNodes[0]);

                    _newPos -= _this.panelWidth;
                    _this._desiredPosition -= _this.panelWidth;
                    _this._startPosition -= _this.panelWidth;

                    _this.startIndex++;
                    if (_this.startIndex > _this.panelCount - 1)
                        _this.startIndex = 0;
                }
                else if (_newPos < 0)
                {
                    _this.divSlider.insertBefore(_this.divSlider.childNodes[_this.divSlider.childNodes.length - 1], _this.divSlider.childNodes[0]);

                    _newPos += _this.panelWidth;
                    _this._desiredPosition += _this.panelWidth;
                    _this._startPosition += _this.panelWidth;

                    _this.startIndex--;
                    if (_this.startIndex < 0)
                        _this.startIndex = _this.panelCount - 1;
                }

                _this.divSlider.style.left = (-_newPos) + "px";

                if (_this._step < _steps)
                {
                    _this._step++;
                    _this.timeoutID = setTimeout(_nextStep, 20);
                }
                else
                {
                    _newPos = _this._desiredPosition
                    _this.timeoutID = 0;
                    _this._step = 0;

                    if (completedCallback)
                        completedCallback();

                    if (_this.afterIndexChanged)
                        _this.afterIndexChanged(index);

                    //                    if (_this._queueIndex > 0)
                    //                        setTimeout(function () { _this._slideToIndex(_this._queueIndex, direction, completedCallback, f); _this._queueIndex = 0; }, 1);
                }

                if (_this.stepChanged)
                    _this.stepChanged(_this.originalStartIndex, index, (_newPos - _this._startPosition) / (_this._desiredPosition - _this._startPosition));
            }
            _this.timeoutID = setTimeout(_nextStep, 20);

            if (_this.slidingStart)
                _this.slidingStart(_this.startIndex, index);

        }
        this.slideShow = slideShow;



        // Home Page
        function homePage()
        {
            var _this = this;
            _this.initialised = false;
            _this._initialise();
        }
        homePage.prototype._initialise = function ()
        {
            var _this = this;

            _this.divFlickrSlideShow = document.getElementById("divFlickrSlideShowSlider");
            _this.aLeftArrow = document.getElementById("aLeftArrow");
            _this.aLeftArrow.style.display = "block";
            _this.aRightArrow = document.getElementById("aRightArrow");
            _this.aRightArrow.style.display = "block";
            _this.flickrSlideShow = new slideShow(_this.divFlickrSlideShow, 10000, true, window.attachEvent ? 800 : 1000);

            domHelper.attachEvent(
                    "click",
                    this.aLeftArrow,
                    function (e)
                    {
                        var evt = typeof event != "undefined" ? event : e;
                        var _index = _this.flickrSlideShow.startIndex - 3;
                        if (_index < 0)
                            _index = _index + _this.flickrSlideShow.panelCount;
                        _this.flickrSlideShow.slideToIndex(_index, _this.flickrSlideShow.directionEnum.left)
                        if (e.preventDefault)
                            e.preventDefault();
                        return false;
                    }
                );

            domHelper.attachEvent(
                    "click",
                    this.aRightArrow,
                    function (e)
                    {
                        var evt = typeof event != "undefined" ? event : e;
                        var _index = _this.flickrSlideShow.startIndex + 3;
                        if (_index > _this.panelCount - 1)
                            _index = _index - _this.flickrSlideShow.panelCount;
                        _this.flickrSlideShow.slideToIndex(_index, _this.flickrSlideShow.directionEnum.right)
                        if (e.preventDefault)
                            e.preventDefault();
                        return false;
                    }
                );

            _this.initialised = true;
        };
        this.homePage = homePage;
    }
    yorkshirewater._construct();
