﻿var dialogs = new Array();

// Loads HTML via Ajax from the given Url and creates a dialog.
function sc_dialog(t, url, w, h, buttons, closeFunc)
{
    // Add randomness to the url to prevent client caching...
    var stringToAppendParam;
    if (url.indexOf('?') >= 0)
    {
        // there are already other GET parameters
        stringToAppendParam = "&";
    }
    else
    {
        stringToAppendParam = "?";
    }
    url += stringToAppendParam + "random=" + new Date().getTime();

    // Create a div for the dialog...
    var div = jQuery("<div id='theDialog' />", document);
    jQuery(div).load(url).dialog({
        modal: true,
        title: t,
        minHeight: h,
        height: h,
        minWidth: w,
        width: w,
        resizable: false,
        buttons: buttons,
        closeOnEscape: false,
        close: function(event, ui)
        {
            jQuery(this).remove();
            if (closeFunc)
            {
                closeFunc();
            }
        },
        open: function(event, ui)
        {
            // Remove the standard close X in the title bar.
            jQuery(".ui-dialog-titlebar-close").remove();
        }
    });

    dialogs.push(div);
}

function tb_remove()
{
    // Remove the last dialog created...
    if (dialogs.length > 0)
    {
        jQuery(dialogs.pop()).dialog("close");
    }
}

function showFolderSettingsDialog(folderId, isNew)
{
    sc_dialog("Edit Folder Properties", "/controls/lightboxes/settingsfolder.aspx?isNewFolder=" + isNew + "&mediaGroupId=" + folderId,
      635, 588,
      {
          "Cancel": function() { cancelEdit(); },
          "Save": function() { saveAll(true); }
      }, function() { doneEditing(folderId); });
}

function showPlaylistSettingsDialog(playlistGuid, isNew)
{
    sc_dialog("Edit Playlist Properties", "/controls/lightboxes/settingsplaylist.aspx?isNewPlaylist=" + isNew + "&playlistGuid=" + playlistGuid,
      635, 558,
      {
          "Cancel": function() { cancelEdit(); },
          "Save": function() { saveAll(true); }
      }, function () { doneEditing(playlistGuid); });
}

function showSendInvitationsDialog(containerType, containerIdType, containerId, mediaSetId)
{
    var containerGuidQueryStringName;
    if (containerIdType === "playlist")
    {
       containerGuidQueryStringName = "playlistGuid"
    }
    else
    {
        containerGuidQueryStringName = containerIdType + "Id";
    }

    sc_dialog("Send Invitations", "/controls/lightboxes/invitationform.aspx?pageType=" + containerType + "&" + containerGuidQueryStringName + "=" + containerId + "&mediaSetId=" + mediaSetId,
      540, 505,
      {
          "Done": function() { clickDone(); },
          "Send": function() { sendInvites(); }
      });
}

function showFolderEnterPasswordDialog(mediaGroupId, isNew)
{
    sc_dialog("Enter Password", "/controls/lightboxes/settingsfolderenterpassword.aspx?isNewFolder=" + isNew + "&mediaGroupId=" + mediaGroupId,
      508, 142,
      {
          "Cancel": function() { doneChanging(mediaGroupId); },
          "Save": function() { save(mediaGroupId); }
      });
}

function showPlaylistEnterPasswordDialog(playlistGuid, isNew)
{
    sc_dialog("Enter Password", "/controls/lightboxes/settingsplaylistenterpassword.aspx?isNewPlaylist=" + isNew + "&playlistGuid=" + playlistGuid,
    508, 142,
   {
       "Cancel": function () { doneChanging(playlistGuid); },
       "Save": function () { save(playlistGuid); }
   });
}

function showUploadDialog(title, url, closeFunc)
{
    sc_dialog(title, url, 557, 460,
      { "Close": function() { tb_remove(); }
      }, closeFunc);
}

// Takes the url of an itunes feed and a function that should be called
// when the done button is pressed in the publish dialog.
function showPublishDialog(feedUrl, doneFunction)
{
    sc_dialog("Publish Feed to iTunes", "/controls/lightboxes/itunesfeedpublish.aspx?url=" + feedUrl,
    577, 400,
    {
        "Done": doneFunction
    });
}

// Takes the url of an itunes feed, and shows the publish dialog
// Only use this function on the playlist page
function showPublishDialogOnLibraryPage(feedUrl)
{
    showPublishDialog( feedUrl, function() { tb_remove(); } );
}

// Takes the url of an itunes feed, and shows the publish dialog
// Only use this function on the playlist page
function showPublishDialogOnPlaylistPage(feedUrl)
{
    document.getElementById("mediaEmbedAreaFrame").src = "/playlistmediaviewer.aspx?showEmpty=true";
    document.getElementById("mediaEmbedAreaFrame").style.display = "none";

    showPublishDialog(feedUrl, function() { showPlaylistAfterHidden(); tb_remove(); });
}

function yesNoMsgBox(t, msg, yesFunc, noFunc)
{
    var div = jQuery("<div id='yesNoDialog'><div class='yesNoDialogIcon'></div><div style='margin-left:36px;'>" + msg + "</div></div>", document);
    jQuery(div).dialog({
        modal: true,
        title: t,
        resizable: false,
        buttons:
      {
          'No': function n() { noFunc(); jQuery(this).dialog('close'); },
          'Yes': function y() { yesFunc(); jQuery(this).dialog('close'); }
      },
        closeOnEscape: false,
        close: function(event, ui)
        {
            jQuery(this).remove();
        },
        open: function(event, ui)
        {
            // Remove the standard close X in the title bar.
            jQuery(".ui-dialog-titlebar-close").remove();
        }
    });
}
