$(window).bind("load", function() {
	$('.vignettes img').fadeIn(1200);
});

$(document).ready(function(){	
	
	/******* Vignettes *******/
	
	$('.vignettes a')
	
	.hover(function(){
		var $vignette = $(this).children('img');
		var c = $(this).parents('div.vignettes').attr('id');		
		var $legende = $('#' + c + ' p.legende');		
		$legende.html($vignette.attr('alt')).fadeIn(300);		
	}, function(){
		$('p.legende').hide();
	})
	
	.click(function(){
		$('object').css('visibility', 'hidden');
		var $a = $(this);
		showImage($a);
		this.blur();
		return false;
	});

});
		
function showImage ($a){
try {
  
  if (document.getElementById('overlay') === null) { appendOverlay(); appendPopup(); firstTime = true; } else { firstTime = false; }

  $('#load').css('display', 'block');
    
  category = $a.parents('div.vignettes').attr('id');
  $categoryLinks = $('#' + category + ' a');
  nbImages = $categoryLinks.length;
  imgSRC = $a.children('img').attr('src').replace(/_s/,'_b');
  legend = $a.children('img').attr('alt') || '';

  Match = imgSRC.match(/_([0-9]+)/);
  currentId = Match[1];

  $firstLink = $categoryLinks.eq(0);
  $prevLink = (currentId > 1) ? $categoryLinks.eq(currentId-2) : $categoryLinks.eq(nbImages-1);
  $nextLink = (currentId < nbImages) ? $categoryLinks.eq(currentId) : $categoryLinks.eq(0);
  $lastLink = $categoryLinks.eq(nbImages-1);
  
  $('#first').click(function(){ go($firstLink); });
  $('#prev').click(function(){ go($prevLink); });
  $('#next').click(function(){ go($nextLink); });
  $('#last').click(function(){ go($lastLink); });
  
  document.onkeydown = function(e){ 	
	if (e == null)	{ keycode = event.keyCode; } // IE
	else 			{ keycode = e.which; }
	if (keycode == 27){ // close
		tb_remove();
	} else if (keycode == 190 || keycode == 37){
		document.onkeydown = '';
		go($prevLink);
	} else if(keycode == 188 || keycode == 39){
		document.onkeydown = '';
		go($nextLink);
	}	
  };


  imgPreloader = new Image();

  /****** Fonction IMG.ONLOAD ******/

  imgPreloader.onload = function(){		

	imgPreloader.onload = null;
				
	var pagesize = tb_getPageSize();
	var x = pagesize[0] - 30;
	var y = pagesize[1] - 76;
	var imageWidth = imgPreloader.width;
	var imageHeight = imgPreloader.height;
	if (imageWidth > x) {
		imageHeight = imageHeight * (x / imageWidth); 
		imageWidth = x; 
	}
	if (imageHeight > y) { 
		imageWidth = imageWidth * (y / imageHeight); 
		imageHeight = y; 
	}

	$('#image').attr({src:imgSRC, width:imageWidth, height:imageHeight, alt:legend});
	$('#legende').html(legend);
	$('#num').html( (currentId<10 ? '0':'') + currentId + '/' + (nbImages<10 ? '0':'') + nbImages );
	
	popupWidth = imageWidth + 30;
	popupHeight = imageHeight + 75;
	
	$('#load').css('display', 'none');
	tb_position();
	
  };

 /****** FIN Fonction IMG.ONLOAD ******/	
 		
  imgPreloader.src = imgSRC;

 }catch(e) {alert(e);}
 
}
  

/************** FUNCTIONS **************/

function appendOverlay() {

  imgLoader = new Image();
  imgLoader.src = "../images/loading.gif";  

  $overlay = $('<div id="overlay"></div>');
  $load = $('<div id="load"><img src="' + imgLoader.src + '" /></div>');

  if (typeof document.body.style.maxHeight === "undefined") {
	$('html, body').css( {width:'100%', height:'100%'} );
	$('html, body').css('overflow', 'hidden');
	var bodyH = document.body.scrollHeight>document.body.offsetHeight ? document.body.scrollHeight:document.body.offsetHeight +'px';
	$('<iframe id="hideSelect"></iframe>').css({height:bodyH, opacity:0}).appendTo('body').click(tb_remove);	
	$overlay.css({position:'absolute', height:bodyH});
	$load.css({position:'absolute'});
  }
  if(tb_detectMacXFF())
	$overlay.css('background', 'url(http://preview.no-mad-online.com/rousset/images/macFFBgHack.png) repeat');
  else
	$overlay.css({backgroundColor:'#000', opacity:0.7});

  $overlay.appendTo('body').click(tb_remove);
  $load.appendTo('body'); 

}

function appendPopup() {  
  legendHTML = '<div id="legende"></div>';
  imgHTML = '<img id="image" title="Fermer" />';
  closeHTML = '<div id="close"><a href="#" title="Clic ou Touche Echap">&nbsp;</a></div>';
  numHTML = '<div id="num"></div>';  	
  firstHTML = '<a id="first" href="javascript:void();" title="D&eacute;but">&nbsp;</a>';
  prevHTML = '<a id="prev" href="javascript:void();" title="Photo pr&eacute;c&eacute;dente">&nbsp;</a>';
  nextHTML = '<a id="next" href="javascript:void();" title="Photo suivante">&nbsp;</a>';
  lastHTML = '<a id="last" href="javascript:void();" title="Fin">&nbsp;</a>';
  buttonsHTML = '<div id="buttons">' + firstHTML + prevHTML + nextHTML + lastHTML + '</div>';
  captionHTML = '<div id="caption">' + numHTML + closeHTML + buttonsHTML + '</div>';
  
  $popup = $('<div id="popup"></div>');
  $popup.html(legendHTML + imgHTML + captionHTML).appendTo('body');
  $('#close, #image').click(tb_remove);
}

function tb_remove() {
	$('object').css('visibility', 'visible');
 	$('#close, #image').unbind('click');
	$('#popup').fadeOut('fast', function(){$('#popup, #overlay, #hideSelect').trigger('unload').unbind().remove();});
	$('#load').remove();
	if (typeof document.body.style.maxHeight == 'undefined') { //if IE6
		$('body','html').css({height:'auto', width:'auto'});
		$('html, body').css('overflow', '');
	}
	document.onkeydown = '';
	document.onkeyup = '';
	return false;
}

function go($link){
	$('#first, #prev, #next, #last').unbind('click').blur();
	//$('#popup').css('display', 'none');
	$('#image, #legende, #caption').css('display', 'none');
	showImage($link);
	return false;
}

function tb_position() {
	marginL = 0 - (popupWidth / 2);
	marginT = 0 - (popupHeight / 2);
 	popupTop = ($('#overlay').height() - popupHeight) / 2;
	popupLeft = ($('#overlay').width() - popupWidth) / 2;

	if(firstTime) {
	  if ( jQuery.browser.msie && jQuery.browser.version < 7 ) { $popup.css({position:'absolute'}); }
	  $('#popup').css({display:'block', width:popupWidth+'px', height:popupHeight+'px', marginTop:marginT+'px', marginLeft:marginL+'px'});
	}else{
		$('#popup').animate({width:popupWidth, height:popupHeight, marginTop:marginT, marginLeft:marginL}, 'fast', function(){
			$('#image, #legende, #caption').fadeIn('slow');
		});	
	}
}

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}