/* 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
* 
* Modiferad av Martin Nilsson, Basement Creative Studio, 2009-10
* För att INTE skala bilder som är MINDRE än den angivna (max)-storleken i en jQuery-selektion
* Även plockat bort animeringsmöjlig
*/

;(function($) { 
	function getnewSize(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);
		if(side=="w"){if(nvalue>this.odimensions.w){this.oBigger = false}}; // Kollar om sida w är större än max (flaggar i så fall för ej skalning)
		if(side=="h"){if(nvalue>this.odimensions.h){this.oBigger = false}};// Kollar om sida w är större än max (flaggar i så fall för ej skalning)
	};
	
	function getnewDimensions($, 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)
		this.oBigger = true; // true om originalbild större än nya värden (undersökts och sätts i getnewSize
		if (sortbyavail.length>0){
			getnewSize(sortbyavail[0], setting[sortbyavail[0]]); //work on side with property that's defined for sure first
			getnewSize(sortbyavail[1], setting[sortbyavail[1]]); //work on side with property that may or may not be defined last
			var callbackfunc=callback || function(){};
			if(this.oBigger){$(imgref).css({width:this.ndimensions.w+'px', height:this.ndimensions.h+'px'});} // ändra bara om org-storlek större
			callbackfunc.call(imgref);
		}
		$tempimg.remove();
	};

$.fn.bcsImgScale=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
			getnewDimensions(jQuery, imgref, setting, callback)
		}
		else{
			$(this).bind('load', function(){
				getnewDimensions(jQuery, imgref, setting, callback)
			})
		}
	})
};

})(jQuery);