/*!
 * jQuery ILAS Navigation Plugin v.1.1.0 slideDownAll
 * http://www.argentovivo.tv/
 *
 * Copyright 2011, Valerio Menna
 * All right reserved to
 * Valerio Menna, ArgentoVivo.tv
 *
 * Date: Dec 13 14:00:00 2010
 
****************************************
			HOW TO USE
****************************************	

***** INCLUDE THE LIBRARY *****
<script type="text/javascript" src="../../testata_2012_3/js/jquery.js"></script>
<script type="text/javascript" src="../../testata_2012_3/js/jquery.easing.js"></script>
<script type="text/javascript" src="../../testata_2012_3/js/jqueryui.js"></script>
<script type="text/javascript" src="../../testata_2012/js/jquery.navILAS-0.2.js"></script>


***** INCLUDE THE STYLE *****
<link rel="stylesheet" href="../../testata_2012_3/js/navILAS.css" />

***** SET THE ELEMENT *****
  <!-- MENU PRINCIPALE -->
  <div id="navILAS">
    <ul>
        <!-- sezione del menù -->
        <li>
          <ul>
            <li></li>
            <li></li>
            <li></li>
          </ul>
        </li>
        <!-- fine sezione del menù --> 
        
		<!-- [CUT] -->
		
        <!-- sezione del menù -->
        <li>
          <ul>
            <li></li>
            <li></li>
            <li></li>
          </ul>
        </li>
        <!-- fine sezione del menù --> 
                
        <li class="clearFloat"> </li>
      </ul>
  </div>
 
***** ANIMATE THE ELEMENT *****
$('#navILAS').navILAS({
							//user's value
						});
 
 
****************************************
			CUSTOMIZE
****************************************	

 
 */
 
