function updateWorkTitle() {
    if ($('#title').val() !== '') {
        $('#work_title').html($('#title').val() + ' ');
        $('#hidden_work_title').val($('#title').val());
        $('#change_title_popup').jqmHide();
    }
    else {
        alert("Enter a title for the work.");
    }
}

function showTitleChange(evt) {
    $('#updated_title').val($('#work_title').html());
    $('#title_change_input').show();
}

$(document).ready(function() {
    $("#works_tab").addClass("here");
    $('#update_title').bind('click', updateWorkTitle);
    $('#cancel_update_title').bind('click', function() {$('#title_change_input').hide()});
    $('#change_title_trigger').bind('click', showTitleChange); 
    $('#deep_tags').jqm({modal: true, trigger: '#deep_tags_control', toTop: true});
    $('#deep_tags').jqmAddClose('#deep_tags_close_dialog'); 
    $('#basic_tags').jqm({modal: true, trigger: '#basic_tags_control', toTop: true});
    $('#basic_tags').jqmAddClose('#basic_tags_close_dialog'); 
    $('#comments').jqm({modal: true, trigger: '#comments_control', toTop: true});
    $('#comments').jqmAddClose('#comments_close_dialog'); 
    $('#license').jqm({modal: true, trigger: '#license_control', toTop: true});
    $('#license').jqmAddClose('#license_close_dialog');
    $(".rte-zone").rte("/css/rte.css", "/images/");
    $('#new_work').jqmAddTrigger('.new_work_trigger');
    $('#change_image_trigger').bind('click', function() {$('#image_change_input').toggle()});
    $('#upload_to_gallery_trigger').bind('click', function() {$('#gallery_change_input').toggle()});
    $('#change_audio_trigger').bind('click', function() {$('#audio_change_input').toggle()});
    $('#new_scene').bind('click', function() { 
    	return confirm("If you leave this page to create a new scene, " +
    			       "this work will be lost. OK to move on to the scenes page?");
    });
    $('#available_packages').jqm({trigger: '#available_packages_control', toTop: true});
    $('#available_packages').jqmAddClose('#available_packages_close_dialog');
    $('#available_scenes').jqm({trigger: '#available_scenes_control', toTop: true});
    $('#available_scenes').jqmAddClose('#available_scenes_close_dialog');
    $('#swb').bind('click', setOrder);
    $('#change_title_popup').jqm({modal: true, onTop:true});
    $('#change_title_popup').jqmAddClose('#change_title_popup_close_dialog');
    $('#change_title_popup').jqmHide();
    $("#update_title_button").click(function() {updateWorkTitle();});
    $('#title_countdown').html("512");   
    $('#title').keydown(function(e) {
        if (e.keyCode == 13) {
            $("#update_title_button").click();
            return false;
        }
        limitText(document.forms.title_update.title, $('#title_countdown'), 512);
    }); 
    $('#title').keyup(function() {
        limitText(document.forms.title_update.title, $('#title_countdown'), 512);
    });
    $('#work_description_control').bind('click', function() {
 		showDescriptionEditor();   	
    });
});

function addPackageWorkReference(id) {
    $("#ulWorkPackageList").append($("#ap_li_" + id));
    $("#ap_li_" + id).addClass("worklistitem");
    $("#apimg_" + id).attr("src", "/images/x.gif");
    $("#apimg_" + id).attr("alt", "Remove work from this collection.");
    $("#apimg_" + id).attr("title", "Remove work from this collection.");
    $("#add_package_" + id).unbind();
    $("#add_package_" + id).click(function() {removeWorkFromPackage(id);});
    $("#wtd").html("");
}

function addSceneWorkReference(id) {
	$("#ulWorkSceneList").append($("#as_li_" + id));
    $("#as_li_" + id).addClass("scenelistitem");
    $("#asimg_" + id).attr("src", "/images/x.gif");
    $("#asimg_" + id).attr("alt", "Remove work from this scene.");
    $("#asimg_" + id).attr("title", "Remove work from this scene.");
    $("#add_scene_" + id).unbind();
    $("#add_scene_" + id).click(function() {removeWorkFromScene(id);});
    $("#std").html("");
}

function removeWorkFromScene(id) {
	$("#as_li_" + id).removeClass("scenelistitem");
	$("#ulAvailableScenes").append($("#as_li_" + id));
    $("#asimg_" + id).attr("src", "/images/plus.gif");
    $("#asimg_" + id).attr("alt", "Add work to this collection.");
    $("#asimg_" + id).attr("title", "Add work to this collection.");
    $("#add_scene_" + id).unbind();
    $("#add_scene_" + id).click(function() {addSceneWorkReference(id);});
    var wct = 0;
	$(".scenelistitem").each(
	        function(){
	            wct += 1;
	        });
	if (wct < 1) {
		$("#std").html(" click \"add this work to a scene\" to change " +
                       "the scene that this work appears in");
	}
}

