var curSharePage = 0;
var commentArea = 0;

$(document).ready(function() {
	$('#addComment').click(function(){
		if(commentArea == 0)
		{
			$('#commentArea').slideDown("slow");
			commentArea = 1;
		}
		else
		{
			$('#commentArea').slideUp("slow");
			commentArea = 0;
		}
	});
	
	$('#commentAreaRight input').click(function(){
		
		addComment($('#dreamId').val(), $('#commentText').val());
	});
	
	$('#rateUp').click(function(){
		doRate($('#dreamId').val(), 1, 0);
	});
	
	$('#rateDown').click(function(){
		doRate($('#dreamId').val(), 0, 1);
	});
	
	$('#editAbout').click(function(){
		if($('#aboutArea').is(':hidden'))
		{
			$('#aboutArea').slideDown();
			$('#aboutText').val(html2bb($('#aboutContent').html()));
			$('#aboutContent').hide();
		}
		else
		{
			$('#aboutArea').slideUp();
			$('#aboutContent').show();
		}		
	});
	
	$('#aboutAreaRight input').click(function(){		
		doAbout($('#dreamId').val(), $('#aboutText').val());
	});
});

function doAbout(dreamId, content)
{
	$.ajax({
		type: 'POST',
		url: 'http://plustg.com/v2/_inc/_php/_ajax/doAbout.php',
		data: 'content='+content+'&dreamId='+dreamId,
		dataType: 'xml',
		success: function(xml){
			if($(xml).find('success').text() == 1)
			{
				$('#aboutArea').slideUp("slow");
				$('#aboutText').val('');
				location.reload(true);
			}
			else
			{
				alert($(xml).find('message').text());
			}
		}
	});	
}

function doRate(dreamId, plus, minus)
{
	$.ajax({
		type: 'POST',
		url: 'http://plustg.com/v2/_inc/_php/_ajax/doRate.php',
		data: 'dreamId='+dreamId+'&plus='+plus+'&minus='+minus,
		dataType: 'xml',
		success: function(xml){
			if($(xml).find('success').text() == 1)
			{
				if(plus == 1)
				{
					var curPlus = parseInt($('#upRating').text());
					$('#upRating').html(curPlus + 1);
				}
				else
				{
					if(minus == 1)
					{
						var curDown = parseInt($('#downRating').text());
						$('#downRating').html(curDown + 1);						
					}
				}
			}
			else
			{
				alert($(xml).find('message').text());
			}
		}
	});
}

function addComment(dreamId, content)
{
	$.ajax({
		type: 'POST',
		url: 'http://plustg.com/v2/_inc/_php/_ajax/addComment.php',
		data: 'content='+content+'&dreamId='+dreamId,
		dataType: 'xml',
		success: function(xml){
			if($(xml).find('success').text() == 1)
			{
				$('#commentArea').slideUp("slow");
				commentArea = 0;
				getComments(1, $('#dreamId').val());
				$('#commentText').val('');
			}
			else
			{
				alert($(xml).find('message').text());
			}
		}
	});
}

function changeSharePage(id)
{
	if(curSharePage != id)
	{
		$('#shareContainer > div').hide();
		$('#shareLinks a').attr('class', '');
		switch(id)
		{
			case 0:
				$('#shareEquipped').show();
				$('#equippedLink').attr('class', 'active');
				curSharePage = 0;
			break;
			case 1:
				$('#shareAbout').show();
				$('#aboutLink').attr('class', 'active');
				curSharePage = 1;
			break;
			case 2:
				getComments(1, $('#dreamId').val());
				$('#shareComments').show();
				$('#commentsLink').attr('class', 'active');
				curSharePage = 2;
			break;
			case 3:
				$('#shareShare').show();
				$('#shareLink').attr('class', 'active');
				curSharePage = 3;
			break;
			case 4:
				$('#shareSnapshot').show();
				$('#snapshotLink').attr('class', 'active');
				curSharePage = 4;
			break;
		}
	}
}

function getComments(page, id)
{
	$.ajax({
		type: 'POST',
		url: 'http://plustg.com/v2/_inc/_php/_ajax/getComments.php',
		data: 'page='+page+'&dreamId='+id,
		dataType: 'xml',
		success: function(xml){
			if($(xml).find('comment').length != 0)
			{
				$('#commentComments').html('');
				$(xml).find('comment').each(function(){
					var oneComment = $('<div />').attr({
						'class':'oneComment'
					}).appendTo('#commentComments');
					
					// User bit
					var oneCommentUser = $('<div />').attr({
						'class':'commentUserInfo'
					}).appendTo(oneComment);
					
					$('<img />').attr({
						'alt':		$(this).find('tmName').text(),
						'title':	$(this).find('tmName').text(),
						'src':		$(this).find('tmAvatar').text()
					}).appendTo(oneCommentUser);
					
					$('<br />').appendTo(oneCommentUser);
					
					var userLink = $('<a />').attr({
						'href':'http://plustg.com/m/'+$(this).find('tmName').text()+'/'
					}).appendTo(oneCommentUser);
					
					// Comment Bit
					var oneCommentContent = $('<div />').attr({
						'class':'commentContent'
					}).appendTo(oneComment);
					
					$('<p />').html(nl2br($(this).find('content').text())).appendTo(oneCommentContent);
					
					$('<div />').attr('style', 'clear:both').appendTo(oneComment);;
					$(userLink).text($(this).find('tmName').text());
				});
				var totalComments = $(xml).find('totalComments').text();
				if(totalComments > 5)
				{
					var totalPages = Math.ceil(parseInt(totalComments) / 5);
					var pageString = 'Page ';
					for(var i = 1; i<=totalPages; i++)
					{
						if(i == page)
						{
							pageString = pageString + i + ' ';
						}
						else
						{
							pageString = pageString + '<a href="javascript:getComments('+i+', '+id+')">' + i + '</a> ';
						}
					}
					
					$('#commentPaging').html(pageString);
				}
			}
			else
			{
				$('#commentComments').html('This selfy has no comments.');
			}
		}
	});
}
