

var postImg_MaxWidth = screen.width - 441;
var postImgAligned_MaxWidth = Math.round(screen.width/3);
var hidePostImg =	false;



function initPost(context)
{
	initPostImages(context);
	initSpoilers(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('titale');
		var srf = $v.attr('alt');
		var $img = $('<img src="'+ src +'" class="'+ $v.attr('className') +'" style="margin:4px" title="'+ srf +'" />');

		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="/templates/dcmagnets/images/wait.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 = $.trim(this.title) || 'скрытый текст';
		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(){
	$('div.post_body, div.signature').each(function(){ initPost( $(this) ) });
});
function imgFit (img, maxW)
{
	if (typeof(img.naturalHeight) == 'undefined') {
		img.naturalHeight = img.height;
		img.naturalWidth  = img.width;
	}
//alert(img.src+'\n\n'+'H: '+img.height+'  natH: '+img.naturalHeight+'\n'+'W: '+img.width+'  natW: '+img.naturalWidth+'  maxW: '+maxW);
	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 = 'move';
		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;
	}
}



var user = {
	opt_js: {"only_new":0,"h_flag":0,"h_av":0,"h_rnk_i":0,"h_post_i":0,"i_aft_l":0,"h_smile":0,"h_sig":0,"sp_op":0,"tr_tm":0},

	set: function(opt, val, days, reload) {
		this.opt_js[opt] = val;
		setCookie('opt_js', $.toJSON(this.opt_js), days);
		if (reload) {
			window.location.reload();
		}
	}
}

function Me(a){ 
var e=document.getElementById(a);
if(!e)return true;
if(e.style.display=="none"){
e.style.display="block"
} else {
e.style.display="block"
}
return true;
}




function Cl(a){ 
var e=document.getElementById(a);
if(!e)return true;
if(e.style.display=="none"){
e.style.display="none"
} else {
e.style.display="none"
}
return true;
}



var height=0; var width=0;
if (self.screen) {
width = screen.width
height = screen.height
}
else if (self.java) {
var jkit = java.awt.Toolkit.getDefaultToolkit();
var scrsize = jkit.getScreenSize();
width = scrsize.width;
height = scrsize.height;
}

if (width > 1024){
    document.writeln('<style type="text/css" media="all">');
    document.writeln('@import url(/templates/dcmagnets/css/fixstyle.css);');
    document.writeln('</style>');
}




$(document).ready(function(){
	$('#cse-search-btn, #cse-search-btn-top').click(function(){
		var text_match_input_id = $(this).attr('href');
		var text_match = $('#'+text_match_input_id).val();
		if (text_match == '') {
			$('#'+text_match_input_id).addClass('hl-err-input').focus();
			return false;
		}
		$('#cse-text-match').val( text_match );
		$('#cse-submit-btn').click();
		return false;
	});

	$('#quick-search').submit(function(){
		var action = $('#search-action').val();
		var txt = $('#search-text').val();
		if (txt=='поиск...' || txt == '') {
			$('#search-text').val('').addClass('hl-err-input').focus();
			return false;
		}
		if (action == 'cse') {
			$('#cse-search-btn-top').click();
			return false;
		}
		else {
			$(this).attr('action', action);
		}
	});
});

