/**
 * jQuery Lightbox
 * Version 0.5 - 11/29/2007
 * @author Warren Krewenki
 *
 * This package is distributed under the BSD license.
 * For full license information, see LICENSE.TXT
 *
 * Based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
 * Originally written to make use of the Prototype framework, and Script.acalo.us, now altered to use jQuery.
 *
 *
 **/

(function($){

	$.fn.lightbox = function(options){
		// build main options
		var opts = $.extend({}, $.fn.lightbox.defaults, options);
        
		return this.each(function(){
			$(this).click(function(){
    		    // initalize the lightbox
    		    initialize();
				start(this);
				return false;
			});
		});
		
	    /**
	     * initalize()
	     *
	     * @return void
	     * @author Warren Krewenki
	     */
	     
	    function initialize() {
		    $('#overlay').remove();
		    $('#lightbox').remove();
		    opts.inprogress = false;
		    
		    // if jsonData, build the imageArray from data provided in JSON format
            if(opts.jsonData && opts.jsonData.length > 0) {
                var parser = opts.jsonDataParser ? opts.jsonDataParser : $.fn.lightbox.parseJsonData;                
                opts.imageArray = [];
                opts.imageArray = parser(opts.jsonData);
	        }
		    
		    var outerImage = '<div id="outerimagecontainer"><div id="imagecontainer"><iframe id="lightboxiframe" ></iframe><img id="lightboximage"></img><div id="hovernav"><a href="javascript://" title="' + opts.strings.prevLinkTitle + '" id="prevlink"></a><a href="javascript://" id="nextlink" title="' + opts.strings.nextLinkTitle + '"></a></div><div id="loading"><a href="javascript://" id="loadinglink"><img src="'+opts.fileLoadingImage+'"></img></a></div></div></div>';
		    var imageData = '<div id="imagedatacontainer" class="clearfix"><div id="imagedata"><div id="imagedetails"><span id="caption"></span><span id="numberdisplay"></span></div><div id="bottomnav">';

		    if (opts.displayHelp)
			    imageData += '<span id="helpdisplay">' + opts.strings.help + '</span>';

		    imageData += '<a href="javascript://" id="bottomnavclose" title="' + opts.strings.closeTitle + '"><img src="'+opts.fileBottomNavCloseImage+'"></img></a></div></div></div>';

		    var string;

		    if (opts.navbarOnTop) {
		      string = '<div id="overlay"></div><div id="lightbox">' + imageData + outerImage + '</div>';
		      $("body").append(string);
		      $("#imagedatacontainer").addClass('ontop');
		    } else {
		      string = '<div id="overlay"></div><div id="lightbox">' + outerImage + imageData + '</div>';
		      $("body").append(string);
		    }

		    $("#overlay").click(function(){ end(); }).hide();
		    $("#lightbox").click(function(){ end();}).hide();
		    $("#loadinglink").click(function(){ end(); return false;});
		    $("#bottomnavclose").click(function(){ end(); return false; });
		    $('#outerimagecontainer').width(opts.widthCurrent).height(opts.heightCurrent);
		    $('#imagedatacontainer').width(opts.widthCurrent);
		
		    if (!opts.imageClickClose) {
        		$("#lightboximage").click(function(){ return false; });
        		$("#hovernav").click(function(){ return false; });
		    }
	    };
	    
	    function getPageSize() {
		    var jqueryPageSize = new Array($(document).width(),$(document).height(), $(window).width(), $(window).height());
		    return jqueryPageSize;
	    };
	    
	    function getPageScroll() {
		    var xScroll, yScroll;

		    if (self.pageYOffset) {
			    yScroll = self.pageYOffset;
			    xScroll = self.pageXOffset;
		    } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
			    yScroll = document.documentElement.scrollTop;
			    xScroll = document.documentElement.scrollLeft;
		    } else if (document.body) {// all other Explorers
			    yScroll = document.body.scrollTop;
			    xScroll = document.body.scrollLeft;
		    }

		    var arrayPageScroll = new Array(xScroll,yScroll);
		    return arrayPageScroll;
	    };
	    
	    function pause(ms) {
		    var date = new Date();
		    var curDate = null;
		    do{curDate = new Date();}
		    while(curDate - date < ms);
	    };
	    
	    function start(imageLink) {
		    $("select, embed, object").hide();
		    var arrayPageSize = getPageSize();
		    $("#overlay").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : opts.overlayOpacity}).fadeIn();
		    imageNum = 0;

		    // if data is not provided by jsonData parameter
            if(!opts.jsonData) {
                opts.imageArray = [];
		        // if image is NOT part of a set..
		        if(!imageLink.rel || (imageLink.rel == '')){
			        // add single image to Lightbox.imageArray
			        opts.imageArray.push(new Array(imageLink.href, opts.displayTitle ? imageLink.title : ''));
		        } else {
		        // if image is part of a set..
			        $("a").each(function(){
				        if(this.href && (this.rel == imageLink.rel)){
					        opts.imageArray.push(new Array(this.href, opts.displayTitle ? this.title : ''));
				        }
			        });
		        }
		    }
		
		    if(opts.imageArray.length > 1) {
		        for(i = 0; i < opts.imageArray.length; i++){
				    for(j = opts.imageArray.length-1; j>i; j--){
					    if(opts.imageArray[i][0] == opts.imageArray[j][0]){
						    opts.imageArray.splice(j,1);
					    }
				    }
			    }
			    while(opts.imageArray[imageNum][0] != imageLink.href) { imageNum++;}
		    }

		    // calculate top and left offset for the lightbox
		    var arrayPageScroll = getPageScroll();
		    var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		    var lightboxLeft = arrayPageScroll[0];
		    $('#lightbox').css({top: lightboxTop+'px', left: lightboxLeft+'px'}).show();


		    if (!opts.slideNavBar)
			    $('#imagedata').hide();

		    changeImage(imageNum);
	    };
	    
	    function changeImage(imageNum) {
		    if(opts.inprogress == false){
			    opts.inprogress = true;
			    opts.activeImage = imageNum;	// update global var

			    // hide elements during transition
			    $('#loading').show();
			    $('#lightboximage').hide();
			    $('#hovernav').hide();
			    $('#prevlink').hide();
			    $('#nextlink').hide();

			    if (opts.slideNavBar) { // delay preloading image until navbar will slide up
				    // $('#imageDataContainer').slideUp(opts.navBarSlideSpeed, $.fn.doChangeImage);
				    $('#imagedatacontainer').hide();
				    $('#imagedata').hide();
				    doChangeImage();
			    } else {
			        doChangeImage();
			    }
		    }
	    };
	    
	    function doChangeImage() {

		    imgPreloader = new Image();

		    // once image is preloaded, resize image container
		    imgPreloader.onload=function(){
		        var newWidth = imgPreloader.width;
		        var newHeight = imgPreloader.height;


			    if (opts.fitToScreen) {
		            var arrayPageSize = getPageSize();
				    var ratio;
				    var initialPageWidth = arrayPageSize[2] - 2 * opts.borderSize;
				    var initialPageHeight = arrayPageSize[3] - 200;

				    if (imgPreloader.height > initialPageHeight)
				    {
					    newWidth = parseInt((initialPageHeight/imgPreloader.height) * imgPreloader.width);
					    newHeight = initialPageHeight;
				    }
				    else if (imgPreloader.width > initialPageWidth)
				    {
					    newHeight = parseInt((initialPageWidth/imgPreloader.width) * imgPreloader.height);
					    newWidth = initialPageWidth;
				    }
			    }

			    $('#lightboximage').attr('src', opts.imageArray[opts.activeImage][0])
							       .width(newWidth).height(newHeight);
			    resizeImageContainer(newWidth, newHeight);
		    };

		    imgPreloader.src = opts.imageArray[opts.activeImage][0];
	    };
	    
	    function end() {
		    disableKeyboardNav();
		    $('#lightbox').hide();
		    $('#overlay').fadeOut();
		    $('select, object, embed').show();
	    };
	    
	    function preloadNeighborImages(){
		    if(opts.loopImages && opts.imageArray.length > 1) {
	            preloadNextImage = new Image();
	            preloadNextImage.src = opts.imageArray[(opts.activeImage == (opts.imageArray.length - 1)) ? 0 : opts.activeImage + 1][0]
	            
	            preloadPrevImage = new Image();
	            preloadPrevImage.src = opts.imageArray[(opts.activeImage == 0) ? (opts.imageArray.length - 1) : opts.activeImage - 1][0]
	        } else {
		        if((opts.imageArray.length - 1) > opts.activeImage){
			        preloadNextImage = new Image();
			        preloadNextImage.src = opts.imageArray[opts.activeImage + 1][0];
		        }
		        if(opts.activeImage > 0){
			        preloadPrevImage = new Image();
			        preloadPrevImage.src = opts.imageArray[opts.activeImage - 1][0];
		        }
	        }
	    };
	    
	    function resizeImageContainer(imgWidth, imgHeight) {
		    // get current width and height
		    opts.widthCurrent = $("#outerimagecontainer").outerWidth();
		    opts.heightCurrent = $("#outerimagecontainer").outerHeight();
            
		    // get new width and height
		    var widthNew = Math.max(350, imgWidth  + (opts.borderSize * 2));
		    var heightNew = (imgHeight  + (opts.borderSize * 2));

		    // scalars based on change from old to new
		    opts.xScale = ( widthNew / opts.widthCurrent) * 100;
		    opts.yScale = ( heightNew / opts.heightCurrent) * 100;

		    // calculate size difference between new and old image, and resize if necessary
		    wDiff = opts.widthCurrent - widthNew;
		    hDiff = opts.heightCurrent - heightNew;

		    $('#imagedatacontainer').animate({width: widthNew},opts.resizeSpeed,'linear');
		    $('#outerimagecontainer').animate({width: widthNew},opts.resizeSpeed,'linear',function(){
			    $('#outerimagecontainer').animate({height: heightNew},opts.resizeSpeed,'linear',function(){
				    showImage();
			    });
		    });

		    // if new and old image are same size and no scaling transition is necessary,
		    // do a quick pause to prevent image flicker.
		    if((hDiff == 0) && (wDiff == 0)){
			    if (jQuery.browser.msie){ pause(250); } else { pause(100);}
		    }

		    $('#prevlink').height(imgHeight);
		    $('#nextlink').height(imgHeight);
	    };
	    
	    function showImage() {
		    $('#loading').hide();
		    $('#lightboximage').fadeIn("fast");
		    updateDetails();
		    preloadNeighborImages();

		    opts.inprogress = false;
	    };
	    
	    function updateDetails() {

		    $('#numberdisplay').html('');

		    if(opts.imageArray[opts.activeImage][1]){
			    $('#caption').html(opts.imageArray[opts.activeImage][1]).show();
		    }

		    // if image is part of set display 'Image x of x'
		    if(opts.imageArray.length > 1){
			    var nav_html;

			    nav_html = opts.strings.image + (opts.activeImage + 1) + opts.strings.of + opts.imageArray.length;

			    if (!opts.disableNavbarLinks) {
                    // display previous / next text links
                    if ((opts.activeImage) > 0 || opts.loopImages) {
                      nav_html = '<a title="' + opts.strings.prevLinkTitle + '" href="#" id="prevlinktext">' + opts.strings.prevLinkText + "</a>" + nav_html;
                    }

                    if (((opts.activeImage + 1) < opts.imageArray.length) || opts.loopImages) {
                      nav_html += '<a title="' + opts.strings.nextLinkTitle + '" href="#" id="nextlinktext">' + opts.strings.nextLinkText + "</a>";
                    }
                }

			    $('#numberdisplay').html(nav_html).show();
		    }

		    if (opts.slideNavBar) {
		        $("#imagedata").slideDown(opts.navBarSlideSpeed);
		    } else {
			    $("#imagedata").show();
		    }

		    var arrayPageSize = getPageSize();
		    $('#overlay').height(arrayPageSize[1]);
		    updateNav();
	    };
	    
	    function updateNav() {
		    if(opts.imageArray.length > 1){
			    $('#hovernav').show();
                
                // if loopImages is true, always show next and prev image buttons 
                if(opts.loopImages) {
		            $('#prevlink,#prevlinktext').show().click(function(){
			            changeImage((opts.activeImage == 0) ? (opts.imageArray.length - 1) : opts.activeImage - 1); return false;
		            });
		            
		            $('#nextlink,#nextlinktext').show().click(function(){
			            changeImage((opts.activeImage == (opts.imageArray.length - 1)) ? 0 : opts.activeImage + 1); return false;
		            });
		        
		        } else {
			        // if not first image in set, display prev image button
			        if(opts.activeImage != 0){
				        $('#prevlink,#prevlinktext').show().click(function(){
					        changeImage(opts.activeImage - 1); return false;
				        });
			        }

			        // if not last image in set, display next image button
			        if(opts.activeImage != (opts.imageArray.length - 1)){
				        $('#nextlink,#nextlinktext').show().click(function(){

					        changeImage(opts.activeImage +1); return false;
				        });
			        }
                }
                
			    enableKeyboardNav();
		    }
	    };
	    
	    function keyboardAction(e) {
            var o = e.data.opts
		    var keycode = e.keyCode;
		    var escapeKey = 27;
            
		    var key = String.fromCharCode(keycode).toLowerCase();
            
		    if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)){ // close lightbox
			    end();
		    } else if((key == 'p') || (keycode == 37)){ // display previous image
		        if(o.loopImages) {
		            disableKeyboardNav();
		            changeImage((o.activeImage == 0) ? (o.imageArray.length - 1) : o.activeImage - 1);
		        } 
		        else if(o.activeImage != 0){
				    disableKeyboardNav();
				    changeImage(o.activeImage - 1);
			    }
		    } else if((key == 'n') || (keycode == 39)){ // display next image
		        if (opts.loopImages) {
		            disableKeyboardNav();
		            changeImage((o.activeImage == (o.imageArray.length - 1)) ? 0 : o.activeImage + 1);
		        }
			    else if(o.activeImage != (o.imageArray.length - 1)){
				    disableKeyboardNav();
				    changeImage(o.activeImage + 1);
			    }
		    }
	    };
	    
	    function enableKeyboardNav() {
		    $(document).bind('keydown', {opts: opts}, keyboardAction);
	    };

	    function disableKeyboardNav() {
		    $(document).unbind('keydown');
	    };
	    
	};
    
    $.fn.lightbox.parseJsonData = function(data) {
        var imageArray = [];
        
        $.each(data, function(){
            imageArray.push(new Array(this.url, this.title));
        });
        
        return imageArray;
    };

	$.fn.lightbox.defaults = {
		fileLoadingImage : '../../phototheque/loading.gif',
		fileBottomNavCloseImage : '../../phototheque/closelabel.gif',
		overlayOpacity : 0.8,
		borderSize : 10,
		imageArray : new Array,
		activeImage : null,
		inprogress : false,
		resizeSpeed : 350,
		widthCurrent: 250,
		heightCurrent: 250,
		xScale : 1,
		yScale : 1,
		displayTitle: true,
		navbarOnTop: false,
		slideNavBar: false, // slide nav bar up/down between image resizing transitions
		navBarSlideSpeed: 350,
		displayHelp: false,
		strings : {
			help: ' \u2190 / P - previous image\u00a0\u00a0\u00a0\u00a0\u2192 / N - next image\u00a0\u00a0\u00a0\u00a0ESC / X - close image gallery',
			prevLinkTitle: ' Image Précédente ',
			nextLinkTitle: ' Image suivante ',
			prevLinkText:  ' Précédent ',
			nextLinkText:  ' Suivant ',
			closeTitle: ' Fermer ',
			image: 'Image ',
			of: ' of '
		},
		fitToScreen: false,		// resize images if they are bigger than window
        disableNavbarLinks: false,
        loopImages: false,
        imageClickClose: true,
        jsonData: null,
        jsonDataParser: null
	};
	
})(jQuery);

		$(document).ready(function(){
		    base_url = document.location.href.substring(0, document.location.href.indexOf('index.html'), 0);
	    
			$(".lightbox").lightbox({
			    fitToScreen: true,
			    imageClickClose: false
		    });
			
			$(".lightbox-2").lightbox({
			    fitToScreen: true
		    });
	    
		});
		
