var multiPanelRotator = new Class({
	
	//implements
	Implements: [Options],

	//options
	options: {
		panelContainer: '',
                panelDescriptionContainer: '',
                startPanel:1,
                panelDuration:5,
                panelTransitionDuration:500,
                activators:'someCont'
	},
	
	//initialization
	initialize: function(options) {
            
		//set options
		this.setOptions(options);
                
                // get containers
                this.panelContainer = this.options.panelContainer;
		if (this.options.panelDescriptionContainer){
			this.panelDescriptionContainer = this.options.panelDescriptionContainer;	
		}

                // get children of containers
                this.panels = $(this.panelContainer).getChildren();
		if (this.options.panelDescriptionContainer){
			this.panelDescriptions = $(this.panelDescriptionContainer).getChildren();
			this.FxDesc = [];
		}
                this.FxPanel = [];
                this.activators = [];
                
                // activators
                this.activators = $(this.options.activators).getChildren();
                
                // duration, start panel, etc
                this.startPanel = this.options.startPanel-1;
                this.panelDuration = this.options.panelDuration;
                
                // set transition for slides
                $each(this.panels, function(el,i){
                    this.FxPanel[i] = new Fx.Morph(el,{duration:'long'});
                }.bind(this));
                
                // set transition for desc
		if (this.options.panelDescriptionContainer) {
			$each(this.panelDescriptions, function(el,i){
			    this.FxDesc[i] = new Fx.Morph(el,{duration:'long'});
			}.bind(this));
		}
                // start rotation
                this.curPanel = Number(this.startPanel)-1;
                
                // start timer
                this.rotate();
                this.timer = this.rotate.periodical(this.panelDuration*1000,this);
                
                
                
                // bind click to activators
                $each(this.activators, function(el,i){
                    el.addEvent('click', function(el){
                        
                        // clear auto rotation
                        this.clearTimer();
                        
                        // set current panel value to clicked item
                        this.curPanel = this.activators.indexOf(el.target)-1;
                        this.rotate();
                    }.bind(this));
                }.bind(this));

	},
        
        clearTimer: function(){
            $clear(this.timer);
        },
        rotate: function(){
            this.curPanel++;
            
            // if at end, start over, reset stacking order
            if (this.curPanel==this.panels.length){
                this.curPanel = 0;
            }
            
            // set stacking order of active panel, and active class
            $each(this.panels, function (el,i){
                
                // set stacking order of panels and fade in
                 if (i==this.curPanel){
                    //
                    $(el).setStyle('z-index',10);
                    this.FxPanel[this.curPanel].start({'opacity':[0,1]});
		    if (this.options.panelDescriptionContainer){
			this.FxDesc[this.curPanel].start({'opacity':[0,1]});
		    }
                }
                 else {
                    this.FxPanel[i].start({'opacity':[0,0]});
		    if (this.options.panelDescriptionContainer){
			this.FxDesc[i].start({'opacity':[0,0]});
		    }
                }
                
                
                // set active class on activator
                $each(this.activators,function(el,i){
                    if (i==this.curPanel){
                        $(el).addClass('rotContActOn');
                    }
                    else {
                        $(el).removeClass('rotContActOn');    
                    }
                }.bind(this));
                
            }.bind(this));
        }
});
