htgForums =
{
	init : function()
	{
		jQuery('a.htg').click(function(e)
		{
			e.preventDefault();

			var self   = jQuery(this);
			var action = jQuery(this).attr('htg').toLowerCase();
			var topic  = topicId;
			var post   = jQuery(this).attr('post') || '';

			jQuery.post('/forum/ajax.php',
			{
				action : action,
				topic  : topic,
				post   : post
			}, function(d)
			{
				switch (action)
				{
					case 'solve':
						if (d.solved)
						{
							if (document.title.toString().indexOf(d.titlePrefix) < 0)
								document.title = d.titlePrefix+document.title;

							if (jQuery('.topictitle').html().indexOf(d.titlePrefix) < 0)
								jQuery('.topictitle').prepend(d.titlePrefix);

							jQuery('#geektopicsolved').fadeOut(2000, function()
							{
								jQuery(this).remove();
							});
						}

						break;
					case 'report':
						if (d.status == 'success')
						{
							jQuery('#geekpostreports'+post).each(function()
							{
								if (jQuery(this).is(':hidden'))
									jQuery(this).show();

								jQuery(this).find('span').hide().html(d.reports).fadeIn(1500);
								if (d.changeBg)
									jQuery('#postContent-'+post).addClass(d.bgClass);

								if (d.collapse)
									jQuery(this).parents('.postExpanded').slideUp(function()
									{
										jQuery(this).parent().find('.postCollapsed').each(function()
										{
											jQuery(this).find('span').html(d.html);
											jQuery(this).slideDown('slow');
										});
									});

								if (d.reports)
									jQuery('.removeReports', '.topicOptions').removeClass('hidden');
							});

							jQuery('#reportpost'+post).find('a').fadeOut(2000, function()
							{
								jQuery(this).parent().remove();
							});
						}

						break;
					case 'solution':
						jQuery('.geeksolveposts').hide();
						jQuery('#geekpostsolved'+self.attr('post')).html('&nbsp;').hide().html(d.html).fadeIn(1500);
						jQuery('.topicOptions li.viewAccepted').removeClass('hidden').attr('answer', d.answer);
						jQuery('.topicOptions li.markSolved').removeClass('markSolved').addClass('markUnsolved').attr('cmd', 'markUnsolved').attr('title', d.newTitle).html(d.newHtml);
						jQuery('#topic-info').trigger('recalcWidth');

						if (document.title.toString().indexOf(d.titlePrefix) < 0)
							document.title = d.titlePrefix+document.title;

						if (jQuery('.topictitle').html().indexOf(d.titlePrefix) < 0)
							jQuery('.topictitle').prepend(d.titlePrefix);

						break;
					default:
						break;
				}
			}, 'json');
		});

		jQuery('.postOptions, .topicOptions, .tableCellOptions').find('li').click(function(e)
		{
			e.preventDefault();

			var self  = jQuery(this);
			var opt   = self.attr('cmd');
			var topic = (self.parents('ul').hasClass('tableCellOptions')) ? self.parents('tr').attr('topic') : (typeof(topicId) != 'undefined') ? parseInt(topicId) : '';

			switch (opt)
			{
				case 'favAdd':
				case 'favRemove':
					jQuery.post('/forum/ajax.php',
					{
						action : opt,
						topic  : topic
					}, function(d)
					{
						if (d.status == 'success')
						{
							if (self.attr('inFav'))
								self.parents('tr').fadeOut('normal', function()
								{
									jQuery(this).remove();
									jQuery('#currentfavorites span').html(d.totalFavs);
								});
							else
								self.removeClass(d.oldClass).addClass(d.newClass).attr('title', d.newTitle).attr('cmd', d.newCmd).html(d.newHtml);
						}
					}, 'json');

					break;
				case 'viewAccepted':
					var answer = self.attr('answer');
					var type   = answer.split(':', 1)[0];
					var value  = answer.substr(type.length+1);

					if (type == 'local')
						jQuery.scrollTo(value, 1000);
					else
						window.location = value;

					break;
				case 'markSolved':
				case 'markUnsolved':
					jQuery.post('/forum/ajax.php',
					{
						action : opt,
						topic  : topic
					}, function(d)
					{
						if (d.status == 'success')
						{
							if (d.solved)
							{
								if (document.title.toString().indexOf(d.titlePrefix) < 0)
									document.title = d.titlePrefix+document.title;

								if (jQuery('.topictitle').html().indexOf(d.titlePrefix) < 0)
									jQuery('.topictitle').prepend(d.titlePrefix);

							}
							else
							{
								jQuery('.topictitle').html(jQuery('.topictitle').html().replace(d.titlePrefix, ''));
								document.title = document.title.toString().replace(d.titlePrefix, '');
							}

							self.attr('cmd', d.newCmd).attr('title', d.newTitle).removeClass(d.oldClass).addClass(d.newClass).html(d.newHtml);
							if (d.solutionPost > 0)
							{
								self.parent().find('.viewAccepted').addClass(d.solClass).attr('answer', '');
								jQuery('.acceptAnswer').show();
								jQuery('.acceptedAnswer', '#postContent-'+d.solutionPost).hide().empty();
							}
						}
					}, 'json');

					break;
				case 'removeReports':
					jQuery.post('/forum/ajax.php',
					{
						action : opt,
						topic  : topic
					}, function(d)
					{
						if (d.status == 'success')
						{
							self.fadeOut(function()
							{
								self.remove();
							});

							jQuery('.'+d.bgClass).removeClass(d.bgClass).each(function()
							{
								if (jQuery('.postCollapsed', this).is(':visible'))
								{
									jQuery('.postCollapsed', this).slideUp(function()
									{
										jQuery('.postExpanded', jQuery(this).parent()).slideDown('slow', function()
										{
											jQuery('.reportCount', this).hide();
										});
									});
								}
								else if (jQuery('.postExpanded', this).is(':hidden'))
								{
									jQuery('.postExpanded', this).slideDown('slow', function()
									{
										jQuery('.reportCount', this).hide();
									});
								}
								else
								{
									jQuery('.reportCount', this).hide();
								}
							});
						}
					}, 'json');

					break;
				case 'expand':
					self.parents('.postCollapsed').each(function()
					{
						if (jQuery(this).is(':visible'))
						{
							jQuery(this).slideUp(function()
							{
								jQuery('.postExpanded', jQuery(this).parent()).slideDown('slow');
							});
						}
						else if (jQuery('.postExpanded', jQuery(this).parent()).is(':hidden'))
						{
							jQuery('.postExpanded', jQuery(this).parent()).slideDown('slow');
						}
					});

					break;
			}

			return false;
		});

		if (jQuery('#topic-info').length)
		{
			jQuery('#topic-info').bind('recalcWidth', function()
			{
				var ti = jQuery(this);
				var mw = ti.width();
				var iw = ti.find('.topicOptions').width();
				var pc = Math.ceil(iw/mw*100);

				if (!ti.find('h2').data('isWrapped'))
				{
					var dv = jQuery('<div />').width((100-pc)+'%');

					ti.find('h2').data('isWrapped', true).wrap(dv);
					ti.find('h2').parent().next().appendTo(ti.find('h2').parent());
				}
				else
					ti.find('h2').parent().width((100-pc)+'%');
			}).trigger('recalcWidth');
		}
	}
}

jQuery(function()
{
	htgForums.init();
});