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 $prev2 = $("#pgPrev");
	var $next2 = $("#pgNext");
//	var $prev2 = $("#prev");
//	var $next2 = $("#next"); 
    var $prev = $("#prev");
	var $next = $("#next");

	var	global = {
		active : 0,
		activeGallery : $galleryNav.find("a").eq(0).attr("href"),
		tspeed : 300,
		fspeed : 4000,
		sspeed : 10,
		thumbWidth : 150,
		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); 
			
			index = 0;
			global.active = 0;
			current_idx = 0;
			prev_idx = 0;
			
			galleries = $galleryNav.find('li');
			
			galleries.each(function(i){
				if ($(this).find('a.active').size()>0) {
					current_idx = i;
				}
			});
			
			if (current_idx==0) {
				prev_idx = galleries.size()-1;
			} else {
				prev_idx = current_idx-1;
			}
			
			global.activeGallery = $galleryNav.find('a').eq(prev_idx).attr('href');
			global.active = index = $(global.activeGallery).find('img').size() - 1;
			
			Gallery.rotator.stop();
			Gallery.nav.init();
			Gallery.images.init();
			Gallery.thumbs.init();
			Gallery.controls.init();
			Gallery.data.init();			
		} else if(rindex >= global.total){ 
			index = 0;
			global.active = 0;
			current_idx = 0;
			next_idx = 0;
			
			galleries = $galleryNav.find('li');
			
			galleries.each(function(i){
				if ($(this).find('a.active').size()>0) {
					current_idx = i;
				}
			});
			
			if (current_idx==galleries.size()-1) {
				next_idx = 0;
			} else {
				next_idx = current_idx+1;
			}
			
			global.activeGallery = $galleryNav.find('a').eq(next_idx).attr('href');
			Gallery.rotator.stop();
			Gallery.nav.init();
			Gallery.images.init();
			Gallery.thumbs.init();
			Gallery.controls.init();
			Gallery.data.init();
			//	Below line make double play, so comment it
			//Gallery.controls.play();
		} 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");
			var text = $('a[href="'+global.activeGallery+'"]').text();
			$('a[href="'+global.activeGallery+'"]').empty().text(text);
			Cufon.set('fontFamily', 'Gotham Light');
			Cufon.replace($('a[href="'+global.activeGallery+'"]'), {
				fontStyle: 'Gotham Light',
				color: '#fff',
				fontSize:'12px'
				
			});
			
			 $('#galleryNav li a:not(.active)').each(function(){
				var text1 = $(this).text();
				$(this).empty().text(text1);
				Cufon.set('fontFamily', 'Gotham Light');
				Cufon.replace($(this), {
					fontStyle: 'Gotham Light',
				
					fontSize:'12px',
					color: '#000',
					hover:{
						color:'#fff'
					}
				
				});
				
				
			}); 
		},
		
		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  //global.active + 1 + " of " + global.total $images.hide().eq(global.active).fadeIn();	
			
		/* 	if( index ==0){
				$images.hide();
				
				
			}else{
				$images.fadeOut();
			}
			 */
			$images.hide();
			
			$images.eq(index).fadeIn();
			
		   
			
			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 $rel = $(this).attr('rel');
				var $a;
				var $span;
				if($rel){
					$a = $("<a />").attr("href","javascript:popUpVideo('"+$rel+"')");
				
					$span = $("<span />").attr("className","thumbMask");
				}else{
					$a = $("<a />").attr("href","#");
				}
				
				
				$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){
		
				if($(this).attr('href')=='#'){
					e.preventDefault();
				}
				
				var index = $(this).parent("li").index();//index != global.active
				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.stop();
			$thumbsList.animate({
				left : distance+"px"
			}, global.sspeed);
			
		}
	
	};

//

	Gallery.data = {
		
		init : function() {
			global.total = $images.length;
			this.update();		
		},
	
		update : function() { 
			Cufon.set('fontFamily', 'Gotham Book');
			$data.empty().text(global.active + 1 + " of " + global.total);
			Cufon.replace('#galleryData', {
				fontStyle: 'Gotham Book',
				color: '#000',
				fontSize:'12px'
				
			});
			
			$desc.empty().text(global.title);
			Cufon.replace('#galleryDescription', {
				fontStyle: 'Gotham Book',
				color: '#000',
				fontSize:'12px'
				
			});
		
			
		}
	
	};
	
//	

	Gallery.controls = {
		
		init : function() {
			this.build();
			this.observe();
		},

		build : function(){
			$play.data("playing",true);
			$next.unbind('click');
			$prev.unbind('click');
			$next2.unbind('click');
			$prev2.unbind('click'); 
		},
		
		observe : function() {
			$play.click(function(){
				if($play.data("playing")){
					Gallery.controls.pause();
				} 
				else {
					Gallery.controls.play();
				}				
			}); 

			$next.click(function(e){
				e.preventDefault();
				Gallery.controls.next();
				Gallery.controls.pause();
			});
			$next2.click(function(e){
				e.preventDefault();
				e.stopPropagation();
				Gallery.controls.next();
				Gallery.controls.pause();
			});
			/*$next2.click(function(e){
				e.preventDefault();
				$next.trigger('click');
			
			}); */
			$prev.click(function(e){
				e.preventDefault();
				Gallery.controls.prev();
				Gallery.controls.pause();
			});
			$prev2.click(function(e){
				e.preventDefault();
				e.stopPropagation();
				Gallery.controls.prev();
				Gallery.controls.pause();
			});
			/*$prev2.click(function(e){
				e.preventDefault();
				$prev.trigger('click');
			});*/
								
		},
		
		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);
		}
	};
};

