var $dom = YAHOO.util.Dom;
var $ = $dom.get;
var $class = $dom.getElementsByClassName;

var slideshows_auto = {
    init: function() {
	    this.slideshows = [];                                                                                                                                     
	    var containers = $class('slideshow-container', 'div');
	    for(var i=0, item; item=containers[i]; i++) {
	        this.slideshows[i] = new Slideshow(item);
	        this.slideshows[i].run_show(true);
	    }
    }
};


Slideshow = function(slideshow_container, speed, pause) {
    if (slideshow_container) {
        this.init(slideshow_container, speed, pause); 
    }
};

Slideshow.prototype = {  
    init: function(slideshow_container, speed, pause) {
        this.container = slideshow_container;
        this.speed = speed || 2;
        this.pause = pause || 5;
        this.pause = this.pause * 1000 // Converting it to milliseconds from seconds
        this.current_slide = 0;
        this.slide_count = 0;
        
        this.stop_show = false;
        this.onSlideLoad = new YAHOO.util.CustomEvent("onSlideLoad");
        this.onSlideUnLoad = new YAHOO.util.CustomEvent("onSlideUnLoad");  
 
        this.slides = []
        var dom_slide_elements = $class('slideshow-slide', '',this.container);
        for(var i=0, slide; slide=dom_slide_elements[i]; i++) {
	        this.slides[i] = slide;
	        if (i != this.current_slide) {
	           $dom.setStyle(this.slides[i], 'opacity', 0);
	           $dom.setStyle(this.slides[i], 'visibility', 'visible');
	        } else {
	            $dom.setStyle(this.slides[i], 'opacity', 1);
	            $dom.setStyle(this.slides[i], 'visibility', 'visible');
	        }
	        ;
	    }
	    this.slide_count = this.slides.length;
    },

    run_show: function(pause_first) {
        if (!this.stop_show) {       
            if (!pause_first) {
                this.show_slide();       
            }
            var thisObj = this;
            setTimeout(function(){ thisObj.run_show() }, this.pause);
        }
    },
  
    show_slide: function(slide_number) {
        if (slide_number==null) {
            slide_number = this.next_slide();
        }
        if (slide_number >= 0 && slide_number <= this.slide_count && slide_number != this.current_slide) {
            var animFadeOut = new YAHOO.util.Anim(this.slides[this.current_slide], { opacity: {from: 1, to: 0 } }, (this.speed/2), YAHOO.util.Easing.easeNone);
            var animFadeIn = new YAHOO.util.Anim(this.slides[slide_number], { opacity: {from: 0,  to: 1 } }, this.speed, YAHOO.util.Easing.easeOut);
            this.onSlideLoad.fire({slide: this.slides[slide_number], slide_number: slide_number});
            animFadeOut.animate();
            animFadeIn.animate();
            this.onSlideUnLoad.fire({slide: this.slides[this.current_slide], slide_number: this.current_slide});
            this.current_slide = slide_number;
        }
    },

    show_stop: function() {
        this.stop_show = true;
    },
    
    show_slide_then_stop: function(slide_number) {
        this.show_stop();
        this.show_slide(slide_number);   
    },    
    
    next_slide: function() {
        if (this.current_slide == (this.slide_count - 1)) {
            return 0;
        } else {
            return this.current_slide + 1;
        }   
    }
    
};


TdShows = function(type, args) {
    var textShow = new Slideshow($('thought-text'));
    markerShow = new Slideshow($('thought-marker'));

    textShow.onSlideLoad.subscribe(slide_change);
    textShow.run_show(true);
}

slide_change = function(type, args) {         
    markerShow.show_slide();
} 


YAHOO.util.Event.addListener(window, 'load', TdShows);



