$(document).ready(function() { 

	// URL shrinking for download links
	$("button#btnShinkDownloadURL").bind("click", function(e) {
		var service = MINIFIER_URL.replace(/SERVICE/, $("#smallDomain").val());
		var bigOne = encodeURIComponent($("#bigLink").val());
		var alias = encodeURIComponent($("#urlAlias").val());
		
		$("#linkResult").hide();
		$("#linkError").hide();
			
		$.ajax({
		  dataType: 'jsonp',
		  data: 'url=' + bigOne + "&alias=" + alias,
		  jsonp: 'jsonp_callback',
		  url: service,
		  success: function (jsObj) {
		  		if ( jsObj.completed )
		  		{
		  			$("#shortLink").val(jsObj.mini);
		  			$("#shortLink").attr("readonly", true);
		  			$("#linkResult").fadeIn();
		  			$("#shrinkForm").slideUp();
		  		}
			  	else
			  	{
			  		$("#linkError").text(jsObj.message);
			  		$("#linkError").fadeIn();
			  	}
		  }
		});	
	});


	// controls character input/counter
	$("textarea#fileDescription").keyup(function() {
		var charLength = $(this).val().length;
		// Displays count
		$('div#descriptionSaveStatus').html(charLength + ' of 250 characters used');
		
		// Alerts when 250 characters is reached
		if (charLength > 250)
		{
			$(this).val($(this).val().substring(0,250));
			$('div#descriptionSaveStatus').html('<strong>250 character limit reached.</strong>');
		}
	});
	
	// Update the file description
	$("button#btnSaveDescription").bind("click", function(e) {
		$("div#descriptionSaveIndicator").show();
		$("div#descriptionSaveStatus").html("Saving...");
		$("button#btnSaveDescription").attr("disabled","true");
		$('textarea#fileDescription').attr("disabled","true");
		
		$("div#descriptionSaveStatus").load("components/editnotes.php",
		                                    {id:$("#fileId").val(), key:$("#fileKey").val(), notes:$("textarea#fileDescription").val()},
		                                    function (data) {
		                                    	$("div#descriptionSaveIndicator").hide();
												$("button#btnSaveDescription").removeAttr("disabled");
												$('textarea#fileDescription').removeAttr("disabled");
		                                    });
	});
	
	// Send to yourself
	$("button#btnSendLink").bind("click", function(e) {
		$("div#sendLinkIndicator").show();
		$("div#sendLinkStatus").html("Sending...");
		$("button#btnSendLink").attr("disabled","true");
		$("#uploaderEmail").attr("disabled","true");
		$("div#sendLinkStatus").load("components/mailout.php",
                                    {cache: false, id:$("#fileId").val(), key:$("#fileKey").val(), cmd:'self', email:$("#uploaderEmail").val()},
                                    function (data) {
                                    	$("div#sendLinkIndicator").hide();
										$("button#btnSendLink").removeAttr("disabled");
										$("#uploaderEmail").removeAttr("disabled");
                                    });

	});
	
	// Send to a friend
	$("button#btnShareLink").bind("click", function(e) {
		$("div#shareLinkIndicator").show();
		$("div#shareLinkStatus").html("Sending...");
		$("button#btnShareLink").attr("disabled","true");
		$("#shareLinkInfo > input").attr("disabled","true");
		$("div#shareLinkStatus").load("components/mailout.php",
                                    {cache: false, id:$("#fileId").val(), key:$("#fileKey").val(), cmd:'share',
                                     fromEmail:$("#fromEmail").val(), fromName:$("#fromName").val(),
                                     toEmail:$("#toEmail").val(), toName:$("#toName").val() },
                                    function (data) {
                                    	$("div#shareLinkIndicator").hide();
										$("button#btnShareLink").removeAttr("disabled");
										$("#shareLinkInfo > input").removeAttr("disabled");
                                    });

		
	});
	
	
	/* For the "delete" page */
	$("button#btnDeleteFile").bind("click", function(e) {
		$("div#delFileIndicator").css("visibility","visible");
		$("div#delFileStatus").html("...");
		$("button#btnDeleteFile").attr("disabled","true");
		$("#delFileErrorIcon").hide();
		$("div#delFileStatus").load("components/deletefile.php",
		                                    {id:$("#fileId").val(), key:$("#deleteCode").val()},
		                                    function (data) {
		                                    	$("div#delFileIndicator").css("visibility","hidden");
												$("button#btnDeleteFile").removeAttr("disabled");
												if ( data.indexOf("ERROR") < 0 )
												{
													// successfully deleted
													$("div#deleteFileBox").fadeOut(900,function() {
														$("div#successMessage").fadeIn();
													});
												}
												else
												{
													$("#delFileErrorIcon").show();
												}
		                                    });
	});

});


