var j = jQuery.noConflict();

jQuery(document).ready(function(){
	
	
	jQuery('.testata_2012_header .extra a').hover(function(){
		jQuery(this).stop().animate({'opacity': 0.7}, 300);
	}, function(){
		jQuery(this).stop().animate({'opacity': 1}, 300);
	});
	
	jQuery('#testata_2012_navILAS').navILAS();
	jQuery('#testata_2012_topSlider').ILASlider({
		auto_slide_seconds: 4500,
		allow_navigation : false,
		allow_arrow_controls : true,
		arrow_controls_visible : true
	});
		
		
	// =====================
	// 
	// =====================
	
	jQuery('#testata_2012_topSlider img').click(function(e){
		e.preventDefault();
		jQuery(this).siblings('a.captionLink').click();
	});
	
	// =====================
		
	//HOVER TOPLINKS
/*	jQuery('.testata_2012_topLinks a').hover(function(){
			jQuery(this).stop().animate({'color': '#D20000'}, 200);
		}, function(){
			jQuery(this).stop().animate({'color': '#F2F2F2'}, 1000);
		});
*/
	_createRollOver('.testata_2012_topLinks a');	
		
	// ===========================
	//	PULSANTI SOTTOMENU
	// ===========================
	
	jQuery('.testata_2012_topLinks_submenu a.hover').remove();
	jQuery('.testata_2012_topLinks .testata_2012_topLinks_link.withsubmenu a').unbind('click').bind('click', function(event) {
		
		if(jQuery(this).parent().find('.testata_2012_topLinks_submenu').hasClass('opened')) {
			jQuery(this).parent().find('.testata_2012_topLinks_submenu').slideUp().removeClass('opened');
		} else {
			jQuery('.testata_2012_topLinks_submenu.opened').slideUp().removeClass('opened');
			jQuery(this).parent().find('.testata_2012_topLinks_submenu').slideDown().addClass('opened');
		}
	});
	// ===========================
	
	//SCHEDE TOPLINKS
	jQuery('#wrapperSchede').css('height', 0).children('.schedaAccessoria').css({'top': '0px', 'bottom': ''}).slideUp(0);
	jQuery('.testata_2012_topLinks a[href*="scheda_"]').bind('click', function(e){
		e.preventDefault();
		var idScheda = jQuery(this).attr('href').substring(8);
		if(jQuery('#'+idScheda).is(':hidden')){
			jQuery('#wrapperSchede .schedaAccessoria:visible').css({'top': '', 'bottom': '0px'}).slideUp(1000);
			if(parseInt(jQuery('#wrapperSchede').css('height')) == 280){
				jQuery('#'+idScheda).css('z-index', 1000).slideDown(1000, 'easeOutQuad', function(){ jQuery(this).css('z-index', 750)});
			}
			else {
				jQuery('#'+idScheda).parent('#wrapperSchede').stop().animate({'height': '280px', 'top': '0px'}, 0, function(){
						jQuery('#'+idScheda).css('z-index', 1000).slideDown(1000, 'easeOutQuad', function(){ jQuery(this).css('z-index', 750)});
					});
			}
		}
		
	});
	jQuery('#wrapperSchede a.chiudiScheda').bind('click', function(e){
		e.preventDefault();
		jQuery(this).parents('.schedaAccessoria').slideUp(1000, function(){
				jQuery('#wrapperSchede').css('height', 0).children('.schedaAccessoria').css({'top': '0px', 'bottom': ''}).slideUp(0);
			});
	})
	//HOVER SLIDER
	//genero gli hover
	jQuery('#testata_2012_topSlider .newsText a.gotoArticle').wrap('<div class="gotoArticle"></div>');
	_createRollOver('#testata_2012_topSlider .newsText a.gotoArticle');	
	
	
	//hover btn tonews|toimg
	jQuery('#testata_2012_topSlider .captionLink, #testata_2012_topSlider .newsTextLink').css({'opacity': 0.6}).hover(function(){
			jQuery(this).stop().animate({'opacity': 1}, 400);
		}, function(){
			jQuery(this).stop().animate({'opacity': 0.6}, 600);
		});
	// HOVER MENU MID
	//genero gli A.hover
	jQuery('#testata_2012_navILAS ul ul li').append('<a></a>')
		jQuery('#testata_2012_navILAS ul ul li a+a').addClass('hover ui-corner-all').each(
			function(){
				jQuery(this).attr('href', jQuery(this).siblings('a').attr('href')).text(jQuery(this).siblings('a').text());	
			}
		).animate({'opacity': 0}, 0);
	//hover function
	jQuery('#testata_2012_navILAS ul ul li').hover(function(){
			jQuery(this).children('.hover').stop().animate({
					'opacity': 1
				}, 200);
		}, function(){
			jQuery(this).children('.hover').stop().animate({
					'opacity': 0
				}, 1000);
		});

	// SCHEDE A SCOMPARSA - HOVER BOTTONI
	jQuery('.schedaBtns a').stop().animate({'opacity': 0.5}, 0)
		.hover(function(){
				jQuery(this).stop().animate({'opacity': 1}, 600)
			}, function(){
				jQuery(this).stop().animate({'opacity': 0.5}, 600)
			});	
	
	// HOVER SCHEDE A SCOMPARSA
	jQuery('#corsi dl dt:not(.title)').hover(function(){
			jQuery(this).stop().animate({'background-color': '#283033'}, 300);
		}, function(){
			jQuery(this).stop().animate({'background-color': '#191c1e'}, 300);
		});
	
	/* ============================== *\
		   SCROLL DELLA PAGINA
	\* ============================== */
// LOG CONSOLE
//jQuery('<div id="consoleLog" style="height: 80px; padding: 10px; width: 400px; position: fixed; bottom: 0; left: 0; z-index: 1000; background: #333; color: #D20;">log</div>').appendTo('body');	
	// =======================
	// V A R I A B I L I
	
	
	var isClosed = false;
	var isClosed2 = false;
	var isGallery = (jQuery('#testata_2012_topSlider').size()>0) ? false : true;
	var altezzaHeader = parseInt(jQuery('.headerWrapper').outerHeight());
	
	/*
	 aggiunta l'altezza della fascia rossa
	*/
	altezzaHeader += parseInt(jQuery('.fasciarossa').outerHeight());
	/*
	
	*/
	var titleWrapperHeight = parseInt(jQuery('.titleWrapper:first').outerHeight());
	var altezzaMenuTop = (jQuery('.topLinksWrapper').size()>0) ? parseInt(jQuery('.topLinksWrapper').height()) : 0;
	var altezzaSlider = (!isGallery && jQuery('.topSliderWrapper').size()>0) ? parseInt(parseInt(jQuery('#testata_2012_topSlider').height()) + parseInt(jQuery('.topSliderWrapper:not(".clone")').css('padding-top')) ) : parseInt(jQuery('.topSliderWrapper:not(".clone")').outerHeight());
	var altezzaMenuWrapper = (jQuery('.menuWrapper').size()>0) ? parseInt(jQuery('.menuWrapper').outerHeight()) : 0;
	
	var position2 = (altezzaHeader + altezzaSlider);
	var initialPosition = jQuery(document).scrollTop();
	
	jQuery(window).bind('scroll', function(){
		/* */
			jQuery('.testata_2012_topLinks_submenu.opened').slideUp(0).removeClass('opened');
		/* */
		var delta = initialPosition - jQuery(document).scrollTop();
// LOG CONSOLE
//jQuery('#consoleLog').html(
//	"top:" + jQuery(document).scrollTop() + 
//	"; delta: " + delta + 
//	"; initialPosition: " + initialPosition + 
//	"; finalPosition: " + jQuery(document).scrollTop() + 
//	";<br /> pos2_1: " + (altezzaHeader)  + 
//	"; pos2_2: " + (altezzaMenuTop)  + 
//	"; pos2_3: " + (altezzaSlider) +
//	";<br /> pos2: " + position2 +
//	";<br /> isClosed: " + isClosed.toString()  +
//	";<br /> position barra: " + ((jQuery('.barra_ricerca').size() >0) ? (jQuery('.barra_ricerca').position().top) : "n.d." ) +
//	";<br /> offset barra: " + ((jQuery('.barra_ricerca').size() >0) ? (jQuery('.barra_ricerca').offset().top) : "n.d." )
//);
		initialPosition = jQuery(document).scrollTop(); 
		//IDENTIFICO IL SENSO DELLO SCROLL
		if(delta<0){
		//--- CASO SE SCROLL VERSO BASSO
			
// LOG CONSOLE
//jQuery('#consoleLog').html(jQuery('#consoleLog').html() + "<br>giu");

			if(!isGallery){ // CASO IN CUI NON SIAMO IN UNA PAGINA DELLA GALLERY

				//BLOCCO IL MENU SUPERIORE
				if(initialPosition >= altezzaHeader && jQuery('.topLinksWrapper.clone').size() <= 0 ){
					jQuery('.topLinksWrapper').clone().appendTo('body').addClass('clone').css({
							'position': 'fixed',
							'top': 0,
							'left': 0,
							'z-index' : 1200
						}).children('#testata_2012_topSlider').removeAttr('id').addClass('testata_2012_topSlider');
						
					if((initialPosition >= position2) && isClosed == false  && jQuery('.topSliderWrapper.clone').size() <= 0 ) {
						jQuery('<div class="topSliderWrapper clone" style="position: fixed; top: ' + altezzaMenuTop + 'px; left: 0; z-index : 1000; padding-top: 0;"><div class="menuWrapper clone"></div></div>').appendTo('body')
						jQuery('#testata_2012_navILAS').appendTo(jQuery('.menuWrapper.clone'));
						jQuery('.menuWrapper:not(".clone")').attr('style', 'height: ' + altezzaMenuWrapper +'px');
						isClosed = true;
						
						if(jQuery('.titleWrapper').size() > 0){
							jQuery('.titleWrapper:first').clone().appendTo(jQuery('.topSliderWrapper.clone'));
						}
						
						/*if(jQuery('.ricerca_wrapper .ricerca').size() > 0){
							jQuery('.ricerca_wrapper').clone().appendTo(jQuery('.topSliderWrapper.clone'));
						}*/
						
					}
				} 
				//BLOCCO IL MENU DI NAVIGAZIONE E BLOCCO IL MENU DEL TITOLO SE ESISTE
				else if((initialPosition >= position2) && isClosed == false  && jQuery('.topSliderWrapper.clone').size() <= 0 ) {
					jQuery('<div class="topSliderWrapper clone" style="position: fixed; top: ' + altezzaMenuTop + 'px; left: 0; z-index : 1000; padding-top: 0;"><div class="menuWrapper clone"></div></div>').appendTo('body')
					jQuery('#testata_2012_navILAS').appendTo(jQuery('.menuWrapper.clone'));
					jQuery('.menuWrapper:not(".clone")').attr('style', 'height: ' + altezzaMenuWrapper +'px');
					isClosed = true;
					
					if(jQuery('.titleWrapper').size() > 0){
						jQuery('.titleWrapper:first').clone().addClass('clone').appendTo(jQuery('.topSliderWrapper.clone'));
					}
					
					/*if(jQuery('.ricerca_wrapper .ricerca').size() > 0){
						jQuery('.ricerca_wrapper').clone().appendTo(jQuery('.topSliderWrapper.clone'));
					}*/
					
				}
				


			} // FINE CASO IN CUI NON SIAMO IN UNA PAGINA DELLA GALLERY 
			else { // CASO IN CUI SIAMO IN UNA PAGINA DELLA GALLERY
				
				//BLOCCO IL MENU SUPERIORE
				if(initialPosition >= altezzaHeader && jQuery('.topLinksWrapper.clone').size() <= 0 ){
					jQuery('.topLinksWrapper').clone().appendTo('body').addClass('clone').css({
							'position': 'fixed',
							'top': 0,
							'left': 0,
							'z-index' : 1200
						})
				}
				
				if((initialPosition >= position2)  && jQuery('.cloneWrapper').size() <= 0 ) {
					jQuery('<div class="cloneWrapper" style="position: fixed; top: ' + altezzaMenuTop + 'px; height: '+ parseInt(parseInt(jQuery('.titleWrapper:first:not(".clone")').outerHeight()) + parseInt(jQuery('.ricerca_wrapper').outerHeight()) + parseInt(jQuery('.barra_ricerca').outerHeight()) ) + 'px; left: 0; z-index : 1000; padding-top: 0; width: 100%"></div>').appendTo('body')
					jQuery('titleWrapper:first:not(".clone"), .ricerca_wrapper:first:not(".clone"), .barra_ricerca:first:not(".clone")').clone().addClass('clone').appendTo(jQuery('.cloneWrapper'));
					//se volessi togliere la barra ricerca dal bloccaggio mi basterebbe togliere parseInt(jQuery('.barra_ricerca').outerHeight()) e .barra_ricerca:first:not(".clone")					
					
					//isClosed = true;
										
				}
				
			} // FINE CASO IN CUI SIAMO IN UNA PAGINA DELLA GALLERY
			
		//--- FINE CASO SCROLL VERSO IL BASSO	
		} else {
		//--- CASO SCROLL VERSO L'ALTO
		
// LOG CONSOLE
//jQuery('#consoleLog').html(jQuery('#consoleLog').html() + "<br>su");
			// CASO GENERALE NON GALLERY
			if(!isGallery){
				
				if(initialPosition <= altezzaHeader){
					jQuery('.topLinksWrapper.clone').remove();
					jQuery('#testata_2012_navILAS').appendTo(jQuery('.menuWrapper:not(".clone")'));
					jQuery('.menuWrapper:not(".clone")').removeAttr('style');
					jQuery('.topSliderWrapper.clone').remove();
					isClosed = false;
				} else if(initialPosition <= position2 && isClosed == true ) {
					jQuery('#testata_2012_navILAS').appendTo(jQuery('.menuWrapper:not(".clone")'));
					jQuery('.menuWrapper:not(".clone")').removeAttr('style');
					jQuery('.topSliderWrapper.clone').remove();
					isClosed = false;
				} else if(jQuery('.barra_ricerca').size() > 0 && initialPosition <= (position2 + jQuery('.ricerca_wrapper').outerHeight()) && isClosed2 == true ) {
					jQuery('.barra_ricerca.clone').remove();
					isClosed2 = false;
				}
			}
			// CASO GALLERY
			else {
				
				if(initialPosition <= altezzaHeader){
					jQuery('.topLinksWrapper.clone').remove();
					jQuery('#testata_2012_navILAS').appendTo(jQuery('.menuWrapper:not(".clone")'));
					jQuery('.menuWrapper:not(".clone")').removeAttr('style');
					jQuery('.topSliderWrapper.clone').remove();
					isClosed = false;
					/*
						vecchio if seguente
						} else if(initialPosition < (position2 + titleWrapperHeight ) && jQuery('.cloneWrapper').size() > 0 ) {
						
					*/
				} else if(initialPosition < (position2 + titleWrapperHeight - parseInt(jQuery('.fasciarossa').outerHeight())-5) && jQuery('.cloneWrapper').size() > 0 ) {
					jQuery('#testata_2012_navILAS').appendTo(jQuery('.menuWrapper:not(".clone")'));
					jQuery('.cloneWrapper').remove();
					isClosed = false;
				}

			}
		//--- FINE CASO SCROLL VERSO L'ALTO
		}
		
	});
	/* ============================== *\
	\* ============================== */
	
	
	/* ============================== *\
		   TEXT SCROLLER TESTATA
	\* ============================== */
	
	jQuery.fn.textScroller = function(options){
		//imposto i valori di default
		options = jQuery.extend({}, jQuery.fn.textScroller.defaults, options);
		
		
		this.find(".button_scroll_wrapper").children(".button_scroll_down").add(this.find(".button_scroll_wrapper").children(".button_scroll_up"))
			.stop().animate({ 'opacity': 0.6 }, 0)
			.hover(function(){
					jQuery(this).stop().animate({ 'opacity':1 }, 400);
				}, function(){
					jQuery(this).stop().animate({ 'opacity':0.6 }, 400)
				})
			.bind('click', function(){
			
			// ===============================================
			// 	V A R I A B I L I
			// ===============================================
			var bottone = jQuery(this);
			var dragMask = jQuery(this).parent().siblings('.newsMask');
			var paragraph = dragMask.find('p');
			
			var textTop = parseInt(paragraph.css('top')) ;

			var ALTEZZA = parseInt(paragraph.height());
			var lineHeight = parseInt(paragraph.css('line-height'));
			
			//definisco il numero di linee da scrollare pari al numero di linee visibili -1
			var numero_linee_da_scrollare = (parseInt((parseInt(dragMask.height()))/lineHeight))-1;
			var tempoAnimazione;
			
			
			// ===============================================
			
			
			// ===============================================
			// FUNZIONE @ DOWN
			// ===============================================
			if( jQuery(this).hasClass('button_scroll_down') && !jQuery(this).hasClass('disabled') ){
				//blocco il pulsante
				jQuery(this).addClass('disabled').siblings('.button_scroll_up').addClass('disabled');
				//controllo se il paragrafo esca dalla zona visibile
				//se non esce lo animo
				if( (textTop - (numero_linee_da_scrollare * lineHeight) ) >= ((-1) *( ALTEZZA - lineHeight)) ) {
					
					//alert( (textTop + (numero_linee_da_scrollare * lineHeight) )  );
					paragraph.stop().animate({
								top: '-=' + (numero_linee_da_scrollare*lineHeight) + 'px'
							},
							 tempoAnimazione, 'easeOutQuad' 
							//sblocco il pulsante
							, function(){ bottone.removeClass('disabled').siblings('.button_scroll_up').removeClass('disabled'); 
							}
 
					);
					
				} 
				//se invece uscirebbe lo imposto al massimo della visibilità
				else {
					//alert ('no');
					//alert( (textTop - (numero_linee_da_scrollare * lineHeight) ) );
					paragraph.stop().animate({
								top: '-' + (ALTEZZA - lineHeight) + 'px'
							},
							 tempoAnimazione, 'easeOutQuad' 
							//sblocco il pulsante
							, function(){ bottone.removeClass('disabled').siblings('.button_scroll_up').removeClass('disabled'); 
							}
  
					);
				}
				
			} //fine primo if
			
			// ===============================================
			// FUNZIONE @ UP
			// ===============================================
			else if ( jQuery(this).hasClass('button_scroll_up') && !jQuery(this).hasClass('disabled') ) {
				//alert('up');
				//blocco il pulsante
				jQuery(this).addClass('disabled').siblings('.button_scroll_down').addClass('disabled');
				//controllo se il paragrafo esca dalla zona visibile
				//se non esce lo animo
				if( (textTop + (numero_linee_da_scrollare * lineHeight) ) <= 0 ) {
					//alert('scroll');
					//alert(textTop + (numero_linee_da_scrollare * lineHeight) );
					paragraph.stop().animate({
								top: '+=' + (numero_linee_da_scrollare*lineHeight) + 'px'
							},
							 tempoAnimazione, 'easeOutQuad' 
							 //sblocco il pulsante
							, function(){ bottone.removeClass('disabled').siblings('.button_scroll_down').removeClass('disabled'); 
							} 
					);
				}
				//se invece uscirebbe lo imposto al massimo della visibilità
				else {
					//alert('no scroll');
					//alert(textTop + (numero_linee_da_scrollare * lineHeight) );
					paragraph.stop().animate({
								top: '0px'
							},
							 tempoAnimazione, 'easeOutQuad' 
							 //sblocco il pulsante
							, function(){ bottone.removeClass('disabled').siblings('.button_scroll_down').removeClass('disabled'); 
							}
					);
				}
				

			}
			
		});
		
		//RETURN OGGETTO JQUERY
		return this;
	};
	//oggetto con i valori di default
	jQuery.fn.textScroller.defaults = {
		
	}
	jQuery('.newsTesto').textScroller();
	
	
	// PULSATE DEL BTN PRENOTA ONLINE
	var timerPulsate = setInterval(function(){ 
			jQuery('.testata_2012_topLinks_link.pulsate a.hover').stop().animate({ 'opacity': 1}, 600, function(){ jQuery('.testata_2012_topLinks_link.pulsate a.hover').animate({ 'opacity': 0}, 600) })
		}, 4000);
});

