/* ------------------------------------------------------------------------
	File containing all the code related to the open lightbox
------------------------------------------------------------------------- */
	var galleryjson;
	gallery = {
		currentPicture : 0,
		totalPictures : 0,
		itemsPerPage : 7,
		currentPage : 0,
		totalPage: 0,
		settings : {
			animationspeed : 5000
		},
		init : function(json){
			slideshow="";
			galleryjson = json;
			gallery.totalPictures = $(json).size();
			gallery.totalPage = Math.ceil(gallery.totalPictures / gallery.itemsPerPage) - 1;
			
			// Update the counter
			$('.features .counter').text((gallery.currentPicture+1) + '/' + gallery.totalPictures);
			
     	var current_pic = json[gallery.currentPicture];
			
			// Update the title/credit
			if(current_pic["caption"] != ""){
				$('.slideshow-nav .photo-info').html('<strong class="icon '+current_pic["type"]+'">'+current_pic["caption"]+'</strong> / crédit: '+current_pic["credits"]);
			}else if (current_pic["credits"]) {
				$('.slideshow-nav .photo-info').html('<span class="icon '+current_pic["type"]+'">crédit: '+current_pic["credits"] + '</span>');
			}else{
        		$('.slideshow-nav .photo-info').html('');
      		}

      if(current_pic['description'])
      $('#slideshow-description').html(current_pic['description']);
      else
      $('#slideshow-description').html(' ');

			// Update the share url
			$('.addthis_button').attr('addthis:url',current_pic["share_url"]);
      
      		if (current_pic['is_wallpaper'])
        	$('.wallpaper_element').show();
      		else
        	$('.wallpaper_element').hide();
			
      		if (current_pic['is_postalcard'])
        	$('.postalcard_element').show();
      		else
        	$('.postalcard_element').hide();
      
			fullscreenPreloader = new Image();
			fullscreenPreloader.onload = function(){
				$('.features').height($('.features .fullscreen').height());
			}
			fullscreenPreloader.src = $('.features .fullscreen').attr('src');
			
			// Set the slideshow nav
			if(gallery.totalPictures == 1){ $('.slideshow-nav .previous,.slideshow-nav .next').addClass('disabled'); }
			
			$('.slideshow-nav .previous').click(function(){
				if(!$(this).hasClass('disabled'))
					gallery.changePicture('previous');
				return false;
			});
			
			$('.slideshow-nav .next').click(function(){
				if(!$(this).hasClass('disabled'))
					gallery.changePicture('next');
				return false;
			});
			
			// Position the images in the image browser
			$('.photos-nav li').each(function(i){
				$(this).css({
					'position':'absolute',
					'left': ($(this).width() + 6) * i
				}).find('a').click(function(){
					gallery.changePicture(i);
					return false;
				});
			});
			
			// Set the image browser nav
			if(gallery.totalPage == 0){ $('.photos-nav .previous:first,.photos-nav .next:first').addClass('disabled'); }
			
			$('.photos-nav .previous:first').click(function(){
				gallery.changePage('previous');
				return false;
			});
			
			$('.photos-nav .next:first').click(function(){
				gallery.changePage('next');
				return false;
			});
			
			// Slideshow nav
			$('.slideshow-nav .pause').click(function(){
				clearInterval(slideshow);
				$('.slideshow-nav .play').removeClass('hide');
				$(this).addClass('hide');
				return false;
			});
			
			$('.slideshow-nav .play').click(function(){
				slideshow = setInterval(function(){ gallery.changePicture('next'); },gallery.settings.animationspeed);
				$('.slideshow-nav .pause').removeClass('hide');
				$(this).addClass('hide');
				return false;
			});
			
			

			var params = {
				wmode:'opaque',
				align: 'middle',
				scale: 'showall',
				menu: 'true',
				allowFullScreen: 'true',
				allowScriptAccess:'always'
			};
			var flashvars = {
				lang: 'fr',
				type: 'document',
				_slug: current_pic.slug,
				resizeToStage: 'true'
			};
			var attributes = {
				id: 'video-player',
				name: 'video-player'
			};
			swfobject.embedSWF("/massif/static/swf/player.swf", 'video-player', '843', '540', "10.0.0","/massif/static/swf/expressInstall.swf",flashvars,params,attributes);
			
			
		},
		changePicture : function(direction){
			if(direction == 'next'){
				gallery.currentPicture ++;
				
				if(gallery.currentPicture > gallery.totalPictures-1){
					gallery.currentPicture = 0;
					gallery.changePage(0);
				}else if(gallery.currentPicture+1 > (gallery.itemsPerPage * (gallery.currentPage+1))){
					gallery.changePage('next');
				}
			}else if(direction == 'previous'){
				gallery.currentPicture --;
				if(gallery.currentPicture < 0){
					gallery.currentPicture = gallery.totalPictures-1;
					gallery.changePage(gallery.totalPage);
				}
				
				// Verify if we need to change the gallery page
				if( gallery.currentPicture+1 < (gallery.itemsPerPage * (gallery.currentPage+1)) && !(gallery.currentPicture+1 > (gallery.itemsPerPage * (gallery.currentPage))) ){
					gallery.changePage('previous');
				}
			}else{
				gallery.currentPicture = direction;
			}
      
      var current_pic = galleryjson[gallery.currentPicture];
			
			$('.features #video-wrapper').html('');
		
      if(current_pic['description'])
      $('#slideshow-description').html(current_pic['description']);
      else
      $('#slideshow-description').html(' ');

	
			if(current_pic.type == 'video')
			{
				$('.features img.fullscreen').remove();
				$('.features').prepend('<div id="video-wrapper"><div id="video-player"></div></div>');
				
				
					var params = {
						wmode:'opaque',
						align: 'middle',
						scale: 'showall',
						menu: 'true',
						allowFullScreen: 'true',
						allowScriptAccess:'sameDomain'
					};
					var flashvars = {
						lang: 'fr',
						type: 'document',
						_slug: current_pic.slug,
						resizeToStage: 'true'
					};
					var attributes = {
						id: 'video-player',
						name: 'video-player'
					};
					swfobject.embedSWF("/massif/static/swf/player.swf", 'video-player', '843', '540', "10.0.0","/massif/static/swf/expressInstall.swf",flashvars,params,attributes);
					// Update du lien download hi res
				
					if(current_pic["hi_res_slug"]){
						console.info(current_pic["hi_res_slug"]);
						$('#download_video_link').html('<a href="'+ current_pic["hi_res_slug"] +'" class="video_download">'+ current_pic["download_label"] +'</a>');
					}else{
						$('#download_video_link').html('');
					}
				
				$('.features img:first').fadeOut(function(){ $(this).remove() });
			}
			else
			{
				// Inject the clone image (for crossfade)
				$('.features').prepend('<img src="'+current_pic["fullscreen"]+'" width="843" height="561" class="fullscreen" alt="" />');
				$('.features img:first').hide().fadeIn();
				$('.features img:eq(1)').fadeOut(function(){ $(this).remove() });
			}
			
			// Update the counter
			$('.features .counter').text((gallery.currentPicture+1) + '/' + gallery.totalPictures);
			
			// Update the title/credit
			if(current_pic["caption"] != ""){
				$('.slideshow-nav .photo-info').html('<strong class="icon '+current_pic["type"]+'">'+current_pic["caption"]+'</strong> / crédit: '+current_pic["credits"]);
			}else if(current_pic["credits"]){
				$('.slideshow-nav .photo-info').html('<span class="icon '+current_pic["type"]+'">crédit: '+current_pic["credits"] + '</span>');
			}else{
        		$('.slideshow-nav .photo-info').html('');
      		}
      
			// Update the wallpaper dropdown
      		if (current_pic['is_wallpaper'])
        	$('.wallpaper_element').show();
      		else
        	$('.wallpaper_element').hide();
			
      		if (current_pic['is_postalcard'])
        	$('.postalcard_element').show();
      		else
        	$('.postalcard_element').hide();
			
			// Select the proper element in the photo nav
			$('.photos-nav li.selected').removeClass('selected');
			$('.photos-nav li:eq('+gallery.currentPicture+')').addClass('selected');
			
			// Update the hastag to resize prettyPhoto
			parent.location.hash = $('.features').width() + '_' + $('body').height();
			
			// Update the share url
			$('.addthis_button').attr('addthis:url',current_pic["share_url"]);
			
			// Update du lien download hi res
			
			if(current_pic["hi_res_slug"]){
				console.info(current_pic["hi_res_slug"]);
				$('#download_video_link').html('<a href="'+ current_pic["hi_res_slug"] +'" class="video_download">'+ current_pic["download_label"] +'</a>');
			}else{
				$('#download_video_link').html('');
			}
			
			
			// Reset the slideshow timer
			if(slideshow){
				clearInterval(slideshow);
				slideshow = setInterval(function(){ gallery.changePicture('next'); },gallery.settings.animationspeed);
			}
		},
		changePage : function(direction) {
			itemWidth = $('.photos-nav li:first').width() + 6;
			pageWidth = gallery.itemsPerPage * itemWidth;
			if(direction=='next'){
				gallery.currentPage ++;
				direction = -1;

				if(gallery.currentPage > gallery.totalPage){
					gallery.currentPage = 0;
				}
			}else if(direction=='previous'){
				gallery.currentPage --;
				direction = 1;
				
				if(gallery.currentPage < 0){
					direction = -1; // Invert the direction to go to the last page
					gallery.currentPage = gallery.totalPage;
				}
			}else{
				desiredpage = direction; // cache the desired page
				if(gallery.currentPage < direction){
					direction = 1;
				}else{
					direction = 1;
				}
				
				gallery.currentPage = desiredpage;
				
			}
			
			// Slide the pages, logic ++
			$('.photos-nav li').each(function(i){
				$(this).animate({
					'left': (i * itemWidth) - (pageWidth * gallery.currentPage)
				});
			});
		},
		toggleInstallation : function(){
			if($('#installation').hasClass('hide')){
				$('#installation').css({
					'overflow' : 'hidden',
					'height' : 1
				})
				.removeClass("hide")
				.animate({height:150},function(){
					// Update the hastag to resize prettyPhoto
					parent.location.hash = $('body').width() + '_' + (parseFloat($('body').height()) + 150);
				});
			}else{
				$('#installation')
				.animate({height:0},function(){
					$(this).addClass('hide');
					
					// Update the hastag to resize prettyPhoto
					parent.location.hash = $('body').width() + '_' + (parseFloat($('body').height()) + 150);
				});
			}
		}
	}

