﻿var ebloc = {};

ebloc.convert = {};

ebloc.convert.twitterDateToLongDate = function (twitterDate, dateSeperator, timeSeperator, dateTimeSeperator)
{
    var dateParts = twitterDate.split(/[\s:]+/);

    var date = "";

    date += dateParts[2];
    date += dateSeperator;

    switch (dateParts[1].toLowerCase())
    {
        case "jan":
            date += "01";
            break;
        case "feb":
            date += "02";
            break;
        case "mar":
            date += "03";
            break;
        case "apr":
            date += "04";
            break;
        case "may":
            date += "05";
            break;
        case "jun":
            date += "06";
            break;
        case "jul":
            date += "07";
            break;
        case "aug":
            date += "08";
            break;
        case "sep":
            date += "09";
            break;
        case "oct":
            date += "10";
            break;
        case "nov":
            date += "11";
            break;
        case "dec":
            date += "12";
            break;
    }
    date += dateSeperator;

    date += dateParts[7];

    date += dateTimeSeperator;

    date += dateParts[3];
    date += timeSeperator;

    date += dateParts[4];
    date += timeSeperator;

    date += dateParts[5];

    return date;
}

ebloc.pageLoad = function ()
{
    ebloc.insertExternalLinks();
    ebloc.insertTitleAttributes();
    ebloc.insertForcedLinks();

    ebloc.insertExpanders()
};

ebloc.insertExternalLinks = function ()
{
    $("a[rel=external]").attr("target", "_blank");
};

ebloc.insertTitleAttributes = function ()
{
    $("img[alt]").each
    (
        function (index, element)
        {
            element.setAttribute("title", element.getAttribute("alt"));
        }
    );
};

ebloc.insertForcedLinks = function ()
{
    var urlElements = $("*[url]");

    urlElements.css("cursor", "pointer");

    urlElements.click
    (
        function ()
        {
            if (this.getAttribute("target") == "_blank")
            {
                window.open(this.getAttribute("url"))
            }
            else
            {
                window.location = this.getAttribute("url");
            }
        }
    );
}

ebloc.insertExpanders = function ()
{
    var expanderElements = $("*.expander[targetid]");

    expanderElements.attr("isanimating", "0");

    expanderElements.each
    (
        function (index, element)
        {
            if (element.getAttribute("active") == "1")
            {
                element.setAttribute("expanded", "1");
            }
            else
            {
                element.setAttribute("expanded", "0");
            }

            var targets = $("#" + this.getAttribute("targetid"));

            if (targets.length > 0)
            {
                element.style.cursor = "pointer";
            }

            targets.each
            (
                function (index, element2)
                {
                    element2 = $(element2);

                    element2.attr("fullheight", element2.height());

                    element2.css("overflow", "hidden");

                    if (element.getAttribute("active") != "1")
                    {
                        element2.css("height", "0px");
                    }
                }
            );
        }
    );

    expanderElements.click
    (
        function ()
        {
            if (this.getAttribute("isanimating") == "0")
            {
                this.setAttribute("isanimating", "1");

                var targets = $("#" + this.getAttribute("targetid"));

                var self = this;

                targets.each
                (
                    function (index, element)
                    {
                        element = $(element);

                        var targetHeight;

                        if (self.getAttribute("expanded") == "0")
                        {
                            self.setAttribute("expanded", "1");
                            targetHeight = parseInt(element.attr("fullheight"));
                        }
                        else
                        {
                            self.setAttribute("expanded", "0");
                            targetHeight = 0;
                        }

                        element.animate
                        (
                            {
                                height: targetHeight
                            },
                            ebloc.ExpanderElements.changeInMilliseconds,
                            "swing",
                            function ()
                            {
                                self.setAttribute("isanimating", "0");
                            }
                        );
                    }
                );
            }
        }
    );
};

ebloc.ExpanderElements = {};
ebloc.ExpanderElements.changeInMilliseconds = 500;

