var gallery = function(params)
{
	this.id = params.id;
	this.data = params.data;
	this.rootURL = params.rootURL;
	this.numixModal = null;
	
	/* DOM - html reference */
	this.mediaHolder = this.id.find('.numix-data-holder');
	this.media = this.mediaHolder.children('img');


	this.titleHolder = this.id.find('.numix-gallery-title');
	this.dateHolder = this.id.find('.numix-gallery-date');
	this.legendHolder = this.id.find('.numix-legend');
	//this.galleryCountHolder = this.id.find('.numix-gallery-count');
	this.pictureCountHolder = this.id.find('.numix-picture-count');
	this.controls = {
		picture : {
			prev : $(this.id).find('a[rel=numix-prev-picture]'),
			next : $(this.id).find('a[rel=numix-next-picture]')
		},
		gallery : {
			toggleGal : $(this.id).find('a[rel=numix-toggle-gallery]'),
			close : $(this.id).find('a[rel=numix-close-gallery]'),
			open : $('a[rel^=numix-open-gallery]')
		}
	};
	
	this.currentGallery = 0;
	this.currentPicture = 0;
	
	this.galleriesCount = this.data.length;
	this.picturesCount = this.data[this.currentGallery]['_documents'].length;
	
	/* If only 1 gallery, no toggle button */
	if(this.galleriesCount <= 1) this.controls.gallery.toggleGal.css({display: 'none'});
	
	var that = this;
	
	this.controls.picture.prev.bind('click', function(){ return that.prevPicture() });
	this.controls.picture.next.bind('click', function(){ return that.nextPicture() });
	this.controls.gallery.toggleGal.bind('click', function(){ return that.navGallery(this) });

	this.media.bind('click', function(){ return that.nextPicture() });
	
	if(params.modal)
	{
		this.numixModal = new modal({ gallery:this.id, rootURL: that.rootURL });
		this.controls.gallery.close.bind('click', function(){ return that.numixModal.close() });
		this.controls.gallery.open.bind('click', function(){ return that.openModal(this) });
	};
	
	// bind mousewheel event
	this.mediaHolder.bind('mousewheel', function(e, delta) {
		e.preventDefault();
		if (delta > 0) {
			that.controls.picture.prev.trigger('click');
		} else {
			that.controls.picture.next.trigger('click');
		}
	});
	
	//that.display();

};

gallery.prototype.display = function()
{
	var that = this;
	
	/* Title */
	this.titleHolder.html(that.data[that.currentGallery]['title']);
	this.dateHolder.html(that.data[that.currentGallery]['subtitle']);
	
	/* Legend */
	this.legendHolder.html(that.data[that.currentGallery]['_documents'][that.currentPicture]['caption']);
	
	/* Gallery Counter */
	//this.galleryCountHolder.html((that.currentGallery + 1) + ' de ' + that.galleriesCount);
	
	/* Picture Counter */
	this.picturesCount = this.data[this.currentGallery]['_documents'].length;
	this.pictureCountHolder.html((that.currentPicture + 1) + ' de ' + that.picturesCount);
	
	/* Picture controls - toggle display */
	if (this.currentPicture == 0) 
		this.controls.picture.prev.css({display:'none'});
	else 
		this.controls.picture.prev.css({display:'block'});
	
	if ((this.currentPicture + 1) == this.picturesCount) 
		this.controls.picture.next.css({display:'none'});
	else 
		this.controls.picture.next.css({display:'block'});
	
	this.media.fadeOut('fast',function(){
		that.media.attr('src', that.data[that.currentGallery]['_documents'][that.currentPicture]['sizes']['large']);
		/*$(this.media).load(function(){
			$(that.mediaHolder).width($(this).width());
			$(that.mediaHolder).height($(this).height());
		});*/
		that.media.fadeIn('fast');
	});
	

};

/* PICTURE navigation - Prev */
gallery.prototype.prevPicture = function()
{
	if(this.currentPicture != 0)
	{
		this.currentPicture -= 1;
		this.display();
	};
	return false;
};

/* PICTURE navigation - Next */
gallery.prototype.nextPicture = function()
{
	if((this.currentPicture+1) < this.picturesCount)
	{
		this.currentPicture += 1;
		this.display();
	};
	return false;
};

gallery.prototype.navGallery = function(item)
{
	/*
	if (this.currentGallery <= 0)
		this.currentGallery += 1;
	else if ((this.currentGallery + 1) == this.galleriesCount)
		this.currentGallery = 0;
*/
	
	if((this.currentGallery+1) == this.galleriesCount)
		this.currentGallery = 0;
	else
		this.currentGallery += 1;
	
	/*
	if((this.currentGallery + 1) == this.galleriesCount)
		$(this.controls.gallery.toggleGal).html('galerie précédente <span class="arrow"></span>');
	else
		$(this.controls.gallery.toggleGal).html('galerie suivante <span class="arrow"></span>');
	*/
	this.currentPicture = 0;
	this.display();
	
	return false;
};

gallery.prototype.openModal = function(item)
{
	var _rel = $(item).attr('rel');
	var _relArray = _rel.split('|');
	
	this.currentGallery = _relArray[1] -1;
	this.currentPicture = 0;
	
	this.numixModal.open();
	this.display();
	
	return false;
};



/* Modalbox */

var modal = function(params)
{
	this.gallery = params.gallery;
	this._modal = $('<div id="numix-modal"></div>');
	this._overlay = $('<div id="numix-modal-overlay"></div>');
	this.rootURL = params.rootURL;
	
	var that = this;
	
	this._modal.css({
		display: 'none',
		width: '100%',
		height: '100%',
		position: 'fixed',
		top: 0,
		left: 0,
		zIndex: 10000
	});
	
	this._overlay.css({
		width: '100%',
		height: '100%',
		position: 'absolute',
		top: 0,
		left: 0,
		backgroundImage: 'url('+that.rootURL+'/images/overlay_ff.png)',
		zIndex: 1
	});
	
	that.gallery.after(that._modal);
	that._modal.append(that.gallery);
	that._modal.append(that._overlay);
	that._overlay.unbind('click').bind('click', function(){ that.close() });

	that.gallery.css({display: 'block'});
};

modal.prototype.position = function()
{
	var that = this;
};

modal.prototype.open = function(item)
{
	this._modal.fadeIn();
};

modal.prototype.close = function()
{
	this._modal.fadeOut();
};
