/*
  BarackSlideshow 0.1
    - Libraries required: MorphList <http://devthought.com>
    - MooTools version required: 1.2
    - MooTools components required: 
        Core: (inherited from MorphList)
        More: Assets
  
    Changelog:
    - 0.1: First release
*/
/*! Copyright: Guillermo Rauch <http://devthought.com/> - Distributed under MIT - Keep this message! */

var BarackSlideshow = new Class({
	
	Implements: [Events, Options],

	initialize: function(menu, items, options){

		var that = this;
		this.menu = $(menu);
		this.menuitems = this.menu.getChildren();
		this.menuitems.addEvents({	
			'mouseover': function(ev){ 
				that.click(ev, this); 				
			},
			'mouseleave': function(ev) { 			    
				that.timer = that.next.periodical(that.interval, that, [false]); 
			}
		}); 

		this.curIndex = 0;
		this.items = $(items);
		this.itemsitems = this.items.getChildren();				

		this.interval = 10000;

		this.timer = this.next.periodical(this.interval, that, [false]);
		
		this.items.addEvents({
		    'mouseover': function(ev){
		        that.click(ev, this); 		        
		    },
		    'mouseleave': function(ev){		        
		        that.timer = that.next.periodical(that.interval, that, [false]); 
		    }
		});
	},
	
	next : function(){	    		    	    
		this.show(this.curIndex);
	},
			
	click: function(ev, item){
		new Event(ev).stop();		
		    
		this.curIndex = this.menuitems.indexOf(item);
		
		$clear(this.timer);
		
		this.show(this.menuitems.indexOf(item));
	},
  
  	show: function(index){     	  	
		var item = this.itemsitems[index];    

		if(item == this.curitem) return;

		item.dispose().fade('hide').inject(this.curitem || this.items.getFirst(), this.curitem ? 'after' : 'before').fade('in');
		item.setStyle('display', 'block');
        
		this.menuitems.each(function(item, index){
	        item.removeClass('active-' + index);
        });
        
        this.menuitems[index].addClass('active-' + index);
        
        this.curIndex++;
        if (this.curIndex == this.itemsitems.length)
        {
	        this.curIndex = 0;
        }
		
		return this;
  	}
});