// jScaler ~ resize and scale images to fit parent container
// written by Tristan Phipps
// version 0.1 // 05 Jan 2012

(function( $ ) {


	var methods = {
    init : function( options ) { 
	 this.each(function() {
		var $container = $(this); 
		var ciw = $container.width();
		var cih = $container.height();
		var $image = $container.find('img');
		var _iw = $image.width();
		var _ih = $image.height();
		var iw = $image.width();
		var ih = $image.height();
		$image.css('position','absolute').css('display','block');
		
		var yPos = 0;
		var xPos = 0;
		
		

			var ratio = iw / ih;
			iw = ciw;
			ih = (iw / (ratio));

			if(ih < cih){
				ih = Math.ceil(cih);
				iw = Math.ceil(ih * ratio);

			}
			

			
		if(iw >= ciw){	
			var xPos = Math.floor(-(iw - ciw) / 2);
		} else  if(iw < ciw){
			var xPos = 0;
		}
		
		if(ih >= cih){
			var yPos = 0;
		} else  if(ih < cih){
			var yPos = 0;
		}
		$image.css('left', xPos + 'px').css('top', yPos + 'px').css('height', ih + 'px').css('width', iw + 'px');

		
	});
	}
  }

  $.fn.jScaler = function( method ) {
    

    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    
  
  };
  

})( jQuery );



$(document).ready(function(){
		$('.bkimg').jScaler();
});

$(window).resize(function(){
		$('.bkimg').jScaler();
});











