﻿jQuery.fn.imageAutoSize = function(width,height)
{
	
	var appname = navigator.appName.toLowerCase();
	
	function IEBrower(event) {
	    if (this.readyState == "complete") {
	    	changeSize(this);
	    }
	}

	function FFBrower(event) {
	    if (this.complete == true) {
	    	changeSize(this);
	    }
	}
	
	function changeSize(obj) {
		
		var image = $(obj);
		
        if(image.width()>width)
        {

            image.height(width/image.width()*image.height());
            image.width(width);
         }
        if(image.height()>height)
        {

            image.width(height/image.height()*image.width());
            image.height(height);
        }
        
        image.show();
	}
	
	
    $("img",this).each(function(width,height)
    {
    	
    	if (appname.indexOf("netscape") == -1) {
            //ie
            $(this).bind("readystatechange"
                        , { width: width, height: height }
                        , IEBrower);
        } else {
        	//firefox
            $(this).bind("load"
                        , { width: width, height: height }
                        , FFBrower);
        }
    	
       
    });
};
