var GENERIC_AJAX_ERROR = "An error occured while attempting to restore your clip, please try again later.";
$(document).ready(function () {
	//Delete Clip Functions
	$('.deleteClipButton').bind('click', function (evt) {
		var currentLinkID = evt.currentTarget.id.replace("deleteClipButton","");
		var userFolderId = $('#deleteClipLink' + currentLinkID).val();
		var userFolderVarName = "UserFolderID";
		if(userFolderId == "-1"){
			userFolderVarName = "NOTCURRENTLYINFOLDER";
		}
		$.ajax({
			type: "POST",
			url: "/reports/cliputils/deleteclip.cfm",
			data: {"LinkID" : currentLinkID, "submitDelete": "1", userFolderVarName: userFolderId, "jrid": $('#jrid').val()},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert(GENERIC_AJAX_ERROR);
			},
			success: function (data, textStatus) {
				var currentLinkID = $.trim(data);
				var currentClip = $('#LinkID' + currentLinkID);
				var currentDeletedClip = $('#deletedClip' + currentLinkID);
				$('#checkBoxLinkId' + currentLinkID).attr("checked", false);
				$('#checkBoxLinkId' + currentLinkID).attr("disabled", "disabled");
				currentClip.hide();
				currentDeletedClip.show();
				if($('#listViewHeadlineContainer' + currentLinkID).length > 0){
					//This indicates that we are on the listview page
					$('#titleBarLeft' + currentLinkID).hide();
					$("#listViewDetails" + currentLinkID).hide();
				}
			}
		});		
	});
	
	//Restore Clip functions
	$('.restoreClipButton').bind('click', function (evt) {
		var currentLinkID = evt.currentTarget.id.replace("restoreClipButton","");
		$.ajax({
			type: "POST",
			url: "/reports/cliputils/restoreclip.cfm",
			data: {"LinkID":currentLinkID, "RestoreSelectedClips": "YES", "showLinkID": "1", "jrid": $('#jrid').val()},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert(GENERIC_AJAX_ERROR);
			},
			success: function (data, textStatus) {
				if(data.indexOf(currentLinkID) >= 0){
					$('#deletedClip' + currentLinkID).hide();
					$('#LinkID' + currentLinkID).show();
					$('#checkBoxLinkId' + currentLinkID).attr("checked", false);
					$('#checkBoxLinkId' + currentLinkID).removeAttr("disabled");
					if($('#listViewHeadlineContainer' + currentLinkID).length > 0){
						//This indicates that we are on the listview page
						$('#titleBarLeft' + currentLinkID).show();
					}
				} else {
					alert(GENERIC_AJAX_ERROR);
				}
			}
		});
		return false;
	});
	
	//Save Folder Functions
	$('.saveFolderButton').bind('click', function (evt) {
		var currentLinkID = evt.currentTarget.id.replace("saveFolderButton","");
		$.ajax({
			type: "POST",
			url: "/reports/cliputils/folderlist.cfm",
			data: {"LinkID": currentLinkID},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert(GENERIC_AJAX_ERROR);
			}, 
			success: function (data, textStatus) {
				data = $.trim(data);
				$("#savefolder" + currentLinkID).html(data);
				$("#savefolder" + currentLinkID).show();
				$("#savelink" + currentLinkID).hide()
				$("#folderDropDown" + currentLinkID)[0].focus();
				$('#folderDropDown' + currentLinkID).bind('blur', function (evt) {
					$('#savefolder' + currentLinkID).hide();
					$('#savelink' + currentLinkID).show();
					$('#folderDropDown' + currentLinkID).unbind('blur');
					$('#folderDropDown' + currentLinkID).unbind('change');
				});
				$('#folderDropDown' + currentLinkID).bind('change', function (evt) {
					var newAbstract = $("#abstract_" + currentLinkID).html();
					if(newAbstract + "" == "undefined" || newAbstract == null){
						newAbstract = "";
					} 
					var userFolderID = $('#folderDropDown' + currentLinkID).val();
					var curLoc = window.location.href;
					queryString = "LinkID=" + currentLinkID + "&UserFolderID=" + userFolderID;
					queryString += "&submitSave=1";
					if(newAbstract.length < 300 && curLoc.indexOf("instant") >= 0){
						queryString += "&NewAbstract=" + escape(newAbstract.replace(/\n|\r|\c/g, "").replace(/\s+$|^\s+|\.\.\./g, "").replace(/<.*?>/g, "").replace(/[^\!\s\Q"#=$%&'()*+,-\.\/0123456789:;<>?@[]^_abcdefghijklmnopqrstuvwxyz{}|]/g, '')); //"
					}
					$.ajax({
						type: 'POST',
						url:"/reports/cliputils/saveclip.cfm",
						data: queryString,
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							alert(GENERIC_AJAX_ERROR);
						},
						success: function (data, textStatus) {
							var thisElement = data.replace(/\r?\n/g,'');
							thisElement = thisElement.replace(/^.*?\"saveButtonText/g, ''); //"
							thisElement = thisElement.replace(/\"\).*?$/g, ''); //"
							if(thisElement == "" || thisElement.replace(/\d+/g, '') != ""){
								alert("An error has occurred. Please try your request again later.");
								return;
							}
							$("#savefolder" + currentLinkID).html(thisElement);
							$("#savefolder" + currentLinkID).hide()
							$('#folderDropDown' + currentLinkID).unbind('blur');
							$('#folderDropDown' + currentLinkID).unbind('change');
							$("#savelink" + currentLinkID).show();
							$("#saveButtonText" + currentLinkID).text("Saved");
							$("#saveButtonText" + currentLinkID).css("fontWeight", "Bold");
						}
					});
				});
			}
		});
	});
	
	//Clip Rating Functions
	$('.rateClipButton').bind('click', function (evt) {
		var currentLinkID;
		if(evt.currentTarget == null || evt.currentTarget + "" == "undefined"){
			currentLinkID = evt.originalTarget.id.replace("rateClipButton","");
		} else {
			currentLinkID = evt.currentTarget.id.replace("rateClipButton","");
		}
		$.ajax({
			type: "POST",
			url: "/reports/cliputils/ratelist.cfm",
			data: {"LinkID" : currentLinkID},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert(GENERIC_AJAX_ERROR);
			},
			success: function (data, textStatus) {
				data = $.trim(data);
				$("#rateicon" + currentLinkID).hide();
				$("#rateClip" + currentLinkID).html(data);
				$("#rateClip" + currentLinkID).show();
				$("#clipRatingDD" + currentLinkID)[0].focus();
				$("#clipRatingDD" + currentLinkID).bind('blur', function (evt) {
				 	$("#rateicon" + currentLinkID).show();
				 	$("#rateClip" + currentLinkID).hide();
				});
				$('#clipRatingDD' + currentLinkID).bind('change', function (evt) {
					rating = $('#clipRatingDD' + currentLinkID).val();
					$.ajax({
						type: "POST",
						url: "/reports/cliputils/rateclip.cfm",
						data: {"LinkID": currentLinkID, "ClipRating": rating, "submitRating": "1"},
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							alert(GENERIC_AJAX_ERROR);
						},
						success: function (data, textStatus) {
							data = $.trim(data);
							var ratingColor = data.substr(data.indexOf("<!--RATINGCOLOR:") + 16, 6);
							var ratingNameStartPoint = data.indexOf("<!--RATINGFOLDERNAME:") + 21;
							var ratingName = data.substr(ratingNameStartPoint, data.indexOf("-->", ratingNameStartPoint) - ratingNameStartPoint);
							$("#titleBarLeft" + currentLinkID ).css("backgroundColor", "#" + ratingColor);
							$("#ratingLabel" + currentLinkID ).html(" (" + ratingName + ")");
							$("#rateicon" + currentLinkID).show();
							$("#rateClip" + currentLinkID).hide();
							$("#rateClip" + currentLinkID).html("&nbsp;");
						}
					});
				});
			}
		});
	});
	
	//Clip Note Functions
	$('.noteCommandButton').bind('click', function (evt) {
		var currentLinkID = evt.currentTarget.id.replace("noteCommandButton","");
		if($('#noteBox' + currentLinkID).length > 0){
			if($('#noteBox' + currentLinkID + ":visible").length > 0){
				$('#noteBox' + currentLinkID).hide();
			} else {
				$('#noteBox' + currentLinkID).show();
				$('#noteBox' + currentLinkID)[0].focus();
			}
		}
	});
	$('.cancelNoteButton').bind('click', function (evt) {
		var currentLinkID = evt.currentTarget.id.replace("cancelNoteButton","");
		$('#noteBox' + currentLinkID).hide();
	  if($('#note' + currentLinkID).length > 0){
	    var textBoxText = $("#noteBoxText" + currentLinkID).val();
	    $("#noteBoxText" + currentLinkID).val($("#note" + currentLinkID).html());
	  }
	});
	$('.saveNoteButton').bind('click', function (evt) {
		var currentLinkID = evt.currentTarget.id.replace("saveNoteButton","");
		if($('#note' + currentLinkID).length > 0 && ($('#noteBoxText' + currentLinkID).val() != $('#note' + currentLinkID).text())){
			var textBoxText = $('#noteBoxText' + currentLinkID).val();
			if(textBoxText == ""){
				$('#noteCommandButton' + currentLinkID).text("add note");
			} else {
				$('#noteCommandButton' + currentLinkID).text("edit note");
			}
			$('#statusImg').show();
			$.ajax({
				type: "POST",
				url: "/reports/cliputils/addnote.cfm",
				data: {"LinkID": currentLinkID, "clipNote": textBoxText},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert(GENERIC_AJAX_ERROR);
					$('#statusImg').hide();
				},
				success: function (data, textStatus) {
					$('#statusImg').hide();
					var currentLinkID = $.trim(data);
					$('#note' + currentLinkID).text($('#noteBoxText' + currentLinkID).val());
					$('#noteBox' + currentLinkID).hide();
				}
			});
		} else {
			$('#noteBox' + currentLinkID).hide();
		}
	});
});