﻿$(document).ready(function() {
	
	// PATCH PNG IE6
	if ( $.browser.msie && parseInt(jQuery.browser.version) < 7 ) 
	{
		var oBt1 = document.getElementById( "SlideShowBt1" )
		var oBt2 = document.getElementById( "SlideShowBt2" )
		var oBt3 = document.getElementById( "SlideShowBt3" )
		var oBt4 = document.getElementById( "SlideShowBt4" )
		var oLoupeImg = document.getElementById( "imgLoupe" )
		
		if ( oBt1 ) {
			oLoupeImg.outerHTML = "<span class='IEFauxBouttons' onclick=\"openLoupe()\" style=\"" + "width:26px; height:24px;display:inline-block;cursor:pointer;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + oLoupeImg.src + "\', sizingMethod='scale');\"></span>"
			oBt1.outerHTML = "<span class='IEFauxBouttons' onclick=\"openLoupe()\" style=\"" + "width:73px; height:73px;display:inline-block;cursor:pointer;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + oBt1.src + "\', sizingMethod='scale');\"></span>"
			oBt2.outerHTML = "<span class='IEFauxBouttons' onclick=\"openLoupe()\" style=\"" + "width:66px; height:73px;display:inline-block;cursor:pointer;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + oBt2.src + "\', sizingMethod='scale');\"></span>"
			oBt3.outerHTML = "<span class='IEFauxBouttons' onclick=\"openLoupe()\" style=\"" + "width:64px; height:73px;display:inline-block;cursor:pointer;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + oBt3.src + "\', sizingMethod='scale');\"></span>"
			oBt4.outerHTML = "<span class='IEFauxBouttons' onclick=\"openLoupe()\" style=\"" + "width:85px; height:73px;display:inline-block;cursor:pointer;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + oBt4.src + "\', sizingMethod='scale');\"></span>"
		}
	}
	EnableAllZoom( );
	
});

function EnableAllZoom( )
{
	$('img.zoomable').each( function( ) {
		
		var _this = this;
		var placeLoupe = function ( )
		{
			var left = $(_this).width( ) - 13 - 4;
			var top = $(_this).height( ) - 12 - 4;
			
			$('<span class="ZoomableContainer" style="position:absolute;margin-top:'+top+'px;margin-left:'+left+'px;width:13px;height:12px;z-index:2;"><a onclick="openZoom(this)" style="cursor:pointer;"><img src="/images/occasions/loupe_big.png" width="13" style="float:left;" alt="" border="0" /></a></span>').insertBefore( $(_this) );
			$(_this).css("cursor", "pointer").attr("title", "Cliquez pour agrandir").attr("alt", "Cliquez pour agrandir").click( function( ) {
				openZoom($(_this).prev().children("a")[0]);
			} );
		}
		
		var testLoupeIsVisible = function( )
		{
			if ( !$(_this).is(":visible") )		setTimeout( testLoupeIsVisible, 200 );
			else								placeLoupe( );
		}
		testLoupeIsVisible( );
	
	
	});
}

function DisableAllZoom( )
{
	$('img.zoomable').each( function( ) {
		if ( !$(this).prev().hasClass( "ZoomableContainer" ) ) return;
		$(this).prev().remove( );
		$(this).css("cursor", "normal").removeAttr("title").removeAttr("alt");
	});
}


var isAnimated = false;
function slideSwitchNext( oSrc  ) {

	if ( ! oSrc ) oSrc = $('#slideshowVO');

	if ( isAnimated ) return;
	
	var $active = $(oSrc).find('IMG.active');

	if ( $active.length == 0 ) $active = $(oSrc).find('IMG:last');

	var $next =  $active.next().length ? $active.next()
		: $(oSrc).find('IMG:first');

	$active.addClass('last-active');
	isAnimated = true;
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 500, function() {
			$active.removeClass('active last-active');
			isAnimated = false;
			setTimeout( "slideSwitchNext()", 3000 );
		});
}

