$(function() {
    $(".gcfixme, #globe").fixPng();
    $("a.editLink.show").click(editPhoto);
    $("a.editLink.hide").click(cancelEdit);
    $(".filter a").click(switchView);
    $("a.checkPhotos").click(selectAll);
    $("#archiveForm").submit(checkPwd);
});

function editPhoto(e) {
    e.preventDefault();
    var pid = $(this).attr("pid");
    $("a.editLink[pid="+pid+"]").toggle();
    
    var editPicForm = $("#dummyFormElmt").clone(true).css("display", "block");
    var caption = $.trim($("#pid_"+ pid + " dd.caption").html().replace(/\n/g, ""))
            .replace(/<br>/gi, "\n").replace(/<br\/>/gi, "\n")
            .replace(/<(\/?[bui])>/gi, function(tag, t) { return "["+t.toLowerCase()+"]"; })
            .replace(/&amp;/gi, "&");
    $("#caption", editPicForm).prev("label").attr("for", "edit_" + pid + "_caption");
    $("#caption", editPicForm).val(caption)
        .attr("name", "edit:" + pid + ":caption")
        .attr("id", "edit_" + pid + "_caption")
        .keyup(function() { limitTextarea(this, captionMax);});
    $(".limit", editPicForm).attr("id", "edit_" + pid + "_caption_limit");

    var title = $.trim($("#pid_"+ pid + " dd.title").html()).replace(/&amp;/gi, "&").replace(/&lt;/gi, "<").replace(/&gt;/gi, ">");
    $("#title", editPicForm).prev("label").attr("for", "edit_" + pid + "_title");
    $("#title", editPicForm).val(title).attr("name", "edit:" + pid + ":title").attr("id", "edit_" + pid + "_title");

    // select the camera in the drop down
    var camera = $.trim($("#pid_"+ pid + " dd.camera").html());
    cameraOpts = $("#camera option", editPicForm);
    cameraOpts.each(function() {
        if ($.trim($(this).text()) == camera)
            $(this).attr("selected", true);
        });
    $("#camera", editPicForm).attr("name", "edit:" + pid + ":camera");
    
    $("select.year", editPicForm).attr("name", "edit:" + pid + ":year");
    $("select.month", editPicForm).attr("name", "edit:" + pid + ":month");
    $("select.day", editPicForm).attr("name", "edit:" + pid + ":day");
    
    $("#edit_pid", editPicForm).val(pid);

    $("#pid_"+ pid).after(editPicForm.attr("id", "edit_pid_" + pid));
    $("#pid_"+ pid).hide();
}

function cancelEdit(e) {
    e.preventDefault();
    var pid = $(this).attr("pid");
    $("a.editLink[pid="+pid+"]").toggle();

    $("#edit_pid_" + pid).remove();
    $("#pid_"+ pid).show();
}

function switchView(e) {
    e.preventDefault();
    var dayList = $("#dayList");
    if (dayList.hasClass("list"))
        dayList.removeClass("list").addClass("grid");
    else
        dayList.removeClass("grid").addClass("list");
    $(".filterLabel").toggle();
    $(".formEnd").toggle();
}

function selectAll(e) {
    e.preventDefault();
    if ($(this).attr("id") == "checkAll")
        $("#archiveForm [name='delete']").attr("checked", true);
    else
        $("#archiveForm [name='delete']").attr("checked", false);            
    $(".checkPhotos").toggle();
}

function checkPwd() {
    if ($.trim($("#passwordField").val()) == "") {
        alert(editPwdError);
        $("#passwordField").focus();
        return false;
    }
    else
        return true;
}

/*
*	
*	Displays the number of photos for a day in the side nav 
*	
*	Arguments:
*		elmtId - the element to display
*		className - the classname to change it to
*
*/	
function mouseOverTip(elmtId, className) {
	if (document.getElementById)
	{
		elmt = document.getElementById(elmtId);
		elmt.className = className;
	}
}


/*
*	Expands or collapses a year list in the left nav
*	
*	Arguments:
*		elmtId - the element to change
*
*/	
function toggleYear(elementId) {
	if (document.getElementById)
	{
		monthsElmt = document.getElementById(elementId);
		if (monthsElmt.className == "selected") {
			monthsElmt.className = "";
		} else {
			monthsElmt.className = "selected";	
		}
	}
}

function limitTextarea(thearea, limit) {
    if (thearea.value.length > limit) {
        thearea.value = thearea.value.substring(0, limit);
        $('#'+thearea.id+'_limit').show();
        // append a little text note with character limit
    }
}