jQuery(function($) {
var regx_skip = /(?:(modules|addons|classes|common|layouts|libs|widgets|widgetstyles)\/)/i;
	var regx_allow_i6pngfix = /(?:common\/tpl\/images\/blank\.gif$)/i;
	/**
	 * º»¹® Æø ±¸ÇÏ±â À§ÇÑ °³Ã¼
	 * IE6¿¡¼­ º»¹®ÆøÀ» ³Ñ´Â ÀÌ¹ÌÁö°¡ ÀÖÀ¸¸é ±× Å©±â·Î ±¸ÇØÁö´Â ¹®Á¦ ¿ìÈ¸¿ë
	 **/
	var dummy = $('<div style="height:1; overflow:hidden; opacity:0; display:block; clear:both;"></div>');

	/**
	 * ¸®»çÀÌÁî ½ÇÇà ÇÔ¼ö
	 **/
	function doResize(contentWidth, count) {
		// Àç½Ãµµ È¸¼ö Á¦ÇÑ
		if(!count) count = 0;
		if(count >= 10) return;

		var $img = this;
		var beforSize = {'width':$img.width(), 'height':$img.height()};

		// ÀÌ¹ÌÁö »çÀÌÁî¸¦ ±¸ÇÏÁö ¸øÇßÀ» ¶§ Àç½Ãµµ
		if(!beforSize.width || !beforSize.height) {
			setTimeout(function() {
				doResize.call($img, contentWidth, ++count)
			}, 200);
			return;
		}

		// ¸®»çÀÌÁî ÇÊ¿ä ¾øÀ¸¸é ¸®ÅÏ
		if(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)
			});
	}

	$('div.xe_content').each(function() {
		var contentWidth = dummy.appendTo(this).width();
		dummy.remove();
		if(!contentWidth) return;

		$('img', this).each(function() {
			var $img = $(this);
			var imgSrc = $img.attr('src');
			if($img.attr("rawsrc")) imgSrc = $img.attr("rawsrc");
			var title =$img.attr('alt');
			if(regx_skip.test(imgSrc) && !regx_allow_i6pngfix.test(imgSrc)) return;
			doResize.call($img, contentWidth);
			
			$img.wrap('<a href="'+imgSrc+'" rel="xe_gallery" title="'+title+'"></a>');
			
		});
	});
	
});

