function imgFit (img, maxW, src)
{
	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  = 'Click image to view full size';
		img.style.cursor = 'hand';
		img.style.border = '3px ridge white';
		img.onclick = function() {return hs.expand(this, { src: src })};  
		img.onmouseover = function(){return this.style.border='3px inset grey';};
		img.onmouseout = function(){return this.style.border='3px ridge white';};
		return false;
	}
	else if (img.width == maxW && img.width < img.naturalWidth) {
		img.height = img.naturalHeight;
		img.width  = img.naturalWidth;
		img.title  = 'Click to fit in the browser window';
		return false;
	}
	else {
		return true;
	}
}

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

function initPostImages(context)
{
   var $in_spoilers = $('div.sp-body var.postImg', context);
   $('var.postImg', context).not($in_spoilers).each(function(){
      var $v = $(this);
      var src = $v.attr('title');
      var $img = $('<img src="'+ src +'" class="'+ $v.attr('className') +'" alt="IMG" />');
      $img = fixPostImage($img);
//    $img.bind('click', function(){ return imgFit(this, maxW, src); });

      $('#preload').append($img);
      var loading_icon = '<a href="'+ src +'" target="_blank"><img src="./images/pic_loading.gif" alt="" border="0"/></a>';
      $v.html(loading_icon);

      if ($.browser.msie || $.browser.opera) {
         $img.one('load', function(){ imgFit(this, maxW, src);   });
         $v.empty().append($img);
         $v.after('<wbr>');
      }
      else
      {
         $img.one('load', function(){
            imgFit(this, maxW, src);
            $v.empty().append(this);
         });
      }
   });
}
function initSpoilers(context)
{
	$('div.sp-body', context).each(function(){
		var $sp_body = $(this);
		var name = this.title || '{L_HIDDEN_TEXT}';
		this.title = '';
		$('<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');
			}
			if (e.shiftKey) {
				e.stopPropagation();
				e.shiftKey = false;
				var fold = $(this).hasClass('unfolded');
				$('div.sp-head', $($sp_body.parents('td')[0])).filter( function(){ return $(this).hasClass('unfolded') ? fold : !fold } ).click();
			}
			else {
				$(this).toggleClass('unfolded');
				$sp_body.slideToggle('fast');
			}
		});
	});
}
function fixPostImage ($img)
{
	var banned_image_hosts = /imagebanana|hidebehind/i;  // imageshack
	var src = $img[0].src;
	// keep4u
	if (src.match(/keep4u/i)) {
		var new_url = src.replace(/(.*?)\/imgs\/s\/(.*?)/, "$1/imgs/b/$2");
		$img.attr('src', src).addClass('clickable').bind('click', function() {return hs.expand(this, { src: new_url })});
	}
	else if (src.match(/ipicture/i)) {
		var new_url = src.replace(/(.*)\/(thumb.?)\/(.*)/, "$1/$3");
		$img.attr('src', src).addClass('clickable').bind('click', function() {return hs.expand(this, { src: new_url })});
	}
	else if (src.match(/immage.de/i)) {
		var new_url = src.replace(/(.*)thumb_(.*?)/, "$1/$2");
		$img.attr('src', src).addClass('clickable').bind('click', function() {return hs.expand(this, { src: new_url })});
	}
	else if (src.match(/radikal.ru/i)) {
		var new_url = src.replace(/(.*?)t\.(....?)/, "$1.$2");
		$img.attr('src', src).addClass('clickable').bind('click', function() {return hs.expand(this, { src: new_url })});
	}
	else if (src.match(/amc.normaplus.com\/data\/posters\/.*_resize/i)) {
		var new_url = src.replace(/(.*)_resize(.*)/, "$1$2");
		$img.attr('src', src).addClass('clickable').bind('click', function() {return hs.expand(this, { src: new_url })});
	}
	else if (src.match(/10pix.ru/i)) {
		var new_url = src.replace(/(.*?10pix.ru.*?)\.th(\..*?)/, "$1$2");
		$img.attr('src', src).addClass('clickable').bind('click', function() {return hs.expand(this, { src: new_url })});
	}
	else if (src.match(/fastpic.ru/i)) {
		var new_url = src.replace(/(.*?)\/thumb|view\/(.*?)\.(jp.?g|png)/, "$1/big/$2.$3");
		$img.attr('src', src).addClass('clickable').bind('click', function() {return hs.expand(this, { src: new_url })});
	}
	else if (src.match(/fotogang.ru/i)) {
		var new_url = src.replace(/(.*?)\/thumbs\/(.*?)/, "$1/images/$2");
		$img.attr('src', src).addClass('clickable').bind('click', function() {return hs.expand(this, { src: new_url })});
	}
	else if (src.match(banned_image_hosts)) {
		$img.wrap('<a href="'+ this.src +'" target="_blank"></a>').attr({ src: "/images/tr_oops.gif", title: "Host for this image is banned!" });
	}


	return $img;
}