/**
* Fullscreenr - lightweight full screen background jquery plugin
* By Jan Schneiders
* Version 1.0
* www.nanotux.com
* MOD By Jon Barragan - Modificado para usarlo como galería.
**/
(function($){	
	$.fn.fullscreenr = function(options,bgID) {
		if(options.height === undefined) alert('Please supply the background image height, default values will now be used. These may be very inaccurate.');
		if(options.width === undefined) alert('Please supply the background image width, default values will now be used. These may be very inaccurate.');
		if(bgID === undefined) alert('Please supply the background image ID, default #bgimg will now be used.');
		var defaults = { width: 1200,  height: 365, bgID: 'bgimg' };
		var options = $.extend({}, defaults, options);
		
		$(document).ready(function() { $(bgID).fullscreenrResizer(options);	});
		$(window).bind("resize", function() { $(bgID).fullscreenrResizer(options); });
		return this; 		
	};	
	$.fn.fullscreenrResizer = function(options) {
		// Set bg size
		var ratio = options.height / options.width;	
		// Get browser window size
		var browserwidth = $(window).width();
		//var browserheight = $(window).height();
		var browserheight = options.maxHeight;
		// Scale the image
		if ((browserheight/browserwidth) > ratio){
			//imgWidth = (browserheight / ratio);
			//imgHeight = browserheight;
			imgWidth = $(this).width(browserheight / ratio);
			imgHeight = $(this).height(browserheight);
		} else {
			//imgWidth = browserwidth;
			//imgHeight = (browserwidth * ratio);
			imgWidth = $(this).width(browserwidth);
			imgHeight = $(this).height(browserwidth * ratio);
		}
		//$(this).width(imgWidth);
		//$(this).height(imgHeight);
		//alert(imgWidth);
		
		// Center the image
		if (browserwidth <= options.width) {
			anchoNew = options.width;
			altoNew = options.height;
		}else{
			anchoNew = browserwidth;
			altoNew = parseInt(browserwidth*ratio);
		}
		//imgLeft = (browserwidth - $(this).width())/2;
		imgLeft = (browserwidth - anchoNew)/2;
		$(this).css('left', imgLeft);
		//$('#head-acc a').html("BW: "+browserwidth+"<br>TW: "+$(this).width());
		
		//$('#head-acc a').html("BW: "+browserwidth+"<br>TW: "+anchoNew);
		
		//alert(imgLeft);
		//imgTop = (browserheight - $(this).height())/2;
		//$('#head-reg a').html("BH: "+browserheight+"<br>TH: "+$(this).height());
		imgTop = (browserheight - altoNew)/2;
		
		//$('#head-reg a').html("BH: "+browserheight+"<br>TH: "+altoNew);
		
		if (!$('#head-handle').hasClass('abierto')) {
			$(this).css('top', imgTop);
		}else{
			$(this).css('top', imgTop);
		}
		return this; 		
	};
})(jQuery);
