function jumpToEntry() {
    $('#entry').show().scrollTo(-50);
}

var cur_open;
function selectOpenDyalogue(v,m) {
    if (v && cur_open) {
	cur_open.submit();
    }
}

function printComment(c,h) {
    var output = '';
    output += '<li id="comment_' + c.id + '"';
    if (h) {
	output += ' style="display: none;"';
    }
    output += '>';
    output += '<img src="/profile_hs/' + c.user_thumbnail_path + '"/>';
    output += '<div class="box_arrow"></div>';
    output += '<p>';
    output += '<a href="/profile/' + c.user_path + '">' + c.user_display_name + '</a>';
    output += '<span class="date">Updated ' + c.date_created + '</span>';
    if (c.comment_delete) {
	output += '<a class="comment_delete cursor">Delete</a>';
    }
    output += '<br/>';
    output += c.comment;
    output += '</p>';
    output += '</li>';
    return output;
}

$(document).ready(function() {

    var w = $('.dyalogue').width();
    $('.dyalogue_arguments_body').width(w-20-2);

    $('.select_open input.button').hoverSwapClass('button','button_hover');
    $('.select_open').click(function() {
	cur_open = $(this);
	var msg = '';
	msg += '<img src="/profile_hs/'+$(this).children('input:hidden[name=responder_thumbnail_path]').val()+'" class="float_left" style="padding-right: 5px;"/>';
	msg += 'Are you sure you want to select <strong>'+$(this).children('input:hidden[name=responder]').val()+'</strong> as your dyalogue counterpart?';
	$.prompt(msg,{
	    callback: selectOpenDyalogue,
	    buttons: {
		Yes: true,
		No: false
	    }
	});
	return false;
    });

    $('#preview').scrollTo(-50);
    $('#publish').scrollTo(-50);

    $('.reminder_action').submit(function() {
	$.post('/notification/reminder_user',
	{
	    dyalogue_id: $('.reminder_action input[name=dyalogue_id]').val(),
	    from_user_id: $('.reminder_action input[name=from_user_id]').val(),
	    to_user_id: $('.reminder_action input[name=to_user_id]').val(),
	    days: $('.reminder_action input[name=days]').val()
	},
	function(json) {
	    $('#reminder_callout').removeClass('reminder_normal');
	    $('#reminder_callout').addClass(json.style);
	    $('#reminder_callout').hide().html(json.message).fadeIn("slow");
	},
	"json");
	return false;
    });


    $('.vote input').rating({
	callback:function(v,l) {
	    var c = $(this);
	    if ($('#logged_in').html()==null) {
		var msg = '<div class="warning">Oops, you must have an account to vote!<br/><a href="/signup">Sign up</a> or <a href="/login">Login</a> now.';
		$.prompt(msg,{ buttons: false });
	    } else {
		    $.post(
			'/vote/argument',
			{
			    dyalogue_id: $('.dyalogue').attr('id').substr(9),
			    argument_id: $(this).attr('name').substr(5),
			    votes: v
			},
			function(json) {
			    if (json.result) {
				c.siblings('div.vote_avg').children('span').html(json.data.votes_average);
			c.rating('disable');
			    }
			},
			'json'
			);
		}
	},
	focus:function(v,l) {
	    $(this).siblings('div.vote_hint').html($(this).attr('title'));
	},
	blur:function(v,l) {
	    $(this).siblings('div.vote_hint').html('');
	}
    });


    $('.comment_add').click(function() {
	if ($('#logged_in').html()==null) {
		var msg = '<div class="warning">Oops, you must have an account to add comments!<br/><a href="/signup">Sign up</a> or <a href="/login">Login</a> now.';
		$.prompt(msg,{ buttons: false });
	} else {
		var p = $(this).parent('.comment');
		var c = $(this);
		var comment_box = $(this).siblings('.comment_box');
		var comment_textarea = comment_box.children('textarea');
		var comment_list = p.children('ul');
		comment_box.slideToggle('slow');

		comment_textarea.keyup(function() {
		    var limit = 600;
		    var t = $(this).val();
		    var ch = $(this).siblings('p').children('span');
		    if (t.length>=limit) {
			t = t.substr(0,limit);
			$(this).val(t);
			ch.addClass('char_limit');
		    } else if (t.length>(limit*.90)) {
			ch.addClass('char_limit');
		    } else {
			ch.removeClass('char_limit');
		    }
		    ch.html(""+(limit-t.length));
		});

		comment_box.children('img').click(function() {
		    var comment_submit = $(this);
		    if (comment_textarea.val()!='') {
			comment_submit.unbind('click');
			$.post(
			    '/comment/add',
			    {
			dyalogue_id: $('.dyalogue').attr('id').substr(9),
			argument_id: p.parent('.argument').attr('id').substr(9),
			comment: comment_textarea.val(),
			topic: $('#dyalogue_desc h2').html(),
			path: document.location.toString()
		    },
		    function(json) {
			if (json.result) {
			    p.children('.comment_view').attr('name',json.number);
			    p.children('.comment_view').children('span').html(json.number);
			    p.children('.comment_view').show();
			    comment_box.fadeOut('normal',function() {
				comment_textarea.val('');
				comment_textarea.unbind('keyup');
				comment_list.prepend(printComment(json.data,true));
				if (comment_list.css('display')=='none') {
				    p.children('.comment_view').trigger('click');
				}
				comment_list.children('li:first-child').fadeIn('slow');
			    });
			}
		    } ,
		    'json'
		);
	    }
		});
	}
    });

    $('.comment_view').click(function() {
	var p = $(this).parent('.comment');
	var c = $(this);
	var comment_list = p.children('ul');
	$.post(
	    '/comment/view',
	    {
		argument_id: p.parent('.argument').attr('id').substr(9)
	    },
	    function(json) {
		if (json.result) {
		    var comments = json.data;
		    var output = '';
		    for(var i=0;i<comments.length;i++) {
			output += printComment(comments[i]);
		    }
		    if (comment_list.attr('id')=='') {
			comment_list.html('');
		    }
		    if (json.anchor) {
			comment_list.attr('id','comment_'+json.anchor);
			p.append('<div class="comment_more cursor">Show More Comments</div>');
			p.children('.comment_more').click(function() {
			    var comment_more = $(this);
			    comment_more.html('Loading...');
			    $.post(
				'/comment/view',
				{
				    argument_id: p.parent('.argument').attr('id').substr(9),
				    anchor: comment_list.attr('id').substr(8)
				},
				function(json) {
				    if (json.result) {
					var comments = json.data;
					var output = '';
					for(var i=0;i<comments.length;i++) {
					    output += printComment(comments[i]);
					}
					comment_list.append(output);
					if (json.anchor) {
					    comment_list.attr('id','comment_'+json.anchor);
					    comment_more.html('Show More Comments');
					} else {
					    comment_more.replaceWith('');
					}
				    } else {
					comment_more.replaceWith('');
				    }
				},
				'json');
			});
		    }
		    comment_list.append(output);
		    comment_list.slideDown('slow');

		    c.unbind('click');
		    c.attr('name',c.children('span').text());
		    c.text('Hide Comments');
		    c.click(function() {
			comment_list.slideToggle('slow',function() {
			    if(c.html()=='Hide Comments') {
				c.html('View Comments (<span>'+c.attr('name')+'</span>)');
				p.children('.comment_more').hide();
			    } else {
				c.html('Hide Comments');
				p.children('.comment_more').show();
			    }
			});
		    });
		}
	    },
	    'json'
	    );
    });

    $('.comment_delete').live('click', function() {
	var c = $(this);
	var p = $(this).parent('p').parent('li');
	var msg = '';
	msg += 'Are you sure you want to delete this comment?';
	$.prompt(msg,{
	    callback: function(v) {
		if (v) {
		    $.post(
			'/comment/delete',
			{
			    comment_id: p.attr('id').substr(8)
			},
			function(json) {
			    if (json.result) {
				p.fadeOut('slow',function() {
				    p.replaceWith('');
				});
				var comment_view = p.parent('ul').siblings('.comment_view');
				var n = parseInt(comment_view.attr('name'));
				n--;
				if (n==0) {
				    comment_view.hide();
				}
				comment_view.attr('name',n);
				if (comment_view.html()!='Hide Comments') {
				    comment_view.children('span').html(n);
				}	
			    }
			},
			'json'
			);
		}
	    },
	    buttons: {
		Delete: true,
		Cancel: false
	    }
	});
	return false;
    });

    var url = document.location.toString();
    if (url.match('#')) {
	var anchor = url.split('#')[1];
	var anchor_array = anchor.split('_');
	if (anchor_array[0]=='view') {
	    if (anchor_array[1]=='comment') {
		$('#argument_'+anchor_array[2]).find('.comment_view').trigger('click');
	    }
	    $('#argument_'+anchor_array[2]).scrollTo(0);
	}
    }
});
