function Gallery() {

	var Gallery = this;

	// jQ objects
	var $allImages = $("#photoContainer img");
	var $images;
	var $thumbsList = $("#thumbsNav");
	var $galleryNav = $("#galleryNav");
	var $data = $("#galleryData");
	var $desc = $("#galleryDescription");
	var $play = $("#pgPlay");
	var $prev = $("#pgPrev");
	var $next = $("#pgNext");
	
	// global vars
	


	var	global = {
		active : 0,
		activeGallery : $galleryNav.find("a").eq(0).attr("href"),
		tspeed : 400,
		fspeed : 3000,
		sspeed : 300,
		thumbWidth : 166,
		show : 6		
	};
	
//

	Gallery.init = function() {
		global.active = 0;
		this.nav.init();
		this.images.init();
		this.thumbs.init();
		this.controls.init();
		this.data.init();
		this.controls.play();
	};
	
//	

	Gallery.change = function(rindex){
		if(rindex < 0) { index = (global.total-1); }			
		else if(rindex >= global.total){ index = 0; }
		else { index = rindex; }
		global.active = index;
		Gallery.images.change(index);
		Gallery.thumbs.activate(index);
		Gallery.thumbs.slide(index);
		Gallery.data.update();		
	};
	
// 

	Gallery.nav = {
		
		init : function(){
			this.build();
			this.observe();
		},
			
		build : function(){
			$galleryNav.find(".active").removeClass("active");
			$('a[href="'+global.activeGallery+'"]').addClass("active");
		},
		
		observe : function(){
			$galleryNav.find("a").click(function(e){
				e.preventDefault();
				if($(this).attr("href") != global.activeGallery){
					global.activeGallery = $(this).attr("href");
					Gallery.rotator.stop();
					Gallery.init();
				}
			});
		}	
	
	};
	
//	

	Gallery.images = {
	
		init : function() {
			this.build();
			this.observe();
		},
		
		build : function () {
			$allImages.hide().unbind('click');
			$images = $(global.activeGallery).find("img");
			$images.hide().eq(global.active).fadeIn(global.tspeed);		
			global.title = $images.eq(global.active).attr("title");			
		},
		
		observe : function(){
			$images.click(function(){
				Gallery.controls.next();
				Gallery.controls.pause();				
			});
		},
		
		change : function(index) {
			$images.fadeOut(global.tspeed);
			$images.eq(index).fadeIn(global.tspeed);			
			global.title = $images.eq(index).attr("title");
		}
	
	};
	
		
//	

	Gallery.thumbs = {
		
		init : function(){
			this.build();
			this.observe();
		},
		
		build : function(){
			$thumbsList.find("li").remove();
			$images.each(function(){
				var $li = $("<li />");
				var $a = $("<a />").attr("href","#");
				var $span = $("<span />").attr("className","thumbMask");
				$a.append($span).appendTo($li);
				$li.appendTo($thumbsList);
					var ximg = new Image();
	        $(ximg).load(function () {
	            $a.prepend(this);
			    }).error(function () {
	            alert("Image could not be loaded");
	        }).attr({
	        	'src': $(this).attr("src")
	        });	        
			});
			Gallery.thumbs.activate(global.active);			
		},
		
		observe : function(){
			$thumbsList.find("a").click(function(e){
				e.preventDefault();
				var index = $(this).parent("li").index();
				if(index != global.active) {
					Gallery.controls.pause();
					Gallery.thumbs.activate(index);
					Gallery.change(index);
				}
			});
		},
		
		activate : function(index){
			$thumbsList.find(".active").removeClass("active");
			$thumbsList.find("a").eq(index).addClass("active");			
		},
		
		slide : function(index){
			var factor = global.show - (index + 1);
			if(factor > 0){ factor = 0;	}
			var distance = factor * global.thumbWidth;
			
			$thumbsList.animate({
				left : distance+"px"
			}, global.sspeed);
			
		}
	
	};

//

	Gallery.data = {
		
		init : function() {
			global.total = $images.length;
			this.update();		
		},
	
		update : function() { 
			$data.text(global.active + 1 + " of " + global.total);
			$desc.text(global.title);
		}
	
	};
	
//	

	Gallery.controls = {
		
		init : function() {
			this.build();
			this.observe();
		},

		build : function(){
			$play.data("playing",true);
			$next.unbind('click');
			$prev.unbind('click');
		},
		
		observe : function() {
			$play.click(function(){
				if($play.data("playing")){
					Gallery.controls.pause();
				} 
				else {
					Gallery.controls.play();
				}				
			});

			$next.click(function(){
				Gallery.controls.next();
				Gallery.controls.pause();
			});
			
			$prev.click(function(){
				Gallery.controls.prev();
				Gallery.controls.pause();
			});
								
		},
		
		pause : function(){
			Gallery.rotator.stop();
			$play.addClass("paused");
		},
		
		play : function(){
			Gallery.rotator.start();		
			$play.removeClass("paused");
		},
		
		next : function(){
			Gallery.change(global.active + 1);
		},
		
		prev : function(){
			Gallery.change(global.active - 1);
		}
	
	};

//

	Gallery.rotator = {
		
		start : function() {
			$.doTimeout('gallery', global.fspeed, function(){
				Gallery.change(global.active + 1);
				$play.data("playing",true);
				return true;
			});
		},
		
		stop : function(){
			$.doTimeout('gallery');
			$play.data("playing",false);
		}

	};
	
//

};