function slideToNext( oSrc ) {
	if ( ! oSrc ) 
	{
		oSrc = $('#slideshowVO');
		if ( ! oSrc.length ) oSrc = $('#slideshow');
	}
	
	var active = null;
	$(oSrc).children("img").each( function(i, val) {
		if ( $(val).attr('src').replace("Large", "Details") == $(LargeImage).attr('src').replace("Large", "Details") )
			active = $(this);
	});
	//active = $("#slideshow IMG[@src='" + LargeImage.src + "']");
	var next =  $(active).next().length ? $(active).next() : $(oSrc).find('IMG:first');
//	$(LargeImage).attr('src', $(next).attr('src').replace("Large", "Details") );
	openLoupe( $(next), true )
}
function slideToPrev( oSrc ) {
	if ( ! oSrc ) 
	{
		oSrc = $('#slideshowVO');
		if ( ! oSrc.length ) oSrc = $('#slideshow');
	}
	
	var active = null;
	$(oSrc).children("img").each( function(i, val) {
		if ( $(val).attr('src').replace("Large", "Details") == $(LargeImage).attr('src').replace("Large", "Details") )
			active = $(this);
	});
	var prev =  $(active).prev().length ? $(active).prev() : $(oSrc).find('IMG:last');
	
	openLoupe( $(prev), true )
	//$(LargeImage).attr('src', $(prev).attr('src').replace("Large", "Details") );
}

function placeLoupe( maxHeight )
{
	if( $('#PhotoVO').length == 0 ) return;
	if ( typeof maxHeight == "undefined" ) maxHeight = $('#PhotoVO').height( );
	var y = $('#PhotoVO').offset( ).top + maxHeight;
	var x = $('#PhotoVO').offset( ).left + $('#PhotoVO').width( );
	y  = (y - 30) + "px";
	x  = (x - 30) + "px";
	$('#Loupe').css('top', y).css('left', x ).show( );
}

function placeLoupeImg( p_oImg )
{
	var maxHeight = $(p_oImg).height( );
	var y = $(p_oImg).offset( ).top + maxHeight;
	var x = $(p_oImg).offset( ).left + $(p_oImg).width( );
	y  = (y - 30) + "px";
	x  = (x - 30) + "px";
	$('#Loupe').css('top', y).css('left', x ).show( );	
}

var LargeImage = null;
function closeLoupe( ) 
{
	SlideisOpen = false;
	$('#mask').hide();
	$(LargeImage).hide().remove();
	$('#SlideshowButtons').hide();
	LargeImage = null;
}


var SlideisOpen = false;
function openLoupe( oSrc, AlreadyOpened )
{
	if( typeof AlreadyOpened == "undefined" || !AlreadyOpened )
	{
		if ( LargeImage != null )  return;
		if ( SlideisOpen )  return;
		if ( ! oSrc ) 
		{
			oSrc = $('#slideshowVO');
			if ( ! oSrc.length ) oSrc = $('#slideshow');
		}
		AlreadyOpened = false;
	}
	SlideisOpen = true;
	if ( ! AlreadyOpened )
		$('#mask').drawAllScreen( ).show( ).fadeTo( 0, 0.9 );
	
	var oImg = new Image();
	oImg.onload = CreateDelegate(oImg, function( ) {
		_openLoupe( oSrc, this.width, this.height, AlreadyOpened );
	});
	
	if ( AlreadyOpened )		oImg.src = $(oSrc).attr('src').replace("Large", "Details");
	else				oImg.src = $(oSrc).find('IMG.active').attr('src').replace("Large", "Details");

	if ( $(oSrc).find('IMG').length <= 1 )
	{
		//$('#SlideShowBt1_Off').show();
		//$('#SlideShowBt3_Off').show();
		//$('#SlideShowBt1').hide();
		//$('#SlideShowBt3').hide();
	}
	else
	{
		//$('#SlideShowBt1_Off').hide();
		//$('#SlideShowBt3_Off').hide();
		//$('#SlideShowBt1').show();
		//$('#SlideShowBt3').show();
	}
	
	$('#SlideShowBt1').show();
	$('#SlideShowBt3').show();
	$('#SlideShowBt1_Off').hide();
	$('#SlideShowBt3_Off').hide();
	//$('span.IEFauxBouttons').show(); // IE
	$('#SlideshowButtons').show().css('zIndex', '1000000');
	//alert("ok");
}