function removeWorkFromPackage(id) {
	$("#ap_li_" + id).removeClass("worklistitem");
	$("#ulAvailablePackages").append($("#ap_li_" + id));
    $("#apimg_" + id).attr("src", "/images/plus.gif");
    $("#apimg_" + id).attr("alt", "Add work to this collection.");
    $("#apimg_" + id).attr("title", "Add work to this collection.");
    $("#add_package_" + id).unbind();
    $("#add_package_" + id).click(function() {addPackageWorkReference(id);});
    var wct = 0;
	$(".worklistitem").each(
	        function(){
	            wct += 1;
	        });
	if (wct < 1) {
		$("#wtd").html(" click \"add this work to a collection\" to change " +
                       "the collections that this work appears in");
	}
}

function setOrder() {
	var wso = "";
	var sso = ""
	var gilo = "";
	$(".worklistitem").each(
	        function(){
	            wso += $(this).attr("id").substr(6) + "|";
	        });
	$(".scenelistitem").each(
	        function(){
	            sso += $(this).attr("id").substr(6) + "|";
	        });
	$(".gallerylistitem").each(
            function(){
                gilo += $(this).attr("id") + "|";
            });
	
//	$(".image_desc").each(
//            function(){
//            	var appendText = '';
//            	appendText += '<input type="hidden" id="txt_';
//            	appendText += $(this).attr("id") + '" value="';
//            	appendText += $(this).html() + '">';
//            	alert("Appending: " + appendText);
//            	$("#gallery_form").append(appendText);
//            });
	$("#sorted_packages").val(wso);
	$("#sorted_scenes").val(sso);
    $("#sorted_gallery_images").val("" + gilo + "");
    return true;
}

function addGalleryImage(file_url, id) {
    var newImageID = id;
    $('#no_gallery_img_msg').html('');
    var addImageHTML = '<li class="gallerylistitem" id="';
    addImageHTML += newImageID + '"><a href="' + file_url;
    addImageHTML += '" class="gallery_image_set" rel="gallery">';
    addImageHTML += '<img height=50 src="' + file_url;
    addImageHTML += '" /></a>&nbsp;<a href="javascript:;" id="del' + newImageID + '"><img src="/images/x.gif" /></a>';
    addImageHTML += '<br /><a href="javascript:;" id="capt_img_' + newImageID + '"><img src="/images/edit.gif" title="Add or edit image caption." /></a>';
    addImageHTML += '&nbsp;<span  class="image_desc" id="img_capt_' + newImageID + '">add a caption</span>';
    addImageHTML += '</li>';    
    $(addImageHTML).appendTo('#ulGalleryImageList');
    $('#del' + newImageID).click(function() {
        if (confirm('Are you sure you want to remove this image from the gallery? You will need to upload the image again if you change your mind.')) {
            $('#' + newImageID).remove();
        }
    });
    $('#capt_img_' + newImageID).click(function() {
        showCaptionPopup(newImageID, $("#img_capt_" + newImageID).html());
     });
    $("#dynamic_image_id").val(generateGuid());
    $(".gallery_image_set").fancybox();
}

function showCaptionPopup(id, current_caption) {
	$("#gi_caption").val("");
	if ($("#img_capt_" + id).html() !== 'add a caption') {
		$("#gi_caption").val($("#img_capt_" + id).html());
	}
    $('#change_caption_popup').jqmShow();
    $("#update_caption_button").unbind();
    $("#update_caption_button").click(function() {
    	updateCaption(id);
    });
}

function updateCaption(id) {
	$("#img_capt_" + id).html($("#gi_caption").val());
	var appendText = '';
	$("#txt_" + id).remove();
	appendText += '<input type="hidden" id="txt_';
	appendText += id + '" ';
	appendText += 'name="txt_' + id + '" ';
	appendText += 'value="';
	if ($("#gi_caption").val() !== "") {
		appendText += $("#gi_caption").val() + '" />';
	}
	else {
		appendText += '" />';
		$("#img_capt_" + id).html("add a caption");
	}
	$("#work_form").append(appendText);
    $('#change_caption_popup').jqmHide();
    return false;
}

function showDescriptionEditor() {
	$("#desc_ctl_txt").html("hide text editor");
    $("#desc_help_txt").html("enter the text below");
    $("#work_description_control").unbind();
    $("#work_description_control").bind('click', function() { hideDescriptionEditor(); });
    $('#work_description').show('slow');
    $("#desc_ctl_img").attr("src", "/images/shrink.gif");
    $("#desc_ctl_img").attr("alt", "Image button to click to hide text editor.");
    $("#work_description_control").attr("title", "click to hide text editor");
    $("#work_description_control").tooltip({ showURL: false, delay: 0 });
}

function hideDescriptionEditor() {
    $("#desc_ctl_txt").html("show text editor");
    $("#desc_help_txt").html("main text of this work");
    $("#work_description_control").unbind(); 
    $("#work_description_control").bind('click', function() { showDescriptionEditor(); }); 
    $('#work_description').hide('slow');
    $("#desc_ctl_img").attr("src", "/images/grow.gif");
    $("#desc_ctl_img").attr("alt", "Image button to click to show text editor.");
    $("#work_description_control").attr("title", "click to show text editor");
    $("#work_description_control").tooltip({ showURL: false, delay: 0 });
}