var j = jQuery.noConflict();
					
function imgFit(img, maxW)
{
	if (typeof(img.naturalHeight) == undefined) {
		img.naturalHeight = img.height;
		img.naturalWidth  = img.width;
	}

	if (img.width > maxW) {
		img.height = Math.round((maxW/img.width)*img.height);
		img.width  = maxW;
		img.title  = imgTitle1;
		img.style.cursor = 'pointer';
		return false;
	}
	else if (img.width == maxW && img.width < img.naturalWidth) {
		img.height = img.naturalHeight;
		img.width  = img.naturalWidth;
		img.title  = imgTitle2;
		return false;
	}
	else {
		return true;
	}
}

function initPostImages(context)
{
	var $in_spoilers = j('.sp-body var.postImg', context);

	j('var.postImg', context).not($in_spoilers).each(function()
	{
		var $v = j(this);
		var src = $v.attr('title');
		var img_align = $v.attr('align') != undefined ? ' align="' + $v.attr('align') + '"' : '';
		var $img = j('<img src="' + src + '"' + img_align + ' class="' + $v.attr('className') + '" alt="" />');

		if (window.banned_hosts && src.match(banned_hosts)) {
			return j(this).replaceWith('<a href="#" title="' + imgBanned + '"><img  src="' + oops + '" alt="Banned!" /></a>');
		}
		else {
			$img = fixPostImage($img);
		}

		if (fancyEffect == false)
		{
			// Original
			$img.bind('click', function(){ imgFit(this, maxW); });
		}
		// Fancybox
		else
		{
			$img.bind('click', function() {
				j(".postImg").fancybox({
					'modal': false,
					'titleShow': true,
					'titlePosition': 'inside',
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'hideOnContentClick' : true,
					'autoScale': true,
					'autoDimensions': true,
					'type': 'image',
					'href': src,
					'scrolling': 'auto',
					'titleFormat': function(title, currentArray, currentIndex, currentOpts)	{
						return '<a href="' + src + '" target="_blank">' +  src + '</a>';
					}
				});
			});
		}

		j('#preload').append($img);
		var loading_icon = '<a href="' + src + '" target="_blank"><img src="' + loading + '" alt="" /></a>';
		$v.html(loading_icon);
		if (j.browser.msie || j.browser.opera)
		{
			$img.one('load', function(){ imgFit(this, maxW); });
			$v.empty().append($img);
			$v.after('<wbr>');
		}
		else
		{
			$img.one('load', function()
			{
				imgFit(this, maxW);
				$v.empty().append(this);
			});
		}
			
		var is_href = $v.parent().attr('href');
		if (is_href) {
			$img.unbind('click');
		}

	});
}
		
function initSpoilers(context)
{
	j('.sp-body', context).each(function()
	{
		var $sp_body = j(this);
		var name = this.title || spoilerTitle;
		this.title = '';
		j('<div class="sp-head folded clickable">' + name + '</div>').insertBefore($sp_body).click(function(e)
		{
			if (!$sp_body.hasClass('inited'))
			{
				initPostImages($sp_body);
				$sp_body.prepend('<div class="clear"></div>').append('<div class="clear"></div>').addClass('inited');
				j('<div class="sp-head unfolded clickable">' + spoilerClose + '</div>').insertAfter($sp_body).click(function()
				{
					if(j(document).scrollTop() > $sp_body.prev().offset().top) {
						j(document).scrollTop($sp_body.prev().offset().top - 200);
					}
					j(this).slideToggle('fast');
					$sp_body.slideToggle('fast');
					$sp_body.prev().toggleClass('unfolded');
				});
			}
			else {
				$sp_body.next().slideToggle('fast');
			}
			if (e.shiftKey)
			{
				e.stopPropagation();
				e.shiftKey = false;
				var fold = j(this).hasClass('unfolded');
				j('.sp-head', j($sp_body.parents('td')[0])).filter( function(){ return j(this).hasClass('unfolded') ? fold : !fold } ).click();
			}
			else
			{
				j(this).toggleClass('unfolded');
				$sp_body.slideToggle('fast');
			}
		});
	});
}
		
function fixPostImage($img)
{
	var src = $img[0].src;
	// keep4u
	if (src.match(/keep4u/i))
	{
		var new_src = src.replace(/http:\/\/keep4u.ru\/imgs\/\w\/(.*)\/(.*)\.(.*)/, "http://keep4u.ru/imgs/s/$1/$2.$3");
		$img.attr('src', new_src).addClass('clickable');
	}
	return $img;
}

function initPost(context)
{
	initPostImages(context);
	initSpoilers(context);
}

var setInnerHTML = function(oElement, sToValue)
{
	if (oElement) {
		oElement.innerHTML = sToValue;
		initPost(oElement);
	}
}

jQuery(document).ready(function($) {
	$('body').each(function() {initPost($(this))});
});
