// slider

var postImg_MaxWidth = screen.width - 202;
var postImgAligned_MaxWidth = Math.round(screen.width/3);
var hidePostImg =	false;

function initPost(context)
{
	$('span.post-hr', context).html('<hr align="left" />');
	initQuotes(context);
	initExternalLinks(context);
	initPostImages(context);
	initSpoilers(context);
}
function initQuotes(context)
{

}
function initPostImages(context)
{
	if (hidePostImg) return;
	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="pic">');
		$img = fixPostImage($img);
		var maxW = ($v.hasClass('postImgAligned')) ? postImgAligned_MaxWidth : postImg_MaxWidth;
		$img.bind('click', function(){ return imgFit(this, maxW); });
		if (user.opt_js.i_aft_l) {
			$('#preload').append($img);
			var loading_icon = '<a href="'+ src +'" target="_blank"><img src="http://static.torrents.ru/images/pic_loading.gif" alt=""></a>';
			$v.html(loading_icon);
			if ($.browser.msie) {
				$v.after('<wbr>');
			}
			$img.one('load', function(){
				imgFit(this, maxW);
				$v.empty().append(this);
			});
		}
		else {
			$img.one('load', function(){ imgFit(this, maxW) });
			$v.empty().append($img);
			if ($.browser.msie) {
				$v.after('<wbr>');
			}
		}
	});
}
function initSpoilers(context)
{
	$('div.sp-body', context).each(function(){
		var $sp_body = $(this);
		var name = this.title || 'скрытый текст';
		this.title = '';
		$('<div class="sp-head folded clickable">'+ name +'</div>').insertBefore($sp_body).click(function(){
			if (!$sp_body.hasClass('inited')) {
				initPostImages($sp_body);
				$sp_body.prepend('<div class="clear"></div>').append('<div class="clear"></div>').addClass('inited');
			}
			$(this).toggleClass('unfolded');
			$sp_body.slideToggle('fast');
		});
	});
}
function initExternalLinks(context)
{
	$("a.postLink:not([@href*='"+ window.location.hostname +"/'])", context).attr({ target: '_blank' });
}
$(document).ready(function(){
	$('div.post_body').each(function(){ initPost( $(this) ) });
});