//  6 Onglets slidetoggle  down to up
		
	$(document).ready(function() {
	
		var ongletpoSlider1 = $("#ongletslider1").position();

		$(".onglettopMenuAction1").click( function() {
			if ($("#ongletopenClose1").is(":visible")) {
				$("#ongletslider1").animate({ top: "1px" }, 500 );
				$("#ongletopenClose1").hide();
			} else {
				$("#ongletslider1").animate({ top: + ongletpoSlider1.top }, 500 );
				$("#ongletopenClose1").show();
			}
		});
		$("#ongletslider1").mouseleave(function(){
				$("#ongletslider1").animate({ top: + ongletpoSlider1.top }, 500 );
				$("#ongletopenClose1").show();
			});
			
	// onglet 2

		var ongletpoSlider2 = $("#ongletslider2").position();

		$(".onglettopMenuAction2").click( function() {
			if ($("#ongletopenClose2").is(":visible")) {
				$("#ongletslider2").animate({ top: "1px" }, 500 );
				$("#ongletopenClose2").hide();
			} else {
				$("#ongletslider2").animate({ top: + ongletpoSlider2.top }, 500 );
				$("#ongletopenClose2").show();
			}
		});
		$("#ongletslider2").mouseleave(function(){
				$("#ongletslider2").animate({ top: + ongletpoSlider2.top }, 500 );
				$("#ongletopenClose2").show();
			});	
	// onglet 3

		var ongletpoSlider3 = $("#ongletslider3").position();

		$(".onglettopMenuAction3").click( function() {
			if ($("#ongletopenClose3").is(":visible")) {
				$("#ongletslider3").animate({ top: "1px" }, 500 );
				$("#ongletopenClose3").hide();
			} else {
				$("#ongletslider3").animate({ top: + ongletpoSlider3.top }, 500 );
				$("#ongletopenClose3").show();
			}
		});
		$("#ongletslider3").mouseleave(function(){
				$("#ongletslider3").animate({ top: + ongletpoSlider3.top }, 500 );
				$("#ongletopenClose3").show();
			});	
	// onglet 4

		var ongletpoSlider4 = $("#ongletslider4").position();

		$(".onglettopMenuAction4").click( function() {
			if ($("#ongletopenClose4").is(":visible")) {
				$("#ongletslider4").animate({ top: "1px" }, 500 );
				$("#ongletopenClose4").hide();
			} else {
				$("#ongletslider4").animate({ top: + ongletpoSlider4.top }, 500 );
				$("#ongletopenClose4").show();
			}
		});
		$("#ongletslider4").mouseleave(function(){
				$("#ongletslider4").animate({ top: + ongletpoSlider4.top }, 500 );
				$("#ongletopenClose4").show();
			});	
	// onglet 5

		var ongletpoSlider5 = $("#ongletslider5").position();

		$(".onglettopMenuAction5").click( function() {
			if ($("#ongletopenClose5").is(":visible")) {
				$("#ongletslider5").animate({ top: "1px" }, 500 );
				$("#ongletopenClose5").hide();
			} else {
				$("#ongletslider5").animate({ top: + ongletpoSlider5.top }, 500 );
				$("#ongletopenClose5").show();
			}
		});
		$("#ongletslider5").mouseleave(function(){
				$("#ongletslider5").animate({ top: + ongletpoSlider5.top }, 500 );
				$("#ongletopenClose5").show();
			});	
	// onglet 6

		var ongletpoSlider6 = $("#ongletslider6").position();

		$(".onglettopMenuAction6").click( function() {
			if ($("#ongletopenClose6").is(":visible")) {
				$("#ongletslider6").animate({ top: "1px" }, 500 );
				$("#ongletopenClose6").hide();
			} else {
				$("#ongletslider6").animate({ top: + ongletpoSlider6.top }, 500 );
				$("#ongletopenClose6").show();
			}
		});
		$("#ongletslider6").mouseleave(function(){
				$("#ongletslider6").animate({ top: + ongletpoSlider6.top }, 500 );
				$("#ongletopenClose6").show();
			});	
	
	});
			

