var TOGallery = new Class({
    Implements: [Options, Events],
    options: {
            galleryEl: null,
            slideShowDelay: 3000,
			slideShowFadeDuration: 500
    },
	initialize: function(options) {	    
	    this.setOptions(options);		
	    
	    this.currentIter = 0;                           
        this.slideShowTimer = 0;
		
		this.galleryFrames = this.options.galleryEl.getElementById('gallery').getChildren('.frame');
		
		this.galleryFrames.each(function(el){el.setStyle('display', 'block');});
		
		this.galleryInfoWindows = this.options.galleryEl.getElementById('gallery').getChildren('.info-window');
		
		this.carouselElements = this.options.galleryEl.getElementById('carousel').getChildren();	
		
		if (this.carouselElements.length>1){		
			for(i=0;i<this.galleryFrames.length;i++)
	        {
	            this.galleryFrames[i].addEvents({
	                    'mouseover': function () {                          
	                                this.pauseSlideShow();
	                            }.bind(this),
	                    'mouseout': function () {
	                                this.playSlideShow()
	                            }.bind(this)                    
	                });  
	        }               
	        for(i=0;i<this.carouselElements.length;i++)
	        {
	            this.carouselElements[i].addEvents({
		            'mouseover': function (num) {                 
		                        this.skipToItem(num);
		                    }.bind(this,i),
                    'mouseout': function () {
                                this.playSlideShow()
                            }.bind(this)       
		        });  
	        }	        
		    this.playSlideShow();  
        }            
	},
	skipToItem: function(num) {
	    this.pauseSlideShow();
	    for(i=0;i<this.galleryFrames.length;i++)
        {
            this.galleryFrames[i].set({opacity: 0});
        }
        this.galleryInfoWindows[this.currentIter].setStyle('display', 'none');
        this.galleryFrames[num].set({opacity: 1});	
        this.galleryInfoWindows[num].setStyle('display', 'block');        
		this.setCurrentIter(num);	
	},
	setCurrentIter: function(iter) {
	   this.currentIter = iter;
	   this.carouselElements.each(function(el){el.removeClass('selected')});
	   this.carouselElements[iter].addClass('selected');
	},	
	nextIteration: function(currentIter) {
	   var nextIter = currentIter+1;
       if (nextIter >= this.galleryFrames.length) nextIter = 0;
       return nextIter;	   
	},
	prevIteration: function(currentIter) {
       var prevIter = currentIter-1;
       if (prevIter < 0) prevIter = this.galleryFrames.length-1;
       return prevIter;    
	},
	pauseSlideShow: function() {
	    $clear(this.slideShowTimer);    
    },
    playSlideShow: function() {
        $clear(this.slideShowTimer);  
        this.slideShowTimer = this.fadeToNextItem.periodical(this.options.slideShowDelay, this);              
    },	
    fadeToNextItem: function() {
		for(i=0;i<this.galleryFrames.length;i++)
		{
			if ((i != this.currentIter)) this.galleryFrames[i].set({opacity: 0});
		}
        var oldFrame = this.galleryFrames[this.currentIter];
        var oldInfoWindow = this.galleryInfoWindows[this.currentIter];
        var newFrame = this.galleryFrames[this.nextIteration(this.currentIter)];
        var newInfoWindow = this.galleryInfoWindows[this.nextIteration(this.currentIter)];
        this.setCurrentIter(this.nextIteration(this.currentIter));
		
		var newFx = new Fx.Morph(newFrame, { 'duration': this.options.slideShowFadeDuration, 'transition': Fx.Transitions.linear });
		var oldFx = new Fx.Morph(oldFrame, { 'duration': this.options.slideShowFadeDuration, 'transition': Fx.Transitions.linear });
		
        oldInfoWindow.setStyle('display', 'none');
        oldFx.start({opacity: 0});        
        
		newFx.start({opacity: 1});		
        newInfoWindow.setStyle('display', 'block');
    }
});

if(typeof(startGalleries)!="undefined")startGalleries();