$(document).ready(function() {

	$("#christmas-map").draggable();
	$("#christmas-map div a").hover(
		function () {
			if ($("em", this).css("display") == 'none') {
				$("span", this).animate({
				paddingLeft: '10px',
				paddingRight: '10px',
				width: '122px'
				}, 100, function() {
					$("em", this).show();
				});
			};
	  	},
	    function () {
			if ($("em", this).css("display") == 'inline') {
				$("em", this).hide();
				$("span", this).animate({
				paddingLeft: '7px',
				paddingRight: '7px',
				width: '0px'
				}, 100);
			};
	  	}
	);

	$("#category-list").after('<div id="media-tooltip"><div><p><span>Progetto la Rondine</span></p></div></div>');

	var m = $("#home-news-wrapper li").length;
	if (m > 2) {
		$('#home-news-wrapper').carousel({
			direction: "vertical",
			dispItems: 1,
			loop: true,
			autoSlide: true, 
	        autoSlideInterval: 5000,
	        delayAutoSlide: 2000
		});
	}

	/*
	var m = $("#home-news-scenario-wrapper li").length;
	if (m > 2) {
		$('#home-news-scenario-wrapper').carousel({
			direction: "vertical",
			dispItems: 1,
			loop: true,
			autoSlide: true, 
	        autoSlideInterval: 5000,
	        delayAutoSlide: 2000
		});
	}
	*/
	var l = $("#main-entries li").length;
	if (l > 2) {
		$('#main-entries').carousel({
			dispItems: 2,
			pagination: true
		});
		$('#main-entries a.carousel-control.next').appendTo($('.carousel-pagination'));
		$('#main-entries a.carousel-control.previous').prependTo($('.carousel-pagination'));
	}

	var n = $("#media-image a").length;
	if (n > 1) {
		$('#media-image').cycle({
		    pause: true,
			fx: 'fade',
			pager:  '#media-nav',
			before: function() {
				$('#caption p').remove();
				if(this.title != ''){
					$('#caption').append('<p>'+this.title+'</p>');
				}
			}
		});
	} else {
		$('#caption p').remove();
		if($('#media-image a:first-child').attr('title') != ''){
			$('#caption').append('<p>'+$('#media-image a:first-child').attr('title')+'</p>');
		}
	};
	
	$("#media-aside h3 a").bind("mouseenter", function(e){
		e.preventDefault();
		$("#category-list").slideDown(300);
	});

	$("#media-aside").bind("mouseleave", function(e){
		e.preventDefault();
		$("#category-list").slideUp(300);
	});
	
	$("#media-list li a").live("click", function(e){
		e.preventDefault();
		$('#media-image').cycle('destroy');
		$('#media-image').unbind('cycle');
		$(this).parent().siblings().children().removeClass("active");
		$(this).addClass("active");
		var link = $(this).attr('href');
		var rel = $(this).attr('rel');
		$('#media-slideshow').fadeOut(300, function(){
			$(this).remove();
			$('#media-content').addClass('loading');
			$.get(link, function(data){
				$('#media-content').append(data);
				$('#media-content').removeClass('loading');
				if(rel == "video"){
					
				} else {
					var n = $("#media-image a").length;
					if (n > 1) {
						$('#media-image').cycle({
						    pause: true,
							fx: 'fade',
							pager:  '#media-nav',
							before: function() {
								$('#caption p').remove();
								if(this.title != ''){
									$('#caption').append('<p>'+this.title+'</p>');
								}
							}
						});
					}
				}
			});
		});
	});
	
	$("#category-list li a").live("click", function(e){
		e.preventDefault();
		$(this).parent().siblings().children().removeClass("active");
		$(this).addClass("active");
		var link = $(this).attr('href');
		$('#media-list ul').fadeOut(300, function(){
			$(this).remove();
			$('#media-list').addClass('loading');
			$.get(link, function(data){
				$('#media-list').append(data);
				$('#media-list').removeClass('loading');
				$("#category-list").slideToggle(300);
				makeScrollable("#media-list", "#media-list ul");
			});
		});
	});
	
	makeScrollable("#media-list", "#media-list ul");

var nmille = $("#media-image-1000 img").length;
	if (nmille > 1) {
		$('#media-image-1000').cycle({
		    pause: true,
			fx: 'fade',
			pager:  '#media-nav',
			before: function() {
				//$('#caption p').remove();
				//if(this.title != ''){
				//	$('#caption').append('<p>'+this.title+'</p>');
				//}
			}
		});
	} else {
		//$('#caption p').remove();
		//if($('#media-image a:first-child').attr('title') != ''){
			//$('#caption').append('<p>'+$('#media-image a:first-child').attr('title')+'</p>');
		//}
	};


});

function makeScrollable(wrapper, scrollable){
	  // Get jQuery elements
	  var wrapper = $(wrapper), scrollable = $(scrollable);
	
	  // Hide images until they are not loaded
	  scrollable.hide();
	  var loading = $(wrapper).addClass("loading"); //$("<div class='loading'></div>").appendTo(wrapper)
	
	  // Set function that will check if all images are loaded
	  var interval = setInterval(function(){
	    var images = scrollable.find("img");
	    var completed = 0;
	
	    // Counts number of images that are succesfully loaded
	    images.each(function(){
	      if (this.complete) completed++;   
	    });
	
	    if (completed == images.length){
	      clearInterval(interval);
	      // Timeout added to fix problem with Chrome
	      setTimeout(function(){
	
	        $(wrapper).removeClass("loading"); // loading.hide();
	        // Remove scrollbars    
	        wrapper.css({overflow: "hidden"});
	
	        scrollable.slideDown("slow", function(){
	          enable(); 
	        });                 
	      }, 1000); 
	    }
	  }, 100);
	
	  function enable(){
	  
		$("#media-list li a").mouseenter(function(){
			var li = $(this);
			var offset = li.offset();
			var caption = $(this).parent().children("span").html();
			$("#media-tooltip").css("top", offset.top-150);
			$("#media-tooltip").css("z-index", "90");
			$("#media-tooltip p span").html(caption);
			$("#media-tooltip").show();
		}).mouseleave(function(){
			$("#media-tooltip").hide();
		});
	  
	    // height of area at the top at bottom, that don't respond to mousemove
	    var inactiveMargin = 100;         
	    // Cache for performance
	    var wrapperWidth = wrapper.width();
	    var wrapperHeight = wrapper.height();
	    // Using outer height to include padding too
	    var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
	    // Do not cache wrapperOffset, because it can change when user resizes window
	    // We could use onresize event, but it&#39;s just not worth doing that 
	    // var wrapperOffset = wrapper.offset();
	  
	    //When user move mouse over menu          
	    wrapper.mousemove(function(e){
	      var wrapperOffset = wrapper.offset();
	      // Scroll menu
	      var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight  - inactiveMargin;
	  
	      if (top < 0){
	        top = 0;
	      }
	  
	      wrapper.scrollTop(top);
	    });       
	  }
}
