/*
 * jQuery Barr-Foot
 *
 * "PIEDINO"
 *
 * Copyright 2011, Marco Poglie
 * THINKPLACE scripts.
 * http://www.thinkplace.it
 * http://www.poglie.com
 * 
 */

(function($) {

    var barr = function(element, options){
		
		//Defaults are below
		var settings = $.extend({}, $.fn.barrFoot.defaults, options);

        
        //Trigger the afterLoad callback
        settings.afterLoad.call(this);
		
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//															INIT-SETTING
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
	   
	   //  ------- gli oggetti ------
	   var _element = $(element); // l'oggetto selezionato
	   var testo;
	   var content;
	   var sx;
	   var dx;
	   var widget;
	   // --------[ PARAMETRI di Configurazione ]-------
	   var alt = settings.altezza;
	   var largSx = 0;
	   var largDx = 0;
		
		var figli = _element.children();
		var lunghezzaElement = figli.length;
		
		
		
		
		
		//
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//														EVENT functions
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//
		
		
		// --------------------------------------[ menu-SX e DX ]
		var onClickSxDx = function(event){
			
          
			
		};
		var onOverSxDx = function(event){
			$(this).animate({opacity:.5});
			
			
		};
		var onOutSxDx = function(event){
			$(this).animate({opacity:1});

			
		};
		
		
		
		
		
		
		
		
		
		
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//														 NIT 
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// 
		var init = function(setting){
			
			// coclo i figli di element
			figli.each(function() { 
				var figlio = $(this);
				var sottofigli = figlio.children();
				
				content = figlio; // <--- 
				
				// imposto l'altezza del contenitore al massimo ma lo nascondo dietro
				// poi quando lo alzo e abbasso gestisco solo la y..
				
				
				// Posizionamento iniziale degli oggetti, è tutto nascosto o fuori dalla scena 	
				//
				// ---[ div#footer ]---
				_element.css('bottom','-'+alt+'px');
				//
				//window.alert(figlio.attr('class'));
				if(figlio.attr('class') == "content"){
					
					figlio.children().each(function() {
						var sottofiglio = $(this);
						//
						//
						// ---[ p#indirizzo ]--------------------------------------------------------------------------------
						if(sottofiglio.is('div') && sottofiglio.attr('id') == "address"){
							sottofiglio.css({'position':'relative', 'bottom':'-'+alt-50+'px'});
							
							
							testo = sottofiglio; // <--- 
						}
						// ---[ ul#sx ]---------------------------------------------------------------------------------------
						if(sottofiglio.is('ul') && sottofiglio.attr('id') == "sx"){
							//sottofiglio.css({'position':'relative', 'bottom':'-'+alt+50+'px'});
							
							sx = sottofiglio; // <--- 
							
							sx.children().each(function(){
											   //_element.append($(this).width()+' - ');
											   largSx += $(this).outerWidth(true);
											   
												$(this).children('a').bind("click",onClickSxDx);
												$(this).children('a').hover(onOverSxDx, onOutSxDx);
											   
											    $(this).children('a').css({'position':'relative', 'bottom':'-'+alt+'px'});
											   });// fine del ciclo ul#sx
						}
						// ---[ ul#dx ]---------------------------------------------------------------------------------------
						if(sottofiglio.is('ul') && sottofiglio.attr('id') == "dx"){
							//sottofiglio.css({'position':'relative', 'bottom':'-'+alt+50+'px'});
							
							dx = sottofiglio; // <--- 
							
							dx.children().each(function(){
											   //_element.append($(this).width()+' - ');
											   largDx += $(this).outerWidth(true);
											   
												$(this).children('a').bind("click",onClickSxDx);
												$(this).children('a').hover(onOverSxDx, onOutSxDx);
											   
											   	$(this).children('a').css({'position':'relative', 'bottom':'-'+alt+'px'});
											   });// fine del ciclo ul#dx
						}
						// ---[ div#widget-foot]--------------------------------------------------------------------------------
						if(sottofiglio.is('div') && sottofiglio.attr('id') == "widget-foot"){
							sottofiglio.css({'position':'relative', 'bottom':'-'+alt-50+'px'});
							
							
							widget = sottofiglio; // <--- 
						}
						// -------------------------------------------------------------------------------------------------
					});
					
				
				}
				
				
			});
		}
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//															APRI - (crea)
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// 
		var apri = function(){
			
			// ------------------------[ START ]----------------------------
			// l'animazione iniziale quella che fa entrare la lama head
			_element.delay(settings.delay).animate({
				
				bottom: 0
				
			  }, settings.velocita, 'easeOutExpo', function() {
				//////////////////////////////////////////////////////////////
				// al completamento dell'animazione di entrata di _element...
				//////////////////////////////////////////////////////////////
				var count = 0;
				
				// ... controlla se esiste content (#content)
				if(content.attr('class') == "content"){
					var contentFigli = content.children();
					// ciclo solo i figli di content
					contentFigli.each(function() {
						var sottofiglio = $(this);
						
						//
						//
						
						// ---[ ul#sx ]---------------------------------------------------------------------------------------
						if(sottofiglio.is('ul') && sottofiglio.attr('id') == "sx"){
							//sottofiglio.delay(0).animate({bottom:0}, 500, 'easeOutExpo', function() {});
							
							sx.children().each(function(i){
														
											   		$(this).children('a').delay(200*i).animate({bottom:0}, 700, 'easeOutExpo', function() {
																																		 
																						 if(i==sx.children().length-1){
																							// window.alert(sx.children().length+'  '+i);
																							testo.delay(0).animate({bottom:0}, 700, 'easeOutExpo', function() {});	
																						 }
																						 
																																		 	});
											   });// fine del ciclo ul#sx
						}
						// ---[ p#indirizzo ]---------------------
						/*if(sottofiglio.is('div') && sottofiglio.attr('id') == "address"){
							sottofiglio.delay(1000).animate({bottom:0}, 1000, 'easeOutExpo', function() {});
						}*/
						// ---[ ul#dx ]---------------------------------------------------------------------------------------
						if(sottofiglio.is('ul') && sottofiglio.attr('id') == "dx"){
							//sottofiglio.delay(0).animate({bottom:0}, 1000, 'easeOutExpo', function() {});
							
							dx.children().each(function(i){
													
											   		$(this).children('a').delay(200*i).animate({bottom:0}, 700, 'easeOutExpo', function() {});
											   });// fine del ciclo ul#dx
						}
						// ---[ div#widget-foot]--------------------------------------------------------------------------------
						if(sottofiglio.is('div') && sottofiglio.attr('id') == "widget-foot"){
							sottofiglio.delay(0).animate({bottom:0}, 700, 'easeOutExpo', function() {});
						}
						// -------------------------------------------------------------------------------------------------
						
						count++;
					});
				 }
			  });
		}
        //
      
		
		
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//														  CHIUDI - CANCELLA
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
		
		
		
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//															  MOSTRA
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
		
		
		
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//															NASCONDI
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
		
		
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//															RESIZING
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
		
		
		
		
		
		
		
		
		/***********************************************************************************************************************/
		var vars = {
            
			
            
        };
		_element.data('vars',vars)
		/***********************************************************************************************************************/
		
		
		
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//															GOOOOOOO
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//
        init(settings);
		apri();
		
		
		return this;
    };
        
    $.fn.barrFoot = function(options) {
    
        return this.each(function(key, value){
            var element = $(this);
			
            // Rientro anticipato dell'attrubuto se questo elemento ha già un plugin istanza
            if (element.data('barrfoot')) return element.data('barrfoot');
            
			// COSTRUTTORE: -----------------------------
            var barrfoot = new barr(this, options);
			// ------------------------------------------
			
            // memorizzo il l'istanza in data
            element.data('barrfoot', barrfoot);
        });

	};
	
	//Default settings
	$.fn.barrFoot.defaults = {
		delay: 0,
		
		altezza: 20,
		
        afterLoad: function(){}
	};
	
	$.fn._reverse = [].reverse;
	
})(jQuery);
