/**
	Automatically fade in and fade out a list of items (updating the navigation bar).
*/
var PoiseFader = Class.create({

    initialize: function(items, itemsNav, duration) {	
			this.duration = duration || 5; 
			this.items = items;
			this.container =	$(items.first().parentNode.id);		
			
			if($(items)) {
			  nav_id = 'nav-'+this.container.id;
			  nav_html = "<div id='"+nav_id+"'><ul>";
        for(var i = 1; i < this.items.length+1; i++) {
          if(i==1) { liclass=" class='current'"; } else { liclass=''; }
          nav_html+="<li"+liclass+"><a href='#' id='option="+i+"' rel='rotator-"+i+"' class='option' onclick='return(false)'>Item "+i+"</a></li>";
        }
        nav_html += "</ul></div>";	  
        
        $(this.container).insert({ after: nav_html });
        
        this.navigation = $$('#'+nav_id+' li'); 
			}
			

			if (this.items) {
				this.nodes = $A([]);
				this.currentIndex = 0;
				this.setupFader();		
				this.navigation.invoke('observe', 'click', this.showItem.bind(this));
				this.start();						
			}
		},

    setupFader: function() {     
			this.items.each(function(child) {
				this.nodes.push(child);
				child.hide();
			}.bind(this));
			this.items.first().show();
			this.nodes_length = this.nodes.length;
    },

		stop: function () { clearTimeout(this.timer); },

		start: function () { this.periodicallyUpdate(); },

		periodicallyUpdate: function () {
			if (this.timer != null) {
				clearTimeout(this.timer);
				this.next();
			}
			this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.duration * 1000);
		},

    next: function() {
			var nextIndex = (this.items.length - 1 == this.currentIndex) ? 0 : this.currentIndex + 1;      
			// hide all the items
			this.items.each(function(child){ child.hide() });
			// display the next item
			Effect.Appear(this.items[nextIndex]);							
			this.updateNavigation(nextIndex);
			this.currentIndex = nextIndex;
    },

		showItem: function(event) {
			this.stop();			
			var element = event.findElement('a');
			var itemToShow = this.items.find(function(item){ return item.id == element.rel })
			// hide all the items
			this.items.each(function(child){ child.hide() });
			Effect.Appear(itemToShow);
			// update navigation
			this.navigation.each(function(nav){ nav.removeClassName('current') });
			element.up().addClassName('current');
		},
            
    updateNavigation: function(nextIndex) {
			var current_li = this.navigation[this.currentIndex];
			current_li.removeClassName('current');
			var next_li = this.navigation[nextIndex];
			next_li.addClassName('current');
    }

});