function _openLoupe( oSrc, w, h, AlreadyOpened )
{
	var destW = w;
	var destH = h;
	
	// Force 800x600
	if ( destH > 600 )
	{
		var ratio = destW/destH;
		destH = 600;
		destW = parseInt( ratio * destH );
	}
	if ( destW > 800 )
	{
		var ratio = destH/destW;
		destW = 800;
		destH = parseInt( ratio * destW );
	}

	//alert(w + " by " + h);
	
	var destTop =  ($(window).height() - destH ) / 2 + $(window).scrollTop();
	var destLeft = ($(window).width() - destW ) / 2 + $(window).scrollLeft();
	
	destTop -= $('#SlideshowButtons').height( ) / 2
	
	var curSrc = "";
	if( AlreadyOpened ) 	curSrc = $(oSrc).attr('src').replace("Large", "Details");
	else					curSrc = $(oSrc).find('IMG.active').attr('src').replace("Large", "Details");
	
	var top = parseInt($(oSrc).offset().top)
	var left = parseInt($(oSrc).offset().left)
	
	//alert( "<img src='"+curSrc+"' style='top:"+top+"px;left:"+left+"px;width:10px;height:10px;position:absolute;' />" );
	if ( !AlreadyOpened )
	{
		$('body').append( "<img src='"+curSrc+"' style='top:"+top+"px;left:"+left+"px;position:absolute;' />" );
		//$("<img src='"+curSrc+"' style='top:"+top+"px;left:"+left+"px;position:absolute;' />").insertBefore( $('body').children()[0] );
		LargeImage = $('body').children('img:last-child');
	}
	else
	{
		LargeImage.attr('src', curSrc);
	}
	var oImg = LargeImage
	
	if ( AlreadyOpened )
	{
		$(oImg).show( ).animate({ 
				top: destTop+"px", 
				left: destLeft+"px", 
				width: destW+"px",
				height: destH+"px"
			}, 0, function( ) {

				var x = ($(window).width() - $('#SlideshowButtons').width() ) / 2 + $(window).scrollLeft();
				$('#SlideshowButtons').css( 'top', (destTop+destH)+'px' ).css( 'left', x+'px' ).show( );

			} );
	}
	else
	{
		$( oImg ).bind( 'dragstart', function( event ){ return false; } )
		$( oImg ).bind( 'onselectstart', function( event ){ return false; } )
		$( oImg ).bind( 'onmousedown', function( event ){ return false; } )

		$(oImg).drawOver( $(oSrc) ).show( ).animate({ 
				top: destTop+"px", 
				left: destLeft+"px", 
				width: destW+"px",
				height: destH+"px"
			}, 250, function( ) {

				var x = ($(window).width() - $('#SlideshowButtons').width() ) / 2 + $(window).scrollLeft();
				$('#SlideshowButtons').css( 'top', (destTop+destH)+'px' ).css( 'left', x+'px' ).show( );
				
			} );
	}
}





function openZoom( oSrc )
{
	if ( typeof ADMIN_IsInEditionMode != "undefined" && ADMIN_IsInEditionMode ) return;
	if ( typeof ADMIN_IsInEditionModePresentation != "undefined" && ADMIN_IsInEditionModePresentation ) return;
	
			
	$('span.IEFauxBouttons').hide();	// IE
	$('#SlideShowBt1').hide();
	$('#SlideShowBt2').hide();
	$('#SlideShowBt3').hide();
	$('#SlideShowBt4').hide();
	
	if( typeof AlreadyOpened == "undefined" || !AlreadyOpened )
	{
		if ( LargeImage != null )  return;
		if ( SlideisOpen )  return;
		if ( ! oSrc ) oSrc = $('#slideshowVO');
		AlreadyOpened = false;
	}
	SlideisOpen = true;
	if ( ! AlreadyOpened )
		$('#mask').drawAllScreen( ).show( ).fadeTo( 0, 0.9 ).css('zIndex', '20000');
	
	var oImg = new Image();
	oImg.onload = CreateDelegate(oImg, function( ) {
		_openZoom( oSrc, this.width, this.height, AlreadyOpened );
	});
	
	oImg.src = $($(oSrc)[0].parentNode.nextSibling).attr('zoom_img');
}

