Gallery = function(){
	this.jPtr = $("#our_objects");
	this.init();
}

Gallery.prototype = {
	
	HOVER_TIMEOUT : 5,
	
	init : function(){
		this.initObjects();
		this.initTooltip();
	},
	
	initObjects : function(){
		var that = this;
		this.aPreviews = [];
		
		this.jPtr.find("dl dd a").each(
			function(){
				that.aPreviews.push(
					new Preview($(this), that)
				);
			}
		);
	},
	
	initTooltip : function(){
		this.oTooltip = new Tooltip(this);
	},
	
	getCurrentPreview : function(){
		return this.oCurrentPreview;
	},
	
	setCurrentPreview : function(oPreview){
		if(this.oCurrentPreview){
			this.oCurrentPreview.deselect();
		}
		
		this.oCurrentPreview = oPreview;
		
		if(oPreview){
			oPreview.select();
		}
	}
	
}




Preview = function(jPtr, oGallery){
	this.jPtr = jPtr;
	this.oGallery = oGallery;

	this.init();
}

Preview.prototype = {
	
	init: function() {
		this.sText = this.jPtr.siblings('.desc').text();
		this.attache_events();
    },
	
	attache_events: function(){
		var oThis = this;

		this.jPtr.hover(
			function() {
				clearTimeout(oThis.oGallery.intPreviewTimer);
				clearTimeout(oThis.oGallery.intTooltipTimer);
				oThis.oGallery.setCurrentPreview(oThis);
			},
			function(){
				oThis.oGallery.intPreviewTimer = setTimeout(
					function(){
						oThis.oGallery.setCurrentPreview(null);
						oThis.getTooltip().hide();
					}
					, oThis.oGallery.HOVER_TIMEOUT
				);
			}
		);
	},
	
	getTooltip : function(){
		return this.oGallery.oTooltip;
	},
	
	select : function(){
		this.jPtr.addClass("selected");
		this.oGallery.oTooltip.add_text(this.sText);
	},

	deselect : function(){
		this.jPtr.removeClass("selected");
	}
	
}


$(function() {
	new Gallery();
});