	
// Diaporama Class /////////////////////////////////////////////////////////////

function FlickrDiaporama(id, diapoList, ars, arb) {
	
	// VARS
	this.ID = id;
	this.currentPosition = 0;
	this.Pause = false;
	this.First = true;
	this.crossFade = true;
	this.Speed = 7000;
	this.DiapoList = diapoList;
	this.DiapoImages = new Array();
	this.numberOfDiapos = 0;
	this.timeoutID = {};
	this.arrowSize = (ars!=null ? ars : 24);
	this.showArrows = (arb!=null ? arb : false);
	
	
	// METHODS
	this.manageControls = function(position) {
		var self = this;
		if (position==0) {$('#rightDiapoControl'+self.ID).css('opacity',.25).data('active',false)} else {$('#rightDiapoControl'+self.ID).css('opacity',1).data('active',true)}
		if (position==this.numberOfDiapos-1) {$('#leftDiapoControl'+self.ID).css('opacity',.25).data('active',false)} else {$('#leftDiapoControl'+self.ID).css('opacity',1).data('active',true)}
	}

	this.fade = function() {
		var self = this;
		this.manageControls(this.currentPosition);
		var nextHtml = (this.DiapoList[this.currentPosition]['link']==undefined ? "" : "<a href='"+this.DiapoList[this.currentPosition]['link']+"' "+(this.DiapoList[this.currentPosition]['imglightboxlink']==undefined ? "" : "class='imgLightboxLink'")+">")+"<img src='"+this.DiapoList[this.currentPosition]['file']+"' id='diapoImage"+this.ID+"' title='"+(this.DiapoList[this.currentPosition]['title']==undefined ? "" : this.DiapoList[this.currentPosition]['title'])+"' />"+(this.DiapoList[this.currentPosition]['link']==undefined ? "" : "</a>");
		$("#diapo"+self.ID)
							.css({opacity:0})
							.html(nextHtml);
		if (self.DiapoList[self.currentPosition]['imglightboxlink'] != undefined)
	    	bindImgLightboxLinks("#diapo"+self.ID);
		$("#diapo"+self.ID).animate({opacity:1}, 750, "linear", function(){});
	};
	
	this.diaporama = function() {
		if (this.First) {
		
		    if (!this.First) {
		        if (this.currentPosition == this.numberOfDiapos-1)
		          this.currentPosition = 0;
		        else
		          this.currentPosition += 1;
		    }
		    
		    this.fade();
		    
		    this.First = false;
		    
		    var self = this;
		    if (!this.Pause)
		        this.timeoutID = setTimeout(function(){self.diaporama();}, self.Speed);
		        
		}
	};
	
	
	// BINDS & CO
	
	// Insert controls in the DOM
	this.bindDiapoControls = function() {
		var self = this;
		
		$('#diaporama'+self.ID)
	    	.prepend('<div class="diapoControl'+self.ID+'" id="leftDiapoControl'+self.ID+'"></div>')
	    	.append('<div class="diapoControl'+self.ID+'" id="rightDiapoControl'+self.ID+'"></div>'); // pas de <a> pour éviter les pointillés autour générés par les browsers qd on clique
		$('#leftDiapoControl'+self.ID).bind('mouseenter mouseover', function(){$(this).css('background-position',-self.arrowSize+'px 0px')});
		$('#leftDiapoControl'+self.ID).bind('mouseleave mouseout', function(){$(this).css('background-position','0px 0px')});
		$('#rightDiapoControl'+self.ID).bind('mouseenter mouseover', function(){$(this).css('background-position','0px 0px')});
		$('#rightDiapoControl'+self.ID).bind('mouseleave mouseout', function(){$(this).css('background-position',-self.arrowSize+'px 0px')});
	  
		// Create event listeners for #diaporama rollovers
		$('#diaporama'+self.ID)
	    	.bind('mouseenter mouseover', function() {
	        	clearTimeout(self.timeoutID);
	        	self.Pause = true;
				$('.diapoControl'+self.ID).show();
	    	});
		$('#diaporama'+self.ID)
			.bind('mouseleave mouseout', function() {
				clearTimeout(self.timeoutID);
				self.Pause = false;
				self.timeoutID = setTimeout(function(){self.diaporama();}, 1500); // on attend 1500ms avant de renvoyer le diaporama
				if (!self.showArrows)
			        $('.diapoControl'+self.ID).hide();
	    	});
	
		// Create event listeners for .control clicks
		$('.diapoControl'+self.ID)
			.bind('click', function(){
				if (!$(this).data('active')) return;
				self.currentPosition += ($(this).attr('id')=='rightDiapoControl'+self.ID ? -1 : 1);
				self.fade();
			});
			
		// setup control visibility at start
    	if (this.showArrows)
    		$('.diapoControl'+self.ID).show();
    	else
    		$('.diapoControl'+self.ID).hide();
	};
	
	
	// LAUNCHING
	
	// Loading diapos
	this.loadDiapos = function() {
		this.numberOfDiapos = this.DiapoList.length;
		for (i = 0; i < this.DiapoList.length; i++) { 
		    this.DiapoImages[i] = new Image();
		    this.DiapoImages[i].src = this.DiapoList[i]['file'];
		}
	};
	
	// Launching diaporama
	this.launchDiaporama = function(crossFade) {
		var self = this;
		self.crossFade = crossFade;
		var full = 0;
		for (i = 0; i < this.numberOfDiapos; i++) { 
		    if (this.DiapoImages[i].complete)
		        full++;
		}
		if (full == this.numberOfDiapos) { // Quand c'est prêt on envoie la sauce
			$('#loadingDiaporama'+self.ID)
		    	.animate({'opacity' : 0}, 1000, 'linear', function(){
					$(this).remove();
				});
			this.manageControls(this.currentPosition);
			this.diaporama();
		} else {
			var self = this;
		    setTimeout(function(){self.launchDiaporama();}, 100);
		}
	};
}

