/* jScale Image Scaler v1.01
* Last updated: Aug 6th, 2009: Fixed bug when "ls (largest side)" option is used
* Author: JavaScript Kit at http://www.javascriptkit.com/
* Visit http://www.javascriptkit.com/script/script2/jScale/ for full source code
*/
/*
 * Changed: Projecto24 - ECRO 20110218 - Added setting.k for cropping (uses css 'top'/'left').
 */
jQuery.jScale={
	getnewSize:function(side, nvalue){
		var otherside=(side=="w")? "h" : "w"
		if (typeof nvalue=="undefined" || nvalue==null) //if this side has no explicit size set, scale it
			var newSize=this.ndimensions[otherside] * this.odimensions[side] / this.odimensions[otherside]
		else
			var newSize=(/%/.test(nvalue))? parseInt(nvalue)/100 * this.odimensions[side] : parseInt(nvalue)
		this.ndimensions[side]=Math.round(newSize)
	},
	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
		var sortbysize=(this.odimensions.w>this.odimensions.h)? ["w","h"] : ["h","w"] //array to determine [largerside, shorterside]
		this.ndimensions={}
		if (typeof setting.ls!="undefined"){ //if setting.ls defined
			setting[sortbysize[0]]=setting.ls //set the correct side to the longest side's value setting
			setting[sortbysize[1]]=null
		}
		var sortbyavail=(setting.w)? ["w","h"] : (setting.h)? ["h","w"] : [] //check which side to work on based on availibility (which property is set by user)
		if (sortbyavail.length>0){
			this.getnewSize(sortbyavail[0], setting[sortbyavail[0]]) //work on side with property that's defined for sure first
			this.getnewSize(sortbyavail[1], setting[sortbyavail[1]]) //work on side with property that may or may not be defined last
			var callbackfunc=callback || function(){}
			if (setting.k>0){ //ECRO 20110218
				if (setting.w) {
					var lvTop = Math.round((parseInt(setting.k) - this.ndimensions.h) / 2);
					$(imgref).css({ position: 'relative', top: lvTop + 'px', left: '0px' });
				}
				else if (setting.h) {
					var lvLeft = Math.round((parseInt(setting.k) - this.ndimensions.w) / 2);
					$(imgref).css({ position: 'relative', top: '0px', left: lvLeft + 'px' });
				}
			}
			if (setting.speed>0)
				$(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'})
				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" || 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)
			})
		}
	})
};


/**
 * Projecto24 - Faz scale da image e crop para ajustar à div que a contem.~
 * 				(Baseado no jScale)
 * Created	-	ECRO 	2011-02-28
 */
jQuery.kScale={
	getnewXSize:function(nWidth, nHeight){
		var scWidth = nWidth;
		var scHeight = scWidth * this.odimensions.h / this.odimensions.w;
		if( scHeight < nHeight ) {
			scHeight = nHeight;
			scWidth = scHeight * this.odimensions.w / this.odimensions.h;
		}
		this.ndimensions["h"]=Math.round(scHeight);
		this.ndimensions["w"]=Math.round(scWidth);
	},
	getnewXDimensions: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
		//calculates the new dimensions
		this.ndimensions={}
		this.getnewXSize( parseInt(setting.w), parseInt(setting.h) );
		//sets the new dimensions
		var callbackfunc=callback || function(){}
		var lvTop = Math.round((parseInt(setting.h) - this.ndimensions.h) / 2);
		var lvLeft = Math.round((parseInt(setting.w) - this.ndimensions.w) / 2);
		$(imgref).css({ position: 'relative', top: lvTop + 'px', left: lvLeft + 'px' });
		if (setting.speed>0)
			$(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'});
			callbackfunc.call(imgref)
		}
		$tempimg.remove()
	}
};
jQuery.fn.kScale=function(setting, callback){
	return this.each(function(){ //return jQuery obj
		var imgref=this
		if (typeof setting=="undefined" || imgref.tagName!="IMG")
			return true 	  	//skip to next matched element
		if (imgref.complete){ 	//account for IE not firing image.onload
			jQuery.kScale.getnewXDimensions(jQuery, imgref, setting, callback)
		}
		else{
			$(this).bind('load', function(){
				jQuery.kScale.getnewXDimensions(jQuery, imgref, setting, callback)
			})
		}
	})
};