function _openZoom( oSrc, w, h, AlreadyOpened )
{
	var destW = w;
	var destH = h;
	
	// Force 800x600
	if ( destH > 600 )
	{
		var ratio = destW/destH;
		destH = 600;
		destW = parseInt( ratio * destH );
	}
	if ( destW > 800 )
	{
		var ratio = destH/destW;
		destW = 800;
		destH = parseInt( ratio * destW );
	}

	//alert(w + " by " + h);
	
	var destTop =  ($(window).height() - destH ) / 2 + $(window).scrollTop();
	var destLeft = ($(window).width() - destW ) / 2 + $(window).scrollLeft();
	
	destTop -= $('#SlideshowButtons').height( ) / 2
	
	var curSrc = "";
	curSrc = $($(oSrc)[0].parentNode.nextSibling).attr('zoom_img');
	
	var top = parseInt($(oSrc).offset().top)
	var left = parseInt($(oSrc).offset().left)
	
	//alert( "<img src='"+curSrc+"' style='top:"+top+"px;left:"+left+"px;width:10px;height:10px;position:absolute;' />" );
	$('body').append( "<img src='"+curSrc+"' style='top:"+top+"px;left:"+left+"px;position:absolute;' />" );
	//$("<img src='"+curSrc+"' style='top:"+top+"px;left:"+left+"px;position:absolute;' />").insertBefore( $('body').children()[0] );
	LargeImage = $('body').children('img:last-child');
	var oImg = LargeImage
	
	$( oImg ).bind( 'dragstart', function( event ){ return false; } )
	$( oImg ).bind( 'onselectstart', function( event ){ return false; } )
	$( oImg ).bind( 'onmousedown', function( event ){ return false; } )
	
	$( oImg );

	/*
	$(oImg).click( function( ) {
		closeLoupe( );
	}).css("cursor","pointer").attr("title","Cliquez pour fermer").attr("alt","Cliquez pour fermer");
	
	$('#mask').click( function( ) {
		closeLoupe( );
	});
	*/

	$(oImg).drawOver( $(oSrc) ).show( ).animate({ 
			top: destTop+"px", 
			left: destLeft+"px", 
			width: destW+"px",
			height: destH+"px"
		}, 250, function( ) {
			
			var x = ($(window).width() - $('#SlideshowButtons').width() ) / 2 + $(window).scrollLeft();
			$('#SlideshowButtons').css( 'top', (destTop+destH)+'px' ).css( 'left', x+'px' ).show( );

			
			var nodeClose = $('body').append( "<img src=\"images/galerie-close.png\" style=\"display:none;cursor:pointer;position:absolute;z-index:9999999999999;top:200;left:200;width:26px;\" />" )[0].lastChild;
			// Patch PNG Close Gallery
			if ( $.browser.msie && parseInt($.browser.version) < 7 ) 
				nodeClose.outerHTML = "<span class='IEFauxBouttons' style=\"" + "width:26px;z-index:9999999999999; height:26px;display:inline-block;position:absolute;cursor:pointer;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/galerie-close.png\', sizingMethod='scale');\"></span>"
			nodeClose = $('body')[0].lastChild;
			
			$(nodeClose).show().css( "left", $(this).offset().left + $(this).width() - ($(nodeClose).width() /2 ) ).css( "top", $(this).offset().top - ($(nodeClose).height() /2 ) ).click( function(){
				closeLoupe(  );
				$(this).remove();
			} );
			
			
		} ).css( "border", "1px solid #FFFFFF" ).css("zIndex", 20001 );
}

















function swapPhoto( oNode )
{
	var oNodeLarge = document.getElementById( "imgLarge" );
	var sUrlPetite = oNode.src;
	var sUrlGrande = oNodeLarge.src;
	
	oNodeLarge.src = sUrlPetite;
	oNode.src = sUrlGrande;
}
				
function CreateDelegate(contextObject, delegateMethod)
{
	return function()
	{
		return delegateMethod.apply(contextObject, arguments);
	}
}

				