(function(jQuery) {
	//estendo jquery con un metodo per estrarre la massima altezza in un set di elementi (comprensiva di bordi margin e padding)
	jQuery.fn.maxHeight = function() {
  
		var max = 0;
	
		this.each(function() {
		  max = Math.max( max, parseInt(jQuery(this).outerHeight(true)) );
		});

		return max;
	  };
	  
	  
	// ========================================
	//			DEFAULT METHODS
	// ========================================	
	var delayTime;
	var methods = {
			// =========
			// FIRST DECLARATION
			init: function( options ){
						// ========================================
						//			    DEFAULT VALUE
						// ========================================	
						var config = {
										
										defaults: {
											//***** COLORE DEL PRIMO ELEMENTO DI OGNI MENU' *****//
											firstLIColor: '#FFF',
											//***** ALTEZZA MASSIMA TRA I MENU' INTERNI *****//
											heightmax: (this.find('ul ul').maxHeight()) + 'px',
											//***** ALTEZZA INIZIALE DEL CONTENITORE *****//
											thisheight: this.height() + 'px',
											//***** MARGINE INFERIORE DEI MENU' INTERNI *****//
											thismarginbottom: this.find('ul ul').css('margin-bottom'),
											//***** PADDING SUPERIORE DEI MENU' INTERNI *****//
											thispaddingtop: this.find('ul ul').css('padding-top'),
											
											ULwrapperHeight: '20px',
											
											SLIDER: this
										}
										
									};
						//-----save user value
						if ( options ) jQuery.extend(config.defaults, options);
						
						// PLUGIN CODE HERE
						
						// ========================================
						//         DEFINE BASIC VARS ELEM
						// ========================================
						
						this.navILAS('basicOps', config);	

						// ========================================
						//         DEFINE HOVER EFFECTS
						// ========================================

//						this.find('.navUL_UL_wrapper li:not(.first)').bind('mouseenter', function(){
						
						/*
						
						commentato perché utilizziamo il click sulla prima voce
						this.find('li').bind('mouseenter', function(e){
								jQuery(this).navILAS('show', config);
							});
						
						config.defaults.SLIDER.bind('mouseleave', function(){
								jQuery(this).navILAS('hide', config);
							});
						*/
						this.find('li.first').bind('click', function(e){	
							if(jQuery('#testata_2012_navILAS').hasClass('opened'))
							{						
								jQuery(this).navILAS('hide', config);
							}
							else
							{						
								jQuery(this).navILAS('show', config);
							}
							});
						
						
// ==== CHIUSURA MENU AL MOUSELEAVE ==== //						
//						config.defaults.SLIDER.bind('mouseleave', function(){
//								jQuery(this).navILAS('hide', config);
//							});
						
						
				},
			
			show: function(config){
					var maxH = config.defaults.SLIDER.find('.navUL_UL_wrapper ul').maxHeight();
					//imposto il delay
					config.defaults.SLIDER.addClass('running');
					delayTime = window.setTimeout(function(){
						if(!config.defaults.SLIDER.hasClass('opened') && config.defaults.SLIDER.hasClass('running')){
							config.defaults.SLIDER.addClass('opened').css("overflow", "visible") //overflow: visible
								.children('ul').add('.navUL_UL_mask').stop().animate({
										'height': maxH
									}, 400);
						}
					}, 0); //ritardo del menù a comparire
					
					jQuery(this).navILAS('highlight', config);
					
				},
			
			hide: function(config){
					var minH = config.defaults.ULwrapperHeight;
					//cancello il delay
					config.defaults.SLIDER.removeClass('running')
						.find('.ul_active').removeClass('ul_active');
					config.defaults.SLIDER.find('.navUL_UL_mask').unbind( 'mouseenter').unbind( 'mouseleave');
					window.clearTimeout(delayTime);
					if(config.defaults.SLIDER.hasClass('opened')){
						config.defaults.SLIDER.removeClass('opened').children('ul').add('.navUL_UL_mask').stop().animate({
									'height': minH
								}, 400);
						config.defaults.SLIDER.css("overflow", "hidden");
					}
				},
				
			basicOps: function(config){
					//========================================
					//	  ELEMENT SETTINGS & WRAP CREATION
					//========================================
					jQuery('.menuWrapper').css('height', '');
					this.children('ul').css('height', config.defaults.ULwrapperHeight);

					var navUL = this.children('ul');
					//seleziono tutti gli ul figli
					var navUL_UL = this.find('li ul');
					//e li wrappo in un div di classe wrapper
					navUL_UL.wrap('<div class="navUL_UL_mask"><div class="navUL_UL_wrapper"></div></div>');

					//========================================
					//			SET THE EXTRA CLASSES
					//========================================
					//ciclo tra tutti gli li e seleziono solo i first/last child > aggiungo la classe first / last
					jQuery('.navUL_UL_wrapper').children('ul').find('li:first-child').addClass('first');
					jQuery('.navUL_UL_wrapper').children('ul').find('li:last-child').addClass('last');
				},
			
			highlight: function(config){
					
					//alert(jQuery(this).html());
					this.find('.ul_active').removeClass('ul_active');
					jQuery(this).parents('.navUL_UL_mask').addClass('ul_active');
					
					if(config.defaults.SLIDER.hasClass('opened') || config.defaults.SLIDER.hasClass('running')){
						config.defaults.SLIDER.find('.navUL_UL_mask').bind( 'mouseenter',
								function(){
									//alert('x');
									if(!jQuery(this).is('.ul_active')){
										//alert(1);
										jQuery('#testata_2012_navILAS').find('.ul_active').removeClass('ul_active');
										jQuery(this).addClass('ul_active');
									}
								}).bind('mouseleave', 
								function(){
									
								}
							);
					}
					//alert(0);
				}
				
		}
	  

	  	
	
	// ========================================
	//			BUILD THE PLUGIN
	// ========================================	
	jQuery.fn.navILAS = function( method ) { 
	   
		
		// Method calling logic
		if ( methods[method] ) {
		  return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
		  return methods.init.apply( this, arguments );
		} else {
		  $.error( 'Method ' +  method + ' does not exist on jQuery.navILAS' );
		}
		alert('base');    
		
	    // ========================================
		//			 MANTAIN THE CHAIN
		// ========================================	
			
		return this;
    }
	
	
})(jQuery);	
