function updatePackageTitle() {
    if ($('#title').val() !== '') {
        $('#package_title').html($('#title').val());
        $('#hidden_package_name').val($('#title').val());
        $('#change_title_popup').jqmHide();
        }
    else {
        alert("Enter a title for this collection.");
    }
}

function showTitleChange(evt) {
    $('#updated_name').val($('#package_title').html());
    $('#name_change_input').show();
}

$(document).ready(function() {
    $("#packages_tab").addClass("here");
    
    $('#update_name').bind('click', updatePackageTitle);
    $('#cancel_update_name').bind('click', function() {$('#name_change_input').hide()});
    $('#change_name_trigger').bind('click', showTitleChange);  
    $('#package_basic_tags').jqm({modal: true, trigger: '#package_basic_tags_control'});
    $('#package_basic_tags').jqmAddClose('#package_basic_tags_close_dialog');
    $('#package_deep_tags').jqm({modal: true, trigger: '#package_deep_tags_control'});
    $('#package_deep_tags').jqmAddClose('#package_deep_tags_close_dialog'); 
    $(".rte-zone").rte("/css/rte.css", "/images/");
    $('#package_pic_upload').jqm({modal: true, trigger: '#pic_upload_control'});
    $('#package_pic_upload').jqmAddClose('#package_pic_upload_close_dialog');
    $('#change_image_trigger').bind('click', function() {$('#image_change_input').toggle()});
    $('#unpackaged_works').jqm({trigger: '#unpackaged_works_control'});
    $('#unpackaged_works').jqmAddClose('#unpackaged_works_close_dialog'); 
    $('#pkg_sv_btn').bind('click', setOrder);
    $('#available_scenes').jqm({trigger: '#available_scenes_control', toTop: true});
    $('#available_scenes').jqmAddClose('#available_scenes_close_dialog');
    $('#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() {updatePackageTitle()});
    $('#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);
    });
    $('#package_description_control').bind('click', function() {
 		showDescriptionEditor();   	
    });
});

function addToPackage(id) {
	$("#content_ctl_" + id).unbind();
    $("#content_ctl_" + id).click(function() {removeFromPackage(id);});
	$("#ulWorkList").append($("#work_li_" + id));
    $("#work_li_" + id).addClass("worklistitem");
    $("#work_li_" + id).attr("title", "click a work and drag it to change its order in the collection");
    $("#work_li_" + id).tooltip({ showURL: false, delay: 0 });
    $("#awimg_" + id).attr("src", "/images/x.gif");
    $("#awimg_" + id).attr("alt", "Remove from collection.");
    $("#awimg_" + id).attr("title", "Remove from collection.");
    $("#wtd").html("");
}

function removeFromPackage(id) {
    $("#content_ctl_" + id).unbind();
    $("#content_ctl_" + id).click(function() {addToPackage(id);});
	$("#work_li_" + id).removeClass("worklistitem");
	$("#ulUnpackagedWorkList").append($("#work_li_" + id));
    $("#awimg_" + id).attr("src", "/images/plus.gif");
    $("#awimg_" + id).attr("alt", "Add to collection.");
    $("#awimg_" + id).attr("title", "Add to collection.");
    var wct = 0;
	$(".worklistitem").each(
	        function(){
	            wct += 1;
	        });
	if (wct < 1) {
		$("#wtd").html("click \"add or remove works\" " +
				"to change the contents of this collection");
	}
}

function addScenePackageReference(id) {
    $("#ulPackageSceneList").append($("#pas_li_" + id));
    $("#pas_li_" + id).addClass("scenelistitem");
    $("#pas_li_" + id).attr("title", "click and drag to change the order of the scenes");
    $("#pas_li_" + id).tooltip({ showURL: false, delay: 0 });
    $("#pasimg_" + id).attr("src", "/images/x.gif");
    $("#pasimg_" + id).attr("alt", "Remove collection from this scene.");
    $("#pasimg_" + id).attr("title", "Remove collection from this scene.");
    $("#padd_scene_" + id).unbind();
    $("#padd_scene_" + id).click(function() {removePackageFromScene(id);});
    $("#pstd").html("");
    $('#pswb').bind('click', setOrder);
}

function removePackageFromScene(id) {
    $("#pas_li_" + id).removeClass("scenelistitem");
    $("#ulAvailableScenes").append($("#pas_li_" + id));
    $("#pasimg_" + id).attr("src", "/images/plus.gif");
    $("#pasimg_" + id).attr("alt", "Add collection to this scene.");
    $("#pasimg_" + id).attr("title", "Add collection to this scene.");
    $("#padd_scene_" + id).unbind();
    $("#padd_scene_" + id).click(function() {addScenePackageReference(id);});
    var wct = 0;
    $(".scenelistitem").each(
            function(){
                wct += 1;
            });
    if (wct < 1) {
        $("#pstd").html(" click \"add this collection to a scene\" to change " +
                        "the scene that this collection appears in");
    }
}

function setOrder() {
	var wso = "";
	var sso = "";
	$(".worklistitem").each(
	        function(){
	            wso += $(this).attr("id").substr(8) + "|";
	        });
    $(".scenelistitem").each(
        function(){
            sso += $(this).attr("id").substr(7) + "|";
        });
	$("#sorted_package_contents").val(wso);
	$("#sorted_scenes").val(sso);
	return true;
}

function showDescriptionEditor() {
	$("#desc_ctl_txt").html("hide text editor");
    $("#desc_help_txt").html("enter the text below");
    $("#package_description_control").unbind();
    $("#package_description_control").bind('click', function() { hideDescriptionEditor(); });
    $('#package_description').show('slow');
    $("#desc_ctl_img").attr("src", "/images/shrink.gif");
    $("#desc_ctl_img").attr("alt", "Image button to click to hide text editor.");
    $("#package_description_control").attr("title", "click to hide text editor");
    $("#package_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");
    $("#package_description_control").unbind(); 
    $("#package_description_control").bind('click', function() { showDescriptionEditor(); }); 
    $('#package_description').hide('slow');
    $("#desc_ctl_img").attr("src", "/images/grow.gif");
    $("#desc_ctl_img").attr("alt", "Image button to click to show text editor.");
    $("#package_description_control").attr("title", "click to show text editor");
    $("#package_description_control").tooltip({ showURL: false, delay: 0 });
}