/*
	functions.gallery.js
	Photo Gallery Functions
	Created: April 5, 2010
	Creator: Matt Kircher, Mainline Media LLC
	
	FRENCH SYMBOL TABLE
	&aacute; = \u00E1	&agrave; = \u00E0	&eacute; = \u00E9
	&egrave; = \u00E8	&iacute; = \u00ED	&igrave; = \u00EC
	&oacute; = \u00F3	&ograve; = \u00F2	&uacute; = \u00FA
	&ugrave; = \u00F9	&ccedil; = \u00E7	
	
	&Aacute; = \u00C1	&Agrave; = \u00C0	&Eacute; = \u00C9
	&Egrave; = \u00C8	&Iacute; = \u00CD	&Igrave; = \u00CC
	&Oacute; = \u00D3	&Ograve; = \u00D2	&Uacute; = \u00DA
	&Ugrave; = \u00D9	&Ccedil; = \u00C7
	
	[ http://www1.tip.nl/~t876506/utf8tbl.html ]
*/

function getPageLanguage(){
	return ($('.english').length == 1)?"EN":"FR";
}

/* GALLERY */
function getGallery(target, type, setId, tags, func){
	
	var apiKey 	= 'a63818542d6066acbb39a493cd35841b';		//MB Flickr API key
	var userId 	= '40529603@N05';					//MB Flickr ID
	var set 	= (setId != null)?setId:'72157621598599724'; 	//VMB set ID
	
	var targetElement = (target != null)?target:'#main-content #gallery-images';
	
	$(targetElement)
	.flickr({
		api_key: apiKey,
		type: type,
		user_id: userId,
		photoset_id: set,
		tags: tags,
		tag_mode: 'any',
		per_page: 100,
		page: 1,
		callback: func
	});
}

//initialize gallery
function initGallery(){
	
	$('#gallery-display').append('<ul class="clearfix"></ul>');
	$('#gallery-display ul').css({ opacity:0 });
	
	var accumulated_ul_width_display = 0;	
	$('#gallery-images li a').each(function(index){
		
		var title = $(this).attr('title');
		
		//parse title for each language (uses the Flickr title with a '|' separator)
		var title_array = title.split('|');			
		if($('body > div.english').length || title_array.length == 1){
			$(this).attr('title', $.trim(title_array[0]));
		} else {
			$(this).attr('title', $.trim(title_array[1]));
		}
		
		//effects
		$(this).bind('click', {'index':index}, function(){
			$this = $(this);
			
			//append large image
			var href = $(this).attr('href');
			var title = $(this).attr('title');
			
			var newImage = new Image();
			newImage.onload = function(){
				
				//hide container
				$('#gallery-display ul').animate({ opacity:0, left:"-10px" }, 200, function(){
					
					//empty container, place new image
					$('#gallery-display ul')
					.css({ left:"10px" }).empty()
					.append('<li class="clearfix"><div class="image_wrapper"></div></li>');
					$('#gallery-display li:last .image_wrapper').append('<img class="overlay" src="../images/gallery/gallery_shadow_overlay.png" />');
					
					//if IE8... else set width/height
					if($.support.boxModel && !$.support.objectAll){
						$('#gallery-display li:last .image_wrapper').append('<img class="photo" src="'+href+'" alt="'+title+'" />');
					} else {
						$('#gallery-display li:last .image_wrapper').append('<img class="photo" width="'+this.width+'" height="'+this.height+'" src="'+href+'" alt="'+title+'" />');
					}
					
					//append underlay, shadow
					$('#gallery-display li:last .image_wrapper').append('<img class="underlay" src="../images/gallery/gallery_image_backdrop.png" />');
					$('#gallery-display li:last .image_wrapper').append('<span class="title">'+title+'</span><img class="shadow" src="../images/gallery/gallery_image_shadow.png" />');
								
					//adjust heights / widths
					$('#gallery-display li:last img.underlay')
						.height($('#gallery-display li:last img.photo').height())
						.width($('#gallery-display li:last img.photo').width());
					$('#gallery-display li:last img.photo').width($('#gallery-display li:last img.underlay').width() - 50);
					$('#gallery-display li:last img.photo').height($('#gallery-display li:last img.underlay').height() - 50);
					$('#gallery-display li:last img.overlay')
						.height($('#gallery-display li:last img.photo').height())
						.width($('#gallery-display li:last img.photo').width());
					
					accumulated_ul_width_display += $('#gallery-display li:last').width();
					
					//image_wrapper
					$('#gallery-display li:last .image_wrapper')
						.width($('#gallery-display li:last img.underlay').width())
						.css({ paddingTop:eval((500 - $('#gallery-display li:last img.underlay').height()) / 2) })
					$('#gallery-display li:last .image_wrapper img.shadow')
						.width($('#gallery-display li:last img.underlay').width())
						.css({ top:eval($('#gallery-display li:last img.underlay').height()+20)+"px"  });
						
					$('#gallery-display li:last .image_wrapper span.title')
						.css({ top:eval($('#gallery-display li:last img.underlay').height())+"px"  });
											
					//position
					$('#gallery-display li:last img.photo, #gallery-display li:last img.overlay').css({ 
						marginRight:"-"+eval($('#gallery-display li:last img.photo').width())+"px",
						top:eval(Math.round(($('#gallery-display li:last img.underlay').height() - $('#gallery-display li:last img.photo').height()) / 2))+"px",
						left:eval(Math.round(($('#gallery-display li:last img.underlay').width() - $('#gallery-display li:last img.photo').width()) / 2))+"px"
					});
					
					//animate
					$('#gallery-display ul')
					.animate({ opacity:1, left:"0px" }, 400);
				});
			}
			newImage.src = href;
			
			$('#gallery-images a').removeClass('selected');
			$(this).addClass('selected');
			
			if(index > 4){
				$('#gallery-images ul').stop()
				.animate({ left:eval((-index * 78) + 390)+"px" }, 700)
			}
			return false;
		});
	});
	$('#gallery-display').height(550).find('ul').width(accumulated_ul_width_display);
	$('#gallery-images ul').width(78*$('#gallery-images ul li').length);
	$("#gallery-display ul").animate({ opacity:1 }, 700);
	$('#gallery-images ul li a:eq(0)').trigger('click');
}



