(function($){
    $.thinkGallery = function(el, options){
        var base = this;
        
        base.$el = $(el);
        base.el = el;
		
        base.$el.data("thinkGallery", base);

        base.init = function(){
            base.options = $.extend({},$.thinkGallery.defaultOptions, options);
			base.$frontImage = base.$el.children(".frontImage");
			base.$backImage = base.$el.children(".backImage");
			base.$thumbImages = base.options.thumbPhotos;
			base.numImg = base.$thumbImages.length;
			base.lp = base.options.lastPosition;
            base.autoTimeout = "";
			base.popoutTimeout = "";
            base.speed = 5000;            
            base.currentPoint = 0;

            base.writePhotos();
			base.buildEvents();
        };

        base.writePhotos = function(){		

			base.$frontImage.attr("src","//cdn.media910.whipplehill.net/ftpimages/434/photo/"+base.$thumbImages.eq(0).children().attr("lPath"));
			base.$backImage.attr("src","//cdn.media910.whipplehill.net/ftpimages/434/photo/"+base.$thumbImages.eq(1).children().attr("lPath"));
        	if(base.numImg < 5){return false;}

        	base.goto(base.currentPoint,true);
        };
        
		base.closePopout = function($obj){		
			$obj.bind("mouseleave",function(){
				base.popoutTimeout = setTimeout(function(){
					$(".contentContainer").animate({left:508},500,function(){
						$(".popoutContainer").css("display","none");
						$(".thumbArrow").css("display","block");
					});

					base.autoTimeout = setTimeout(function(){
						base.fireClick(base.currentPoint);
					},base.speed)
				},100);
			});
		};

        base.buildEvents = function(){
			$(".popoutContainer").bind("mouseenter",function(e){
				clearTimeout(base.popoutTimeout);
			});
			
			base.$thumbImages.bind("mouseenter",function(e){
				clearTimeout(base.autoTimeout);
				clearTimeout(base.popoutTimeout);				
				$hoverObj = $(this);			
				$hoverImg = $(this).children();

				if($hoverObj.position().top == 0){
					$(".thumbArrow").css("top","29px");
				}
				else if($hoverObj.position().top == 120){
					$(".thumbArrow").css("top","149px");
				}
				else if($hoverObj.position().top == 240){
					$(".thumbArrow").css("top","269px");
				}
				
				$(".thumbArrow").css("display","block");
				$(".popoutTitle").html($hoverImg.attr("pTitle"));
				$(".popoutDescription").html($hoverImg.attr("desc"));
				$(".popoutContainer").css("display","block");
				$(".contentContainer").animate({left:0},500);
				$('.popoutDescription').jScrollPane({scrollbarWidth:20, dragMinHeight:67, dragMaxHeight:67,showArrows:true});
			});

			base.$frontImage.bind("mouseenter",function(e){
				clearTimeout(base.autoTimeout);
				clearTimeout(base.popoutTimeout);
				$hoverObj = $(this);

				$(".thumbArrow").css("display","none");
				$(".popoutTitle").html($hoverObj.attr("pTitle"));
				$(".popoutDescription").html($hoverObj.attr("desc"));				
				$(".popoutContainer").css("display","block");
				$(".contentContainer").animate({left:0},500);
				$('.popoutDescription').jScrollPane({scrollbarWidth:20, dragMinHeight:67, dragMaxHeight:67, showArrows:true});
			});
			
			base.closePopout($(".popoutContainer"));
			base.closePopout(base.$thumbImages);	
			base.closePopout(base.$frontImage);	
        };
        
        base.fireClick = function(ix){
        	var index;
			index = ix + 1;
			
			if(index > base.numImg-1){
				index = 0;
			}
			
			base.currentPoint = index;
			base.goto(index,false);	
        };
        
        base.goto = function(index,init){
			base.$backImage.css("display","block");
			var currImg = "//cdn.media910.whipplehill.net/ftpimages/434/photo/"+base.$thumbImages.eq(index).children().attr("lPath");
			var currTitle = base.$thumbImages.eq(index).children().attr("pTitle");
			var currDesc = base.$thumbImages.eq(index).children().attr("desc");
			
			if(index === base.numImg-1){
				index = -1;
			}
			
			var nextImg = "//cdn.media910.whipplehill.net/ftpimages/434/photo/"+base.$thumbImages.eq(index+1).children().attr("lPath");
			var nextTitle = base.$thumbImages.eq(index+1).children().attr("pTitle");
			var nextDesc = base.$thumbImages.eq(index+1).children().attr("desc");
			
			base.$frontImage.attr("pTitle", currTitle);
			base.$frontImage.attr("desc", currDesc);
			
			base.$backImage.attr("pTitle", nextTitle);
			base.$backImage.attr("desc", nextDesc);
			
        	if(!init){
				base.$frontImage.fadeOut(1000,function() {
					setTimeout(function(){
						base.$frontImage.attr("src", currImg);			
						base.$frontImage.css("display","block");
						
						base.$backImage.attr("src", nextImg);
						base.$backImage.css("display","none");
					},500);
				});					
        	}      	
        	base.animate(init);
        };
        
        base.animate = function(init){
        	for(var i = 0; i < base.$thumbImages.length; i++){
        	    var startPosition = base.$thumbImages.eq(i).position().top;
        	    var endPosition = startPosition - 120;

				if(!init){
					switch(startPosition){
						case 0: base.$thumbImages.eq(i).animate({top:endPosition},1000); break;
						case 120: base.$thumbImages.eq(i).animate({top:endPosition},1000); break;
						case 240: base.$thumbImages.eq(i).animate({top:endPosition},1000); break;
						case 360: base.$thumbImages.eq(i).animate({top:endPosition},1000); break;
						case -120: base.$thumbImages.eq(i).css("top",base.lp); break;
						default: base.$thumbImages.eq(i).css("top",endPosition); break;
					}
				}      	    	
        	}

        	base.autoTimeout = setTimeout(function(){
        		base.fireClick(base.currentPoint);
        	},base.speed);
        };
        base.init();
    };
    
    $.thinkGallery.defaultOptions = {
		thumbPhotos:{},
		lastPosition:9999,
      	galHeight:500
    };
    
    $.fn.thinkGallery = function(options){
        return this.each(function(){
            (new $.thinkGallery(this, options));
        });
    };   
})(jQuery);
