(function($){
	$.fn.extend({ 
		//plugin name - megaGallery
		megaGallery: function(options) {

			//Settings list and the default values
			var defaults = {
				thumbCount: 5,
				childSelector: 'div',
				medImgSelector: '.main-image',
				lightBoxPlugin: 'prettyPhoto',
				lightBoxSelector: "a[rel^='prettyPhoto']",
				prevBtnId: '#image-nav-prev',
				nextBtnId: '#image-nav-next'
			};
			
			var options = $.extend(defaults, options);
		
    		return this.each(function() {
			var o =options;

			//Assign current element to variable, this is the element your applying the MegaGallery plugin on.
			var obj = $(this);				

			
			//Apply the lightbox function to the lightboxrel objects
			switch(o.lightBoxPlugin)
			{
				default:
					$(o.lightBoxSelector).prettyPhoto();
					break;
			}

			//Get all A tags in the ChildSelector element
			var items = $('img', obj);
			
			//onclick hide all med size images and show the one that was clicked
			items.click(function(){
				$(o.medImgSelector).children().hide();
				$(o.medImgSelector + " ." + $(this).attr('class')).show();
			});
			
			var itterations = Math.ceil(items.length / o.thumbCount);
			
			var i=0;
			while(i <= itterations){
				obj.children('img:lt(' + o.thumbCount + ')').wrapAll('<div id="megaGallery-thumb-page' + i + '"></div>');
				i++;
			}
			
			obj.children('div').hide();
			obj.children('div').first().show();
			
			$(o.prevBtnId).click(function(){
				var currentDiv = obj.children(o.childSelector + ':visible');
				if(currentDiv.prev().length != 0){
					currentDiv.hide();
					currentDiv.prev().show();
				}
				return false;
			});
			
			$(o.nextBtnId).click(function(){
				var currentDiv = obj.children(o.childSelector + ':visible');
				if(currentDiv.next().length != 0){
					currentDiv.hide();
					currentDiv.next().show();
				}
				return false;
			});
				
    		});
    	}
	});
})(jQuery);
