﻿/**
*  SlideShow Class
*
*  Date:       2008-06-03
*  Author:     Michael Giuliano
*  Copyright:  Live Nation Music UK
*/

var SlideShow = Class.create();
SlideShow.prototype = {

    ss_timing: 5000,

    ss_id: '',
    ss_images: new Array(),
    ss_alt: '',

    ss_count: 0,
    ss_total: 0,
    ss_image: null,
    ss_link: null,
    ss_back: null,
    ss_forward: null,

    ss_t: null,

    initialize: function(ss_id, ss_images, ss_alt) {

        this.ss_id = ss_id;
        this.ss_images = ss_images;
        this.ss_alt = ss_alt;

        this.ss_count = -1;
        this.ss_total = this.ss_images.length;

        var els = $(this.ss_id).getElementsByTagName('a');
        var ell = els.length;
        for (i = 0; i < ell; i++) {
            switch (els[i].className) {
                case 'ss_image':
                    this.ss_image = els[i].getElementsByTagName('img')[0];
                    this.ss_link = els[i];
                    this.setParam(this.ss_link, 'caller', this);
                    this.ss_link.onmouseover = function() {
                        this.caller.stop();
                        return false;
                    }
                    this.ss_link.onmouseout = function() {
                        this.caller.play();
                        return false;
                    }
                    break;
                case 'ss_back':
                    this.ss_back = els[i];
                    this.setParam(this.ss_back, 'caller', this);
                    this.ss_back.onclick = function() {
                        this.caller.goBack();
                        return false;
                    }
                    this.ss_back.onmouseover = function() {
                        this.caller.stop();
                        return false;
                    }
                    break;
                case 'ss_forward':
                    this.ss_forward = els[i];
                    this.setParam(this.ss_forward, 'caller', this);
                    this.ss_forward.onclick = function() {
                        this.caller.goForward();
                        return false;
                    }
                    this.ss_forward.onmouseover = function() {
                        this.caller.stop();
                        return false;
                    }
                    break;
            }
        }

        this.goForward();
        this.play();
    },

    setParam: function(e, param, value) {
        e[param] = value;
    },

    goBack: function() {
        this.ss_count--;
        if (this.ss_count < 0) { this.ss_count = this.ss_total - 1 };
        this.changeSlide();
    },

    goForward: function() {
        this.ss_count++;
        if (this.ss_count > this.ss_total - 1) { this.ss_count = 0 };
        this.changeSlide();
    },

    play: function() {
        var caller = this;
        function loop(o) {
            clearTimeout(o.ss_t);
            o.goForward();
            o.play();
        }
        this.ss_t = setTimeout(function() { loop(caller); }, this.ss_timing);
    },

    stop: function() {
        clearTimeout(this.ss_t);
    },

    changeSlide: function() {
        var src = this.ss_images[this.ss_count][0];
        var href = this.ss_images[this.ss_count][1];
        var alt = this.ss_images[this.ss_count][2];
        this.ss_image.src = src;
        this.ss_image.alt = alt;
        this.ss_link.href = href;
        this.ss_link.title = alt;
    }

};