/**
 * @author Bluntworks
 */

jQuery.bluntScale = function ($t,$b,p) { 
	// $t ->  scale target (should be an image), b -> boundary ref, p -> percentage of b that t will be scaled to.

	var tw,th,bw,bh;
	var maxw,maxh,nw,nh,r;
	
	
	tw=$t.width();
	th=$t.height();
	
	bw=$b.width();
	bh=$b.height();
	
	maxw = Math.round((bw*p)/100);
	maxh = Math.round((bh*p)/100);
			
	r = Math.min( maxw/tw,maxh/th);
			
	nw = tw*r; 
	nh = th*r;
	
	bdb('tw : '+tw + ' th : '+th+' bw : '+bw+' bh : '+bh + ' maxw '+maxw+' maxh : '+maxh+ ' r : '+r);
	$t.css( { 'width' : nw+'px', 'height' : nh+'px'} );
}

jQuery.bluntScaleImage = function (img,$b,p) { 
	// $t ->  scale target (should be an image), b -> boundary ref, p -> percentage of b that t will be scaled to.

	var tw,th,bw,bh;
	var maxw,maxh,nw,nh,r;
	
	
	tw=img.width;
	th=img.height
	

	
	bw=$b.width();
	bh=$b.height();
	
	maxw = Math.round((bw*p)/100);
	maxh = Math.round((bh*p)/100);
			
	r = Math.min( maxw/tw,maxh/th);
			
	nw = tw*r; 
	nh = th*r;
	
	bdb('tw : '+tw + ' th : '+th);
	$(img).css( { 'width' : nw+'px', 'height' : nh+'px'} );
}

jQuery.bluntAlign = function($t,$b,vm,hm) { 
	// $t ->  align target, b -> boundary ref, vm -> vertical align mode, hm ->  horizontal align mode.
	
	var tw,th,bw,bh;
	var top, left;
	
	tw=$t.width();
	th=$t.height();
	
	bw=$b.width();
	bh=$b.height();
	
	
	bdb($t.attr('src')+' tw' + tw + ' bw : '+bw);
	
	(vm == undefined) ? top = ((bh/2) - (th/2)) +'px' : top = vm;
	(hm == undefined) ? left = ((bw/2) - (tw/2))+ 'px' : left = hm;
	
	$t.css({ 'position' : 'absolute', 'top' : top, 'left' : left});
} 

jQuery.bluntAlignImage = function($t,$b,vm,hm) { 
	// $t ->  align target, b -> boundary ref, vm -> vertical align mode, hm ->  horizontal align mode.
	
	var tw,th,bw,bh;
	var top, left;
	
	tw=$t.width()+hm;
	th=$t.height();
	
	bw=$b.width();
	bh=$b.height();
	
	
	bdb($t.attr('src')+' tw' + tw + ' bw : '+bw);
	
	(vm == undefined) ? top = ((bh/2) - (th/2)) +'px' : top = vm;
	(hm == undefined) ? left = ((bw/2) - (tw/2))+ 'px' : left = ((bw/2) - (tw/2))+ 'px';
	
	$t.css({ 'position' : 'absolute', 'top' : top, 'left' : left});
} 

jQuery.bluntAlignTo = function($t,$b,vm,hm) { 
	// $t ->  align target, b -> boundary ref, vm -> vertical align mode, hm ->  horizontal align mode.
	
	var tw,th,bw,bh,bl,bt;
	var top, left;
	
	tw=$t.width();
	th=$t.height();
	
	bw=$b.width()+40;
	bh=$b.height();
	
	bl=Math.floor($b.offset().left);
	bt=Math.floor($b.offset().top);
	
	
	//bdb($t.attr('id')+' bt' + bt + ' bh : '+bh+' vm : '+vm);
	
	(vm == undefined) ? top = (bh/2) - (th/2) : top = (bt+bh)+vm;
	(hm == undefined) ? left = ((bw/2) - (tw/2))+bl : left = hm;
	
	//bdb($t.attr('id')+' top : ' + top );
	
	$t.css({ 'position' : 'absolute', 'top' : top, 'left' : left});
}
