﻿
if (!SN.UI) {
    SN.UI = new Object();
}

SN.UI.Slideshow = function Slideshow(slides, frame, prevBtn, nextBtn) {
    this.inheritFrom = SN.Events.EventDispatcher;
    this.inheritFrom();
    // No fades if IE, just sliding :)
    var home = this;

    var currentIndex = 0;

    var trans = null;

    this.slide = function(targetIndex) {
        if (trans) {
            return;
        }
        home.dispatchEvent('onstart', { 'index': currentIndex });
        if (targetIndex >= slides.length) {
            targetIndex = 0;
        } else if (targetIndex < 0) {
            targetIndex = slides.length - 1;
        }
        var positions = [-slides[currentIndex].offsetWidth * 1.5, frame.offsetWidth * 1.5];
        var target;
        if (targetIndex > currentIndex) {
            target = 0;
        } else if (targetIndex < currentIndex) {
            target = 1;
        } else {
            return;
        }
        //var fader = new SN.Time.Animation.Fade(slides[currentIndex], 0, null, 85);
        trans = new SN.Time.Animation.Translate(slides[currentIndex], 'x', positions[target], function() {
            // We've animated this mofo!  Now we have to animate the next one into the frame :)
            slides[currentIndex].style.display = 'none';
            currentIndex = targetIndex;
            slides[currentIndex].style.left = positions[(target) ? 0 : 1] + 'px';
            slides[currentIndex].style.display = 'block';
            //slides[currentIndex].className = ' transparent';
            //fader = new SN.Time.Animation.Fade(slides[currentIndex], 100, null, 85);
            trans = new SN.Time.Animation.Translate(slides[currentIndex], 'x', 0, function() {
                home.dispatchEvent('ondone', { 'index': currentIndex });
                //fader = null;
                trans = null;
            }, 95);
            // We need to set the height, THEN adjust it!
            //if (slides[currentIndex].offsetHeight != frame.offsetHeight) {
                //resizeBy = new SN.Time.Animation.Resize(frame, 'y', slides[currentIndex].offsetHeight);
            //}
        }, 95);
    };

    var init = function() {
        prevBtn.onclick = function() {
            this.blur();
            home.slide(currentIndex - 1);
            return false;
        };

        nextBtn.onclick = function() {
            //this.blur();
            home.slide(currentIndex + 1);
            return false;
        };
    };

    init();
};
