/**
 *	jQuery.bgFullSize
 *	@version 0.1.5 {beta}
 *	Junio 08, 2008
 *
 *	Author: Ralph Moran
 *	Web: http://www.ralphmoran.com
 *	Contact: ralphmoran2003@gmail.com
 *	MSN: jack_jack40@hotmail.com
 *	Skype: ralph_moran
 *
 *	BGFullSize: "Es un plugin que coloca una imagen como fondo (background) a pantalla completa.
 *				 La imagen es reescalada para evitar la deformación."
 *
 *				 "Tambien BGFullSize tiene la propiedad de centar la imagen que por default
 *				  estará alineada a la izquierda."
 *
 *	@usage 
 *			$("img.bg").bgFullSize(); <-- IMG de la clase BG alineada a la izquierda.
 *			$("img.bg").bgFullSize({center:true}); <-- IMG de la clase BG centrada.
 *
 *	@return {jQuery Object}
*/

(function($)
{
	/**
	 *	LET'S WORKS!...
	 *	
	 *	Inicialización del PlugIn.
	*/
	jQuery.fn.bgFullSize = function(options)
	{		
		var opts = jQuery.extend({}, jQuery.fn.bgFullSize.defaults, options);
		
		// Iteracion de los elementos DOM seleccionados
		return this.each(function()
		{
			var $bg    = jQuery(this);
			var $win   = jQuery(window);								
			var $bgSrc = $bg.src;
			var $bgW   = $bg.width();
			var $bgH   = $bg.height();
				
			jQuery.fn.bgFullSize.setDimensions({bg:$bg, win:$win, bgSrc:$bgSrc, bgW:$bgW, bgH:$bgH, center:opts.center, opts:opts});
			
			$win.bind("resize", function(){	jQuery.fn.bgFullSize.setDimensions({bg:$bg, win:$win, bgSrc:$bgSrc, bgW:$bgW, bgH:$bgH, center:opts.center, opts:opts}); });
		});		
	};
	
	/**
	 *	Propiedades iniciales.
	*/
	jQuery.fn.bgFullSize.defaults = {top:0,left:0,position:"absolute",zIndex:"-9999",center:false};
	
	/**
	 * 	Aplicar las dimensiones.
	*/
	jQuery.fn.bgFullSize.setDimensions = function(props)
	{	
		var $bg    = props.bg; 
		var $win   = props.win;							
		var $bgSrc = props.bgSrc;
		var $bgW   = props.bgW; // <-- Ancho inicial de la imagen
		var $bgH   = props.bgH; // <-- Alto inicial de la imagen
		
		var $winW  = $win.width();
		var $winH  = $win.height();
		var $flo_PorcientoCambio = ( $winH > $bg.height() ) ? $winH/$bgH : $winW/$bgW ; // <-- % de cambio
		
		$bg.width( $flo_PorcientoCambio * $bgW );
		$bg.height( $flo_PorcientoCambio * $bgH );
		
		if( $winW > $bg.width() ){ $bg.width( ($winW/$bgW) * $bgW ); $bg.height( ($winW/$bgW) * $bgH ); }  // <-- Rectificar las dimensiones
		if( $winH > $bg.height() ){	$bg.width( ($winH/$bgH) * $bgW ); $bg.height( ($winH/$bgH) * $bgH ); } // <-- Rectificar las dimensiones
			
		var $center = ( props.opts.center ) ? ($bg.width()/2) - ($winW/2) : 0 ;
		$bg.css({top:props.opts.top, left:"-"+$center+"px", position:props.opts.position, zIndex:props.opts.zIndex});
	};
})(jQuery);
