function PGallery(startGallery) {

	var Gallery = this;

	// jQ objects
	var $allImages = $("#pgImages img");
	var $images;
	var $thumbsList = $("#pgScroller");
	var $galleryNav = $("#pgSectionNav");
	var $data = $("#pgImgNum");
	var $desc = $("#pgImgDesc");
	var $play = $("#pgPlay");
	var $prev = $("#pgPrev");
	var $next = $("#pgNext");
	
	// global vars
	


	var	global = {
		active : 0,
		tspeed : 400,
		fspeed : 3000,
		sspeed : 300,
		thumbWidth : 130,
		show : 7		
	};
	
	if(startGallery == ''){
		global.activeGallery = $galleryNav.find("a").eq(0).attr("href");
	}
	else {
		global.activeGallery = "#"+startGallery;
	}	
	
	
//

	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.css("left","0px");
			$thumbsList.find("a").hide();
			$thumbsList.find("."+global.activeGallery).find("a").show();
			Gallery.thumbs.activate(0);
		},
		
		observe : function(){
			$thumbsList.find("a").click(function(e){
				e.preventDefault();
				var index = $(this).index();
				if(index != global.active) {
					Gallery.controls.pause();
					Gallery.thumbs.activate(index);
					Gallery.change(index);
				}
			});
		},
		
		activate : function(index){
			$thumbsList.find("a").removeClass("current").css({ "borderColor" : "transparent"	});
			$thumbsList.find("."+global.activeGallery).find("a").eq(index).addClass("current").css({ "borderColor" : "#9E6D3B" });			
		},
		
		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(e){
				e.preventDefault();			
				if($play.data("playing")){
					Gallery.controls.pause();
				} 
				else {
					Gallery.controls.play();
				}				
			});

			$next.click(function(e){
				e.preventDefault();
				Gallery.controls.next();
				Gallery.controls.pause();
			});
			
			$prev.click(function(e){
				e.preventDefault();
				Gallery.controls.prev();
				Gallery.controls.pause();
			});
								
		},
		
		pause : function(){
			Gallery.rotator.stop();
			$play.addClass("pause");
		},
		
		play : function(){
			Gallery.rotator.start();		
			$play.removeClass("pause");
		},
		
		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);
		}

	};
	






//

};
