﻿Type.registerNamespace('Asbmr.ClassLibrary.Behaviors');

Asbmr.ClassLibrary.Behaviors.PhotoGallery = function(element) {
    Asbmr.ClassLibrary.Behaviors.PhotoGallery.initializeBase(this, [element]);

    this._processingIntervalId = null;
    this._imageLink = null;
    this._image = null;
    this._indexLinkContainer = null;

    this._delayInMilliseconds = null;
    this._fadeTimeInMilliseconds = null;

    this._photoGalleryInfo = null;
    this._photoGalleryItems = null;

    this._currentIndex = 0;
    this._currentlyProcessing = false;

    this._intervalElapsedDelegate = null;
    this._loadItemDelegate = null;

    this._backButtonClickDelegate = null;
    this._playPauseButtonClickDelegate = null;
    this._nextButtonClickDelegate = null;
}

Asbmr.ClassLibrary.Behaviors.PhotoGallery.prototype = {
    initialize: function() {
        Asbmr.ClassLibrary.Behaviors.PhotoGallery.callBaseMethod(this, 'initialize');

        this._photoGalleryItems = Sys.Serialization.JavaScriptSerializer.deserialize(this._photoGalleryInfo);

        if (this._photoGalleryItems.length > 1) {

            this._backButtonClickDelegate = Function.createDelegate(this, this._backButtonClick);
            $addHandler(this._findBackButton()[0], "click", this._backButtonClickDelegate);

            this._nextButtonClickDelegate = Function.createDelegate(this, this._nextButtonClick);
            $addHandler(this._findNextButton()[0], "click", this._nextButtonClickDelegate);

            this._playPauseButtonClickDelegate = Function.createDelegate(this, this._playPauseButtonClick);
            $addHandler(this._findPlayPauseButton()[0], "click", this._playPauseButtonClickDelegate);

            this._intervalElapsedDelegate = Function.createDelegate(this, this._intervalElapsed);

            this._loadItemDelegate = Function.createDelegate(this, this._loadItem);
        }

        this._loadItem();

        if (this._photoGalleryItems.length > 1) {
            this._startProcess();
        }
    },

    dispose: function() {
        this._pause();

        if (this._findBackButton()[0] && this._backButtonClickDelegate) {
            $removeHandler(this._findBackButton()[0], "click", this._backButtonClickDelegate);
        }

        if (this._findNextButton()[0] && this._nextButtonClickDelegate) {
            $removeHandler(this._findNextButton()[0], "click", this._nextButtonClickDelegate);
        }

        if (this._findPlayPauseButton()[0] && this._playPauseButtonClickDelegate) {
            $removeHandler(this._findPlayPauseButton()[0], "click", this._playPauseButtonClickDelegate);
        }

        this._processingIntervalId = null;
        this._imageLink = null;
        this._image = null;
        this._indexLinkContainer = null;
        this._intervalElapsedDelegate = null;
        this._loadItemDelegate = null;
        this._backButtonClickDelegate = null;
        this._playPauseButtonClickDelegate = null;
        this._nextButtonClickDelegate = null;
        this._delayInMilliseconds = null;
        this._fadeTimeInMilliseconds = null;
        this._photoGalleryInfo = null;
        this._photoGalleryItems = null;

        this._currentIndex = 0;
        this._currentlyProcessing = false;

        Asbmr.ClassLibrary.Behaviors.PhotoGallery.callBaseMethod(this, 'dispose');
    },

    _startProcess: function() {
        if (!this._currentlyProcessing) {
            this._processingIntervalId = setInterval(this._intervalElapsedDelegate, this._delayInMilliseconds);
            this._currentlyProcessing = true;
            return;
        }
    },

    _intervalElapsed: function() {
        var element = this._findImageElement();
        this._currentIndex = this._incrementIndex();
        var loadItemDelegate = this._loadItemDelegate;
        element.fadeOut(this._fadeTimeInMilliseconds, loadItemDelegate).fadeIn(this._fadeTimeInMilliseconds);
    },

    _loadItem: function() {
        this._populate();
        this._preloadImage();
    },

    _backButtonClick: function(evt) {
        this._currentIndex = this._decrementIndex();
        this._loadItem();
        this._pause();
    },

    _nextButtonClick: function(evt) {
        this._currentIndex = this._incrementIndex();
        this._loadItem();
        this._pause();
    },

    _playPauseButtonClick: function(evt) {
        if (this._currentlyProcessing) {
            this._pause();
        }
        else {
            this._resume();
        }
        return;
    },

    _pause: function() {
        this._findPlayPauseButton().attr("src", "/images/ui/gallery_play.jpg");
        clearInterval(this._processingIntervalId);
        this._currentlyProcessing = false;
    },

    _resume: function() {
        this._findPlayPauseButton().attr("src", "/images/ui/gallery_pause.jpg");
        this._startProcess();
    },

    _populate: function() {
        if (this._photoGalleryItems[this._currentIndex]) {
            var title = this._photoGalleryItems[this._currentIndex].Title;

            var image = this._findImageElement();
            image.attr("src", this._photoGalleryItems[this._currentIndex].ImageSource);
            image.attr("title", title);

            this._findTitleContainer().html(title);
            this._findCaptionContainer().html(this._photoGalleryItems[this._currentIndex].Caption);

            var indexString = "" + (this._currentIndex + 1) + " of " + this._photoGalleryItems.length;
            this._findIndexContainer().html(indexString)

            // var imageLink = this._findImageLinkElement()
            // imageLink.attr("href", this._photoGalleryItems[this._currentIndex].Destination);
            // imageLink.attr("target", this._photoGalleryItems[this._currentIndex].Target);
        }
    },

    _preloadImage: function() {
        var index = this._incrementIndex();
        var image = new Image();
        if (this._photoGalleryItems[index]) {
            $(image).attr("src", this._photoGalleryItems[index].ImageSource);
        }
    },

    _incrementIndex: function() {
        var index;
        if (this._currentIndex < this._photoGalleryItems.length - 1) {
            index = this._currentIndex + 1;
        }
        else {
            index = 0;
        }
        return index;
    },

    _decrementIndex: function() {
        var index;
        if (this._currentIndex > 0) {
            index = this._currentIndex - 1;
        }
        else {
            index = this._photoGalleryItems.length - 1;
        }
        return index;
    },

    _findImageElement: function() {
        return $("#" + this._image).eq(0);
    },

    _findBackButton: function() {
        return $("#backButton").eq(0);
    },

    _findPlayPauseButton: function() {
        return $("#playPauseButton img").eq(0);
    },

    _findNextButton: function() {
        return $("#nextButton").eq(0);
    },

    _findIndexContainer: function() {
        return $("#galleryIndex").eq(0);
    },

    _findTitleContainer: function() {
        return $("#photoTitle").eq(0);
    },

    _findCaptionContainer: function() {
        return $("#photoCaption").eq(0);
    },

    get_Image: function() {
        return this._image;
    },

    set_Image: function(elt) {
        this._image = elt;
    },

    get_DelayInMilliseconds: function() {
        return this._delayInMilliseconds;
    },

    set_DelayInMilliseconds: function(val) {
        this._delayInMilliseconds = val;
    },

    get_FadeTimeInMilliseconds: function() {
        return this._fadeTimeInMilliseconds;
    },

    set_FadeTimeInMilliseconds: function(val) {
        this._fadeTimeInMilliseconds = val;
    },

    get_PhotoGalleryInfo: function() {
        return this._photoGalleryInfo;
    },

    set_PhotoGalleryInfo: function(val) {
        this._photoGalleryInfo = val;
    }
}

Asbmr.ClassLibrary.Behaviors.PhotoGallery.registerClass('Asbmr.ClassLibrary.Behaviors.PhotoGallery', AjaxControlToolkit.BehaviorBase);
if (typeof (Sys) !== 'undefined') { Sys.Application.notifyScriptLoaded(); }
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();