
jQuery.fn.extend({
    revealSlideDown: function() { 
        return this.each(function() {
            $(this).hide().animate({opacity: 1}, 500).slideDown();
        });
    }
});

function permaLink() {
    var request = "<?php echo $_SERVER['SCRIPT_NAME'] ?>?custom=1&"
                + $("#cameraSpecifications :input").serialize();
    return request;
}

function sendSaveRequest() {
    var request = $("#cameraSpecifications :input, #savedialog :input").serialize();
	$.get("savecamera.php?" + request, function(data, status) {
		showResponse("Your changes were sent to EyeSee360 for review. Thanks for your support!");
	});
}

function clickSaveChanges() {
    $("#response").hide();
    $("#savedialog").fadeIn();
    $(".bgmask").fadeIn(500);
}

function dismissDialog() {
    $(".bgmask").fadeOut(500);
    $("#response").hide();
}

function showDisclaimer() {
    $("#response").hide();
    $("#savedialog").hide();
    $("#disclaimer").fadeIn();
    $(".bgmask").fadeIn(500);
}

function dismissDisclaimer() {
    $(".bgmask").fadeOut(500);
    $("#disclaimer").hide();
}

function showResponse(message) {
    if (!message) message = "Thanks for your feedback!";
    $("#responseMessage").text(message);
    $("#savedialog").fadeOut();
    $("#response").fadeIn();
}

function showSaveBanner() {
    var saveBanner = $("#saveBanner");
    if (saveBanner.length == 0) {
        saveBanner = $("<div id=\"saveBanner\"></div>").addClass("banner")
                                                       .prependTo($("#cameraSpecifications"))
                                                       .revealSlideDown();
    }
    saveBanner.empty()
              .append($("<a>[Link]</a>").attr('href',permaLink()))
              .append("&nbsp;")
              .append($("<a>[Send to EyeSee360]</a>").click(clickSaveChanges));
}

function updateField() {
    var val = $(this).val();
    var name = $(this).attr('name');
    if (!val) {
        val = '?';
    } else {
        var request = $("#cameraSpecifications :input").serialize();
        $.getJSON("editcamerarequest.php?" + request, function(data, status) {
            $("#compatibility dl").replaceWith(data.compatibilityHTML);
            $("#opticUsage ul").replaceWith(data.usageHTML);
            $("#mountParts").empty().append(data.mountHTML);
            $("#opticFOV > div.mountDrawing").empty().append(data.fovHTML);
            if (data.missingData) {
                $(".missingFlag").slideDown();
            } else {
                $(".missingFlag").slideUp();
            }
            renderMount();
            if (name != "Configuration:Diopter") {
                showSaveBanner();
            }
        });
    }
};

function clickToEdit() {
    $(this).unbind('click',clickToEdit);
    var value = ($(this).text() == '?') ? '' : $(this).text();
    var editor = $("<input>").attr('name',$(this).attr('id')).attr('value',value)
                         .change(updateField);
    $(this).html(editor);
    editor.focus();
};

function editAllFields() {
    $(this).hide();
    $(".specfield").removeClass("missing").click(clickToEdit).click();
}

function renderMount() {
    if (true) { //$.browser.msie == false) {
        $(".mountDrawing").css('padding',0);
        fovView = renderMountForDOM($("#opticFOV"));
        if (fovView) {
            fovView.setMaximumDrawSize('260px','300px');
            mountView = renderMountForDOM($("#mount"));
            mountView.setMaximumDrawSize('260px','300px');
            $(".mountDrawing canvas + *").css('padding-top', '300px');
            if ($.browser.msie == false) {
                animateMountSpacing(mountView, 30.0, 0.0);
            }
        }
    } else {
        $(".mountDrawing").prepend($("<div class=\"missingFlag\"><div>Not compatible with Internet Explorer.<br> We recommend <a href=\"http://www.getfirefox.com\">Firefox</a>.</div></div>"));
    }
}

function animateMountSpacing(mountView, startSpacing, endSpacing) {
    var epsilon = 0.1;
    var exponent = 0.6;
    var elementSpacing = startSpacing;
	mountView.setElementSpacing(elementSpacing);
    
    window.setTimeout( function() {
        var ivl = window.setInterval( function animate() {
        	if (elementSpacing > endSpacing + epsilon) {
        		elementSpacing = (elementSpacing - endSpacing) * exponent + endSpacing;
        	} else {
        		elementSpacing = endSpacing;
        		window.clearInterval(ivl);
        	}
        	mountView.setElementSpacing(elementSpacing);
        }, 50);
    }, 100 );
}

$(document).ready( function() {
//    $(".missing").click(clickToEdit);
    $(".edit").click(editAllFields);
    $("#c").change(function() { $(this.form).submit(); });
    $("#l").change(function() { $(this.form).submit(); });
    $("#cameraSpecifications :input").change(updateField).blur(updateField);
    $("select[name=Configuration:Diopter]").val();

    renderMount();
    $(".missingFlag div").revealSlideDown();
});