//  3 Onglets slidetoggle  down to up
		
	$(document).ready(function() {
	
		var poSlider1 = $("#slider1").position();

		$(".topMenuAction1").click( function() {
			if ($("#openClose1").is(":visible")) {
				$("#slider1").animate({ top: "1px" }, 500 );
				$("#openClose1").hide();
				$("#sliderWrap").css("z-index",25);

			} else {
				$("#slider1").animate({ top: + poSlider1.top }, 500 );
				$("#openClose1").show();
			}
		});
		$("#slider1").mouseleave(function(){
				$("#slider1").animate({ top: + poSlider1.top }, 500 );
				$("#openClose1").show();
				$("#sliderWrap").css("z-index",25);
			});
			
		// onglet 2	
		var poSlider2 = $("#slider2").position();

		$(".topMenuAction2").click( function() {
			if ($("#openClose2").is(":visible")) {
				$("#slider2").animate({ top: "1px" }, 500 );
				$("#openClose2").hide();
				$("#sliderWrap").css("z-index",25);
			} else {
				$("#slider2").animate({ top: + poSlider2.top }, 500 );
				$("#openClose2").show();
			}
		});
		$("#slider2").mouseleave(function(){
				$("#slider2").animate({ top: + poSlider2.top }, 500 );
				$("#openClose2").show();
				$("#sliderWrap").css("z-index",25);
			});
			
			//onglet 3
		var poSlider3 = $("#slider3").position();

		$(".topMenuAction3").click( function() {
			if ($("#openClose3").is(":visible")) {
				$("#slider3").animate({ top: "1px" }, 500 );
				$("#openClose3").hide();
				$("#sliderWrap").css("z-index",25);
			} else {
				$("#slider3").animate({ top: + poSlider3.top }, 500 );
				$("#openClose3").show();
			}
		});
		$("#slider3").mouseleave(function(){
				$("#slider3").animate({ top: + poSlider3.top }, 500 );
				$("#openClose3").show();
				$("#sliderWrap").css("z-index",25);
			});

	});
		

