(function ($) {
	
	// document ready
    $('document').ready(function () {
		
		var cback = $('body').css('background-image');
		cback = cback.substring(4, cback.length - 1);

		if(cback.indexOf('"') == 0 || cback.indexOf("'") == 0) {
			cback = cback.substring(1, cback.length - 1);
		}
		
		$('body').css('background', '#111');
		
		// background sizing
		$.supersized({
	
			//Background image
			slides	:  [ { image : cback } ]					
		
		});
		
        /************************************************************/
        /* gestione pulsante torna su								*/
        /************************************************************/
        $('#backtop a').click(function (event) {
            event.preventDefault();
			// cerco l'oggetto in base all'ancora
			var myhref = this.href.split('#')[1];
            $(window).scrollTo('#' + myhref, 800);
        });
		
		
		/************************************************************/
        /* scroll news homepage										*/
        /************************************************************/
		
		// raccolta feeds
		var feeds = $('.home .hfeed .hentry');
		var hmax = 0;
		
		if (feeds.length > 1) {
		
			// stato iniziale delle news, determino anche l'altezza massima
			feeds.each(function(){
			
				var $obj = $(this);
				
				// impostazioni iniziali singola news
				$obj.css({'display': 'block', 'opacity': 0});
				 
				 // ritrovo altezza massima dei box
				 if ($obj.height() > hmax) {
					 hmax = $(this).height();
				 }
				 
				 // nascondo i box
				 $obj.css({'display': 'none'});
				 
			});
			
			// adatto altezza dei box
			feeds.height(hmax);
			
			// impostazioni scroll
			var cfeed = {
				index: 0,
				length: feeds.length,
				time: 5000,
				duration: 1500,
				over: false
			}
			
			// adatto altezza del parent + offset per link archivio
			feeds.eq(0).parent().height(hmax + 36);
			
			// mostro la prima
			feeds.eq(0).css({'display': 'block', 'z-index': 2}).stop(true,true).animate({'opacity': 1}, 500);
			
			// disabilito effetti se sono sopra con il muose
			feeds.mouseenter(function(){
				cfeed.over = true;
			}).mouseleave(function(){
				cfeed.over = false;
			});
			
			var nextFeed = function(){
			
				//console.dir(cfeed);
				
				// se sono sopra con il mouse non eseguo effetti
				if(!cfeed.over) {
												
					// nascondo il corrente, calcolo e mostro il prossimo
					feeds.eq(cfeed.index).css({'z-index': 1}).stop(true,true).animate({'opacity': 0}, cfeed.duration, function(){ $(this).css('display', 'none'); });
					cfeed.index = (cfeed.index + 1) % cfeed.length;
					feeds.eq(cfeed.index).css({'display': 'block', 'z-index': 2}).stop(true,true).animate({'opacity': 1}, cfeed.duration);
				
				}
				
				// ripeto la funzione per mostrare la prossima news
				setTimeout(function(){ nextFeed(); }, cfeed.time);
			
			}
			
			// attivazione ciclo news
			setTimeout(function(){ nextFeed(); }, cfeed.time);
			
		}

    }) // Chiusura DOMREADY
	
	$(window).scroll(function () {
	
		/************************************************************/
        /* display pulsante torna su								*/
        /************************************************************/
		$('#backtop').css('display', 'none');
		// se la window è scrollata mostro il pulsante
		if ($(window).scrollTop() > 0) {
			$('#backtop').css('display', 'block');
		}
		
    })// Chiusura SCROLL
	
})(jQuery);
