function showCommentForm(ultimatumId) {
    $('#addCommentForm'+ultimatumId).show('slow'); 
}

function toggleCommentList(link) {
    var ultimatumId = link.id.substring("commentListLink".length);    
    
    var visible = $('#commentList'+ultimatumId).is(':visible');
    var commentCountText = $('#commentListLink'+ultimatumId).html().substring('show comments'.length);
    
    if(visible) {
        $('#commentList'+ultimatumId).hide('slow'); 
        $('#commentListLink'+ultimatumId).html('show comments' + commentCountText); 
    } else {
        $('#commentList'+ultimatumId).show('slow'); 
        $('#commentListLink'+ultimatumId).html('hide comments' + commentCountText);
    }
}

function hideCommentList(ultimatumId) {
    $('#commentList'+ultimatumId).hide('slow'); 
    $('#commentListLink'+ultimatumId).html('Show Comments'); 
    $('#commentListLink'+ultimatumId).click('Hide Comments'); 
}

function submitVote(ultimatumId, choiceId) {
    $('#voteCount'+choiceId).html(
        "<img src='/media/img/loading.gif'/>"
    );

    var formActionUrl = "/ultimatums/"+ultimatumId+"/vote/";
    
    var data = {};
    data.choice = choiceId;
    
    $.post(formActionUrl,
           data,
           function(responseData) {
               if(responseData.errorMessage) {
                   alert(responseData.errorMessage);
               } else {
                   for(choiceInd in responseData.choices) {
                       var choice = responseData.choices[choiceInd];
                       
                       $('#voteCount'+choice.id).html(choice.votes);
                       
                       if(choice.is_voted) {
                           $('#choiceStatus'+choice.id).html("");
                           $('#choice'+choice.id).addClass("voted");
                       } else {
                           $('#choice'+choice.id).removeClass("voted"); 
                       }
                   }
               }
           },
           "json");
}

// On page load
$(function(){
	// Dialog			
	$('#popup_dialog').dialog({
		autoOpen: false,
        modal: true,
        resizable: false,
        overlay: {opacity: 0.5, background: "black"}
        /*
         width: 500
         buttons: {
			"Login": function() { 
				$(this).dialog("close"); 
			}, 
			"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}*/
	});
    
    // Login Link
    $('#login_link').click(function(){
        $('#popup_dialog').data("width.dialog", 300)
        $('#popup_dialog').dialog('open');
        $("#popup_dialog").load($(this).attr("href"));

        return false;
    });
    
    $('#register_link').click(function(){
        $('#popup_dialog').data("width.dialog", 500)
        $('#popup_dialog').dialog('open');
        $("#popup_dialog").load($(this).attr("href"));

        return false;
    });

    // Rounded Corners
    $('.rounded').corners();
});
