/*
 Moogets - MorphList 0.5 (formerly SlideList, aka Fancy Menu)
	- MooTools version required: 1.23
	- MooTools components required: 
		Core: Fx.Tween, Fx.Morph, Selectors, Element.Event and dependencies
		More: -

	Changelog:
		- 0.1: First release
		- 0.2: MooTools 1.2 compatible
		- 0.3: Now alters morphs width/height/top properties by default
		- 0.4: Morphing code now part of the function and not of the event.
		- 0.5: setOpacity changed to fade('show') so that visibility css is altered.
*/

/* Copyright: Guillermo Rauch <http://devthought.com/> - Distributed under MIT - Keep this message! */

var MorphList = new Class({   
	
	Implements: [Events, Options],
	
	options: {
	/*             
		onClick: $empty,
		onMorph: $empty,*/
		morph: { 'link': 'cancel' }
	},
	
	initialize: function(menu, options) {
		var that = this;
		this.setOptions(options);
		this.content =$('contenidos');
		this.menu = $(menu);
		this.menuitems = this.menu.getChildren();
		this.contentitems = this.content.getChildren();
		this.menuitems.addEvents({
			
			mouseover : function(){
                       if(!this.getProperty('title_js')){
                            this.set('title_js',this.get('title'));
                            this.set('title','');
			                
			           //alert(this.getProperty('title_js'))
			           }
			          },

            mousedown: function(){ 
            if(!this.getProperty('title_js')){
                            this.set('title_js',this.get('title'));
			                this.set('title','');
			           //alert(this.getProperty('title_js'))
			           }
			 
            if(this.getProperty('title_js')){
            that.morphTo(this);}
            return false;	
            }		
		});  
		 
		this.bg = new Element('li', {'class': 'background'});
		
		this.bg.inject(this.menu).set('morph', this.options.morph);
		this.setCurrent(this.menu.getElement('.current'));

	},          
    
    mousedown: function(ev, item) {

		this.setCurrent(item, true);
		return false;
		//this.fireEvent('onClick', [ev, item]);
	},
	
	setCurrent: function(el, effect,l){  
		if(el && ! this.current) {
			this.bg.set('styles', { left: el.offsetLeft ,width: el.offsetWidth });
			(effect) ? this.bg.fade('hide').fade('in') : this.bg.fade('show');
			
			var elemento=el.id.split("_")[1];
			$("contenidos").set('styles', { marginLeft : -el.get('title_js')  });
			(effect) ? $(elemento).fade('hide').fade('in') : $(elemento).fade('show');

			

		}
		
		if(this.current) this.current.removeClass('current');
		if(el) this.current = el.addClass('current');
		
	},         
         
	morphTo: function(to) {
		if(! this.current) return; 
		this.bg.morph({
			left: to.offsetLeft,
			width: to.offsetWidth
		});


		    $("contenidos").morph({
			    marginLeft : -to.get('title_js')
			});

        if(this.current) this.current.removeClass('current');
		if(to) this.current = to.addClass('current');
		
$clear(mov);
$clear(siz);
$('bar').setStyle('width',"0px");
mov=move.periodical(12000);
siz=size.periodical(120);
		
        
		this.fireEvent('onMorph', to);
	}

});
var mov="";
var siz="";
window.addEvent('domready', function() {
new MorphList($('menu_inicio').getElement('ul'), {transition: Fx.Transitions.backOut, duration: 600,onClick: function(ev, item) { 
ev.stop(); 

}
});
$('bar').setStyle('width',"0px");
mov=move.periodical(12000);
siz=size.periodical(120);
})
function size(){
    if ($('bar').getStyle('width')=="100px"){
        $('bar').setStyle('width',"0px");
    }
    $('bar').setStyle('width',$('bar').getStyle('width').toInt()+1);
}
function move(){
    var actual="";
    $('menu_inicio').getChildren()[0].getChildren().each(function(el,i){
        if(el.hasClass('current')==true){
            actual=i; 
        }
    })
    $('menu_inicio').getChildren()[0].getChildren().each(function(el,i){
        if(actual==4&&i==0||actual+1==i){
            el.fireEvent('mousedown');
        }
       
    })

}

