(function($) {  
  
$.fn.extendBox = function (options){ // réglages par défaut
	options = jQuery.extend({
		speed:400, // vitesse d'animation du défilement
		BgColor : '#a92245', // couleur de fond derrière les images (utile si l'opacité est présente);
		opacity : 0.6, // opacité des images non-survolées
		ease : '', // accélération
		baseHei : 100,
		baseWi : 100,
		image : 'Image',
		of : '/'
},options);	
	
	
function viewScroll() {
			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;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};	
	
pageHeight = $('body').height() + parseInt($('body').css('marginTop')) + parseInt($('body').css('marginBottom'));	

$('#ebBgOverlay').remove();	
$('#ebContainer').remove();	

$('body').append('<div id="ebBgOverlay" style="display:none;"></div><div id="ebContainer" style="display:none;"></div>');

var nbImg = this.size();

var ebCount = 0;

var global = this;

var selector = this.selector;


var ebLegend= new Array();
var ebSrc= new Array();

var currentHei = options.baseHei;
var currentWi = options.baseWi;

this.each(function(){
				   

	



var self = $(this);

var thisPos = $(this).prevAll(selector).size();

ebSrc.push($(this).attr('href'));
ebLegend.push($(this).attr('title'));



self.bind('click',function(){			   
openImg(thisPos);						   		   
return false;
});


function openImg(pos){
//alert(ebSrc[pos]+' - '+ebLegend[pos]);	
ebCount = pos+1;

legend = ebLegend[pos];

//position = $(this).offset();

currentPos = viewScroll();
   
imgDest = ebSrc[pos];



$('#ebBgOverlay').css({height:'100%',opacity:options.opacity,backgroundColor:options.BgColor});
$('#ebBgOverlay').show();
$('#ebContainer').show();
$('#ebBgOverlay').bind('click',function(){closeImg();});

if(pos>0){var btPREV = '<div id="ebDivPrev" style="position:absolute; z-index:500; background-color:#FFF; display:none;"></div><a href="javascript:;" id="ebPrevBtn" style="display:none"><span>PREV</span></a>';} else {var btPREV='';}
if(pos<(nbImg-1)){var btNEXT = '<div id="ebDivNext" style="position:absolute; z-index:500; background-color:#FFF; display:none;"></div><a href="javascript:;" id="ebNextBtn" style="display:none"><span>NEXT</span></a>';} else {var btNEXT='';}

$('#ebContainer').html('<div id="ebExtenOverlay"></div><div id="ebImgContent" style="overflow:hidden">'+btPREV+btNEXT+'<img id="eBCurrentImg" src="'+imgDest+'" alt="" style="visibility:hidden"><div id="ebContentText" style="display:none"><a href="javascript:;" id="ebClose"><span>CLOSE</span></a><div id="ebLegend"></div></div></div>');




$('#ebClose').bind('click',function(){closeImg()});

$('#ebLegend').html('<strong>'+legend+'&nbsp;</strong><br />'+options.image+' '+ebCount+' '+options.of+' '+nbImg);

HeiContent = currentHei;
WiContent = currentWi;


HeiBg=HeiContent + parseInt($('#ebImgContent').css('paddingTop')) + parseInt($('#ebImgContent').css('paddingBottom'));
WiBg=WiContent + parseInt($('#ebImgContent').css('paddingLeft')) + parseInt($('#ebImgContent').css('paddingRight'));
marginContent = parseInt((WiContent + parseInt($('#ebImgContent').css('paddingLeft')) + parseInt($('#ebImgContent').css('paddingRight')))/2);
marginBg = parseInt((WiBg + parseInt($('#ebExtenOverlay').css('paddingLeft')) + parseInt($('#ebExtenOverlay').css('paddingRight')))/2);


$('#ebExtenOverlay').css({top:(20+parseInt(currentPos[1]))+'px',opacity:0.7,height:HeiBg+'px',width:WiBg+'px',left:'50%',marginLeft:'-'+marginBg+'px'});
$('#ebImgContent').css({top:(20+parseInt(currentPos[1])+parseInt($('#ebExtenOverlay').css('paddingTop')))+'px',height:currentHei+'px',width:currentWi+'px',left:'50%',marginLeft:'-'+marginContent+'px'});




$('#eBCurrentImg').load(function(){
var imgWi = parseInt($('#eBCurrentImg').attr('width'));
var imgHei = parseInt($('#eBCurrentImg').attr('height'));
$('#eBCurrentImg').hide();
$('#eBCurrentImg').css({visibility:'visible'});


var btNextMargin = parseInt((imgHei/2) - parseInt($('#ebNextBtn').css('height'))/2)+'px 0 0 '+parseInt(imgWi - parseInt($('#ebNextBtn').css('width')))+'px';
$('#ebNextBtn').css({margin:btNextMargin});

var btPrevMargin = parseInt((imgHei/2) - parseInt($('#ebPrevBtn').css('height'))/2)+'px 0 0 0px';
$('#ebPrevBtn').css({margin:btPrevMargin});


$('#ebDivPrev').css({cursor:'pointer',height:imgHei+'px',width:(parseInt(imgWi/2)-15)+'px',opacity:0});
$('#ebDivNext').css({cursor:'pointer',height:imgHei+'px',width:(parseInt(imgWi/2)-15)+'px',marginLeft:parseInt(imgWi - parseInt(imgWi/2)-15)+30+'px',opacity:0});



nextHeiContent = imgHei;
nextWiContent = imgWi;


if(currentHei==imgHei){speedHei = 1;} else {speedHei = options.speed;}
if(currentWi==imgWi){speedWi = 1;} else {speedWi = options.speed;}

currentHei = imgHei;
currentWi = imgWi;

nextHeiBg=nextHeiContent + parseInt($('#ebImgContent').css('paddingTop')) + parseInt($('#ebImgContent').css('paddingBottom'));
nextWiBg=nextWiContent + parseInt($('#ebImgContent').css('paddingLeft')) + parseInt($('#ebImgContent').css('paddingRight'));
nextmarginContent = parseInt((nextWiContent + parseInt($('#ebImgContent').css('paddingLeft')) + parseInt($('#ebImgContent').css('paddingRight')))/2);
nextmarginBg = parseInt((nextWiBg + parseInt($('#ebExtenOverlay').css('paddingLeft')) + parseInt($('#ebExtenOverlay').css('paddingRight')))/2);








$('#ebExtenOverlay').animate({height:nextHeiBg+'px'},speedHei,options.ease,function(){
$(this).animate({width:nextWiBg+'px',marginLeft:'-'+nextmarginBg+'px'},speedHei,options.ease);
});

$('#ebImgContent').animate({height:nextHeiContent+'px'},speedWi,options.ease,function(){
$(this).animate({width:nextWiContent+'px',marginLeft:'-'+nextmarginContent+'px'},speedWi,options.ease,function(){

$('#eBCurrentImg').fadeIn(600,function(){

$('#ebDivPrev').show();
$('#ebDivNext').show();



$('#ebDivPrev').bind('mouseover',function(){
 	$('#ebPrevBtn').show();
});

$('#ebDivPrev').bind('mouseout',function(){
 	$('#ebPrevBtn').hide();
});


$('#ebDivPrev').bind('click',function(){
 	openImg((pos-1));
});

$('#ebDivNext').bind('mouseover',function(){
 	$('#ebNextBtn').show();
});

$('#ebDivNext').bind('mouseout',function(){
 	$('#ebNextBtn').hide();
});


$('#ebDivNext').bind('click',function(){
 	openImg((pos+1));
});




adjustHei = $('#ebContentText').height();
$('#ebExtenOverlay').animate({height:(nextHeiBg+adjustHei)+'px'},300);
$('#ebImgContent').animate({height:(nextHeiContent+adjustHei)+'px'},300);
$('#ebContentText').slideDown(300);

});

});



});
});

}


function closeImg(){
	 $('#ebContainer').hide();	$('#ebBgOverlay').fadeOut(1);
	currentHei = options.baseHei;
	currentWi = options.baseWi;
}



});


}})(jQuery);

	

