//basic trim() function for js
function trimAll(sString) { 
	while (sString.substring(0,1) == ' ') { sString = sString.substring(1, sString.length); } 
	while (sString.substring(sString.length-1, sString.length) == ' ') { sString = sString.substring(0,sString.length-1); } 
	return sString;
}
//strip out admin/hidden tags from taglist
function stripHiddenTags(tagList) {
	return tagList.replace(/\btlc\S*\b/g,'');
}

function clickTitle(imageId) {			
	//hide title
	$("#" + imageId + "_title").css("display", "none");
	//show title input
	$("#" + imageId + "_title_edit").css("display", "block");
}

function saveTitle(imageId) {
	//alert(imageId);
	var dataToSend = new Object();
	
	dataToSend["imageId"] = imageId;
	dataToSend["title"] = $("#" + imageId + "_input").val();
				
	$.post("/components/scripts/saveTitle.php", dataToSend, 
		function(data){
			if (data.result == "1") {
				$("#" + imageId + "_title").css("display", "inline");
				$("#" + imageId + "_title_edit").css("display", "none");
				$("#" + imageId + "_input").val(data.title);
				var href = '<a href="javascript:clickTitle(' + imageId + ');">' + data.title + '</a>';
				$("#" + imageId + "_title").html(href);
			} else {
				alert("there was an error editing this image. try again later.");
			}
		}, "json");
	
}

function cancelTitle(imageId) {
	//show title
	$("#" + imageId + "_title").css("display", "inline");
	//hide title input
	$("#" + imageId + "_title_edit").css("display", "none");
}

function editTags(imageId) {
	//hide tags
	$("#" + imageId + "_tags").css("display", "none");
	//show tag input
	$("#" + imageId + "_taglist").css("display", "block");
}

function saveTags(imageId) {
	var dataToSend = new Object();
	dataToSend["imageId"] = imageId;
	//assemble full tag list (with hidden team id and home page-related tags)
	//team tag
	var taglist = $("#" + imageId + "_teamtag").val();
	//optional user tags
	var user_tags = $("#" + imageId + "_tag_input").val();
	if (user_tags != "") taglist += " " + user_tags;
	//optional show on home tag
	if ( $("#" + imageId + "_show_on_home").val() != "" ) taglist += " " + $("#"+imageId+"_show_on_home").val();	

	dataToSend["taglist"] = taglist;
	$.post("/components/scripts/saveTags.php", dataToSend,
		function(data) {
			if (data.result) {
				$("#" + imageId + "_tags").css("display", "inline");
				var taglist = unescape(data.taglist);
				taglist = stripHiddenTags(taglist);
				$("#" + imageId + "_tags").html(taglist);
				$("#" + imageId + "_taglist").css("display", "none");
				$("#" + imageId + "_tag_input").val(taglist);
				
			} else {
				alert("there was an error adding the tags. try again later.");
			}

		}, "json");	
}

function cancelTags(imageId) {
	//show tags
	$("#" + imageId + "_tags").css("display", "inline");
	//hide tags input
	$("#" + imageId + "_taglist").css("display", "none");
}

function deletePhoto(photoSetId, imageId) {
	var dataToSend = new Object();
	dataToSend["photoSetId"] = photoSetId;
	dataToSend["imageId"] = imageId;
	
	if(confirm("Are you sure you want to remove this? (This cannot be undone.)")) {
		$.post("/components/scripts/flickr_delete_photo.php", dataToSend,
			function(data) {
				if (data.result) {
					$("#photo_" + imageId).parent().parent().css("display", "none");
				} else {
					alert("there was an error removing photo " + imageId + " from " + photoSetId + ". try again later. " + data.result);
				}
	
			}, "json");	
	}
}

function toggleShowHome(imageId) {
	var checked = $("#showhome_" + imageId).attr("checked");
	var tag = (checked) ? "tlchomerequest" : "";
	$("#" + imageId + "_show_on_home").val(tag);
	saveTags(imageId);
}

function pageSelect(value) {
	window.location = value;
}

function deletePhotoSet(photoSetId, entryId) {
	var dataToSend = new Object();
	dataToSend["photoSetId"] = photoSetId;
	dataToSend["entryId"] = entryId;
	
	if(confirm("Are you sure you want to delete this? (This cannot be undone.)")) {
		$.post("/components/scripts/flickr_delete_gallery.php", dataToSend,
			function(data) {
				if (data.result || data.sqlresult) {
					$("#" + photoSetId + "_container").css("display", "none");
				} else {
					alert("there was an error deleting gallery " + photoSetId + ". try again later. " + data.result);
				}
	
			}, "json");	
	}
}