$(document).ready(function(){

	// loading mask
	$('#loading_mask').fadeOut();
	
	// other window link
	$("a.window").each(function(){
		$(this).attr('target', '_blank');
	});
		
	// emails
	$("a.email").each(function(){
		$(this).attr('href', 'mailto:' + $(this).text().replace(" chez ", "@").replace(" point ", "."))
			.text($(this).attr('title')).removeAttr('title');
	});

	// tip
	$(".credits .tip").data('fadeOut', 5000);
	$(".tip").each(function(){
		$(this).prev().tipTip({maxWidth:'auto', delay:100, defaultPosition:'top', content:$(this).html(), fadeOut:$(this).data('fadeOut')});
	});
	
	// galery thumbs
	if ($("#galery").length > 0)
	{
		// slider
		$("#galery").removeClass("standard").addClass("slider");
		var thumbs_container = $('#thumbs_container');
		var thumbs_list = $('#thumbs');

		// slider navigation
		thumbs_container.prepend($('<a>').attr('id', 'thumbs_previous').addClass('navigation').attr('title', 'images précédentes'));
		thumbs_container.prepend($('<a>').attr('id', 'thumbs_first').addClass('navigation').attr('title', 'première image'));
		thumbs_container.prepend($('<div>').attr('id', 'thumbs_navigation_first_mask').addClass('thumbs_navigation_mask'));
		thumbs_container.prepend($('<div>').attr('id', 'thumbs_container_mask'));
		thumbs_container.append($('<div>').attr('id', 'thumbs_navigation_last_mask').addClass('thumbs_navigation_mask'));
		thumbs_container.append($('<a>').attr('id', 'thumbs_next').addClass('navigation').attr('title', 'images suivantes'));
		thumbs_container.append($('<a>').attr('id', 'thumbs_last').addClass('navigation').attr('title', 'dernière image'));
		$("#thumbs_previous").click(function(){
			slideThumbs(-1);
		});
		$("#thumbs_next").click(function(){
			slideThumbs(1);
		});
		$("#thumbs_first").click(function(){
			slideThumbs('first');
		});
		$("#thumbs_last").click(function(){
			slideThumbs('last');
		});
		// slider position
		thumbs_list.width(130 * $('#thumbs').children('li.thumb').length + 1200);
		thumbs_list.css('margin-left',  $('#thumbs_first').width() +  $('#thumbs_previous').width() + 'px');
		for (i = 0; i < 5; i ++)
		{
			thumbs_list.append($('<li>').addClass('no_thumb').attr('title', 'il n\'y a plus de photo disponible'));
		}
		
		var number_thumbs = $('#thumbs').children('li.thumb').length;
		var current_thumb = 0;
		var window_width = $(window).width();
		var max_next_thumb = Math.ceil(number_thumbs - (window_width / $('li.thumb').width())) + 2;
		
		$(window).resize(function() {
			window_width = $(window).width();
			max_next_thumb = Math.ceil(number_thumbs - (window_width / $('li.thumb').width())) + 2;
			if (current_thumb > max_next_thumb)
			{
				slideThumbs('last');
			}
			showWidePicture('current');
		});
		
		function slideThumbs(move)
		{
			thumbs_list = $('#thumbs');
			if (move == 'first')
			{
				current_thumb = 0;
				thumbs_list.animate({left:'0'});
			}
			else if (move == 'last')
			{
				current_thumb = max_next_thumb;
				thumbs_list.animate({left:'-' + max_next_thumb * 135});
			}
			else
			{
				if ((move < 0 && current_thumb > 0) || (move > 0 && current_thumb < max_next_thumb))
				{
					current_thumb += move;
					thumbs_list.animate({left:'-=' + (move * 135)}, 'fast');
				}
			}
		}
		
		// wide screen viewer
		var wsv_mask = $('<div>').addClass('wide_viewer_mask');
		var wsv_content = $('<div>').addClass('wide_viewer_content');
		// image
		var wsv_image_file = $('<img>').attr('id', 'wide_viewer_image_file');
		// navigation
		var wsv_navigation_previous = $('<a>').attr('id', 'wide_viewer_navigation_previous').addClass('navigation').attr('title', 'voir l\'image précédente');
		wsv_navigation_previous.click(function(){
			showWidePicture('previous');
		});
		var wsv_navigation_next = $('<a>').attr('id', 'wide_viewer_navigation_next').addClass('navigation').attr('title', 'voir l\'image suivante');
		wsv_navigation_next.click(function(){
			showWidePicture('next');
		});
		var wsv_navigation_close = $('<a>').attr('id', 'wide_viewer_navigation_close').addClass('navigation').attr('title', 'fermer la vue plein écran');
		wsv_navigation_close.attr('href', '#');
		wsv_navigation_close.click(function(){
			hideWidePicture();
		});
		var wsv_navigation = $('<div>').attr('id', 'wide_viewer_navigation').append(wsv_navigation_previous).append(wsv_navigation_next).append(wsv_navigation_close);
		// text
		var wsv_title = $('<h3>').attr('id', 'wide_viewer_title');
		var wsv_desc = $('<p>').attr('id', 'wide_viewer_desc');
		var wsv_flickr = $('<p>').attr('id', 'wide_viewer_links');
		var wsv_sizes = $('<p>').attr('id', 'wide_viewer_sizes');
		wsv_sizes.append($('<a>').attr('id', 'wide_viewer_size_s').attr('target', '_blank').attr('title', 'voir l\'image en taille S').text('s'));
		wsv_sizes.append($('<a>').attr('id', 'wide_viewer_size_m').attr('target', '_blank').attr('title', 'voir l\'image en taille M').text('m'));
		wsv_sizes.append($('<a>').attr('id', 'wide_viewer_size_l').attr('target', '_blank').attr('title', 'voir l\'image en taille L').text('l'));
		//wsv_sizes.append($('<a>').attr('id', 'wide_viewer_size_xl').attr('target', '_blank').text('xl'));
		// appending
		var wsv_image = $('<div>').attr('id', 'wide_viewer_image').append(wsv_image_file);
		var wsv_text = $('<div>').attr('id', 'wide_viewer_text');
		wsv_text.append(wsv_navigation).append(wsv_title).append(wsv_flickr).append(wsv_sizes).append(wsv_desc);
		wsv_content.append(wsv_image).append(wsv_text);
		var wsv =$('<div>').attr('id', 'wide_viewer').append(wsv_mask).append(wsv_content);
		wsv.hide();
		$('body').append(wsv);
		// thumb links
		$('#thumbs').find('li.thumb a').each(function(n, link){
			link = $(link);
			link.click(function(){
				showWidePicture(link);
			});
			link.attr('href', '#' + link.data('photo_file') + "-" + unescape_quotes(link.data('photo_title')));
		});
		var wsv_current_link = null;
		
		// keyboard shortcuts
		$(document).keyup(function(event) {
			if (event.keyCode == 37) // previous
			{
				showWidePicture('previous');
			}
			else if (event.keyCode == 39) // next
			{
				showWidePicture('next');
			}
			else if (event.keyCode == 27) // close
			{
				hideWidePicture();
			}
		});
		
		function hideWidePicture()
		{
			addUrlAnchor('');
			wsv_current_link = null;
			wsv.fadeOut();
		}
		
		function showWidePicture(link)
		{
			if (link == 'previous')
			{
				slideThumbs(-1);
				showWidePicture($(wsv_navigation_previous.data('other_link')));
			}
			else if (link == 'next')
			{
				slideThumbs(1);
				showWidePicture($(wsv_navigation_next.data('other_link')));
			}
			else if (link == 'current')
			{
				if (wsv_current_link != null)
				{
					showWidePicture(wsv_current_link);
				}
			}
			else
			{
				if (link.length == 0)
				{
					link = $($('#thumbs').find('li.thumb a')[0]);
				}
				wsv_current_link = link;
				
				wsv.fadeIn();
				orientation = link.data('photo_orientation');
				
				available_width = $('#wide_viewer_image').width();
				available_height = $(window).height() - $('#footer').height() - $('#thumbs_container').height() - 20;
				size_choice = dimensions[0];
				for (s in dimensions)
				{
					if (orientation == 'landscape')
					{
						image_width = max_dimensions.width[dimensions[s]];
						image_height = image_width * 2/3;
					}
					else if (orientation == 'portrait')
					{
						image_height = max_dimensions.height[dimensions[s]];
						image_width = image_height * 2/3;
					}
					else
					{
						image_width = Math.min(max_dimensions.width[dimensions[s]], max_dimensions.height[dimensions[s]]);
						image_height = image_width;
					}
					if (image_width <= available_width && image_height <= available_height)
					{
						size_choice = dimensions[s];
					}
				}

				photo_file = link.data('photo_file');
				wsv_image_file.attr('src', image_folder + photo_file + '_' + size_choice + '.jpg');
				if (link.data('photo_file') != '')
					wsv_title.text(link.data('photo_file') + ' - ' + unescape_quotes(link.data('photo_title')));
				else
					wsv_title.text(unescape_quotes(link.data('photo_title')));
				wsv_desc.text(unescape_quotes(link.data('photo_desc')));
				wsv_sizes.find('#wide_viewer_size_s').attr('href', image_folder + photo_file + '_s.jpg');
				wsv_sizes.find('#wide_viewer_size_m').attr('href', image_folder + photo_file + '_m.jpg');
				wsv_sizes.find('#wide_viewer_size_l').attr('href', image_folder + photo_file + '_l.jpg');
				//wsv_sizes.find('#wide_viewer_size_xl').attr('href', image_folder + photo_file + '_xl.jpg');
				if (link.data('photo_flickr_id') != '')
				{
					wsv_flickr.html($('<a>').attr('target', '_blank').addClass('flickr').attr('title', 'voir la page flickr de cette photo').attr('href', flickr_url.replace('%1', link.data('photo_flickr_id'))).text('flickr'));
				}
				else
				{
					wsv_flickr.html($('<br/>'));
				}
				addUrlAnchor(link.data('photo_file') + "-" + unescape_quotes(link.data('photo_title')));
	
				if (undefined == link.parent().prev().children('a')[0])
				{
					wsv_navigation_previous.animate({opacity:0.5}, 'fast');
				}
				else
				{
					prev = link.parent().prev().children('a')[0];
					wsv_navigation_previous.animate({opacity:1}, 'fast');
					wsv_navigation_previous.data('other_link', prev);
				}
				if (undefined == link.parent().next().children('a')[0])
				{
					wsv_navigation_next.animate({opacity:0.5}, 'fast');
				}
				else
				{
					next = link.parent().next().children('a')[0];
					wsv_navigation_next.animate({opacity:1}, 'fast');
					wsv_navigation_next.data('other_link', next);
				}
			}
		}
		
		// auto display current picture (anchor)
		if (document.URL.indexOf('#') > 0)
		{
			file_name = document.URL.substr(document.URL.indexOf('#') + 1).split('-')[0];
			if ($('ul#thumbs a.picture_' + file_name).length > 0)
			{
				showWidePicture($('ul#thumbs a.picture_' + file_name));
			}
		}
	}
});

function unescape_quotes(string)
{
	return string.replace(/&#39;/g, "'").replace(/&#34;/g, '"');
}

function addUrlAnchor(anchor)
{
	if (location.href.indexOf('#') > 0)
	{
		location.href = location.href.substring(0, location.href.indexOf('#')) + '#' + anchor;
	}
	else
	{
		location.href += '#' + anchor; 
	}
}
