jQuery(document).ready(function($) {
	// config
	var mGalleryPagerDelimiter = ' / ';
	var mGalleryShowPattern = "-show-";
	
	// initialize
	var mGalleryImages = $('#myrtillus-gallery-list > li > img');
	var mGalleryPage = 1;
	var mGalleryPages = mGalleryImages.size();
	
	if(mGalleryPages > 0) {
		// show first image
		$('#myrtillus-gallery-canvas').append($('#myrtillus-gallery-list > li > img:first').clone());
		// show pager / title / description
		$('#myrtillus-gallery-pager').text(mGalleryPage+' / '+mGalleryPages);
		$('#myrtillus-gallery-title').text(mGalleryImages.eq(mGalleryPage-1).attr('title'));
		$('#myrtillus-gallery-description').text($('#myrtillus-gallery-list > li').eq(mGalleryPage-1).text());
		// show navigation buttons
		$('#myrtillus-gallery-buttons').css('display','inline');
		// set click action on image
		$('#myrtillus-gallery-canvas > img').click(function() {
			myrtillusGalleryNext();
		});
		// set click action on buttons
		$('#myrtillus-gallery-button-prev').click(function() {
			myrtillusGalleryPrev();
			return false;
		});
		$('#myrtillus-gallery-button-next').click(function() {
			myrtillusGalleryNext();
			return false;
		});
	}
	
	// go next image
	function myrtillusGalleryNext() {
		if(mGalleryPage >= mGalleryPages) {
			mGalleryPage = 0;
		}
		$('#myrtillus-gallery-canvas > img').attr('src',mGalleryImages.eq(mGalleryPage).attr('src'));
		$('#myrtillus-gallery-canvas > img').attr('title',mGalleryImages.eq(mGalleryPage).attr('title'));
		$('#myrtillus-gallery-title').text(mGalleryImages.eq(mGalleryPage).attr('alt'));
		$('#myrtillus-gallery-description').text(myrtillusGalleryShowText($('#myrtillus-gallery-list > li').eq(mGalleryPage).text()));
		mGalleryPage++;
		$('#myrtillus-gallery-pager').text(mGalleryPage+mGalleryPagerDelimiter+mGalleryPages);
	}
	
	// go prev image
	function myrtillusGalleryPrev() {
		if(mGalleryPage <= 1) {
			mGalleryPage = mGalleryPages+1;
		}
		mGalleryPage--;
		$('#myrtillus-gallery-canvas > img').attr('src',mGalleryImages.eq(mGalleryPage-1).attr('src'));
		$('#myrtillus-gallery-canvas > img').attr('title',mGalleryImages.eq(mGalleryPage-1).attr('title'));
		$('#myrtillus-gallery-title').text(mGalleryImages.eq(mGalleryPage-1).attr('alt'));
		$('#myrtillus-gallery-description').text($('#myrtillus-gallery-list > li').eq(mGalleryPage-1).text());
		$('#myrtillus-gallery-pager').text(mGalleryPage+mGalleryPagerDelimiter+mGalleryPages);
	}
	
	// check for no show pattern and return empty string if found
	function myrtillusGalleryShowText(inputString) {
		if(inputString.match(mGalleryShowPattern)) {
			return inputString.replace(mGalleryShowPattern,'');
		}
		return '';
	}
});
