/*if ($.browser.msie  && parseInt($.browser.version) < 9) {
  alert('non IE9'); 
} else {
  alert('IE9');
}*/



Cufon.replace('.box h2', {fontFamily: 'ColaborateThin'});
Cufon.replace('#article h1, .tagline, .issue-date, .footer-issue-date, #noresults h1', {
	fontFamily: 'ColaborateThin'
});
Cufon.replace('#article .intro p, #sidebar h2, #sidebar .related h3, .related_post_title, .entry h3', { fontFamily: 'ColaborateLight' });
Cufon.replace('#header .category, a.home-link, .menu li, .box h4, #article .post-meta, .gallery-navigation', { fontFamily: 'Colaborate' });
Cufon.replace('.invito-mostre span, .segnala-azioni span', { fontFamily: 'ColaborateBold' });
Cufon.replace('#article .entry blockquote p', { fontFamily: 'Timeless' });

function boxPosition(){
	$('.box.news, .box.personaggio').each(function(index) {
	 	marginTop = 280-$(this).children('h2').height();
	 	$(this).children('h2').css('margin-top', marginTop);
	 	marginTop = 269-$(this).children('h2').height()-$(this).children('a.category').height();
	 	$(this).children('a.category').css('margin-top', marginTop);
	 	marginTop = 269-$(this).children('h2').height()-$(this).children('.date').height();
	 	$(this).children('.date').css('margin-top', marginTop);
	 });
	 $('.box.big').each(function(index) {
	  	marginTop = 255-$(this).children('h2').height();
	  	$(this).children('h2').css('margin-top', marginTop);
	  	marginTop = 244-$(this).children('h2').height()-$(this).children('a.category').height();
	  	$(this).children('a.category').css('margin-top', marginTop);
	  });	 
};


function animateGallery(){
	//if (!$.browser.msie  && parseInt($.browser.version) < 9) {
	$("ul#carousel-gallery li.page:nth-child(" + galleryCount + ")").delay(5000).animate({
	    opacity: 0
	  }, 200, function(){
	    $("ul#carousel-gallery li.page:nth-child(" + galleryCount + ")").hide();
	    if(galleryCount < 5){
	    	galleryCount++;	
	    }else{
	    	galleryCount = 1;
	    };
	    $("ul#carousel-gallery li.page:nth-child(" + galleryCount + ")").show().animate({
	        opacity: 1.0
	      }, 800, function(){
	        animateGallery();
	      });
	  });
	  //}
};

function animateVideos(){
	//if (!$.browser.msie  && parseInt($.browser.version) < 9) {
	$("ul#carousel-video li.page:nth-child(" + videoCount + ")").delay(5000).animate({
	    opacity: 0
	  }, 200, function(){
	    $("ul#carousel-video li.page:nth-child(" + videoCount + ")").hide();
	    if(videoCount < 5){
	    	videoCount++;	
	    }else{
	    	videoCount = 1;
	    };
	    $("ul#carousel-video li.page:nth-child(" + videoCount + ")").show().animate({
	        opacity: 1.0
	      }, 800, function(){
	        animateVideos();
	      });
	  });	  
	 //}
};

$(document).ready(function(){		
	
	boxPosition();	
	$('a[href$=".jpg"],a[href$=".jpeg"],a[href$=".png"],a[href$=".gif"]').fancybox();
	$("ul#carousel-gallery li.page").css('opacity', 0).hide();
	$("ul#carousel-gallery li.page:first-child").css('opacity', 1).show();
	
	$("ul#carousel-video li.page").css('opacity', 0).hide();
	$("ul#carousel-video li.page:first-child").css('opacity', 1).show();
	
	galleryCount = 1;
	videoCount = 1;
	animateGallery();
	animateVideos();
	
	if (!$.browser.msie  && parseInt($.browser.version) < 9) {
		$(".box.rubrica.tv .rubrica-tv").hover(
		  function () {
		    $(".icon", this).animate({
		        marginTop: -120
		      }, 500, 'easeOutBounce');
		  }, 
		  function () {
		    $(".icon", this).animate({
		        marginTop: -190
		      }, 1000, 'easeOutElastic');
		  }
		);
	}
	
	$("ul.inviti li a, ul.segnalazioni li a, .rubrica-tv a, .titolo-personaggio").tipTip({
		'defaultPosition': 'bottom'
	});
	
	/*var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/,0));
	var footer = $('#footer').offset().top

	  $(window).scroll(function () {
	    // what the y position of the scroll is
		    y = $(this).scrollTop();
			fineSidebar = y+$('#sidebar').height();
		    // whether that's below the form
		    if (y >= top) {
		      // if so, ad the fixed class
		      $('#sidebar').addClass('fixed');
		    } else {
		      // otherwise remove it
		      $('#sidebar').removeClass('fixed');
				if(fineSidebar > footer){
					topOffset = fineSidebar - footer;
					$('#sidebar').css('top','-'+ topOffset +'px');
				}
		    }
	  });*/
	
		var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/,0));
		//var footer = $('#footer').position();
		var footer; 
		
		$(window).load(function(){
		  footer = $("ul.footer-categories").offset().top;
		});
		
		  $(window).scroll(function () {
		    // what the y position of the scroll is
			    y = $(this).scrollTop();
				var fineSidebar = y+$('#sidebar').outerHeight();
				var topOffset = footer - fineSidebar;
				if((fineSidebar) > footer){
					// whether that's below the form
				    if (y >= top) {
				      // if so, ad the fixed class
				      $('#sidebar').addClass('fixed');
					
					 $('#sidebar').css('margin-top',topOffset);
				    } else {
				      // otherwise remove it
				      $('#sidebar').removeClass('fixed');
					  $('#sidebar').css('margin-top',-topOffset);
				    }
				}else{
					// whether that's below the form
				    if (y >= top) {
				      // if so, ad the fixed class
				      $('#sidebar').addClass('fixed');
					  $('#sidebar').css('margin-top',0);
				    } else {
				      // otherwise remove it
				      $('#sidebar').removeClass('fixed');
						$('#sidebar').css('margin-top',0);
				    }	
				}
			    
		  });
});






