jQuery(function($){
	var regx_skip = /(?:(modules|addons|classes|common|layouts|libs|widgets|widgetstyles)\/)/i;
	var regx_allow_i6pngfix = /(?:common\/tpl\/images\/blank\.gif$)/i;
	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;
			doResize.call($img, contentWidth);
		});
	});
	/* Making img link -misol님의 exif 애드온과 이미지에 링크 등을 생성하는 데 순서 상에 문제 발생하므로(순서상 exif애드온이 먼저 작동해야 함). js 로드 순서를 정해야 하는데 -1000 이런거 있던데 할줄을 모름. 매뉴얼 봤는데 구 문법기준이라 잘 몰겠음.. */
	$('div.xe_content').find('img').each(function() {
		var t = $(this);
		var img = t.attr('src');
		var title =t.attr('title');
		t.wrap('<a href="'+img+'" rel="prettyPhoto[mixed]" title="'+title+'"></a>');
	});

	/* 동작 코드.  애드온 옵션 처리로 바꿔야 하는데.. 그러나 아래의 코드를 php에서 html로 스크립트로 삽입하면 작동하지 않음.. 조언을 구해야.. */
	$("div.xe_content: a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'normal',theme:'facebook',slideshow:5000, autoplay_slideshow:false});
});
