/*************************************
**
** Copyright (c) 2009 - Chris Neal
** For use with permission only.
** Please do not remove this header.
** For support email: 
** chris [a] tophcreative dot com
**
**************************************/
var slideshow = {
    w: '960',
    h: '420',
    createSlideShow: function (vars) {
        if ($$('#' + vars.containerId).length < 1) return;
        if (vars.images.length == 0) return;
        this.v = vars;
        this.currentDivNum = 0;
        this.loaded = 1;
        this.currentShown = Math.floor(Math.random() * (this.v.images.length + 1));
        this.running = false;
        this.overlayVisible = false;
        this.container = $(this.v.containerId);
        if (this.v.images.length < 2) {
            this.makeOverlay();
            return;
        }
        this.picDiv0 = new Element('div').writeAttribute('id', 'pic0').addClassName('pic0').setStyle({
            position: 'absolute',
            width: this.w + 'px',
            height: this.h + 'px',
            overflow: 'hidden',
            'z-index': '1',
            opacity: '1',
            display: 'block'
        });
        this.picDiv1 = new Element('div').writeAttribute('id', 'pic1').addClassName('pic1').setStyle({
            position: 'absolute',
            width: this.w + 'px',
            height: this.h + 'px',
            overflow: 'hidden',
            'z-index': '0',
            opacity: '0',
            display: 'block'
        });
        this.picDiv0.update(new Element('a', {
            href: this.v.images[this.currentShown].link,
            title: this.v.images[this.currentShown].alt,
            target: this.v.images[this.currentShown].target
        }).update(new Element('img', {
            src: this.v.images[this.currentShown].src,
            alt: this.v.images[this.currentShown].alt
        })));
        this.picDiv1.update(new Element('a', {
            href: this.v.images[1].link,
            title: this.v.images[1].alt,
            target: this.v.images[1].target
        }).update(new Element('img', {
            src: this.v.images[1].src,
            alt: this.v.images[1].alt
        })));
        this.container.update('').insert(this.picDiv0).insert(this.picDiv1);
        this.makeSlideNumbers();
        if (this.v.autoStart == true) {
            this.startSlideShow();
        }
    },
    goToSlideWrapper: function (e) {
        var data = $A(arguments);
        this.goToSlide(data[1]);
    },
    goToSlide: function (num) {
        var next = (this.currentDivNum == 0 ? 1 : 0);
        var nextEl = $('pic' + next);
        new Effect.Parallel([new Effect.Morph(nextEl, {
            style: 'opacity: 1',
            duration: this.v.transitionLength,
            sync: true
        }), new Effect.Morph('pic' + this.currentDivNum, {
            style: 'opacity: 0; z-index: 0',
            sync: true
        })], {
            duration: this.v.transitionLength,
            delay: 0,
            beforeStart: this.beforeMorphStart.bind(this, nextEl, num),
            afterFinish: this.afterMorphFinish.bind(this, next),
            queue: {
                position: 'end',
                scope: 'slideshowscope',
                limit: 2
            }
        });
    },
    beforeMorphStart: function () {
        var args = $A(arguments);
        var nextEl = args[0];
        var num = args[1];
        if (this.running == true) {
            this.resetSlideShow();
        }
        if (num == this.v.images.length) num = 0;
        if (num < 0) num = parseInt(this.v.images.length - 1);
        if (this.loaded != num) {
            $($(nextEl).select('a')[0]).writeAttribute('href', this.v.images[num].link).writeAttribute('alt', this.v.images[num].alt).writeAttribute('target', this.v.images[num].target);
            $($(nextEl).select('img')[0]).writeAttribute('src', this.v.images[num].src).writeAttribute('alt', this.v.images[num].alt);
        }
        $('pic' + this.currentDivNum).setStyle({
            'zIndex': 0
        });
        nextEl.setStyle({
            'zIndex': 1
        });
        this.currentShown = num;
        this.updateSlideNumbers();
    },
    afterMorphFinish: function () {
        var args = $A(arguments);
        var next = args[0];
        this.currentDivNum = next;
        this.loadNextSlide();
    },
    loadNextSlide: function () {
        var next = (this.currentDivNum == 0 ? 1 : 0);
        var nextEl = $('pic' + next);
        var nextImg = parseInt(this.currentShown + 1);
        nextImg = (nextImg == this.v.images.length ? 0 : nextImg);
        $($(nextEl).select('a')[0]).writeAttribute('href', this.v.images[nextImg].link).writeAttribute('alt', this.v.images[nextImg].alt).writeAttribute('target', this.v.images[nextImg].target);
        $($(nextEl).select('img')[0]).writeAttribute('src', this.v.images[nextImg].src).writeAttribute('alt', this.v.images[nextImg].alt);
        this.loaded = nextImg;
    },
    makeSlideNumbers: function () {
        this.pageNumbers = new Element('ul').addClassName('pagenumbers').setStyle({
            zIndex: 2
        });
        this.rightArrow = new Element('li').insert(new Element('a').addClassName('right')).addClassName('arrow');
        this.leftArrow = new Element('li').insert(new Element('a').addClassName('left')).addClassName('arrow');
        $(this.pageNumbers).insert(this.leftArrow);
        var n = this.v.images.length;
        for (var i = 0; i < n; i++) {
            this.v.images[i].button = new Element('li').insert(new Element('div').setOpacity(0.9));
            this.v.images[i].buttonlink = new Element('a').update(parseInt(i + 1));
            $(this.v.images[i].button).insert(this.v.images[i].buttonlink);
            $(this.pageNumbers).insert(this.v.images[i].button);
        }
        $(this.pageNumbers).insert(this.rightArrow);
        $(this.pageNumbers).insert(new Element('div').addClassName('break'));
        $(this.v.images[this.currentShown].button).addClassName('on');
        this.container.insert(this.pageNumbers);
        $(this.rightArrow).observe('click', this.goToNextSlide.bind(this));
        $(this.leftArrow).observe('click', this.goToPrevSlide.bind(this));
        for (var i = 0; i < n; i++) {
            $(this.v.images[i].buttonlink).observe('click', this.goToSlideWrapper.bindAsEventListener(this, i));
        }
    },
    updateSlideNumbers: function () {
        var n = this.v.images.length;
        for (var i = 0; i < n; i++) {
            $(this.v.images[i].button).removeClassName('on');
        }
        $(this.v.images[this.currentShown].button).addClassName('on');
    },
    makeOverlay: function () {
        this.overlay = new Element('div').addClassName('overlay').setStyle({
            zIndex: 2
        }).setOpacity(0);
        this.overlayBackground = new Element('a').addClassName('overlaybackground');
        this.overlayTitle = new Element('a').addClassName('overlaytitle');
        this.overlayDescription = new Element('a').addClassName('overlaydescription');
        this.overlay.insert(this.overlayBackground).insert(this.overlayTitle).insert(this.overlayDescription);
        this.container.insert(this.overlay);
        this.showOverlay();
    },
    updateOverlay: function () {
        this.overlayBackground.writeAttribute('href', this.v.images[this.currentShown].link);
        this.overlayTitle.update(this.v.images[this.currentShown].heading).writeAttribute('href', this.v.images[this.currentShown].link);
        this.overlayDescription.update(this.v.images[this.currentShown].description).writeAttribute('href', this.v.images[this.currentShown].link);
    },
    showOverlay: function () {
        new Effect.Appear(this.overlay, {
            from: 0,
            to: 0.7
        });
    },
    hideOverlay: function () {
        new Effect.Fade(this.overlay, {
            from: 0.7,
            to: 0
        });
    },
    goToNextSlide: function () {
        this.goToSlide(parseInt(this.currentShown + 1));
    },
    goToPrevSlide: function () {
        this.goToSlide(parseInt(this.currentShown - 1));
    },
		goToRandomSlide: function () {
			  var next = Math.floor(Math.random() * (this.v.images.length + 1));
        this.goToSlide(parseInt(next));
		},
    startSlideShow: function () {
        this.running = true;
        //this.pe = new PeriodicalExecuter(this.goToNextSlide.bind(this), this.v.interval);
        this.pe = new PeriodicalExecuter(this.goToRandomSlide.bind(this), this.v.interval);
    },
    stopSlideShow: function () {
        this.pe.stop();
    },
    resetSlideShow: function () {
        this.stopSlideShow();
        this.startSlideShow();
    },
    setState: function (state) {
        this.working = state;
    },
    setStateFalse: function () {
        this.working = false;
    },
    getState: function () {
        return this.working;
    }
}
