(function($){
	
/* DOM READY */
$(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() {
		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;

			$img.attr('rel', 'colorbox-group1');
			
			doResize.call($img, contentWidth);

			if(!$img.parent('a').length && !$img.attr('onclick')) {
				var aWrap = document.createElement('a');
				aWrap.href = $img.attr('src');
				aWrap.className = '';
				aWrap.setAttribute('rel', 'colorbox-group1');
				$img.wrap(aWrap);
			}
		});

		/* live 이벤트로 적용 (image_gallery 컴포넌트와의 호환 위함) */
		$('img[rel=colorbox-group1]', this).live('mouseover', function() {
			aWrap = $(this).parent('a').attr('href', $(this).attr('src'));
		});
	});
});

})(jQuery);
