(function($){
$.thread = {
article: function($id,$sid) {
	var _s = $.thread.s;
	_s.id = $id||_s.id||0;
	_s.secretid = $sid||_s.secretid||0;
	_s.completed = false;

	$.post($.thread.s.cgi,
	{ func: 'get', id: _s.id, secretid: _s.secretid },
	function($j) {
		var _o = JSON.parse($j);
		if (!_o || _o.status == 'err') {
			$('#content').addClass('err').html($.Trans.NotExists);
			return false;
		}

		var _flag 
			= ((_o.flag & (1<<29)) ? 'new'
			: ((_o.flag & (1<<7))  ? 'nil' 
			: (((_o.flag & 24576) == 24576) ? 'pri' 
			: ((_o.flag & (1<<14)) ? 'fri' : 'pub'))));

		if (_o.flag & (1<<10)) { _s.locked = true };

		$('#content').html('<table class="columns"><tr><\/tr><\/table>');
		var _tab = $('#content table.columns').width(800);
		_tab.find('tr:first').html('<td><div id="thread" class="corners"><\/div><\/td><td><div class="col"><\/div><\/td>');

		if (_s.uid != _o.name && $id)
			$('#display').prepend('<a href="/'+_o.login+'"><img class="i" src="/='+_o.name+'&s=30"><\/a>');

		$.thread.story(_o);

		$.thread.images(_tab);
		$.thread.drawer(_tab,_flag,_o.id,_o.name);

		$('div.corners').css({width: '570px',padding: '9px'}).corners();
		$.thread.viewed();
		$.thread.replies();
	});
},
story: function($o) {
	var _s = $.thread.s;
	$('#thread').html('<img class="star" id="s'+$o.id+'" src="/=i_star'+($o.flag & (1<<30) ? 1:2)+'.gif">'
		+ '<div class="fade">'+ $o.stamp +'<\/div>'
		+ '<h1>' + $o.subject + '<\/h1>' + $o.text
		+ '<div class="data">'
		+ ($o.url?'<a target="_blank" class="id" href="'+$o.url+'"><img class="noprint" src="/=i_link.gif"><\/a>':'')
		+ (_s.locked ? '<img style="float:right" title="'+$.Trans.Locked+'" src="/=i_lock.png">'
		: '<img style="float:right" class="hide" title="'+$.Trans.Reply+'" src="/=i_reply.png">')
		+ $.Trans.By
		+ ' <a href="/'+$o.login+'">' + $o.display + '<\/a> //&nbsp;'
		+ $.aqStamp($o.uts,true) + ($o.flag & (1<<12)?'&middot;':'')+ ' //&nbsp;'
		+ $o.read + ' ' + $.Trans.Views + ' //&nbsp;'
		+ $o.children + ' ' + $.Trans.Comments + ' //&nbsp;'
		+ $o.score + ' ' + $.Trans.Points + ' //&nbsp;'
		+ '<a class="noprint" href="/'+($o.secretid||$o.id)+'">'
		+ ($o.secretid?'Privalink':'Permalink')+'<\/a>'
		+ '<\/div>'
	).data('id',$o.id).data('ctrl','repl').data('locked',_s.locked);
},
drawer: function($div,$img,$id,$owner) {
	var _s = $.thread.s;

	var _drawer = $('<div class="drawer"><span><\/span><\/div>')
		.css({backgroundColor: $.mau.s3, color: $.mau.s2,
			padding: '5px',
			marginTop: $('#thread').css('marginBottom').replace(/px/,'')*-1
		}).insertAfter('#thread').find('> span');
	
	$('<img class="b" title="'+$img+'" src="/=i_'+$img+'.gif">')
		.css({margin: '2px',border: 0}).appendTo(_drawer);
	_drawer.append('<a href="javascript:$.thread.replyto('+$id+')">'
		+($('#thread').data('locked')? $.Trans.Locked:$.Trans.Reply)+'<\/a>');

	if ((_s.friend == 'owner' && $id == _s.id) 
		|| ($.defaults.id == $owner) || $.defaults.admin) {
		_drawer.find('img.b')
		.css({cursor:'pointer'})
		.click(function(){
			var _self = $(this);
			$(this).securibox({ cb: function($i){
				$.thread.chmod({perm:$i,id:$id},function($state){
					return false;
				});
			}});
		});
		_drawer.append('|<a href="javascript:$.thread.lock('+$id+')">'+(_s.locked?$.Trans.Unlock:$.Trans.Lock)+'<\/a>'
		+ ' | <a href="javascript:$.thread.edit('+$id+')">'+$.Trans.Edit+'<\/a>'
		+ ' | <a href="javascript:$.thread.move('+$id+')">'+$.Trans.Move+'<\/a>');
	} 

	var _w = _drawer.width()+70;
	_drawer.parent().css({width: _w, marginLeft: 550-_w});
	_drawer.find('A').css({color:$.mau.s6,padding:'0px 5px'}).hover(
		function(){$(this).css({backgroundColor:$.mau.s4,color:$.mau.sF})},
		function(){$(this).css({backgroundColor:'transparent',color:$.mau.s6})}
	);
	$('div.drawer').corners("5px transparent bottom")
},
move: function($id) {
	$.nvzDialog({ yOffset: 0, buttons: 0, bgcolor: 'transparent', closeBtn: false,
		dlgPadding: '3px', dlgBgColor: '#000', include: '/:?_ob=forums&cur='+($.thread.s.forum||$.thread.s.name)+'&id='+$id,
		buttons: [[$.Trans.Close, "$.nvzDialog.close()"] ] });
},
lock: function($id) {
	$.post($.thread.s.cgi,{func:'chmod',perm:'loc',id:$id},function($j){
		var _o = JSON.parse($j);
		if (_o.status == 'ok')
			document.location.href = '/'+$id;
		else alert($.Trans.ESystemError);
	});
},
replyto: function($id) {
	var _s = $.thread.s;
	if ($id != _s.id && $('#r'+_s.id).find('img.hide').attr('alt') == 'reply')
		$('#r'+_s.id).find('img.hide').trigger('click');
	else 
		$('#thread img.hide').trigger('click');
},
chmod: function($o,$cb) {
	$.post($.thread.s.cgi,{func:'chmod',perm:$o.perm,id:$o.id},function($j){
		var _o = JSON.parse($j);
		if (_o.status == 'ok')
			$cb(_o.state);
		else alert($.Trans.ESystemError);
	});
},
images: function($div) {
	if (!$div) return false;
	$div.find('div.image')
		.each(function(){
			var _self = $(this);
			var _i = new Image;
			var _ihandler = function() {
				if (_i.width >= 560) {
					_i.width = 560;
				}
				var _alt = _self.find('img').width(_i.width).attr('alt');
				if (_alt)
					_self.append('<br>'+_alt).width(_i.width);
				else
					_self.css('backgroundColor','transparent');
				if (_i.width > 470) {
					var _pad = (570 - _i.width) / 2;
					_self.css({float:'none',clear:'both',marginLeft:_pad+'px'});
				}
				_self.fadeIn();
			};
			_i.onload = _ihandler;
			_i.src = $(this).find('img').attr('src');
			if (_i.complete) _ihandler;
		})
		.click(function(){
			var _img = $(this).find('img').attr('src').replace(/_[mts]\.jpg/,'.jpg');
			if (_img.match(/flickr/)) {
				$.nvzDialog({ yOffset: 0, buttons: 0, bgcolor: 'transparent',
					dlgPadding: '3px 3px 0px 3px', dlgBgColor: '#000', image: _img });
			}
		});
},
dress: function() {
	if ($.thread.s.completed) {
		if (!$.defaults.id) 
			$('div.corners').find('img.star, div.star img:first').remove();
		$('div.corners')
			.corners()
			.find('img.star, div.star img:first')
			.unbind('click')
			.click(function(){
				var _id = $(this).attr('id').replace(/^s/,'');
				if (_id) $.reply.star(_id);
			});
		$('div.corners').find('img').css({cursor:'pointer'});
		if ($.thread.s.scroll)
			scroll(0,$('#replies').position().top);

		$('#thread div.source').prepend($.Trans.Source+': ')
			.css({textAlign: 'right',color: $.mau.sA, marginTop: '5px'});
	} else
		setTimeout("$.thread.dress()",500);
},
viewed: function() {
	var _v = $.nvzCookie.get('_view');
	if (!_v || _v.match(/^[&1]|null|undefined/)) _v = '';
	if (_v.indexOf($.thread.s.id+'&') == -1) {
		_v = !_v ? '' : (_v + '&');
		if (_v.length > 500)
			_v = _v.slice(300).replace(/&\d{2,}&?$/,'');
		$.nvzCookie.set('_view', _v+$.thread.s.id+'&1');
	};
},
replies: function() {
	var _s = $.thread.s;
	_s.completed = false;
	$.thread.dress();

	$.post($.thread.s.cgi, 
	{func: 'get', replies: 1, id: _s.id, secretid: _s.secretid},
	function($j) {
		_s.completed = true;
		var _j = JSON.parse($j);
		if (!_j || _j.status == 'err')
			return false;

		var _json = JSON.parse($j);
		var _o = _json.data;

		var _this = null;
		if (_json.type == 'large') {
			$('#replies').empty();
			$('<div class="replies"><\/div>').width(266*3).appendTo('#replies');
			_this = $('#replies div.replies');
		} else  {
			$.nvzColumns.callback = $.thread.replies;
			$.nvzColumns.create('#replies');
		}

		var _idx = 0;
		for (var i=0;i<_o.length;i++) {
			if ((_o[i].flag & 256) && !eval(_o[i].children))
				continue;

			var _flag 
				= (_o[i].flag & (1<<29)) ? '<img src="/=i_new.gif"> ':'';

			var _name = _o[i].uid 
				? '<a href="/'+_o[i].login+'">' + _o[i].login + '<\/a>'
				: _o[i].login;

			var _level = _o[i].parent==_o[i].top?0:1;

			var _block = $('<div id="r'+_o[i].id+'" class="corners">'
				+ (_o[i].flag & 256 ? ''
				: ('<div class="star"><img id="s'+_o[i].id+'" src="/=i_star'+(_o[i].flag & (1<<30) ? 1:2)+'.gif"><br>'
				+ (_o[i].uid == $.defaults.id ? '<img class="hide" alt="delete" title="'+$.Trans.Delete+'" src="/=i_delete.png">&nbsp;'
				+ '<img class="hide" alt="edit" title="'+$.Trans.Edit+'" src="/=i_edit.png">'
				: '<img class="hide" alt="reply" title="'+$.Trans.Reply+'" src="/=i_reply.png">')
				+ '<\/div>' + (_o[i].url?'<a target="_blank" href="'+_o[i].url+'">'
				+_o[i].url.replace(/^https?.{3}(www.)?([^\/]+)(\/.*)?$/,"$2")+' <img class="noprint" src="/=i_link.gif"><\/a>&nbsp;':'')
				+ _o[i].comment))
				+ '<div class="data">'
				+ '<img class="id" src="/='+(_o[i].uid||'999&&b=1203805224')+'&s=17">'
				+ _flag + _name + ' ' + $.aqStamp(_o[i].uts,true) + (_o[i].flag & (1<<12) ? '&middot;':'')
				+ (eval(_o[i].children) > 2 ? ' <img class="expand" src="/=i_stack.png">'+_o[i].children:'')
				+ '<\/div><\/div>')
			.data('id',_o[i].id)
			.data('ctrl',(_o[i].uid==$.defaults.id?'del':'repl'));

			if (_level)
				_block.data('author','@'+_o[i].login+' ');

			if (_this) {
				if (_level) 
					_block.attr('title',_idx)
						.css({marginLeft:'30px',width:'540px',backgroundColor:$.mau.sC});
				else {
					_block.css({width:'570px',backgroundColor:$.mau.sE});
					_idx = _o[i].id;
				}
				_block.appendTo(_this);
			} else {
				_block.find('div.data img.id').after(i+1+'&rsaquo; ');
				if (_level) {
					_block.css({marginLeft:'10px',width:'230px',backgroundColor:$.mau.sC})
						.attr('title',_idx);
				} else {
					_block.css({width:'240px',backgroundColor:(eval(_o[i].children)?$.mau.sF:$.mau.sE)});
					_idx = _o[i].id;
				}
				if (_level && _j.level1 != 1)
					_block.appendTo($.nvzColumns.pointer());
				else
					_block.appendTo($.nvzColumns.shortest());
			}

			if (_level && !_flag && _j.level1 != 1 && _o[i].index > 2) 
				_block.hide()

			if (_o[i].flag & 256)
				_block.css({opacity: .2});
		}
		if (_s.locked) $('div.corners').find('img.hide').remove();

		$('div.corners').hover(
			function(){$(this).find('.hide').show()},
			function(){$(this).find('.hide').hide()})
		.find('img.hide').click(function(){
			var _box = $(this).closest('div.corners');
			if (_box.data('ctrl') == 'repl') {
				_box.comment({
					id: _box.data('id'), perm: 0,
					replyto: (_box.data('author')||''),
					anon: true, name: $.thread.s.name,
					padding: $(this).parent().parent().css('paddingLeft').replace(/px$/,''),
					margin: _box.css('marginLeft'),
					callback: function($r) {
						$.reply.write(
						{parent: _box.data('id'), text: $r.text, name: $.defaults.sid||$r.name},
						function(){ $.thread.s.scroll = true; $.thread.replies() });
					}
				});
			} else if ($(this).attr('alt') == 'delete')
				$.reply.del(_box.data('id'),function(){ _box.fadeOut(); });
			else if ($(this).attr('alt') == 'edit')
				$.thread.edit(_box.data('id'),function(){ alert('done') });
		});
		$('div.corners').find('img.expand').click(function(){
			if (_j.level1 == 1) return false;
			var _box = $(this).closest('div.corners');
			_box.find('~ div.corners[title="'+_box.data('id')+'"]:hidden').slideDown();
			$(this).remove();
		});
		$.thread.images($.nvzColumns.object||_this);
	});
},
edit: function($id,$cb) {
	$.nvzDialog({ yOffset: 0, buttons: 0, bgcolor: 'transparent', closeBtn: false,
		dlgPadding: '3px', dlgBgColor: '#000', include: '/:?_ob=editor_post&id='+$id,
		buttons: [[$.Trans.Close, "$.nvzDialog.close()"] ] });
},
s: { cgi: '/--/p2', cols: 0, uid: 0, secretid: 0, id: 0, completed: false, locked: false,
	scroll: false }
};
})(jQuery);
