﻿function SlideshowAnimation() {
    this._products;
    this._timerInterval = 1000;
    this._timerId = 0;
    this._currentProductIndex = 0;
    this._targetDiv;
    this._loop = true;
    this._imageTagId;
    this._authorTagId;
    this._productNameTagId;
    this._linkTagId;
    this._ContainerDiv;
    this._loaderImage;

    this._next$delegate = Function.createDelegate(this, this.next);

}
SlideshowAnimation.prototype = {
    set_loaderImage: function(value) {
        this._loaderImage = value;
    },
    set_ContainerDiv: function(value) {
        this._ContainerDiv = value;
    },
    set_products: function(value) {
        this._products = value;
    },
    get_products: function() {
        return this._products;
    },
    set_timerInterval: function(value) {
        this._timerInterval = value;
    },
    get_timerInterval: function() {
        return this._timerInterval;
    },
    set_targetDiv: function(value) {
        this._targetDiv = value;
    },
    get_imageTagId: function() {
        return this._imageTagId;
    },
    set_imageTagId: function(value) {
        this._imageTagId = value;
    },
    get_productNameTagId: function() {
        return this._productNameTagId;
    },
    set_productNameTagId: function(value) {
        this._productNameTagId = value;
    },
    get_authorTagId: function() {
        return this._authorTagId;
    },
    set_authorTagId: function(value) {
        this._authorTagId = value;
    },
    get_linkId: function() {
        return this._linkTagId;
    },
    set_linkId: function(value) {
        this._linkTagId = value;
    },
    get_targetDiv: function() {
        return this._targetDiv;
    },
    set_loop: function(value) {
        this._loop = value;
    },
    get_loop: function() {
        return this._loop;
    },
    _loadImageDiv: function() {
        if (this._timerId != 0) {
            var image = new Image();
            var ss = this;
            var imageUrl = ss._products[ss._currentProductIndex].Filename;

            if (imageUrl == null) {
                ss.next();
                return;
            }
            
            ss._loaderImage.show();
            $(image).load(function() {
                ss._loaderImage.hide();
                $(ss._ContainerDiv).fadeOut("slow", function() {
                    if (ss._linkTagId) {
                        var link = "Product_Details.aspx?Id=" + ss._products[ss._currentProductIndex].Id
                        $get(ss._linkTagId).href = link;
                        $($get(ss._imageTagId)).wrap("<a href='" + link + "'></a>");
                    }
                    if (ss._imageTagId) $get(ss._imageTagId).src = imageUrl;
                    if (ss._authorTagId) $get(ss._authorTagId).innerHTML = ss._products[ss._currentProductIndex].Authors;
                    if (ss._productNameTagId) $get(ss._productNameTagId).innerHTML = ss._products[ss._currentProductIndex].ProductName;
                    if (ss._products[ss._currentProductIndex + 1]._imageTagId) image.src = ss._products[ss._currentProductIndex + 1]._imageTagId;
                    
                    $(ss._ContainerDiv).fadeIn();
                });

                this._timerId = setTimeout(ss._next$delegate, ss._timerInterval);
            }).attr("src", imageUrl).error(function() { ss.next(); });
        }
    },
    
    start: function() {
        this._timerId = null;
        this.next();
    },
    
    stop: function() {
        clearInterval(this._timerId);
        this._timerId = 0;
    },
    
    previous: function() {
        if (this._currentProductIndex > 0) { this._currentProductIndex--; }
        else { this._currentProductIndex = this._products.length - 1; }
        this._loadImageDiv();
    },
    
    next: function() {
        if (this._currentProductIndex < this._products.length - 1) { this._currentProductIndex++; this._loadImageDiv(); }
        else if (this._loop == true) { this._currentProductIndex = 0; this._loadImageDiv(); }
    }
}
SlideshowAnimation.registerClass('SlideshowAnimation');