$(document).ready(function() {
	$("a[class ^= 'view-']").click(function()
    {
        classname = $(this).attr('class');
        temp = classname.split('-');
        id = temp[1];
        $.post("/updateview.php", { 'id': id, 'type': 'view'}, function(data)
        {
        }, "text");
    });

//	$("div[class='user-rel-single']:even").addClass('alt');
//	$("div[class ^= 'user-rel-single']:last").addClass('nodash');

	$("a[class = 'addfav']").click(function()
	{
		news_id = $(this).attr('value');
		if (news_id)
		{
			$.post("/addFavourite.php", { 'news_id' : news_id}, function(data)
			{
				if (data == 1)
				{
					$("a[class = 'addfav']").parent().removeClass('nostar');
					$("a[class = 'addfav']").parent().addClass('starred');
					$("a[class = 'addfav']").parent().prepend("Favourite!");
					$("a[class = 'addfav']").remove();
					favCount = $("li[class='fave']").attr('value');
					favCount += 1;
					$("li[class='fave']").attr('value', favCount);
					$("li[class='fave']").children(0).html(favCount + " Favourites");
				}
				else
				{
					alert('Please login to add to Favourites.');
				}
			}, "text");
		}	
		return false;	
	});

	$("a[class = 'a-delete']").live("click", function()
	{
		storyid = $("div[class='story-title title']").attr('value');
		comment_id = $(this).attr('value');
		var answer = confirm("Delete this comment?")
		if (answer)
		{
			$.post("/addComment.php", { 'comment_id' : comment_id, 'action' : 'delete' }, function(data)
			{
				if (data)
				{
					//$("div[class='login']").load('userbox.php');
					cmtCount = $("li[class='cmt']").attr('value');
					cmtCount -= 1;
					$("li[class='cmt']").attr('value', cmtCount);
					$("li[class='cmt']").children(0).html(cmtCount + " Comments");
					add = '/comment.php?news_id=' + storyid;
					$("div[id='comment-holder']").load(add);
				}
			}, "text");
		}
		return false;	
	});

	$("a[class = 'a-edit']").live("click", function()
	{
		comment_id = $(this).attr('value');
		if ($(this).html().toLowerCase() == "cancel")
		{
			content = $("input[name='orig-comment-" + comment_id + "']").attr('value');
			$(this).parent().parent().parent().siblings().remove();
			$(this).parent().parent().parent().parent().prepend("<div class='content' value='" + comment_id + "'>" + unescape(content) + "</div>");
			//alert(content);	
			$(this).parent().parent().parent().prepend("<ul class='cmt-panel'><li class='delete'><a href='#' class='a-delete' value='" + comment_id + "'>Delete</a></li><li class='edit'><a href='#' class='a-edit' value='" + comment_id + "'>Edit</a></li><li class='reply'><a href='#' class='a-reply' value='" + comment_id + "'>Reply</a></ul>");
			$(this).parent().parent().remove();
			return false;
		}
		else
		{
			//$(this).html('<strong>Cancel</strong>');
		
			content = $("div[class='content'][value=" + comment_id + "]").html();
			//alert(content);
			form = "<div class='edit-cmt-frm'><p><input type='hidden' name='orig-comment-" + comment_id + "' value=\"" + escape(content) + "\"><textarea class='comment-form' name='" + comment_id + "'>" + content + "</textarea></p><p><button id='editComment' class='logingo' name='" + comment_id + "'>Save comment</button></p></div>";
			$("div[class='content'][value=" + comment_id + "]").remove();
			$(this).parent().parent().parent().parent().prepend(form);

			$(this).parent().parent().parent().prepend("<ul class='cmt-panel'><li class='delete'>Delete</li><li class='cancel'><a href='#' class='a-edit' value='" + comment_id + "'>Cancel</a></li><li class='reply'>Reply</ul>");
			$(this).parent().parent().remove();

			return false;
			//$("div[class='content'][value=" + comment_id + "]").html("Test");
			//alert(content);
		}
	});

	$("button[id='editComment']").live("click", function()
	{
		$("button[id='editComment']").attr('disabled', 'disabled');
		storyid = $("div[class='story-title title']").attr('value');
		parent_id = $(this).attr('name');
		//alert(parent_id);
		comment = $("textarea[class = 'comment-form'][name = '" + parent_id + "']").attr('value');
		$("textarea[class = 'comment-form'][name = '" + parent_id + "']").attr('disabled', 'disabled');
		
		//alert(comment);
		if ((comment == null) || (comment.length == 0))
		{
			alert('Empty comment!');
		}
    else
    {
    	var $loadingIndicator = $('<img/>')
    	.attr({
    		'src'	:	'http://www.populii.com/img/loading.gif',
    		'alt'	:	'Saving comment. Please wait.',
    		'class'	:	'loading'
    	});
    	$(this).after($loadingIndicator);
    	
      $.post("/addComment.php", { 'comment' : comment, 'comment_id' : parent_id, 'action' : 'update' }, function(data)
    	{
        if (data)
        {
					add = '/comment.php?news_id=' + storyid;
					$("div[id='comment-holder']").load(add);
        }
        //$("img[class='loading']").remove();
        
      }, "text");
    }
    //$("button[id='editComment']").attr('disabled', '');
    //$("textarea[class = 'comment-form'][name = '" + parent_id + "']").attr('disabled', '');
    
		return false;
	});

	$("a[class = 'a-reply']").live("click", function()
	{
		//alert('Here!');
		//event.preventDefault();
		if ($(this).html().toLowerCase() == "cancel")
		{
			//alert('There!');
			$(this).parent().siblings(".reply-cmt-frm").remove();
			$(this).parent().removeClass('cancel');
			$(this).parent().addClass('reply');
			$(this).html("Reply");
			return false;
		}
		else
		{
			userid = $("input[name='commenter']").attr('value');
			username = $("input[name='commenter-name']").attr('value');
			if (userid == null)
			{
				alert("Please register or login to add comment");
				return false;
			}
			else
			{
				$(this).parent().removeClass('reply');
            	$(this).parent().addClass('cancel');
				indent = $(this).parent().parent().parent().parent().parent().attr('style');
				ind = indent.indexOf(' ');
				end = indent.indexOf('px');
				indent = indent.substring(ind+1, end);
				parentid = $(this).attr('value');
				
				form = "<div class='reply-cmt-frm' value=" + parentid + "><p><input type='hidden' name='rep-commenter' value=" + userid + "><input type='hidden' name='rep-commenter-name' value=" + username + "><textarea class='comment-form' name='" + parentid + "'></textarea></p><p><button id='replyComment' name='" + parentid + "' class='logingo' indent=" + indent + ">Save comment</button></p></div>";
				$(this).parent().parent().append(form);
				$(this).html("Cancel");
				return false;
			}
		}
	});

	$("button[id = 'replyComment']").live("click", function()
	{
		$("button[id = 'replyComment']").attr('disabled', 'disabled');
		parentid = $(this).attr('name');
		parentindent = $(this).attr('indent');
		indent = parseInt(parentindent) + 40;
		storyid = $("div[class='story-title title']").attr('value');
		comment = $("textarea[name=" + parentid + "]").attr('value');
		$("textarea[name=" + parentid + "]").attr('disabled', 'disabled');
		userid = $("input[name='rep-commenter']").attr('value');
		username = $("input[name='rep-commenter-name']").attr('value');

		if ((comment == null) || (comment.length == 0))
    	{
      		alert('Empty comment!');
    	}
    	else
    	{
    		var $loadingIndicator = $('<img/>')
    		.attr({
    			'src'	:	'http://www.populii.com/img/loading.gif',
    			'alt'	:	'Saving comment. Please wait.',
    			'class'	:	'loading'
    		});
    		$(this).after($loadingIndicator);
    	
      		$.post("/addComment.php", { 'user_id' : userid, 'comment' : comment, 'story_id' : storyid, 'parent_id' : parentid }, function(data)
      		{
      			if (data)
      			{
      				content = "<div class='xingle-cmt flash' style='margin-left: " + indent + "px;' value='" + data + "'><div class='single-cmt-cmter'><strong>" + username + "</strong><BR><span class='ago'>0 minutes ago</span></div><div class='single-cmt-content'><div class='content' value='" + data + "'>" + comment + "</div><div class='reply-link'><ul class='cmt-panel'><li class='delete'><a class='a-delete' value='" + data + "' href='#'>Delete</a></li><li class='edit'><a class='a-edit' value='" + data + "' href='#'>Edit</a></li><li class='reply'><a href='#' class='a-reply' value='" + data + "'>Reply</a></li></ul></div></div></div>";

					a = $("div[class^='xingle-cmt'][value=" + parentid +"]");
					//style = a.attr('style');
					pI = parseInt(parentindent);
					//alert(pI);

					prev = a;
					a = a.next();
					nextstyle = a.attr('style');
					if (nextstyle != undefined)
					{
						ind = nextstyle.indexOf(' ');
                		end = nextstyle.indexOf('px');
                		nextstyle = parseInt(nextstyle.substring(ind+1, end));
					}
					//alert(nextstyle);
					while (nextstyle > pI)
					{
						if (nextstyle == undefined)
						{
							break;
						}
						prev = a;
						a = a.next();
						nextstyle = a.attr('style');
						if (nextstyle != undefined)
						{
							ind = nextstyle.indexOf(' ');
							end = nextstyle.indexOf('px');
							nextstyle = parseInt(nextstyle.substring(ind+1, end));
						}
						//alert(nextstyle);
					}
					prev.after(content);
						
	
					//.after(content);
//                    $(this).parent().parent().siblings(".reply-link").children(0).html("Reply");
//					$(this).parent().parent().remove();
					$("div[class='reply-cmt-frm'][value=" + parentid + "]").siblings(".reply-link").children(0).html("Reply");
					$("div[class='reply-cmt-frm'][value=" + parentid + "]").remove();
					add = '/comment.php?news_id=' + storyid;
					
					cmtCount = $("li[class='cmt']").attr('value');
					cmtCount += 1;
					$("li[class='cmt']").attr('value', cmtCount);
					$("li[class='cmt']").children(0).html(cmtCount + " Comments");
					
					//$("div[class='login']").load('userbox.php');
					$("div[id='comment-holder']").load(add);
					//location.reload();
                }
            }, "text");
        }

	});	
});


