(function($) {
	$.fn.rGallery = function(opt) {
    	var defaults = { 
            start: 1,
            scrolljump: 1,
			eventStart: 'mousedown',
			eventStop: 'mouseup',
			speed: 500,				
        	easing: 'linear',		
			mouseWheel: false,
            callbacks: {
            	init: false,
            	afterImageVisible: false,
            	beforeImageVisible: false,
            	slideShowStart: false,
             	slideShowStop: false
			}    
		};
    	var opt = $.extend(defaults, opt);
		return this.each(function() {
			var o = $(this);
			var thumbs = o.find("table");
			var tItems = thumbs.find("td");
			//var maxHeight = tItems.eq(0).height();
			var startpos = 0;
			for (var i=0; i<opt.start; ++i) {
				startpos = startpos + tItems.eq(i).outerWidth(); 	
			}
			
			var thumbsWidth = thumbs.width();
			
			//thumbs.wrap('<div class="container"></div>');
			var container = thumbs.parent().css({"position":'relative', "overflow":'hidden'});
			var visible = container.width();
			
			
			var aa = o.find("a.prev");
			if ($("a.prev, a.next", $(this)).length==0) {
				o.append('<a href="#" class="prev disabled">&laquo</a><a href="#" class="next disabled">&raquo;</a>');
			}
			var prev = o.find("a.prev").bind("click", function(){return false});
			var next = o.find("a.next").bind("click", function(){return false});;
			
			if (thumbsWidth<=visible) {
				thumbs.css({"position":'relative', "margin":'0'});
				prev.addClass("disabled");
				next.addClass("disabled");
				if ($("#all_gal_photos").length) {
					$("#all_gal_photos").css("visibility",'visible');
				}
			}
			else if (opt.start == '0') {
				prev.addClass("disabled");
				next.removeClass("disabled");
			} else if (thumbsWidth-startpos<=visible) {
				startpos = thumbsWidth-visible-1;
				next.addClass("disabled");
				prev.removeClass("disabled");
			}
			else {
				prev.removeClass("disabled");
				next.removeClass("disabled");
			}
			thumbs.css({"position":'relative', "top":'0', "left":-startpos+'px', "overflow":'hidden'});
			
			var run = false;
			var napram = null;
			
            prev.bind(opt.eventStart, function() {
                //prev.css({'opacity':"0.6"});
                run = true;
                napram = 'back';
                return prevSlide(); 
            });            
            prev.bind(opt.eventStop, function() {
            	//prev.css({'opacity':"1.0"});
            	run = false; 
            	napram = null;
                return stopSlide(); 
            });        
			next.bind(opt.eventStart, function() {
				//next.css({'opacity':"0.6"});
				run = true;
				napram = 'next';
                return nextSlide(); 
            });
			next.bind(opt.eventStop, function() {
				//next.css({'opacity':"1.0"});
				run = false;
				napram = null;
                return stopSlide(); 
            });
			
			var curr = opt.start;
			var sj = opt.scrolljump;
			
			if(opt.mouseWheel && o.mousewheel){
            	o.mousewheel(function(e, d) {	                
					var p = Math.abs(parseInt(thumbs.css("left")));
	                if(run === false){
						if(d > 0){
							if(thumbsWidth-(p+visible)>0) { 
								nextSlide();								
								run = true;
							}
							else {
								run = false;
							}
						}
	                	else {
							if (curr>0) { 
								prevSlide();
								run = true;
							}
							else {
								run = false;
							}
						}
					}
					return false;
            	});
        	}
			
			function nextSlide(){
				if(run === true && napram === "back"){ return; } 
				var s = 0;
				var p = 0;
				for (var k=0; k<curr; k++) {
    				p = p + tItems.eq(k).outerWidth();
				}
				for (var j=1; j<=sj; j++) { 
					s = s + tItems.eq(curr).outerWidth();
					curr = curr + 1;
					if (thumbsWidth-(p+visible)<=s) {
						s = thumbsWidth-(p+visible);
						next.addClass("disabled");						
						if ($("#all_gal_photos").length) {
							$("#all_gal_photos").css("visibility",'visible');
						}						
						if ($("i.fadepic2").length) {
							$("i.fadepic2").css("visibility",'visible');			
						}
						if ($("i.fadepic").length) {
							$("i.fadepic").css("visibility",'hidden');			
						}
						prev.removeClass("disabled");
						break;
					}
					else {
						if (prev.hasClass("disabled")){
							prev.removeClass("disabled");
						}
						
							if ($("i.fadepic2").length) {
								$("i.fadepic2").css("visibility",'visible');			
							}
	
					}
				}
    			thumbs.animate({left: -(p+s)+'px'}, o.speed, o.easing, function(){run=false});
        	}        
			function prevSlide(){
				if(run === true && napram === "next"){ return; } 
    			var s = 0;
    			var p = 0;
				for (var k=0; k<curr; k++) {
    				p = p + tItems.eq(k).outerWidth();
				}
				for (var j=1; j<=sj; j++) { 
					s = s + tItems.eq(curr-1).outerWidth();
					curr = curr - 1;
					if (p<=s) {
						s = p;
						prev.addClass("disabled");
						if ($("#all_gal_photos").length) {
							$("#all_gal_photos").css("visibility",'hidden');
						}
						if ($("i.fadepic").length) {
							$("i.fadepic").css("visibility",'visible');			
						}
						if ($("i.fadepic2").length) {
							$("i.fadepic2").css("visibility",'hidden');			
						}
						next.removeClass("disabled");
						curr = 0;
						break;
					}
					else {
						if (next.hasClass("disabled")){
							next.removeClass("disabled");
							if ($("#all_gal_photos").length) {
								$("#all_gal_photos").css("visibility",'hidden');
							}
							if ($("i.fadepic").length) {
								$("i.fadepic").css("visibility",'visible');			
							}
						}
	
					}
				}
    			thumbs.animate({left: -(p-s)+'px'}, o.speed, o.easing, function(){run=false});
        	}
        	function stopSlide(){
        		//thumbs.stop();
				run = false;
				napram = null;
        	}	
		});
	};
})(jQuery);
