(function($){
	/*
		
	Sample setup:
	 
	<div id="myGallery">
		<div class="gallery-image"><p></p></div> <!-- Paragraph will wrap the image and caption together nicely -->
		<div class="gallery-strip">	
			<a class="gallery-prev f-left"></a>					
			<a class="gallery-next f-right"></a>	
			<div class="gallery-thumbs"> <!-- Overflow: hidden is needed to hide the sliding UL -->
				<ul>
					<li><a href="big-image.jpg" title="Image caption"><img src="thumbnail.jpg" alt=""  /></a></li>
					<li><a href="big-image.jpg" title="Image caption"><img src="thumbnail.jpg" alt=""  /></a></li>
					<li><a href="big-image.jpg" title="Image caption"><img src="thumbnail.jpg" alt=""  /></a></li>
					<li><a href="big-image.jpg" title="Image caption"><img src="thumbnail.jpg" alt=""  /></a></li>
					<li><a href="big-image.jpg" title="Image caption"><img src="thumbnail.jpg" alt=""  /></a></li>
					<li><a href="big-image.jpg" title="Image caption"><img src="thumbnail.jpg" alt=""  /></a></li>
					<li><a href="big-image.jpg" title="Image caption"><img src="thumbnail.jpg" alt=""  /></a></li>					
				</ul>
			</div>
		</div>
	</div>
	
	Call out the gallery $("#myGallery).Gallery()
	
	*/
	$.Gallery = function(el, options){
    	var self = this;
		self.$el = $(el);
		self.el = el;
		self.maxh = 0;
		self.direction = "horizotal";
		self.thumbs = $(el).find(".gallery-thumbs")
		self.thumbslist = $(el).find(".gallery-thumbs UL")
		self.prev = $(el).find(".gallery-prev")
		self.next = $(el).find(".gallery-next")
		self.img = $(el).find(".gallery-image")
		self.imagelist = $("li",self.thumbs);
		self.totalwidth = 0;
		self.totalheight = 0;
		self.stripwidth = self.thumbs.outerWidth();
		self.stripheight = self.thumbs.outerHeight();
		self.$el.data("Gallery", self);
		
		self.init = function(){
			var i = 0;
			self.options = $.extend({},$.Gallery.defaultOptions, options);
			self.maxh = self.options.imheight;
			self.direction = self.options.direction;
			if(self.direction=="horizontal"){
				self.imagelist.each(function(){							
					margins = parseInt($(this).css('margin-left'))+parseInt($(this).css('margin-right'))
					self.totalwidth+=($(this).width()+margins);	
				})			
				$("ul",self.thumbs).css({"width":self.totalwidth,"left":0});						
				
				self.next.click(function(){
					currentpos = parseInt(self.thumbslist.css('left'))							
					newpos = currentpos-self.stripwidth;
					if(newpos+self.totalwidth<self.stripwidth){newpos = self.stripwidth-self.totalwidth;}
					$(self.thumbslist).animate(
						{left: newpos},
						1000,
						function(){self.checkScroll()});
					return false;
				});
						
				self.prev.click(function(){
					currentpos = parseInt(self.thumbslist.css('left'));							
					newpos = currentpos+self.stripwidth;
					if(newpos> 0){newpos = 0;}
					$(self.thumbslist).animate(
						{left: newpos},
						1000,
						function(){self.checkScroll()});				
					return false;
				});
			}
			else {
				self.imagelist.each(function(){							
					margins = parseInt($(this).css('margin-top'))+parseInt($(this).css('margin-bottom'))
					self.totalheight+=($(this).height()+margins);	
				})
				$("ul",self.thumbs).css({"height":self.totalheight,"top":0});						
				
				self.next.click(function(){
					currentpos = parseInt(self.thumbslist.css('top'))							
					newpos = currentpos-self.stripheight;
					if(newpos+self.totalheight<self.stripheight){newpos = self.stripheight-self.totalheight;}
					$(self.thumbslist).animate(
						{top: newpos},
						1000,
						function(){self.checkScroll()});
					return false;
				});
						
				self.prev.click(function(){
					currentpos = parseInt(self.thumbslist.css('top'));							
					newpos = currentpos+self.stripheight;
					if(newpos> 0){newpos = 0;}
					$(self.thumbslist).animate(
						{top: newpos},
						1000,
						function(){self.checkScroll()});				
					return false;
				});

			}
					
			$("a",self.imagelist)
				.attr("onclick","return false")
				.click(function(){
					self.loadimage($(this));
					return false
				});	
			self.checkScroll();		
        };
		
		self.checkScroll = function(){
			if(self.direction=="horizontal"){
				currentPos = parseInt($("ul",self.thumbs).css('left'));
				if(currentPos==0){
					self.prev.toggleClass("gallery-prev-disabled",true);
				}
				else{
					self.prev.toggleClass("gallery-prev-disabled",false);
				}	
				if(currentPos<=self.stripwidth-self.totalwidth){
					self.next.toggleClass("gallery-next-disabled",true);
				}
				else{
					self.next.toggleClass("gallery-next-disabled",false);
				}
			}
			else { 
				currentPos = parseInt($("ul",self.thumbs).css('top'));
				if(currentPos==0){
					self.prev.toggleClass("gallery-prev-disabled",true);
				}
				else{
					self.prev.toggleClass("gallery-prev-disabled",false);
				}	
				if(currentPos<=self.stripwidth-self.totalwidth){
					self.next.toggleClass("gallery-next-disabled",true);
				}
				else{
					self.next.toggleClass("gallery-next-disabled",false);
				}

			}
		}
				
		self.loadimage = function(targ){
			$(self.img).children().first().empty()
			self.img.toggleClass("loading",true);
			filetoload  =	targ.attr("href");
			ext = filetoload.split('.').pop();
			if(ext=="jpg" || ext=="png" || ext=="gif" || ext=="bmp"){
				var i = new Image();
				$(i).load(function () {
					$(this).hide();
					self.img.toggleClass("loading",false);									
					$(self.img).children().first().append(this);
					$(self.img).children().first().append("<span class='caption'>"+targ.attr("title")+"</span>")
					if($(this).height()>self.maxh){
						$(this).height(self.maxh);
					}
					$("p",self.img).width($(this).outerWidth())
					$(this).fadeIn();
				})
				.attr('src', targ.attr("href"));
				$("a",self.imagelist).css('opacity',1);
				targ.css('opacity',0.7);
			}
			else {
				$(self.img).children().first().hide();
				$(self.img).children().first().load(filetoload,function() {
				  $(self.img).children().first().fadeIn();
				  tb_init('a.thickbox, area.thickbox, input.thickbox');
				});
			}
		};     
        self.init();
		self.loadimage($("li:first a",self.thumbs));
    };

    $.Gallery.defaultOptions = {
        imheight: 380,
		direction: 'horizontal'
    };

    $.fn.Gallery = function(options){
        return this.each(function(){
            (new $.Gallery(this, options));
        });
    };

})(jQuery);
