﻿/* Slideshow
 *******************************************************************************/
function slideshow(element, continuous)
{
    this.element = element;
    this.slider;
    this.items;
    this.itemWidth;
    this.current = 0;
    this.transition = "slide";
    this.continuous = (continuous) ? continuous : false;
    this.speed = 500;
    this.animating = false;

    // events
    this.onSlide = null;

    this.initialise();
}
slideshow.prototype.initialise = function ()
{
    var _this = this;

    // Show the slideshow so initialisation occurs properly.
    var display = _this.element.style.display;
    _this.element.style.display = "block";

    _this.slider = $(".slider", _this.element);
    _this.items = $(_this.slider).children("div");

    // Add the first item to the end for continuous scrolling. Initialise it with the correct look - info panel collapsed.
    if (_this.items.length > 1)
    {
        if (_this.continuous)
            $(_this.slider).children("div").first().clone().appendTo(_this.slider);
    }
    else
    {
        $(".left-control, .right-control", _this.element).hide();
    }

    $(".information-inner", $(_this.slider).children("div").last()).animate({ "bottom": $(".information h3", $(_this.slider).children("div").last()).outerHeight() - $(".information-inner", $(_this.slider).children("div").last()).outerHeight() + 20 }, 250);

    if (_this.items.length > 0)
        _this.itemWidth = _this.items[0].offsetWidth;

    _this.element.style.display = display;
}
slideshow.prototype.next = function (callback)
{
    var _this = this;
    _this.current++;
    if (_this.current > _this.items.length - 1)
        _this.current = 0;

    return _this.scrollTo(_this.current, callback, true);
}
slideshow.prototype.previous = function (callback)
{
    var _this = this;
    _this.current--;
    if (_this.current < 0)
        _this.current = _this.items.length - 1;

    return _this.scrollTo(_this.current, callback, true);
}
slideshow.prototype.scrollTo = function (position, callback, scrollContinuously)
{
    var _this = this;
    if (!_this.animating)
    {
        _this.animating = true;
        var pos = -1 * position * _this.itemWidth;

        switch (_this.transition)
        {
            default:
            case "slide":
                if (scrollContinuously)
                {
                    if (_this.current == 0 && $(_this.slider).position().left != -_this.itemWidth)
                    {
                        $(_this.slider).animate({ "left": -1 * _this.items.length * _this.itemWidth }, _this.speed, function ()
                        {
                            _this.animating = false;
                            $(_this.slider).css({ "left": 0 });
                        });
                    }
                    else if (_this.current == _this.items.length - 1 && $(_this.slider).position().left == 0)
                    {
                        $(_this.slider).css({ "left": -1 * _this.items.length * _this.itemWidth });
                        $(_this.slider).animate({ "left": pos }, _this.speed, function () { _this.animating = false; });
                    }
                    else
                        $(_this.slider).animate({ "left": pos }, _this.speed, function () { _this.animating = false; });
                }
                else
                    $(_this.slider).animate({ "left": pos }, _this.speed, function () { _this.animating = false; });
                break;
            case "fade":
                $(_this.slider).fadeOut(_this.speed / 2, function ()
                {
                    $(_this.slider).animate({ "left": pos });
                    $(_this.slider).fadeIn(_this.speed / 2);
                    _this.animating = false;
                });
                break;
        }

        _this.current = position;

        if (callback)
            callback(_this.current);

        if (_this.onSlide)
            _this.onSlide();
    }
    return _this.current;
}

/* Project Slideshow
*******************************************************************************/
function projectSlideshow(element, continuous)
{
    this.continuous = (continuous) ? continuous : false;
    this.slideshow = new slideshow(element, continuous);
    this.initialise();
}
projectSlideshow.prototype.initialise = function ()
{
    var _this = this;

    // Show the slideshow so initialisation occurs properly.
    var display = _this.slideshow.element.style.display;
    _this.slideshow.element.style.display = "block";

    // Information slides
    $(_this.slideshow.items).each(function (index)
    {
        try
        {
            $(".information", this).height($(".information-inner", this).outerHeight());
            $(".information-inner", this).animate({ "bottom": $(".information h3", this).outerHeight() - $(".information-inner", this).outerHeight() + 20 }, 250);
        }
        catch (err) { }
    });

    $(".information", _this.slideshow.element).hover(function ()
    {
        if ($(this).hasClass("has-description"))
            $(".information-inner", this).animate({ "opacity": 0.8, "bottom": 0 }, 250);
        else
            $(".information-inner", this).animate({ "opacity": 0.8 }, 250);
    },
    function ()
    {
        if ($(this).hasClass("has-description"))
            $(".information-inner", this).animate({ "opacity": 0.5, "bottom": $("h3", this).outerHeight() - $(".information-inner", this).outerHeight() + 20 }, 250);
        else
            $(".information-inner", this).animate({ "opacity": 0.5 }, 250);
    });

    // Left
    $(".left-control", _this.slideshow.element).click(function (evt)
    {
        evt.preventDefault();
        _this.slideshow.previous();
    });

    // Right
    $(".right-control", _this.slideshow.element).click(function (evt)
    {
        evt.preventDefault();
        _this.slideshow.next();
    });

    // Hover
    $(".right-control, .left-control", _this.slideshow.element).hover(function ()
    {
        $("span", this).fadeTo(150, 0.8);
    },
    function ()
    {
        $("span", this).fadeTo(150, 0);
    });

    _this.slideshow.element.style.display = display;
}


/* Thumbnail Slideshow
*******************************************************************************/
function thumbnailSlideshow(element)
{
    this.slideshow = new slideshow(element);
    this.controls = $(".controls", this.slideshow.element);
    this.initialise();
}
thumbnailSlideshow.prototype.initialise = function ()
{
    var _this = this;

    // Show the slideshow so initialisation occurs properly.
    var display = _this.slideshow.element.style.display;
    _this.slideshow.element.style.display = "block";

    $(_this.slideshow.element).after($(".controls", _this.slideshow.element));

    $(".move-left", _this.controls).hide();
    if (_this.slideshow.items.length == 1)
        $(".move-right", _this.controls).hide();

    $(".move-left", _this.controls).click(function (evt)
    {
        evt.preventDefault();
        var i = _this.slideshow.previous();

        if (i == 0)
            $(this).fadeOut(100);
        if (i < _this.slideshow.items.length - 1)
            $(".move-right", _this.controls).fadeIn(200);
    });
    $(".move-right", _this.controls).click(function (evt)
    {
        evt.preventDefault();
        var i = _this.slideshow.next();

        if (i == _this.slideshow.items.length - 1)
            $(this).fadeOut(100);
        if (i > 0)
            $(".move-left", _this.controls).fadeIn(200);
    });

    _this.slideshow.element.style.display = display;
}
