
function rn() {
	var rn = Math.floor(Math.random()*10001);
	return rn;
}

(function($) {
	$.fn.fillPageWithImage = function() {

		// traverse all nodes
		this.each(function() {
						   
			// express a single node as a jQuery object
			var $t = $(this);
			$t.hide();
			
			var winH = $(window).height(); 
			var winW = $(window).width(); 
			var imgH = $t.height(); 
			var imgW = $t.width(); 	
			
			// the lower the ratio, the more 'landscapy' the dimensions are
			var winHwRatio = winH/winW;
			var imgHwRatio = imgH/imgW;

				// the image is the lower ratio. enalarge until img is height of screen + chop off the landscapey ends
				var setImgH;
				var transformRatio;
				var setImgW;
				var offsetValueTop;
				var offsetValueLeft;
			
			if (winHwRatio > imgHwRatio) {
				// the image is the lower ratio. enalarge until img is height of screen + chop off the landscapey ends
				setImgH = winH;
				transformRatio = imgH/winH;
				setImgW = imgW*transformRatio;
				offsetValueTop = '0px';
				offsetValueLeft = '-'+((setImgW - winW)/2)+'px';
				
			} else if (winHwRatio < imgHwRatio) {
				// the window is the lower ratio. enalarge until img is width of screen + chop off the top and bottom
				setImgW = winW;
				transformRatio = winW/imgW;
				setImgH = imgH*transformRatio; 
				offsetValueTop = '-'+((setImgH - winH)/2)+'px';
				offsetValueLeft = '0px';
			} else {
				// they're equal. some kinda miracle.
				setImgW = winW;
				setImgH= winH;
				offsetValueTop='0px';
				offsetValueLeft='0px';
			}
			var cssObj = {
				'height': setImgH+'px', 
				'width': setImgW+'px',
				'position':'absolute',
				'top': offsetValueTop,
				'left': offsetValueLeft
			}
			$t.css(cssObj);
			$t.fadeIn('fast');

		});

		// allow jQuery chaining
		return this;
	};

})(jQuery);



function doJb() {
	//alert("slides");
	$(".jbgallery").jbgallery({
		caption   	: 	true,
		style 		:	'zoom',
		menu 		:	false,
		slideshow 	:	true,
		timers		: 	{
			fade		:	2000,
			interval  	:	9000
		}
	});
}

$(function() {
/*
	$('#page-bg img').load(function(){
		$(this).fillPageWithImage();
	 });	
	 
	 $('#page-bg').cycle({
						 speedOut:1000,
						 speedIn:4000,
						 timeout:4000
					});*/
	 
	
	$("#links").superfish({
		pathLevels:    1,                  // the number of levels of submenus that remain open or are restored using pathClass 
		delay:         800,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing 
		speed:         'normal',           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
		autoArrows:    true,               // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
		dropShadows:   true               // completely disable drop shadows by setting this to false 
	});

	doJb();


});		

LIGHTBOX = {
	lb_o: 'lb_overlay',
	lb_c: 'lb_content',
	closeClass: 'lb_close',
	htmlId: 'lb_html',

	open: function(){
		 //if there's no lb on the page, add it
		 if ($('#'+this.lb_o).length == 0) {
			lb_html = '<div id="'+this.lb_o+'" class="'+this.closeClass +'"></div><div id="'+this.lb_c+'"><a class="'+this.closeClass +'">Close</a><div id="'+this.htmlId+'"></div></div>'
			$(lb_html).appendTo('body');
		  }
		$('#'+this.lb_o).slideDown(500);
		$('#'+this.lb_c).delay(500).fadeIn(500);
		
		return this.closeClass;
	
	},
	
	close: function(){
		$('#'+this.lb_c).fadeOut(1000);											 
		$('#'+this.lb_o).delay(1000).slideUp(1000);			

	},
	loadUrl: function(url){
		$('#'+this.htmlId).load(url);
	}
}

GALLERY = {
	galleryContentId : LIGHTBOX.htmlId, 
	galleryClass : 'gallery',
	open : function(f){
		LIGHTBOX.open();
		GALLERY.loadGallery(f);
	},
	close : function(){
		LIGHTBOX.close();
	},
	loadGallery : function(f){
		$('#'+this.galleryContentId).load('/inc/custom/ajax/galleries/getgallery.asp?rn='+rn()+'&gf='+f, this.cycleGallery);
	},
	cycleGallery : function(){
		$('.' + GALLERY.galleryClass).cycle({
						prev:'h1'
				});			
	}
}


$(function(){
		
	$('a.doGalleryNow').click(function(){
		var gal_folder = $(this).attr('data');
		GALLERY.open(gal_folder);	
	});
	
	$('.lb_close').live('click', function(){
		LIGHTBOX.close();							  
	});
	
});


 
