/**
* imgResize // 10/01/2010 // jQuery 1.4.4+
* http://twelve6.com/rep/jqresize.js
* Luis Ramirez @ twelve6.com
* call this plug in Ex:
*$('img#yourimage').imgResize();
*options: animate: true or false;
*speed: 1000 //////1sec for animation
*/


(function($) {
 $.fn.extend({ 
 	imgResize: function(options) {
 		var defaults = {
 		autoCenter: false, //future
 		animate: false,
 		speed: 1000,
 		maxWidth: 10000, //future
 		maxHeight: 10000, //future
 		minWidth: 0, //future
 		minHeight: 0 //future
 		};
 		
 		var options = $.extend(defaults, options);
 		
		return this.each(function() {
			var o = options;
			
			var obj = $(this);
			
			//get all images inside obj
			var items = $('img', obj);

			

  	//this fixed safari issues with loading js before page load as opposed to just the resize(obj); declaration
  	$(window).load(function() {
 		resize(obj);
	});

	
	$(window).bind('resize', function() {
	    resize(obj);
	});//end window bind resize
	
	//resize the image, based on windows width and height
    function resize(obj){
        var windowH      = $(window).height();
        var windowW      = $(window).width();
        var theImage     = new Image();
        theImage.src     = obj.attr("src");
        var imgwidth     = theImage.width;
        var imgheight    = theImage.height;

        if((imgwidth > windowW)||(imgheight > windowH)) {
            if(imgwidth > imgheight){
                var newwidth = windowW;
                var ratio = imgwidth / windowW;
                var newheight = imgheight / ratio;
                theImage.height = newheight;
                theImage.width= newwidth;
                if(newheight > windowH) {
                    var newnewheight = windowH;
                    var newratio = newheight/windowH;
                    var newnewwidth =newwidth/newratio;
                    theImage.width = newnewwidth;
                    theImage.height= newnewheight;
                }
            }
            else{
                var newheight = windowH;
                var ratio = imgheight / windowH;
                var newwidth = imgwidth / ratio;
                theImage.height = newheight;
                theImage.width= newwidth;
                if(newwidth > windowW) {
                    var newnewwidth = windowW;
                    var newratio = newwidth/windowW;
                    var newnewheight =newheight/newratio;
                    theImage.height = newnewheight;
                    theImage.width= newnewwidth;
                }
            }
        }
        if((o.animate == true)&&(!$.browser.msie))
            obj.stop(true).animate({
                'width':theImage.width+'px',
                'height':theImage.height+'px'
            },o.speed);
        else
            obj.css({
                'width':theImage.width+'px',
                'height':theImage.height+'px'                
            });
    }
	
	
});//return this each /main code above this
}//plug in name end
}); //fn extend end
})(jQuery);//end ready
