/**
 * @author BNU <bnufactory@gmail.com>
 * http://bnufactory.com/
 *
 * Licence:
 * Highslide JS is licensed under a Creative Commons Attribution-NonCommercial 2.5
 * License (http://creativecommons.org/licenses/by-nc/2.5/).
 *
 * You are free:
 *   * to copy, distribute, display, and perform the work
 *   * to make derivative works
 *
 * Under the following conditions:
 *   * Attribution. You must attribute the work in the manner  specified by  the
 *     author or licensor.
 *   * Noncommercial. You may not use this work for commercial purposes.
 **/
jQuery(function($) {

	$.extend(hs, {
		graphicsDir : request_uri + 'addons/highslide/highslide/graphics/',
		outlineType: 'rounded-white',
		lang : {
			restoreTitle : ''
		}
	});

	var regx_skip = /(?:(modules|addons|classes|common|layouts|libs|widgets|widgetstyles)\/)/i;
	var regx_allow_i6pngfix = /(?:common\/tpl\/images\/blank\.gif$)/i;
	var highslideWrapObj = $('<a class="highslide" rel="highslide" />');
	var dummy = $('<div styel="height:1; overflow:hidden; opacity:0; display:block; clear:both;"></div>');

	$('div.xe_content').each(function() {
		dummy.appendTo(this);
		var contentWidth = dummy.width();
		dummy.remove();
		if(!contentWidth) return;

		$('img', this).each(function() {
			var $img = $(this);
			var imgSrc = $img.attr('src');
			if(regx_skip.test(imgSrc) && !regx_allow_i6pngfix.test(imgSrc)) return;

			// highslide
			if(!$img.parent('a').length) {
				var wrapObj = highslideWrapObj.clone().attr('href', imgSrc);
				$img.wrap(wrapObj);
				wrapObj = null;
			}

			var beforSize = {'width':$img.width(), 'height':$img.height()};
			if(beforSize.width && beforSize.width < contentWidth) return;
			var resize_ratio = contentWidth / beforSize.width;

			$img
				.removeAttr('width').removeAttr('height')
				.css({
					'width':contentWidth,
					'height':parseInt(beforSize.height * resize_ratio, 10)
				});
		});
	});
	dummy = null;
	highslideWrapObj = null;
});

