(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;


    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);


    }
  }
})(jQuery)



function Gallery (slug, target) {

	this.hasGallery = true;

	// count the numbers of images
	var j=0;
	if ($("#numbers_"+slug) != null) {
		
		 $("#numbers_"+slug+" > a").each (function() {
			j++;
		})
	}
	// we don't have a gallery if there is one or zero images
	if (j <= 1) {
		 this.hasGallery = false;
		

		

		// on change le copyright dans tous les cas, également si on n'a qu'une image
		var firstTitle = $("#numbers_"+slug+" > a:first").attr ('title');
		$("#copyright").html (firstTitle);
		// on efface le compteur mais pas le copyright
		$("#numbers_"+slug).html (' ');
		$("#playPause_"+slug).html (" ");
		
		
		
	//	$(".gallery_navigation").html ('&nbsp;');
	}
	
	
	
	this.slug = slug;
	this.target = target;
	
	
	var this_gallery = this;	
	
	this.copyright = $("#copyright");

	
	this.prevBtn = $("#prev_"+slug);
	this.nextBtn = $("#next_"+slug);
	
	this.counter = $("#counter_"+slug);
	this.counter.hide ();
	this.playPauseBtn = $("#playPause_"+slug);

	this.loadingImage = false;
	this.playing = false;
	this.playInterval = null ;
	
	
	this.imgNumber = 0;
	this.crtImage = 1;	
	this.imagesUrl = [];
	
	// public functions
	this.next = Gallery_next;
	this.prev = Gallery_prev;
	this.play = Gallery_play;
	this.pause = Gallery_pause;
	this.toggle = Gallery_toggle;
	this.goto = Gallery_goto;
	this.addImage = Gallery_addImage;
	this.initButtons = Gallery_initButtons;

	// private functions
	
	this._preloadSiblings = Gallery_preloadSiblings;
	this._changeImage = G_changeImage;
	
	
	
		var firstNum = $("#numbers_"+this.slug+" > a");
		this_gallery.copyright.html (firstNum.attr('title'));

	
	// INITIALIZE Gallery
	if (this.hasGallery) {
		this.initButtons ();
		this._preloadSiblings ();
	
	
	
	
	
	
	
	
		this.play ();
	}
	
}


function Gallery_preloadSiblings () {
	var crt = this.crtImage;
	
	var nxt = 0;
	var prv = 0;
	if (crt == 1) 
		prv = this.imgNumber;
	else prv = crt-1;
	
	if (crt == this.imgNumber)
		nxt = 1;
	else nxt = crt +1;
	
	jQuery.preLoadImages(this.imagesUrl [prv-1], this.imagesUrl [nxt-1]);
	// var imagePrv = document.createElement ('img');
	// imagePrv.src = this.imagesUrl [prv];
	// var imageNxt = document.createElement ('img');
	// imageNxt.src = this.imagesUrl [nxt];

	
	
	
}

function G_changeImage (src) {
	// Get image instance.
    //var image = new Image();
	var this_gallery = this;
	
		 $("#numbers_"+this.slug+" > a").each (function() {
			var $btn = $(this);
			$btn.removeClass('selected');
			if (Number ($btn.attr('num')) == this_gallery.crtImage) {
				$btn.addClass ('selected');
				this_gallery.copyright.html ($btn.attr('title'));
			}
		})
		
	
	this.target.stop ();

	this.loadingImage = false;

	var tgt = this.target;
	var imgs = this.imagesUrl;
	var crt = this.crtImage;
	
	this.target.animate({
		opacity: 0
		}, 800, "linear", function () {
			tgt.attr("src", imgs [crt-1]);
			tgt.animate ({opacity:1}, 1600, 'easeInSine');
		});
		
		// opacity: 0
		// 	}, 500, "linear", function () {
		// 		tgt.attr("src", imgs [crt-1]);
		// 		tgt.animate ({opacity:1}, 1200, 'easeInSine');
		// 	});
		
	this._preloadSiblings ();

}

function Gallery_toggle () {
	if (this.playing) {
		this.pause ();
	}
	else  {
		this.next ();
		this.play ();
	}
}

function Gallery_pause () {
	this.playing = false;
	
	this.playPauseBtn.html ('PLAY');
	clearInterval (this.playInterval);
	
}


function Gallery_play () {
	this.playing = true;

	this.playPauseBtn.html ('PAUSE');
	this.playInterval = setInterval ((function(self) {
	  return function() {self.next(); } } )(this), 7000);
	
	
}

function Gallery_next () {

	if (!this.loadingImage) {
	
		var crt = this.crtImage;
		var num = this.imgNumber;
	
		this.loadingImage = true;

	
		// 0. find next image url
		if ((crt +1) > num)
			crt = 1;
		else 
			crt ++;

		var crtIndex = crt - 1;

	
		this.crtImage = crt; 
		this.counter.html ("Images ("+crt+"/"+num+")");
		
		this._changeImage ();	
	}
	return false;

}

function Gallery_prev () {
	if (!this.loadingImage) {
		this.loadingImage = true;
		
		var crt = this.crtImage;
		var num = this.imgNumber;
	
		if ((crt -1) < 1)
			crt = num;
		else 
			crt --;

		var crtIndex = crt - 1;
	
		this.crtImage = crt; 
		this.counter.html ("Images ("+crt+"/"+num+")");
		
		this._changeImage ();
	}
	return false;
}

function Gallery_goto (imgNum) {
	
	this.pause ();
		
	if (!this.loadingImage) {
		this.loadingImage = true;
		
		var crt = this.crtImage;
		this.crtImage = imgNum;
		
		this._changeImage ();
		
		
	}
	
	
}


function Gallery_initButtons () {
	
	// save a reference to this because in the click function, 
	// this refers to the DOM
	// don't forget 'var' !!! 
	// else the variable is not correctyl set in memory and will be global
	var this_gallery = this;	
	
	
	this.playPauseBtn.click (function() {
		this_gallery.toggle ();
	})
	
	this.prevBtn.click (function () {
		this_gallery.pause ();
		this_gallery.prev ();
		return false;
	})
	
	this.nextBtn.click (function () {
		this_gallery.pause ();
		this_gallery.next ();
		return false;
	})
	
	this.target.css ({cursor:"pointer"});
	this.target.click (function () {
		this_gallery.pause ();
		this_gallery.next ();
		return false;
	})
	
	if ($("#numbers_"+this.slug) != null) {
		var i=0;
		 $("#numbers_"+this.slug+" > a").each (function() {
			if (i == 0) {this_gallery.copyright.html ($(this).attr('title'))}
			
			
			$(this).click (function () {
							this_gallery.goto (Number($(this).attr('num')));
							return false;
						});			
				i++;
		})
	}
	
}



function Gallery_addImage (imgUrl) {
	this.imagesUrl.push (imgUrl);
	this.imgNumber ++;
	this.counter.html ("Images ("+this.crtImage +"/"+this.imgNumber+")");
	
	this._preloadSiblings ();

}



jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
return false;
}


