// $Id: galerie.js,v 1.8 2008-02-20 16:39:09 christian Exp $

jQuery(function($) {

	// absolute images paths
	var loadingAnimationSrc = '/images/iu/galerie/loadingAnimation.gif';
	var selectedImageSrc =  '/images/iu/galerie/puce_bleu.gif';

	try { // try and get relative paths from hidden html
		var getSrc = new RegExp("url\\(([^\\)]+)\\)", "i");
		var stripQuotes = new RegExp("(\"|\')*", "g");
		loadingAnimationSrc = $('#galerie-loading-animation')
			.css('background-image')
			.match(getSrc)[1]
			.replace(stripQuotes,'');
		selectedImageSrc = $('#galerie-selected-image')
			.css('background-image')
			.match(getSrc)[1]
			.replace(stripQuotes,'');
	}catch(e){}

	// check for photo_id in url
	//console.log(_GET['photo_id']);
	var imageId = _GET['photo_id'] || _GET['id'] || null;

	// find index of queried image
	var imageIndex = $('#mycarousel img').index( $('#mycarousel img[src*='+ imageId +']').get(0) ) + 1;
	var start = imageIndex || 1;

	// init carousel
	$('#mycarousel').jcarousel({
		start: start,
		vertical: true,
		scroll: 7,
		visible: 8,
		animation: "slow",
		itemLastInCallback: {
			onBeforeAnimation: function() {
				$('#galerie-active-image').hide();
			},
			onAfterAnimation: function () {
				function getImageId(img) {
					var regex = new RegExp(".*/([^\.]+)\.", "i");
					var fetch = img.src.match(regex);
					return fetch[1] || 0;
				}
				var imageId = getImageId($('#pleinecran-img').get(0));
				var activeLi = $('#mycarousel img[src*='+ imageId +']')
					.parent('li.jcarousel-item');
				setActiveEffect(activeLi);
			}
		}
	}).show();

	// init events and elements
	$('#pleinecran-infobulle')
		.click(function(){$(this).slideToggle()}) // no toggle() for Safari
		.hide();
	$('#pleinecran-icones-send')
		.hide();
	$('#pleinecran-icones-info')
		.click(function(){$('#pleinecran-infobulle').slideToggle()}) // no toggle() for Safari
		.hide();
	$('#pleinecran-img').load( function() {

		$('#pleinecran-div img[src*="loadingAnimation"]').hide();
		$(this).fadeIn("slow", function() { // callback

			var infobulleHtml = $('#pleinecran-infobulle p').html();
			var hasInfobulle = !!$.trim(infobulleHtml).length || 0;
			if (hasInfobulle)
				$('#pleinecran-icones-info').show();
			$('#pleinecran-icones-send').show();
			$(this).focus();

			/*
			 IF WE RETURN FROM SEND POSTCARD...
			 bug:	the load event on images is dispatched twice on page load in my Firefox
				my workaround is casting to number and incrementing
				3 times before removing confirm msg
			*/
			var msg = "Votre carte postale a été envoyée avec succès.";
			if (_GET['confirm']) {
				if (_GET['confirm'] == 1) {
					$('#contenu').append('<span id="confirm-msg">' + msg + '</span>');
					if (hasInfobulle)
						$('#pleinecran-infobulle').show('slow');
					_GET['confirm'] = 2;
				} else if ($('#confirm-msg') && _GET['confirm'] > 3) {
					$('#confirm-msg').fadeOut("slow", function() { // callback
						$(this).remove();
					});
				}
				_GET['confirm']++;
			}
		});
	});
	$('#pleinecran-div')
		.append('<img src=" ' + loadingAnimationSrc + ' ">');
	$('#endessous-menu-secondaire a').show();

	$(document.body)
		.prepend('<img id="galerie-active-image" src="' + selectedImageSrc + '">');
	$('#galerie-active-image').hide();

	// set click on thumbs
	$('#mycarousel img').click(function() {

		// hide all now and later show back only what is needed for this picture
		$('#galerie-active-image, #pleinecran-img, #pleinecran-infobulle, #pleinecran-icones-info, #pleinecran-icones-send')
			.hide();

		$('#pleinecran-div img[src*="loadingAnimation"]').show();

		// get
		var li = $(this).parent('li.jcarousel-item');
		var infobulleHtml = $('div.infobulle', li).html();
		var hasInfobulle = !!$.trim(infobulleHtml).length || 0;
		var envoyerHref = $('a.cartepostale', li).attr('href') || '#no-url-supplied-for-postal-card';
		var newSrc = $('a.pleinecran', li).attr('href');

		// set
		$('#pleinecran-infobulle p').empty();
		if (hasInfobulle)
			$('#pleinecran-infobulle p').html(infobulleHtml);
		$('#pleinecran-icones-send a, #endessous-menu-secondaire a')
			.attr('href', envoyerHref);

		$('#pleinecran-icones-send, #endessous-menu-secondaire a').click(function() {
			var action = $(this).attr('href') || $('a', this).attr('href');
			$(document.body).append(
				'<form style="display:none" id="infobulle-form" action="' + action + '" method="post">' +
				'<input type="hidden" name="titre" value="' + $('.titre', li).text() + '">' +
				'<input type="hidden" name="lieu" value="' + $('.lieu', li).text() + '">' +
				'<input type="hidden" name="credits" value="' + $('.credits', li).text() + '">' +
				'</form>'
			);
			$('#infobulle-form').submit();
			return false;
		});

		$('#pleinecran-img').attr('src', newSrc);

		setActiveEffect(li);
	});

	// show first or queried image
	$('#mycarousel img:eq(' + (start-1) + ')').trigger('click');

	function setActiveEffect(li) {
		var clipdiv = $('.jcarousel-clip-vertical');
		var minFromTop = clipdiv.offset().top;
		var maxFromTop = minFromTop + clipdiv.height();
		var pos = li.offset();
		if (pos && pos.top >= minFromTop && pos.top <= maxFromTop) {
			$('#galerie-active-image').css({
				'top': (pos.top + 27) + 'px',
				'left': (pos.left + 98) + 'px'
			}).show();
		}
	}
});