//  onglet slide  down
		
	$(document).ready(function() {
	
		var poSlider4 = $("#slider4").height() + 15;

		$(".topMenuAction4").click( function() {
			if ($("#openClose4").is(":hidden")) {
				$("#openCloseWrap4").delay(100).fadeOut(200);
				$("#sliderMenu4").animate({ height: "45px" }, 1200 );
				$("#openClose4").show();
				
			} else {
				$("#sliderMenu4").animate({ height: + poSlider4 }, 500 );
				$("#openCloseWrap4").css("display","none");
				$("#openCloseWrap4").delay(300).fadeIn(300, function () {
				$("#openClose4").hide();
				$("#sliderMenu4").css("z-index",15);
				if(jQuery.browser.msie)	this.style.removeAttribute('filter');  // filtre supprimé pour IE
				});
			}
		});
				
	});
	
	

	
	$(document).ready(function(){
	
		var poSlidermenu = $("#menu-panel").height() - 37;
	
		$("#sub-panel").click(function(){
		if ($("#openClosemenu").is(":hidden")) {
				$("#top-panel").animate({ height: "7px" }, 500 );
				$("#openClosemenu").show();
			} else {
				$("#top-panel").animate({ height: + poSlidermenu }, 500 );
				$("#openClosemenu").hide();
			}
			
		}); 
	}); 
	
		// mouse over image
	
	$(document).ready(function(){
	
		$(".photo-roll").hover(function(){
			$(this).find("img.roll").fadeOut(500);
		}, function(){
			$(this).find("img.roll").fadeIn(500);
		})		
	
	});
	


