/**
 * Copyright (c) 2009 Yakabod, inc.
 * File: MicroProfile.inc
 *
 * $Id: MicroProfileDialog.js,v 1.19 2012/01/11 15:37:37 danderson Exp $
 * $Revision: 1.19 $
 * $Date: 2012/01/11 15:37:37 $
 * Last edited by: $Author: danderson $
 *
 * A dialog display of a MicroProfile
 * displayed automatically when an object is added
 * via the Content Add framework/
 *
 */

function loadMicroProfile(sURN, oData)
{
  var oMicroProfileDialog = new MicroProfileDialog(sURN, oData);
}

function getMicroProfile(oEvent, oData)
{
  /* Method that we will bind to the success event. */
  var aUrn = oData.object.sURN.split(":");
  if (aUrn[2] != null && aUrn[2].toLowerCase() != 'message')
  {
    loadMicroProfile(oData.object.sURN, oData);
  }
}

MicroProfileDialog = function(sURN, oData)
{
  this.init = function( sURN, oData )
  {
    this.sURN  = sURN;
    this.oData = oData || {};

    var url = "/microProfiles/viewMicroProfile.html?urn=" + this.sURN + '&sMicroMessage=' + escape(oData.sMessage);
    if (oData.bDisplayBanners !== undefined)
    {
      url += '&bDisplayBanners=' + oData.bDisplayBanners;
    }
    var sDialogDiv = 'sDialogDiv';
    var sMicroProfileDiv = 'sNewMicroProfileDiv';

    var oDialogDiv = document.getElementById( sDialogDiv );
    var oMicroProfileDiv = document.getElementById( sMicroProfileDiv );
    if (oDialogDiv != null && oMicroProfileDiv != null)
    {
      bindMicroProfile(sMicroProfileDiv, url);
    }
    else
    {
      /* UI Objects */
      this.oDialog = null;              /* Container for Dialog */
      this.oMicroProfile = null;        /* Container for MicroProfile*/

      this._showMicroProfile(sDialogDiv, sMicroProfileDiv, url);
    }
  };

  this._showMicroProfile = function(sDialogDiv, sMicroProfileDiv, url)
  {
    var oThis = this;

    this.oDialog = $( document.createElement( 'div' ) ).attr( 'name', sDialogDiv ).attr( 'id', sDialogDiv );
    $( 'body' ).append( this.oDialog );

    var sTitle = 'New Item Confirmation';
    if ( typeof oThis.oData == 'object' )
    {
      if ( typeof oThis.oData.sMessage == 'string' )
      {
        sTitle = oThis.oData.sTitle;
      }
    }

    this.oMicroProfile = $( document.createElement( 'div' )).attr( 'name', sMicroProfileDiv ).attr( 'id', sMicroProfileDiv );
    this.oDialog.append( this.oMicroProfile );

    bindMicroProfile(sMicroProfileDiv, url);

    this.oDialog.dialog({ modal : false,
                          bgiframe: true,
                                  position  : "center",
                                  title     : sTitle,
                                  closeOnEscape: false,
                                  overlay   : {
                                                opacity: 0.5,
                                                background: "black"
                                              },
                                  buttons   : {
                                                "Close": {
                                                           text: "Close",
                                                           id: "ybx-dlgConfirm-close",
                                                           click: function()
                                                           {
                                                             $(this).dialog("close");
                                                           }
                                                }
                                              },
                                  close     : function(event, ui)
                                              {
                                                oThis._destroyModalContainer();
                                              },
                                  open      : function( event, ui )
                                              {
                                                oThis._bindCloseToMicroProfile();
                                              },
                                  resizable : false,
                                  draggable : true} ).dialog( 'open' );
  };

  ( function( oThis )
  {
    var nInterval, bFunctionRunning = false, bContinueWaiting = true, bBehaviorAdded = false;
    oThis._bindCloseToMicroProfile = function()
    {
      if( ! bFunctionRunning )
      {
        bFunctionRunning = true;

        if( typeof nInterval !== 'number' )
        {
          nInterval = window.setInterval( oThis._bindCloseToMicroProfile, 10 );
        }

        oThis
          .oDialog
            .find( '.ybx-microprofile-content' )
              .each( function()
              {
                $( this ).delegate( 'a, button', 'click', function(e)
                         {
                           if ( $('#'+e.target.id).hasClass( 'ybx-microprofile-dialog-noclose' ) === false)
                           {
                              oThis.oDialog.dialog( 'close' );
                           }
                         } );

                bBehaviorAdded = true;
              } );

        if( ! bContinueWaiting || bBehaviorAdded)
        {
          window.clearInterval( nInterval );
        }

        bFunctionRunning = false;
      }
    };

    oThis._destroyModalContainer = function()
    {
      /* tell above interval to stop waiting for microprofile DIV if it hasn't found it yet */
      bContinueWaiting = false;
      /* empty and remove this dialog */
      oThis.oMicroProfile.empty().remove();
      oThis.oDialog.empty().remove();
    };
  }( this ) );

  /* call constructor */
  this.init( sURN, oData );
};


$( document ).bind( ContentAddModuleController.callbacks.defaultEvents.success, getMicroProfile);

