/*******************************************************************************

jQuery DGPGallery (c) DGProductions
http://www.dgproductions.nl
Developer: Stefan van den Dungen Gronovius

When:       Who:    					What:
-----       ----    					-----
29-April-10   S. v.d. Dungen Gronovius	Plugin created


USAGE:
------
$('#Gallery').dgpGallery();
  

*******************************************************************************/
jQuery.fn.dgpGallery = function(options)
{
	settings = jQuery.extend({
		imageBtnNext:	'/images/dgpgallery_next.gif',
		imageBtnPrev:	'/images/dgpgallery_prevnext.gif'
	}, options);
	
	var jQ = this;
	// Init the gallery
	this.find('li:first').addClass('first active');
	this.find('li:last').addClass('last');
	this.prepend('<div class="navigation"><a class="prev"><img src="'+settings.imageBtnPrev+'" alt="Previous"/></a><a class="next"><img src="'+settings.imageBtnNext+'" alt="Next"/></a></div>');
	this.prepend('<div class="viewer"><a href="" title="Vergroot de afbeelding"><img/></a></div><div class="navigation"></div>');
	this.find('ul').addClass('access');
	this.find('.navigation a.prev').hide();
	
	link = this.find('li:first a');
	img = this.find('li:first img');
	$(this).find('.viewer img').attr('src',img.attr('src') );
	$(this).find('.viewer a').attr('href',link.attr('href') );
	
	this.find('.navigation a').click( function(){
		if( $(this).hasClass('next') ){
			NextPicture( );
		} else {
			PrevPicture( );
		}		
	});
	
	
	function NextPicture(){

		newli = jQ.find('li.active').next();
		jQ.find('li.active').removeClass('active');
		newli.addClass('active');
		img = jQ.find('li.active').find('img');
		link =  jQ.find('li.active').find('a');
		// Set new img src + link
		jQ.find('.viewer img').attr('src',img.attr('src') );
		jQ.find('.viewer a').attr('href',link.attr('href') );
		// See if buttons must be on
		initButtons();
	}
	
	function PrevPicture(){
		newli = jQ.find('li.active').prev();
		jQ.find('li.active').removeClass('active');
		newli.addClass('active');
		img = jQ.find('li.active').find('img');
		link =  jQ.find('li.active').find('a');
		// Set new img src
		jQ.find('.viewer img').attr('src',img.attr('src') );
		jQ.find('.viewer a').attr('href',link.attr('href') );		
		// See if buttons must be on
		initButtons();	
	}
	
	function initButtons(){
		countRight = jQ.find('li.active').next().length;
		countLeft = jQ.find('li.active').prev().length;
		if( countRight == 0 ){
			jQ.find('.navigation .next').hide();
		} else {
			jQ.find('.navigation .next').show();
		}
		if( countLeft == 0 ){
			jQ.find('.navigation .prev').hide();
		} else {
			jQ.find('.navigation .prev').show();
		}	
	}
	
}
