/**
* This namespaced selection of utilities methods is made available in the
* frontend of a site.
*
* Requires Buan.
*
* @package Sandfire
*/
if (typeof Buan !== 'undefined') {

	var Sandfire = {
	
		/**
		* Creates a URL that will generate a thumbnail of the specified image at the
		* specified max dimensions.
		*
		* Mirrors the functionality of MediaAssetManager::thumbnailUrl().
		*
		* @param string Original absolute URL
		* @param int Max. width
		* @param int Max height
		* @param bool Don't crop, fit entire image within the dimensions
		* @return string
		*/
		generateThumbnailUrl: function(url, width, height, nocrop) {
			if(!url) {
				return Buan.Config.get('ext.Sandfire.urlRoot')+'/images/media-thumbnail-error.png';
			}
			var mediaUrlRoot = Buan.Config.get('ext.Sandfire.uploads.url');
			var re = new RegExp(mediaUrlRoot);
			var assetRelUrl = url.replace(re, '');
			var assetExt = url.substr(url.indexOf('.')+1);
			if($.inArray(assetExt, ['bmp', 'gif', 'jpg', 'jpeg', 'png', 'swf', 'tiff'])<0) {
				assetExt = 'png';
			}
			width = isNaN(parseInt(width)) ? 0 : Math.max(0, parseInt(width));
			height = isNaN(parseInt(height)) ? 0 : Math.max(0, parseInt(height));
			return mediaUrlRoot+'/.thumbs'+assetRelUrl+'-'+width+'-'+height+(nocrop ? '-nc' : '')+'.'+assetExt;
		}
	};
}

if (typeof $ !== 'undefined') {
	(function ($) {
	
		/**
		* Open "external" links in a new window
		*/
		$('a[rel*=external]').live('click', function () {
			window.open(this.href);
			return false;
		});
	})($.noConflict());
}
