/*
	Class:    	Servizi Tab
	Author:   	Marco Dell'Anna
	Website:    #
	Version:  	1.0
	Date:     	07/09/2010
	Built For:  MooTools 1.2
*/

var Tabs = new Class({
	active:"",
	activeId:"",
	Implements: Options,
    options: { 
			itemleave:"",
			deafultpanel:""
	  },
	//Function run when class is created
	initialize : function(a, options){
		this.setOptions(options);
		this.tab    = $$('#' + a + " a.tab") 
		this.panels = Array();
		this.tab.each(function(el,indice){
			// Aggiunge link id Name
			this.panels.push( $( el.getAttribute('rel',2).substr(1) ) );
			
			el.addEvent('mouseover', function(e){
				e.stop()
				//Attiva pannello
				this.setActivePanels( indice );
				$(this.options.deafultpanel).fade("out");
			}.bind( this ));
			
		}.bind( this ))
		
		// nasconde Pannelli
		this.hideAllPanels();
		this.hideAllTabs();
	},
	
	setActivePanels: function( id ){
		// Imposta elemento attivo
		this.activeId = id;
		// Verifica init
		if( this.activeId !== ""){
			this.setItemLeave();
		}
		
		this.hideAllPanels();
		this.hideAllTabs();
		// Set Attivo
		this.tab[id].addClass("active-tab");
		this.panels[id].setStyle("display","block").set('opacity',0).fade("in");
	},	

	hideAllPanels: function(){
		$each( this.panels, function(e, i){
			e.setStyle("display","none");
			e.hide();
		});
	},
	
	hideAllTabs: function(){
		$each( this.tab, function(e, i){
			e.removeClass("active-tab");
		});
	},
	
	automaticDelete:function(){
		$(this.options.deafultpanel).fade("in")
		this.panels[this.activeId].fade("out");//.setStyle("display","none");
		this.tab[this.activeId].removeClass("active-tab");
	},
	
	setItemLeave: function(){
		$( this.options.itemleave ).addEvent("mouseleave", function(){
			this.automaticDelete();
		}.bind( this ));
		/*$( this.options.itemleave ).addEvent("mouseleave", function(){
				this.automaticDelete = (function(){
					if( typeof(this.active) == "object" ){
						$(this.options.deafultpanel).setStyle("display","block");
						var myFx = new Fx.Tween(this.active, {property: 'opacity'});
								myFx.start(1,0).chain(
								    function(){
											this.active.setStyle("display","none");
											this.active   = "";
										//	this.activeId = "";
								}.bind( this ));
								// Rimuove Stile
								myEffect1 = new Fx.Morph( $(this.anchors[this.activeId]), {duration: 100, transition: Fx.Transitions.Sine.easeOut });
								myEffect1.start( {"opacity":0} );

								(function(){ 
									$(this.anchors[this.activeId]).removeClass("activetab").set("opacity", 1);
						//			myEffect1.start( {"opacity":1} );
								}.bind(this)).delay(100);
					}
				}.bind( this )).delay(2000)
		}.bind( this ))*/
	/*	$( this.options.itemleave ).addEvent("mouseenter", function(){
			$clear(this.automaticDelete);
		}.bind( this ));
		$( this.options.itemleave ).addEvent("mouseleave", function(){
			$clear(this.automaticDelete);
		}.bind( this ))*/
		
	}
});