/* MINI GALLERY */
function formatMiniGallery(){
	
	//no IE support for corners, or thmbnail shadow right now...
	if($.support.leadingWhitespace){
		$('.mini_gallery').corners('10px'); //make corners if not IE
	} else {
		$('.mini_gallery .photos li').css('background', 'none'); //remove shadows if IE
	}
	
	$(".mini_gallery").each(function(){
		
		$(this).find('.photos, .photos ul, .photos li').addClass('clearfix');
		var open_txt  = (getPageLanguage() == "EN")?'Click to open...':'cliquez ici...';
				
		if(!$(this).find('.photo_count').length){
			$(this)
			.prepend('<div class="top"></div>')
			.append('<div class="bottom"></div>')
			.find('.title')
			.wrapInner('<span class="photo_count"></span>')
			.append('<span class="photo_click_open">'+open_txt+'</span>')
			.wrapInner('<a href="#"></a>');
			
			$(this).find('.title a').bind('click', function(){
				$(this).toggleClass('open').blur();
				var open_txt  = (getPageLanguage() == "EN")?'Click to close...':'fermer...';
				var close_txt = (getPageLanguage() == "EN")?'Click to open...':'cliquez ici...';
				
				if($(this).hasClass('open')){
					$(this).parent().parent().find('.photos').css({ opacity:0 }).slideDown(400, function(){ $(this).animate({ opacity:1 }, 400); });
					$(this).find('.photo_click_open').text(open_txt);
				} else {
					$(this).parent().parent().find('.photos').css({ opacity:1 }).animate({ opacity:1 }, 400, function(){ $(this).slideUp(400); });
					$(this).find('.photo_click_open').text(close_txt);
				}
				
				return false;
			});
		}
	});
	
	
	//parse title for each language (uses the Flickr title with a '|' separator)
	
	$(".mini_gallery .photos a").each(function(){
							 
		var title = $(this).find('img').attr('alt');
		var title_array = title.split('|');
		
		if(getPageLanguage() == "EN" || title_array.length == 1){
			$(this).attr('title', $.trim(title_array[0]));
			$(this).find('img').attr('alt', $.trim(title_array[0]));
		} else {
			$(this).attr('title', $.trim(title_array[1]));
			$(this).find('img').attr('alt', $.trim(title_array[1]));
		}
	
		$(this).attr('rel','[photo]');
	}).colorbox();												     
}