if (!TT)
	var TT = {};

TT.Slideshow = Class.create({
    initialize: function(domId) {
        this.options = Object.extend({
            delay: 3,
			controls: "yes",
            slidesSelector: ".slide",
            startingIndex: 0,
            fadeTime: 1,
            nextClass: ".slideshow-thumb.next",
            previousClass: ".slideshow-thumb.previous"
        }, arguments[1] || {})

        this.slides = $(domId).select(this.options.slidesSelector);
        this.currentIndex = Math.abs(this.options.startingIndex); //0 based
        this.lastView = 1;
        this.zindex = 500;
        this.locked = false;
        this.timer = 0;
        this.delay = this.options.delay;  // in seconds
		this.controls = this.options.controls;  // in seconds
        this.slides[this.currentIndex].setStyle({
            zIndex: this.zindex,
            visibility: "visible",
            display: "block"
        })

		if(this.controls =="yes") {
        	$(domId).down(this.options.nextClass).observe("click", this.next.bind(this));
        	$(domId).down(this.options.previousClass).observe("click", this.previous.bind(this));
        }

        this.timedNext(this.delay);
	},
	
	slidejump: function(jumpto) {
		this.resetTimer();
		
		//var current_elements = document.getElementsByClassName("slideshow-thumb-img-container-" + this.lastView);  
	    //current_elements[0].style.display="none";
    	//document.getElementById("slideshow-item-"+this.lastView).className = "slideshow-headline-container";
		var newIndex = jumpto;
		$("slideshow-item-"+(this.lastView)).removeClassName("highlight");
        $("slideshow-item-"+ (newIndex)).addClassName("highlight");
		this.updateCounterLabel(newIndex);
		this.showSlide(newIndex-1);
		this.lastView = newIndex;
		this.timedNext(this.delay);
	},

    next: function() {
        this.resetTimer();
        
        var newIndex = this.currentIndex + 1 >= this.slides.length ? 0 : this.currentIndex + 1;
        $("slideshow-item-"+(this.currentIndex + 1)).removeClassName("highlight");
        $("slideshow-item-"+ (newIndex + 1)).addClassName("highlight");
        this.updateCounterLabel(newIndex+1);
        this.showSlide(newIndex);
        this.lastView = newIndex+1;
        this.timedNext(this.delay);
     },
     
    timedNext: function(delay) {
        this.timer = setTimeout(this.next.bind(this), (delay * 1000));  // delay converted to milliseconds
    },

    previous: function() {
        this.resetTimer();

        var newIndex = this.currentIndex - 1 < 0 ? this.slides.length - 1 : this.currentIndex - 1;
        
        $("slideshow-item-"+(newIndex+1)).addClassName("highlight");       
        this.updateCounterLabel(newIndex+1);
        $("slideshow-item-"+(this.currentIndex+1)).removeClassName("highlight");
        this.showSlide(newIndex);
        this.lastView = newIndex+1;
        this.timedNext(this.delay);
    },

    showSlide: function(index) {
		if (this.locked) 
            return;
            
        if (this.zindex == 1) {
            this.zindex = 5000;
            this.slides[this.currentIndex].style.zIndex = this.zindex;
        }

        this.slides[index].setStyle({
            zIndex: --this.zindex,
            visibility: "visible",
            display: "block",
            opacity: 1
        });
        this.locked = true;
        this.slides[this.currentIndex].style.zIndex = 0;
        this.slides[this.currentIndex].style.display = "none";
		this.locked = false;
        this.currentIndex = index;
    },

    resetTimer: function() {
        clearTimeout(this.timer);
        this.timer = 0;
    },

 	updateCounterLabel: function(current_count) {
		if (document.getElementById("slideshow-headline-count") != null) {
			var counter_label = document.getElementById("slideshow-headline-count");
			counter_label.innerHTML = current_count;
		}
	}

});