/* jScale Image Scaler v1.0
* Last updated: Feb 18th, 2009. This notice must stay intact for usage 
* Author: JavaScript Kit at http://www.javascriptkit.com/
* Visit http://www.javascriptkit.com/script/script2/jScale/ for full source code
*/

jQuery.jScale={
	getnewSize:function(){
			
		var maxh = parseInt($(window).height() * 0.94);	// max height (95% of window)
		if (maxh > 800) {
			maxh=800;
		}
		var maxw = $(window).width();					// max width
		if (maxw > 1200) {
			maxw=1200;
		}
		
		if (this.odimensions['w'] >= maxw && this.odimensions['h'] >= maxh) {
			//alert(0);
			// w&h too big
			var newh = Math.round(maxw * this.odimensions['h'] / this.odimensions['w']);
			var neww = Math.round(maxh * this.odimensions['w'] / this.odimensions['h']);
			//alert(newh);
			if (newh > maxh) {
				//alert('a');
				this.ndimensions['h']=maxh;
				this.ndimensions['w']=Math.round(this.ndimensions['h'] * this.odimensions['w'] / this.odimensions['h']);
				//alert(this.ndimensions['h']);
			} else {
				//alert('b');
				this.ndimensions['w']=maxw;
				this.ndimensions['h']=Math.round(this.ndimensions['w'] * this.odimensions['h'] / this.odimensions['w']);
			}
			
		} else if (this.odimensions['w'] >= maxw) {
			//alert(1);
			// too wide
			this.ndimensions['w']=maxw;
			this.ndimensions['h']=Math.round(this.ndimensions['w'] * this.odimensions['h'] / this.odimensions['w']);
			
		} else if (this.odimensions['h'] >= maxh) {
			//alert(2);
			// too high
			this.ndimensions['h']=maxh;
			this.ndimensions['w']=Math.round(this.ndimensions['h'] * this.odimensions['w'] / this.odimensions['h']);
			
		} else {
			//alert(3);
			// fits: show in original size
			this.ndimensions['w']=this.odimensions['w'];
			this.ndimensions['h']=this.odimensions['h'];
		}

	},
	getnewDimensions:function($, imgref, setting, callback){
 		//create temporary floating image to get original image's true dimensions (in case width/height attr set)
		var $tempimg=$('<img src="'+imgref.src+'" style="position:absolute; top:0; left:0; visibility:hidden" />').prependTo('body');
		this.odimensions={w:$tempimg.width(), h:$tempimg.height()}; //get image dimensions
		
		this.ndimensions={};
		this.getnewSize();
		var callbackfunc=callback || function(){}
		if (setting.speed>0) {
			//$(imgref).toggleClass('invisible');
			$(imgref).animate({width:this.ndimensions.w+'px', height:this.ndimensions.h+'px'}, setting.speed, callbackfunc);
		} else {
			$(imgref).css({width:this.ndimensions.w+'px', height:this.ndimensions.h+'px'});
			//$(imgref).toggleClass('invisible');
			callbackfunc.call(imgref);
		}
		$tempimg.remove();
	}
};

jQuery.fn.jScale=function(setting, callback){
	return this.each(function(){ //return jQuery obj
		var imgref=this;
		if (typeof setting=="undefined") {
			setting = {};
		}
		if (imgref.tagName!="IMG") {
			return true; //skip to next matched element
		}
		if (imgref.complete){ //account for IE not firing image.onload
			jQuery.jScale.getnewDimensions(jQuery, imgref, setting, callback);
		}
		else{
			$(this).bind('load', function(){
				jQuery.jScale.getnewDimensions(jQuery, imgref, setting, callback);
			});
		}
	});
};