$(document).ready(function(){

    $('.comment_form .inputsubmit').live('click',function() {
        // The rest of the function will go here. . .
        var comment_form = $(this).parent();
		var params = comment_form.serialize();
		comment_form.find("#id_comment").val("adding comment...").attr("disabled",true);
		comment_form.find("#id_submit").val("Adding..").attr("disabled",true);
		$.post(comment_form.attr("action"), params, function(data) {
			comment_form.closest("li").before(data);
			comment_form.find("#id_comment").val("").attr("disabled",false);
			comment_form.find("#id_submit").val("Comment").attr("disabled",false);
		});	
		//failed
		return false;	
    });
	$(".comment-remove").live('click',function(){
		var delete_link = $(this);
		var url = $(this).attr("delete_url");
		var comment_listing = delete_link.closest('li');
		var item = comment_listing.find(".black");

		$("#delete_dialog").html("Are you sure you want to remove this comment?<br><br>" + item.html());
		delete_dialog.dialog('option', 'buttons', 
			{"Cancel": function() { $(this).dialog("close");},
			"Remove": function(){
				$.get(url, null, function(data){
					if(data.is_ok){
						delete_dialog.dialog("close");
						comment_listing.fadeOut(500);
					}
				}, "json");
			}
			});
		delete_dialog.dialog("open");		
		return false;
	});

	/* in story listing on video page */
	$(".comment-remove-s").live('click',function(){
		var delete_link = $(this);
		var url = $(this).attr("delete_url");
		var comment_listing = delete_link.closest('li');
		var item = comment_listing.find(".black");

		$("#delete_dialog").html("Are you sure you want to remove this comment?<br><br>" + item.html());
		delete_dialog.dialog('option', 'buttons', 
			{"Cancel": function() { $(this).dialog("close");},
			"Remove": function(){
				$.get(url, null, function(data){
					if(data.is_ok){
						delete_dialog.dialog("close");
						comment_listing.fadeOut(500);
					}
				}, "json");
			}
			});
		delete_dialog.dialog("open");		
		return false;
	});


	$("#videoplayer-edit").text('Edit Video Details')
	  .click(function() { 
		  $("#videoplayer-edit-widget").slideToggle();
		  $(this).text($(this).text() == 'Edit Video Details' ? 'Hide Video Details' : 'Edit Video Details');
	  });
	var delete_dialog = uShow.deleteDialog("#delete_dialog");

	$(".video-remove").live('click',function(){
		var delete_link = $(this);
		var url = $(this).attr("delete_url");
		var video_listing = delete_link.closest('li.videolisting');
		var item = video_listing.find(".black");

		$("#delete_dialog").html('Are you sure you want to remove this video?<br><br>' + item.html());
		delete_dialog.dialog('option', 'buttons', 
			{"Cancel": function() { $(this).dialog("close");},
			"Remove": function(){
				$.get(url, null, function(data){
					if(data.is_ok){
						delete_dialog.dialog("close");

						var video_id = video_listing.attr("video_id");
						$(".listing[video_id="+video_id+"]").each(function() {
							$(this).fadeOut(500);
						});
					}
					else{
						// need to report to user that delete failed
						delete_dialog.dialog("close");
					}
				}, "json");
			}
			});
		delete_dialog.dialog("open");		
		return false;
	});

	$(".video-restore").live('click', function() {
		var url = $(this).attr("href");
		var anchor = $(this);
		$.get(url,{}, function(data){
			if (data.is_ok) {
				anchor.parent().html("<em>Undelete Done</em>");
				if (pageTracker) pageTracker._trackPageview(url);
			}
			else {
				uShow.showDialogOK("Error", data.message);
			}
		}, "json");
		return false;
	});

	$(".story-remove").live('click',function(){
		
		var delete_link = $(this);
		var url = $(this).attr("delete_url");
		var story_listing = delete_link.closest('li.storylisting, li.storylisting-side');
		var item = story_listing.find(".black");
		
		$("#delete_dialog").html("Are you sure you want to remove this story?<br><br>" + item.html() );
		delete_dialog.dialog('option', 'buttons', 
			{"Cancel": function() { $(this).dialog("close");},
			"Remove": function(){
				$.get(url, null, function(data){
					if(data.is_ok){
						delete_dialog.dialog("close");
						story_listing.fadeOut(500);
						
						var frame_id = story_listing.attr("frame_id_v");
						$(".story-summary-item[frame_id="+frame_id+"]").each(function() {
							$(this).parent("li").fadeOut(500, function(){$(this).remove(); uShow.setStoryCount()});
						});
					}
				}, "json");
			}
			});
		delete_dialog.dialog("open");		
		return false;
	});

	var retweet_dialog = uShow.deleteDialog("#retweet_dialog", "Confirm Retweet");

	$(".retag").live('click', function() {
		var link = $(this);
		var url = $(this).attr("url");
		var listing = link.closest('li.listing');
		var item = listing.find(".black");

		$("#retweet_dialog").html('You are Retweeting this item:<br><br>' + item.html());
		retweet_dialog.dialog('option', 'buttons',
			{"Cancel": function() { $(this).dialog("close");},
			"Retweet": function(){
				$.get(url, null, function(data){
					if(data.is_ok){
						retweet_dialog.dialog("close");
					}
					else{
						// need to report to user that delete failed
						retweet_dialog.dialog("close");
					}
				}, "json");
			}
		 });
		retweet_dialog.dialog("open");		
		return false;
	});

	$(".geturl").live('click', function() {
		var link = $(this),
		listing = link.closest('li.listing'),
		item = listing.find(".black"),
		url = link.attr('url'),
		stripped = stripHTML(item.html()),
		body = trim(stripped+'%0A%0A'+url),
		msg = item.html() + '<br><br>Click on URL below to select it<br><input id="url_text" value="'+url+'" style="width: 200px"/>';
		msg += '<br><br><a href="mailto:?subject=See this on uShow&body='+body+'">Share by Email</a>';
		uShow.showDialogOK("Share", msg);
	});

	$("#url_text").live('click', function() {
		this.select();
	});
	

/****
* Report video
****/

	$(".report-video").live('click', function() {
		var link = "#video_report";
		var jLink = $(link);
		jLink.dialog({
			title : "Report this Video",
			buttons : {Cancel: function() { $(this).dialog('close'); },
					   Submit: function() { 
						   var form = $(link + " form");
						   var url = form.attr("action");
						   $.post(url, form.serialize(), function(data, textStatus) {
							   uShow.clearErrors(link);
							   if (data.is_ok) jLink.dialog('close');
							   if (data.message) uShow.showDialogOK("Confirmed", data.message);
							   if (data.error) uShow.setErrors(link, data.error);
						   }, "json");
					   }
		  }
		});
		if (!jLink.dialog('isOpen')) jLink.dialog('open');
		return false;
	});

});
