var loadingHtml = '<div id="loader" class="span-18 last" style="line-height: 115px; text-align: center;"><img alt="activity indicator" src="/strange/images/loading.gif"/></div>';
function showStateDialog(releaseId, content) {
    if($("#stateDialog_"+releaseId).is('div.ui-dialog-content')) {
        $("#stateDialog_"+releaseId).html(content).dialog("open");
        $("#stateDialog_"+releaseId).css({"height":"450px","width":"600px","padding":"0px","overflow":"hidden"});
    } else {
        var stateDialogDiv = document.createElement("div");
        stateDialogDiv.setAttribute('id', 'stateDialog_'+releaseId);
        document.body.appendChild(stateDialogDiv);
        stateDialogDiv.setAttribute("title", "Edit State Press Release");
        stateDialogDiv.innerHTML = content;
        $("#stateDialog_"+releaseId).dialog({
            modal: true,
            bgiframe: true,
            height: 530,
            width: 600,
            resizable: false,
            draggable: false,
            buttons: {
                "Save": function Save() {
                    $("#stateDialog_"+releaseId).find("form").ajaxSubmit({
                        complete: function(r){
                            $("#pressReleaseContent").html(r.responseText).show("slow");
                            $("#stateDialog_"+releaseId).remove();
                            $("#editRelease").click(function (e) {
                                $(this).hide();
                                e.preventDefault();
                                var releaseId = $(this).attr("releaseId");
                                $.ajax({
                                    type: "GET",
                                    cache: false,
                                    data: { releaseId: releaseId },
                                    url: "pressroom!editRelease.action",
                                    success: function(result) {
                                        showStateDialog(releaseId, result);
                                    }
                                });
                                $(this).show();
                            });
                        }
                    });
                },
                "Cancel": function Close() {
                    $("#stateDialog_"+releaseId).dialog("close");
                }
            }
        });
        $("#stateDialog_"+releaseId).css({"height":"450px","width":"600px","padding":"0px","overflow":"hidden"});
    }
}
$(document).ready(function() {
    $("a[id^='state_']").click(function() {
        $("#pressReleaseContent").html(loadingHtml);
        var selectedState = $(this).attr("id").substr($(this).attr("id").indexOf("_")+1,$(this).attr("id").length);
        $.ajax({
            type: "GET",
            cache: false,
            data: { selectedState: selectedState },
            url: "pressroom!ajaxSelectState.action",
            success: function(result) {
                $("#pressReleaseContent").html(result);
            },
            complete: function(xhr) {
                $("#editRelease").click(function (e) {
                    $(this).hide();
                    e.preventDefault();
                    var releaseId = $(this).attr("releaseId");
                    // load the contact form using ajax
                    $.ajax({
                        type: "GET",
                        cache: false,
                        data: { releaseId: releaseId },
                        url: "pressroom!editRelease.action",
                        success: function(result) {
                            showStateDialog(releaseId, result);
                        }
                    });
                    $(this).show();
                });
            }
        });
        return false;
    });

    
//    $("#state").change(function () {
//         $("#pressReleaseContent").fadeOut();
//         $.ajax({
//           type: "POST",
//           url: "pressroom!ajaxSelectState.action?selectedState="+$("#state :selected").val(),
//           success: function(result){
//              $("#pressReleaseContent").html(result).hide();
//              $("#editRelease").click(function (e) {
//                  e.preventDefault();
//		  // load the contact form using ajax
//		  $.get("pressroom!editRelease.action?releaseId="+$(this).attr("href"), function(data){
//			// create a modal dialog with the data
//			$(data).modal({
//				close: false,
//				position: ["15%",],
//				overlayId: 'contact-overlay',
//				containerId: 'contact-container',
//				onOpen: contact.open,
//				onShow: contact.show,
//				onClose: contact.close
//			});
//		  });
//              })
//           },
//           complete: function() {
//               $("#pressReleaseContent").fadeIn("slow");
//           }
//         });
//    });
});