jQuery.fn.gallery = function(s,slideshow,styling) {	
		var gallery = this;
		var img = [];
		var speed = 1000; if(s) speed = parseInt(s,10);

		var take = 0;
		if(slideshow==undefined) {
			slideShowSpeed = speed*2.5;
		} else if(slideshow) {
			slideShowSpeed = slideshow;
		} else if (!slideshow) {			
			take = 1;
		}
		//var galleryStructure = '<div id="img-gallery"><img style="display:none" /><ul></ul></div>';
		var started = false;
		$(gallery).each(function(i){
			$(this).hide();
			img[i] = [this.src,this.alt];
			this.onload = function(){
				$(this).remove();
			}
			gallery[gallery.length-1].onload = function(){
				$(this).remove();
				start();
				started = true;
			}
			setTimeout(function(){
				if(!started) start();
			},2000)
		})
		
		function start(){		

			// EDITABLE:
			//$('#gallery-container').prepend(galleryStructure); // DESTINATION OF GALLERY (YOU CAN CHANGE THIS)			
			// --------
						
			changeImage(0);
			$('#trueContainer a:not(#playstop)').click(function(){
				var imgToLoad = $(this).attr('href');
				imgToLoad = imgToLoad.split('#');
				imgToLoad = parseInt(imgToLoad[1].substr(3)) - 1;
				$('input[name=infoval]').val(imgToLoad+1);
				changeImage(imgToLoad); 
				if(window['ssr']) {
                    $('#nav-gallery ul a#playstop').click();
                    $('#nav-gallery ul a#playstop').html('Play');
                    stopSlideShow();
                }
				return false;
			})
			$('#nav-gallery  ul a#playstop').toggle(function(){
				$(this).toggleClass('stop');
				$('#nav-gallery ul a#playstop').html('Stop');
				startSlideShow();
				return false;
			}, function(){
				$(this).toggleClass('stop');
				$('#nav-gallery ul a#playstop').html('Play');
				stopSlideShow();
				return false;
			})
			//edit for next prev
			$('#nav-gallery ul a#next').click(function(){
				//alert($('#trueContainer ul a.active').attr('href'));
				var imgToLoad = $('#trueContainer a.active').attr('href');
				imgToLoad = imgToLoad.split('#');
				imgToLoad = parseInt(imgToLoad[1].substr(3)); 

				$('input[name=infoval]').val(imgToLoad+1);

                if (imgToLoad == gallery.length) {
					imgToLoad = 0;
                    $('input[name=infoval]').val(1);
				}
                changeImage(imgToLoad);
				if(window['ssr']) {
                    $('#nav-gallery ul a#playstop').click();
                    $('#nav-gallery ul a#playstop').html('Play');
                    stopSlideShow();
                }
				return false;
			})
			$('#nav-gallery ul a#prev').click(function(){
				var imgToLoad = $('#trueContainer a.active').attr('href');
				imgToLoad = imgToLoad.split('#');
				imgToLoad = parseInt(imgToLoad[1].substr(3)) - 2;       
				$('input[name=infoval]').val(imgToLoad+1);       
                if (imgToLoad == -1) {
					imgToLoad = (gallery.length - 1);      
                    $('input[name=infoval]').val(gallery.length);
				}
				changeImage(imgToLoad);
				if(window['ssr']) {
                    $('#nav-gallery ul a#playstop').click();
                    $('#nav-gallery ul a#playstop').html('Play');
                    stopSlideShow();
                }
				return false;
			})
			//end edit for next prev
			function changeImage(n, callback){			
				$('#img-gallery img').fadeOut(speed / 4, function(){
					var originalWidth = $('#img-gallery img').width();
					$('#img-gallery img').attr('src', img[n][0]).attr('alt', img[n][1]);
					var width = $('#img-gallery img').width();
					var height = $('#img-gallery img').height();
					if (width == originalWidth) { fadeInAll(); } else { animate(); }
					function animate(){
						$('#img-gallery ul').fadeOut(speed / 2, function(){
							$('#img-gallery').animate({
								width: width,
								height: height
							}, speed / 2, function(){
								fadeInAll(true)
							})
						})
					}
					function fadeInAll(fromAnimate){
						var localSpeed = speed;
						if (!fromAnimate) 
							localSpeed = speed / 2;
						$('#img-gallery img').fadeIn(localSpeed / 2);
						$('#img-gallery ul').fadeIn(localSpeed / 2);
						$('#trueContainer a:eq(' + (n - take) + ')').addClass('active');
						if (callback) callback();
						if (styling) styling();
					}
				})
				$('#trueContainer a').removeClass('active');
			}
			function startSlideShow(){
				var imgToLoad = $('#trueContainer a.active:eq(0)').attr('href');
				imgToLoad = imgToLoad.split('#');
				window['ssr'] = true;
				imgToLoad = parseInt(imgToLoad[1].substr(3));
				if (imgToLoad == gallery.length) {
					imgToLoad = 0;
				}
				window['galleryTimeout'] = setTimeout(function(){
					startSlideShow()
				}, slideShowSpeed)
				changeImage(imgToLoad, function(){
					eval(galleryTimeout);
				});
			}
			function stopSlideShow(){
				window['ssr'] = false;
				clearTimeout(eval(galleryTimeout));
			}
		}
}
