$.fn.image = function(src, alt, f){ 
	return this.each(function(){ 
		var i = new Image(); 
		i.src = src; 
		i.alt = alt;
		i.onload = f;
	}); 
}

$.fn.miniMenu = function() {
	return this.each(function() {		
		$(this).mouseleave(function() {
			$(this).children('li.mainNavDropdown').children('ul').hide();
		});
		
		$(this).children('li.mainNavDropdown').prev().toggleClass('hasMenu', true).mouseover(function() {
			$(this).next().children('ul').show().mouseleave(function() {
				$(this).hide();
			});
		})

		$(this).children('li:not(.hasMenu):not(.mainNavDropdown)').mouseover(function() {
			$(this).siblings('li.mainNavDropdown').children('ul').hide();
		});

	});
}

$.fn.miniGallery = function () {
	return this.each(function () {	
		$(this).toggleClass('miniGallery', true);
		$(this).children('div.thumbs').children('a').click(function() {
			var thumbnailImg = $(this).children('img');
			$('div.miniGallery:not([id=' + $(this).parent().parent().attr('id') + '])').children('div.big').html('').hide();
			$('div.miniGallery:not([id=' + $(this).parent().parent().attr('id') + '])').children('div.caption').html('').hide();
			if($(this).hasClass('image')) {
				$(this).parent().siblings('div.big').html('<img src="' + thumbnailImg.attr('src').replace('thumbnail', 'big') + '" />').show();
			}
			else {
				var videoUrl = thumbnailImg.attr('src').replace('/2.jpg', '').replace('http://img.youtube.com/vi/', 'http://www.youtube.com/v/');
				$(this).parent().siblings('div.big').html('<object width="370" height="320"><param name="movie" value="' + videoUrl + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + videoUrl + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="370" height="320"></embed></object>').show();				
			}
			$(this).parent().siblings('div.caption').html(thumbnailImg.parent().next('div.title').html()).show();
			return false;
		});
   });
}

$.fn.slideGallery = function() {
	return this.each(function() {
		var slideGallery = $(this);
		var currImage = 1;
		var numImages = slideGallery.children('img').length;

		slideGallery.children('p.paging').children('span.position').text(currImage + " / " + numImages);

		slideGallery.parent().parent().children('div.pubblicationMenu').children('ul').children('li').children('a.preview').click(function() {
			slideGallery.parent().parent().parent().find('div.slideGallery.current').toggleClass('current', false).hide();	
			slideGallery.toggleClass('current', true);
			slideGallery.children('img:not(.image1)').hide();
			slideGallery.children('img.image1').show();		
			slideGallery.children('p.paging').children('span.position').text("1 / " + numImages);	
			slideGallery.show();
			return false;			
		});
		
		slideGallery.parent().children('a.preview').click(function() {
			slideGallery.parent().parent().parent().find('div.slideGallery.current').toggleClass('current', false).hide();	
			slideGallery.toggleClass('current', true);			
			slideGallery.children('img:not(.image1)').hide();
			slideGallery.children('img.image1').show();	
			slideGallery.children('p.paging').children('span.position').text("1 / " + numImages);						
			slideGallery.show();
			return false;
		});
		
		slideGallery.children('p.paging').children('a.prev').click(function() {
			if(currImage == 1)
				currImage = numImages;
			else
				currImage--;
			slideGallery.children('img:not(.image' + currImage + ')').hide();
			slideGallery.children('img.image' + currImage).show();		
			slideGallery.children('p.paging').children('span.position').text(currImage + " / " + numImages);
			return false;
		});
		
		slideGallery.children('p.paging').children('a.next').click(function() {
			if(currImage == numImages)
				currImage = 1;
			else
				currImage++;
			slideGallery.children('img:not(.image' + currImage + ')').hide();
			slideGallery.children('img.image' + currImage).show();
			slideGallery.children('p.paging').children('span.position').text(currImage + " / " + numImages);
			return false;
		});
	});
}

$(function() {
	$('div.expositionItem div.gallery').miniGallery();
	$('div.pubblicationItem div.pubblicationContent div.slideGallery').slideGallery();
	$('ul#mainNav').miniMenu();
	
	$('a#archiveMenu').click(function() {
		$(this).siblings('ul').mouseleave(function() {
			$(this).hide();
		}).show();
		return false;
	});
});