ebloc.initJsonReaders = function ()
{
    $("*[json]").each
    (
        function (index, element)
        {
            var jsonUrl = element.getAttribute("json");

            if (jsonUrl.indexOf("?") == -1)
            {
                jsonUrl += "?";
            }
            else
            {
                jsonUrl += "&";
            }

            jsonUrl += "callback=?";

            var template = $(".template", element)[0];

            $.ajax
		    (
                {
                    url: jsonUrl,
                    crossDomain: true,
                    dataType: "json",
                    success: function (data)
                    {
                        for (var index = 0; index < data.length && index < 4; ++index)
                        {
                            var copy = data[index].text;
                            var date = data[index].created_at;

                            date = ebloc.convert.twitterDateToLongDate(date, ".", ":", " ")

                            var item = template.cloneNode(true);

                            element.appendChild(item);
                            item = element.childNodes[element.childNodes.length - 1];

                            item.innerHTML = item.innerHTML.replace("-date-", date);
                            item.innerHTML = item.innerHTML.replace("-copy-", ebloc.insertAnchorLinks(copy));

                            item.style.display = "block";
                        }
                    }
                }
		    );
        }
    );
}

ebloc.insertAnchorLinks = function (value, internal)
{
    if (!internal)
    {
        var internal = false;
    }

    value = value.replace(/(http:\/\/[^ ]+)/g, "<a href=\"$1\"" + (internal ? "" : " target=\"_blank\"") + ">$1</a>/");
    value = value.replace(/(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/g, "<a href=\"mailto:$1\">$1</a>/");

    return value;
}

ebloc.Carousel = function (scrollElement, fadeElement, navigationElement, scrollImages, fadeCopies, onChangeCallback, startIndex)
{
    scrollElement = $(scrollElement);
    this._fadeElement = $(fadeElement);

    if (navigationElement)
    {
        this._navigationElement = $(navigationElement);
    }
    else
    {
        this._navigationElement = null;
    }

    if (!onChangeCallback)
    {
        this._onChangeCallback = null;
    }
    else
    {
        this._onChangeCallback = onChangeCallback;
    }

    this._scrollElementScroller = document.createElement("div");
    scrollElement[0].appendChild(this._scrollElementScroller);
    this._scrollElementScroller = scrollElement[0].childNodes[scrollElement[0].childNodes.length - 1];

    this._scrollElementWidth = scrollElement.width();
    var scrollElementHeight = scrollElement.height();

    var scrollElementScrollerItem;
    var scrollElementScrollerItemPlay;

    var self = this;

    for (var index = 0; index < scrollImages.length + 1; ++index)
    {
        scrollElementScrollerItem = document.createElement("div");
        this._scrollElementScroller.appendChild(scrollElementScrollerItem);
        scrollElementScrollerItem = this._scrollElementScroller.childNodes[this._scrollElementScroller.childNodes.length - 1];

        (
            function ()
            {
                var scrollImage = scrollImages[index % scrollImages.length].split("??");

                scrollElementScrollerItem.style.backgroundImage = "url('" + scrollImage[0] + "')";
                scrollElementScrollerItem.style.width = self._scrollElementWidth + "px";
                scrollElementScrollerItem.style.height = scrollElementHeight + "px";
                scrollElementScrollerItem.style.cssFloat = "left";

                if (scrollImage.length > 1 && scrollImage[1].trim() != "")
                {
                    scrollElementScrollerItemPlay = document.createElement("div");
                    scrollElementScrollerItem.appendChild(scrollElementScrollerItemPlay);
                    scrollElementScrollerItemPlay = scrollElementScrollerItem.childNodes[scrollElementScrollerItem.childNodes.length - 1];

                    scrollElementScrollerItemPlay.style.backgroundImage = "url('/images/shared/play-icon.png')";
                    scrollElementScrollerItemPlay.style.backgroundPosition = "50% 50%";
                    scrollElementScrollerItemPlay.style.backgroundRepeat = "no-repeat";
                    scrollElementScrollerItemPlay.style.width = self._scrollElementWidth + "px";
                    scrollElementScrollerItemPlay.style.height = scrollElementHeight + "px";
                    scrollElementScrollerItemPlay.style.cursor = "pointer";

                    scrollElementScrollerItemPlay.className = "carouselPlayIcon";

                    $(scrollElementScrollerItemPlay).click
                    (
                        function ()
                        {
                            self.pause();

                            ebloc.showLightBox
                            (
                                scrollImage[1],
                                function ()
                                {
                                    self.resume();
                                }
                            );
                        }
                    );
                }
            }
        )();
    }

    this._scrollElementScroller.style.width = (this._scrollElementWidth * (scrollImages.length + 1)) + "px";
    this._scrollElementScroller.style.height = scrollElementHeight + "px";

    this._scrollElementScroller = $(this._scrollElementScroller);
    this._totalIndex = scrollImages.length;
    if (startIndex)
    {
        this._currentIndex = Math.min(startIndex, this._totalIndex - 1);
    }
    else
    {
        this._currentIndex = 0;
    }

    this._scrollElementScroller.css("marginLeft", (-1 * this._currentIndex * this._scrollElementWidth) + "px");

    this._fadeCopies = fadeCopies;

    this._isAnimating = false;
    this._currentTimeout = null;

    this._isPaused = false;

    if (scrollImages.length > 1)
    {
        this._showFade(this._currentIndex);
        this._activateNavigation(this._currentIndex);

        this._play();
    }
    else
    {
        this._showFade(this._currentIndex);
    }
};

ebloc.Carousel.delayInMilliseconds = 10000;
ebloc.Carousel.changeInMilliseconds = 1000;

ebloc.Carousel.prototype =
{
    _activateNavigation: function (index)
    {
        if (this._navigationElement != null)
        {
            var active = $("li.active", this._navigationElement[0]).removeClass("active");

            $("li", this._navigationElement[0])[index].className = "active";
        }
    },

    pause: function ()
    {
        if (this._isPaused == false)
        {
            this._play();
        }

        this._isPaused = true;
    },

    resume: function ()
    {
        if (this._currentTimeout != null)
        {
            window.clearTimeout(this._currentTimeout);
            this._currentTimeout = null
        }

        this._isPaused = false;
    },

    _next: function ()
    {
        if (!this._isPaused)
        {
            this.next();
        }
    },

    next: function ()
    {
        if (!this._isAnimating)
        {
            ++this._currentIndex;

            this._showScroll(this._currentIndex);

            this._currentIndex = this._currentIndex % this._totalIndex;

            this._showFade(this._currentIndex);
            this._activateNavigation(this._currentIndex);
        }
    },

    _previous: function ()
    {
        if (!this._isAnimating && !this._isPaused)
        {
            this.previous();
        }
    },

    previous: function ()
    {
        if (!this._isAnimating)
        {
            --this._currentIndex;

            if (this._currentIndex < 0)
            {
                this._currentIndex = this._totalIndex - 1;

                this._scrollElementScroller.css("marginLeft", (-1 * this._totalIndex * this._scrollElementWidth) + "px");
            }

            this._showScroll(this._currentIndex);
            this._showFade(this._currentIndex);
            this._activateNavigation(this._currentIndex);
        }
    },

    getIndex: function ()
    {
        return this._currentIndex % this._totalIndex
    },

    show: function (index)
    {
        if (!this._isAnimating)
        {
            this._currentIndex = index % this._totalIndex;

            this._showScroll(this._currentIndex);
            this._showFade(this._currentIndex);
            this._activateNavigation(this._currentIndex);
        }
    },

    _play: function ()
    {
        if (this._currentTimeout != null)
        {
            window.clearTimeout(this._currentTimeout);
            this._currentTimeout = null
        }

        var self = this;

        this._currentTimeout = window.setTimeout
        (
            function ()
            {
                self._next()
            },
            ebloc.Carousel.delayInMilliseconds
        );
    },

    _showScroll: function (index)
    {
        this._isAnimating = true;

        this._fireOnChangeCallback(index % this._totalIndex);

        var self = this;

        this._scrollElementScroller.animate
        (
            {
                marginLeft: -1 * index * this._scrollElementWidth
            },
            ebloc.Carousel.changeInMilliseconds,
            "swing",
            function ()
            {
                self._scrollElementScroller.css("margin-left", -1 * (self._currentIndex % self._totalIndex) * self._scrollElementWidth);
                self._isAnimating = false;

                self._play();
            }
        );
    },

    _showFade: function (index)
    {
        var self = this;

        this._fadeElement.animate
        (
            {
                opacity: 0.0
            },
            ebloc.Carousel.changeInMilliseconds / 2.0,
            "swing",
            function ()
            {
                if (index < self._fadeCopies.length)
                {
                    var nextCopy = self._fadeCopies[index];

                    $("*", self._fadeElement[0]).each
                    (
                        function (index, element)
                        {
                            if (index < nextCopy.length)
                            {
                                element.innerHTML = nextCopy[index];
                            }
                        }
                    );

                    self._fadeElement.animate
                    (
                        {
                            opacity: 0.9
                        },
                        ebloc.Carousel.changeInMilliseconds / 2.0,
                        "swing"
                    );
                }
            }
        );
    },

    _fireOnChangeCallback: function (pageIndex)
    {
        if (this._onChangeCallback != null)
        {
            this._onChangeCallback(pageIndex);
        }
    }
};

ebloc.MultiFader = function (element, images, imagesPerPane, onChangeStartCallback, onChangeEndCallback)
{
    if (!imagesPerPane)
    {
        this._imagesPerPane = 6;
    }
    else
    {
        this._imagesPerPane = imagesPerPane;
    }

    if (!onChangeStartCallback)
    {
        this._onChangeStartCallback = null;
    }
    else
    {
        this._onChangeStartCallback = onChangeStartCallback;
    }

    if (!onChangeEndCallback)
    {
        this._onChangeEndCallback = null;
    }
    else
    {
        this._onChangeEndCallback = onChangeEndCallback;
    }

    this._element = $(element);
    this._images = images;

    while (this._element[0].childNodes.length > 0)
    {
        this._element[0].removeChild(this._element[0].childNodes[0])
    }

    var img;

    for (var index = 0; index < this._imagesPerPane; ++index)
    {
        var img = document.createElement("img");
        element[0].appendChild(img);
        img = element[0].childNodes[element[0].childNodes.length - 1];

        img.style.opacity = "0.0";
    }

    this._currentIndex = 0;
    this._currentTimeout = null;

    this._isAnimating = 0;

    for (var index = 0; index < this._imagesPerPane; ++index)
    {
        ++this._isAnimating;

        this._show(index, true);
    }

    if (this._images.length > this._imagesPerPane)
    {
        this._play();
    }
};

ebloc.MultiFader.delayInMilliseconds = 5000;
ebloc.MultiFader.changeInMilliseconds = 2000;
ebloc.MultiFader.changeOffsetInMilliseconds = 400;

ebloc.MultiFader.prototype =
{
    next: function ()
    {
        if (this._isAnimating == 0)
        {
            this._currentIndex += this._imagesPerPane;

            if (this._currentIndex >= this._images.length)
            {
                this._currentIndex = 0;
            }

            this._next();
        }
    },

    previous: function ()
    {
        if (this._isAnimating == 0)
        {
            this._currentIndex -= this._imagesPerPane;

            if (this._currentIndex < 0)
            {
                this._currentIndex = Math.floor(this._images.length / this._imagesPerPane) * this._imagesPerPane;
            }

            this._next();
        }
    },

    _play: function ()
    {
        if (this._currentTimeout != null)
        {
            window.clearTimeout(this._currentTimeout);
            this._currentTimeout = null
        }

        var self = this;

        this._currentTimeout = window.setTimeout
        (
            function ()
            {
                self.next()
            },
            ebloc.MultiFader.delayInMilliseconds + ebloc.MultiFader.changeInMilliseconds
        );
    },

    _next: function ()
    {
        var fadeDelays = [];

        fadeDelays[0] = 0.0;
        fadeDelays[1] = ebloc.MultiFader.changeOffsetInMilliseconds;
        fadeDelays[2] = ebloc.MultiFader.changeOffsetInMilliseconds * 2.0;
        fadeDelays[3] = ebloc.MultiFader.changeOffsetInMilliseconds;
        fadeDelays[4] = ebloc.MultiFader.changeOffsetInMilliseconds * 2.0;
        fadeDelays[5] = ebloc.MultiFader.changeOffsetInMilliseconds * 3.0;

        var self = this;

        for (var index = 0; index < this._imagesPerPane; ++index)
        {
            (
                function ()
                {
                    var localIndex = self._currentIndex + index;

                    window.setTimeout
                    (
                        function ()
                        {
                            self._hide(localIndex);
                        },
                        fadeDelays[index]
                    );
                }
            )();
        }

        this._play();
    },

    _hide: function (index)
    {
        if (this._isAnimating == 0)
        {
            this._fireOnChangeStartCallback(Math.floor(index / this._imagesPerPane));
        }

        ++this._isAnimating;

        var img = this._element[0].childNodes[index % this._imagesPerPane];

        var self = this;

        $(img).animate
        (
            {
                opacity: 0.0
            },
            ebloc.MultiFader.changeInMilliseconds / 2.0,
            "swing",
            function ()
            {
                self._show(index);
            }
        );
    },

    _show: function (index, supressOnEnd)
    {
        if (!supressOnEnd)
        {
            var supressOnEnd = false;
        }

        if (index < this._images.length)
        {
            var imageData = this._images[index];

            var img = this._element[0].childNodes[index % this._imagesPerPane];

            img.src = imageData[0];
            img.setAttribute("alt", imageData[1]);
            img.setAttribute("title", imageData[1]);

            var self = this;

            $(img).animate
            (
                {
                    opacity: 1.0
                },
                ebloc.MultiFader.changeInMilliseconds / 2.0,
                "swing",
                function ()
                {
                    --self._isAnimating;

                    if (self._isAnimating == 0)
                    {
                        if (!supressOnEnd)
                        {
                            self._fireOnChangeEndCallback(Math.floor(index / self._imagesPerPane));
                        }
                    }
                }
            );
        }
        else
        {
            --this._isAnimating;

            if (this._isAnimating == 0)
            {
                if (!supressOnEnd)
                {
                    this._fireOnChangeEndCallback(Math.floor(this._currentIndex / this._imagesPerPane));
                }
            }
        }
    },

    _fireOnChangeStartCallback: function (pageIndex)
    {
        if (this._onChangeStartCallback != null)
        {
            this._onChangeStartCallback(pageIndex);
        }
    },

    _fireOnChangeEndCallback: function (pageIndex)
    {
        if (this._onChangeEndCallback != null)
        {
            this._onChangeEndCallback(pageIndex);
        }
    }
};

ebloc.Navigation = function (navigationElement, totalPages)
{
    this._navigationElement = $(navigationElement);

    if (this._navigationElement.length > 0)
    {
        while (this._navigationElement[0].childNodes.length > 0)
        {
            this._navigationElement[0].removeChild(this._navigationElement[0].childNodes[0])
        }

        this._totalPages = totalPages;

        var pageNavigationPercenatageWidth = Math.floor(100.0 / (this._totalPages - 1));

        var div;
        var innerDiv;

        var pageNavigationPercenatageWidthTotal = 0;

        for (var index = 0; index < (this._totalPages - 1); ++index)
        {
            div = document.createElement("div");
            this._navigationElement[0].appendChild(div);
            div = this._navigationElement[0].childNodes[this._navigationElement[0].childNodes.length - 1];

            if (index < (this._totalPages - 2))
            {
                div.style.width = pageNavigationPercenatageWidth + "%";
            }
            else
            {
                div.style.width = (100 - pageNavigationPercenatageWidthTotal) + "%";
            }

            innerDiv = document.createElement("div");
            div.appendChild(innerDiv);

            if (index == 0)
            {
                this._navigationElementItemWidth = $(div).width();
            }

            pageNavigationPercenatageWidthTotal += pageNavigationPercenatageWidth;
        }

        this._navigationActive = document.createElement("p");
        this._navigationElement[0].insertBefore(this._navigationActive, this._navigationElement[0].childNodes[0]);
        this._navigationActive = this._navigationElement[0].childNodes[0];

        this._navigationActive.className = "active";

        var span = document.createElement("span");
        this._navigationActive.appendChild(span);

        this._pageIndex = 0;

        this.show();
    }
};

ebloc.Navigation.changeInMilliseconds = 1000;

ebloc.Navigation.prototype =
{
    hide: function (pageIndex, callback)
    {
        var self = this;

        $(this._navigationActive).animate
        (
            {
                opacity: 0.0
            },
            ebloc.Navigation.changeInMilliseconds / 2.0,
            "swing",
            function ()
            {
                if (!isNaN(pageIndex))
                {
                    self._pageIndex = pageIndex;
                }

                self._positionActiveNavigation();

                if (callback)
                {
                    callback();
                }
            }
        );
    },

    show: function (pageIndex)
    {
        if (!isNaN(pageIndex))
        {
            this._pageIndex = pageIndex;

            this._pageIndex %= this._totalPages;
        }

        this._positionActiveNavigation();

        $(this._navigationActive).animate
        (
            {
                opacity: 1.0
            },
            ebloc.Navigation.changeInMilliseconds / 2.0,
            "swing"
        );
    },

    _positionActiveNavigation: function ()
    {
        this._navigationActive.childNodes[0].innerHTML = this._pageIndex + 1;

        this._navigationActive.style.marginLeft = (this._pageIndex * this._navigationElementItemWidth) + "px";
    }
};

ebloc.showLightBox = function (innerHtml, hideCallback, popupClassName, backgroundClassName)
{
    if (hideCallback)
    {
        ebloc.showLightBox.hideCallback = hideCallback;
    }
    else
    {
        ebloc.showLightBox.hideCallback = null;
    }

    if (!ebloc.showLightBox._popup);
    {
        if (!popupClassName)
        {
            var popupClassName = "lightBox";
        }

        if (!backgroundClassName)
        {
            var backgroundClassName = "lightBoxBackground";
        }

        var body = $("body")[0];
        
        ebloc.showLightBox._popup = document.createElement("div");
        body.appendChild(ebloc.showLightBox._popup);
        ebloc.showLightBox._popup = $(body.childNodes[body.childNodes.length - 1]);

        ebloc.showLightBox._popup.addClass(popupClassName);

        ebloc.showLightBox._popup.css("display", "none");
        ebloc.showLightBox._popup.css("position", "fixed");

        ebloc.showLightBox._background = document.createElement("div");
        body.appendChild(ebloc.showLightBox._background);
        ebloc.showLightBox._background = $(body.childNodes[body.childNodes.length - 1]);

        ebloc.showLightBox._background.addClass(backgroundClassName);

        ebloc.showLightBox._background.css("display", "none");
        ebloc.showLightBox._background.css("position", "fixed");
        ebloc.showLightBox._background.css("width", "100%");
        ebloc.showLightBox._background.css("top", "0px");
        ebloc.showLightBox._background.css("left", "0px");

        ebloc.showLightBox._background.click
        (
            function ()
            {
                ebloc.showLightBox.hide();
            }
        );

        ebloc.showLightBox._setSize();

        $(window).resize
        (
            function ()
            {
                ebloc.showLightBox._setSize();
            }
        );
    }

    ebloc.showLightBox._popup[0].innerHTML = innerHtml;

    ebloc.showLightBox._popup.css("left", (($(window).width() - ebloc.showLightBox._popup.width() - 22) / 2) + "px");
    ebloc.showLightBox._popup.css("top", (($(window).height() - ebloc.showLightBox._popup.height() - 22) / 2) + "px");

    ebloc.showLightBox._background.css("opacity", "0.0");
    ebloc.showLightBox._background.css("display", "block");
    ebloc.showLightBox._popup.css("opacity", "0.0");
    ebloc.showLightBox._popup.css("display", "block");

    ebloc.showLightBox._background.animate
    (
        {
            "opacity": 0.8
        },
        500,
        "swing"
    );

    ebloc.showLightBox._popup.animate
    (
        {
            "opacity": 1.0
        },
        500,
        "swing"
    );
}

ebloc.showLightBox._setSize = function ()
{
    ebloc.showLightBox._background.css("width", $(window).width() + "px");
    ebloc.showLightBox._background.css("height", $(window).height() + "px");
}

ebloc.showLightBox.hide = function ()
{
    ebloc.showLightBox._background.animate
    (
        {
            "opacity": 0.0
        },
        500,
        "swing",
        function ()
        {
            ebloc.showLightBox._background.css("display", "none");
        }
    );

    ebloc.showLightBox._popup.animate
    (
        {
            "opacity": 0.0
        },
        500,
        "swing",
        function ()
        {
            ebloc.showLightBox._popup.css("display", "none");
            ebloc.showLightBox._popup[0].innerHTML = "";

            if (ebloc.showLightBox.hideCallback != null)
            {
                ebloc.showLightBox.hideCallback();
            }
        }
    );
}

ebloc.scroller = function (targetElement, previousElement, nextElement, inactiveColour)
{
    targetElement = $(targetElement);

    this._inactiveColour = inactiveColour.replace("#", "");

    this._scrollOffset = targetElement.width();

    this._scroller = $("div", targetElement[0]);
    this._scroller.css("width", ($("img", this._scroller[0]).length * this._scrollOffset) + "px");

    var images = $("img", this._scroller[0]);

    this._currentLeft = 0;
    this._maxLeft = (images.length - 1) * this._scrollOffset;

    var self = this;

    this._nextElement = $(nextElement);
    this._nextElement.click
    (
        function ()
        {
            if (self._currentLeft + self._scrollOffset <= self._maxLeft)
            {
                self._currentLeft += self._scrollOffset;

                self._updateNavigation();

                self._scroller.animate
                (
                    {
                        marginLeft: "-=" + self._scrollOffset
                    },
                    500,
                    "swing"
                );
            }
        }
    );

    this._previousElement = $(previousElement);
    this._previousElement.click
    (
        function ()
        {
            if (self._currentLeft - self._scrollOffset >= 0)
            {
                self._currentLeft -= self._scrollOffset;

                self._updateNavigation();

                self._scroller.animate
                (
                    {
                        marginLeft: "+=" + self._scrollOffset
                    },
                    500,
                    "swing"
                );
            }
        }
    );

    this._previousElement.css("color", "#" + inactiveColour);

    images.click
    (
        function ()
        {
            ebloc.showLightBox("<img src=\"" + this.getAttribute("src") + "\" alt=\"" + this.getAttribute("src") + "\" title=\"" + this.getAttribute("title") + "\" />");
        }
    );

    images.css("cursor", "pointer");
}

ebloc.scroller.prototype._updateNavigation = function ()
{
    if (this._currentLeft < this._maxLeft)
    {
        this._nextElement.css("color", "");
    }
    else
    {
        this._nextElement.css("color", "#" + this._inactiveColour);
    }

    if (this._currentLeft > 0)
    {
        this._previousElement.css("color", "");
    }
    else
    {
        this._previousElement.css("color", "#" + this._inactiveColour);
    }
}

