/**
 * ContentAddSettings.js
 * Define the ContentAddSettings class/prototype
 *
 * $Revision: 1.10 $
 * $Date: 2010/06/03 13:52:19 $
 * Last edited by: $Author: jthorhauer $
 */
ContentAddSettings$Class = {

  sContentType                : '',
  sContentURN                 : '',
  oEventTriggers              : null,
  sOwnerURN                   : '',
  sDeriveFromURN              : '',
  oDeriveProcessing           : {},
  oSeeAlso                    : null,
  oCanEditRestrictions        : null,
  oHandling                   : null,
  oRestrictions               : null,
  oTags                       : null,
  oWorkflowInfo               : null,
  bNewWindow                  : false,
  bCloseOpeningWindow         : false,
  oObjectPreferences          : {},

  __construct : function()
  {
    jQuery.extend( true, this, ContentAddSettings$Class );
  },

  __setContentType : function( sContentType )
  {
    if( typeof this.__super != 'undefined' )
    {
      this.__super.__setContentType( sContentType );
    }
    else
    {
      this.sContentType = sContentType;
    }

    return this;
  },
  /**
   * setEventTriggers - add events to trigger on error or success of the content add, and flag as to whether default events should be triggered
   *
   * @access public
   * @param OBJECT oEventTriggers (see below structure)
   * @return ContentAddSettings
   */
  setEventTriggers : function( oEventTriggers )
  {
    /* Structure of oEventTriggers

      {
        success: {
          events: [ 'EventName', 'EventName' ],
          allowDefault: true //or false
        },
        error: {
          events: [ 'EventName', 'EventName' ],
          allowDefault: true //or false
        }
      }
    */
    if( typeof oEventTriggers === 'object' && oEventTriggers !== null )
    {
      if( typeof this.__super !== 'undefined' )
      {
        this.__super.setEventTriggers( oEventTriggers );
      }
      else
      {
        this.oEventTriggers = oEventTriggers;
      }
    }

    return this;
  },

  addSuccessTriggerEvent : function( oSuccessTrigger )
  {
    if( typeof this.__super !== 'undefined' )
    {
      this.__super.addSuccessTriggerEvent( oSuccessTrigger );
    }
    else
    {
      if( this.oEventTriggers === null )
      {
        this.oEventTriggers = {};
      }
      if( typeof this.oEventTriggers['success'] != 'object' )
      {
        this.oEventTriggers['success'] = { events       : [],
                                           allowDefault : true
                                         };
      }
      this.oEventTriggers['success']['events'].push( oSuccessTrigger );
    }

    return this;
  },

  addErrorTriggerEvent : function( oErrorTrigger )
  {
    if( typeof this.__super !== 'undefined' )
    {
      this.__super.addErrorTriggerEvent( oErrorTrigger );
    }
    else
    {
      if( this.oEventTriggers === null )
      {
        this.oEventTriggers = {};
      }
      if( typeof this.oEventTriggers['error'] != 'object' )
      {
        this.oEventTriggers['error'] = { events       : [],
                                         allowDefault : true
                                       };
      }
      this.oEventTriggers['error']['events'].push( oErrorTrigger );
    }

    return this;
  },

  __initDefaultSettings : function( retval )
  {
    retval                                    = retval                                   || {};
    retval['defaultSettings']                 = retval['defaultSettings']                || {};
    retval['defaultSettings']['processing']   = retval['defaultSettings']['processing']  || {};
    retval['defaultSettings']['preferences']  = retval['defaultSettings']['preferences'] || this.oObjectPreferences;

    return retval;
  },

  addObjectPreference : function( sPreferenceName, sPreferenceValue )
  {
    if(( typeof sPreferenceName !== 'undefined' ) &&
       ( sPreferenceName !== null ) &&
       ( sPreferenceName !== '' ))
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.oObjectPreferences[sPreferenceName] = sPreferenceValue;
      }
      else
      {
        this.oObjectPreferences[sPreferenceName] = sPreferenceValue;
      }
    }
    return this;
  },

  setDeriveFromUrn : function( sDeriveFromURN, oDeriveProcessing )
  {
    if( typeof this.__super != 'undefined' )
    {
      this.__super.setDeriveFromUrn( sDeriveFromURN, oDeriveProcessing );
    }
    else
    {
      if(( typeof sDeriveFromURN == 'string' ) &&
         ( sDeriveFromURN != '' ))
      {
        this.sDeriveFromURN = sDeriveFromURN;

        if(( typeof oDeriveProcessing == 'object' ) &&
           ( typeof oDeriveProcessing.length != 'undefined' ) &&
           ( oDeriveProcessing.length > 0 ))
        {
          var oTmp = {};
          for( var i = 0; i < oDeriveProcessing.length; i++ )
          {
            sTmp = oDeriveProcessing[i];

            oTmp[sTmp] = '';
          }
          jQuery.extend( true, this.oDeriveProcessing, oTmp );
        }
        else if( typeof oDeriveProcessing == 'object' )
        {
          jQuery.extend( true, this.oDeriveProcessing, oDeriveProcessing );
        }
      }
    }

    return this;
  },

  setOwnerURN : function( sOwnerURN )
  {
    if( typeof this.__super != 'undefined' )
    {
      this.__super.sOwnerURN = sOwnerURN;
    }
    else
    {
      this.sOwnerURN = sOwnerURN;
    }
    return this;
  },

  setContentURN : function( sContentURN )
  {
    if(( typeof sContentURN != 'undefined' ) && ( sContentURN.replace( /^\s*|\s*$/g, '' ) != '' ))
    {
      this.__super.sContentURN = sContentURN;

      try
      {
        var aURNInfo = sContentURN.split( ':' );

        /**
         * Make sure we have a valid URN
         */
        if(( aURNInfo.length == 4 ) &&
           ( typeof aURNInfo[2] == 'string' ) &&
           ( !isNaN( Number( aURNInfo[3] ))) &&
           ( Number( aURNInfo[3] ) > 0 ))
        {
          var sContentType = aURNInfo[2];

          if( sContentType != this.sContentType )
          {
            this.__setContentType( sContentType );
          }
        }
      } catch( e ) {}
    }
    return this;
  },

  setSeeAlso : function( oSeeAlso )
  {
    if( typeof oSeeAlso == 'object' )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setSeeAlso( oSeeAlso );
      }
      else
      {
        if( this.oSeeAlso === null )
        {
          this.oSeeAlso = {};
        }
        jQuery.extend( true, this.oSeeAlso, { 'SeeAlso' : oSeeAlso } );
      }
    }

    return this;
  },

  setCanEditRestrictions : function( oCanEditRestrictions )
  {
    if( typeof oCanEditRestrictions == 'object' )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setCanEditRestrictions( oCanEditRestrictions );
      }
      else
      {
        if( this.oCanEditRestrictions === null )
        {
          this.oCanEditRestrictions = {};
        }
        jQuery.extend( true, this.oCanEditRestrictions, { 'CanEditRestrictions' : oCanEditRestrictions } );
      }
    }

    return this;
  },

  setHandling : function( oHandling )
  {
    if( typeof oHandling == 'object' )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setHandling( oHandling );
      }
      else
      {
        if( this.oHandling === null )
        {
          this.oHandling = {};
        }
        jQuery.extend( true, this.oHandling, { 'Handling' : oHandling } );
      }
    }

    return this;
  },

  setRestrictions : function( oRestrictions )
  {
    if( typeof oRestrictions == 'object' ||
        typeof oRestrictions == 'string' )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setRestrictions( oRestrictions );
      }
      else
      {
        if( this.oRestrictions === null )
        {
          this.oRestrictions = {};
        }
        jQuery.extend( true, this.oRestrictions, { 'Restrictions' : oRestrictions } );
      }
    }

    return this;
  },

  setTags : function( oTags )
  {
    if( typeof oTags == 'object' )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setTags( oTags );
      }
      else
      {
        if( this.oTags === null )
        {
          this.oTags = {};
        }
        jQuery.extend( true, this.oTags, { 'Tags' : oTags } );
      }
    }

    return this;
  },

  createWorkflowObject : function()
  {
    if( typeof this.__super != 'undefined' )
    {
      this.__super.createWorkflowObject();
    }
    else
    {
      if( this.oWorkflowInfo === null )
      {
        this.oWorkflowInfo = {};
        jQuery.extend( true, this.oWorkflowInfo, { 'WorkflowInfo' : {}} );
      }
    }

    return this.oWorkflowInfo;
  },

  setWorkflowJobId : function( iJobId )
  {
    if( iJobId !== null )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setWorkflowJobId( iJobId );
      }
      else
      {
        jQuery.extend( true, this.createWorkflowObject().WorkflowInfo, { 'JobId' : iJobId } );
      }
    }

    return this;
  },

  setWorkflowJobName : function( sJobName )
  {
    if( sJobName !== null )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setWorkflowJobName( sJobName );
      }
      else
      {
        jQuery.extend( true, this.createWorkflowObject().WorkflowInfo, { 'JobName' : sJobName } );
      }
    }

    return this;
  },

  setWorkflowRequestId : function( iWorkflowRequestId )
  {
    if( iWorkflowRequestId !== null )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setWorkflowRequestId( iWorkflowRequestId );
      }
      else
      {
        jQuery.extend( true, this.createWorkflowObject().WorkflowInfo, { 'WorkflowRequestId' : iWorkflowRequestId } );
      }
    }
    return this;
  },

  setWorkflowRequestStepId: function( iRequestStepId )
  {
    if( iRequestStepId !== null )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setWorkflowRequestStepId( iRequestStepId );
      }
      else
      {
        jQuery.extend( true, this.createWorkflowObject().WorkflowInfo, { 'WorkflowRequestStepId' : iRequestStepId } );
      }
    }
    return this;
  },

  setWorkflowStepNumber : function( iStepNumber )
  {
    if( iStepNumber !== null )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setWorkflowStepNumber( iStepNumber );
      }
      else
      {
        jQuery.extend( true, this.createWorkflowObject().WorkflowInfo, { 'StepNumber' : iStepNumber } );
      }
    }
    return this;
  },

  setWorkflowStepData : function( oStepData )
  {
    if( oStepData !== null )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setWorkflowStepData( oStepData );
      }
      else
      {
        jQuery.extend( true, this.createWorkflowObject().WorkflowInfo, { 'StepData' : oStepData } );
      }
    }
    return this;
  },

  setWorkflowObjectURN : function( sObjectURN )
  {
    if( sObjectURN !== null )
    {
      if( typeof this.__super != 'undefined' )
      {
        this.__super.setWorkflowObjectURN( sObjectURN );
      }
      else
      {
        jQuery.extend( true, this.createWorkflowObject().WorkflowInfo, { 'ObjectURN' : sObjectURN } );
      }
    }
    return this;
  },

  setCloseOpeningWindow : function( bCloseOpeningWindow )
  {
    if( typeof this.__super != 'undefined' )
    {
      this.__super.setCloseOpeningWindow( bCloseOpeningWindow );
    }
    else
    {
      this.bCloseOpeningWindow = bCloseOpeningWindow;
    }

    return this;
  },

  __setNewWindow : function( bNewWindow )
  {
    if( typeof this.__super != 'undefined' )
    {
      this.__super.__setNewWindow( bNewWindow );
    }
    else
    {
      this.bNewWindow = bNewWindow;
    }

    return this;
  },

  /**
   *
   * QuickAddSettings['sContentType']
   * QuickAddSettings['oEventTriggers']
   * QuickAddSettings['bNewWindow']
   * QuickAddSettings['sOwnerURN']
   * QuickAddSettings['defaultSettings']['processing']['CanEditRestrictions']
   * QuickAddSettings['defaultSettings']['processing']['Handling']
   * QuickAddSettings['defaultSettings']['processing']['Restrictions']
   * QuickAddSettings['defaultSettings']['processing']['Tags']
   * QuickAddSettings['sDeriveFromURN']
   * QuickAddSettings['deriveSettings']['processing']['CanEditRestrictions']
   * QuickAddSettings['deriveSettings']['processing']['Handling']
   * QuickAddSettings['deriveSettings']['processing']['Restrictions']
   * QuickAddSettings['deriveSettings']['processing']['Tags']
   *
   */
  getFullSettings : function()
  {
    var retval = {};

    retval['sContentType'] = this.sContentType;
    retval['oEventTriggers'] = this.oEventTriggers;
    retval['bNewWindow'] = this.bNewWindow;
    retval['bCloseOpeningWindow'] = this.bCloseOpeningWindow;

    if( this.sContentURN )
    {
      retval['sContentURN'] = this.sContentURN;
    }
    if( this.sOwnerURN )
    {
      retval['sOwnerURN'] = this.sOwnerURN;
    }
    if( this.oCanEditRestrictions !== null )
    {
      this.__initDefaultSettings( retval );

      jQuery.extend( true, retval['defaultSettings']['processing'], this.oCanEditRestrictions );
    }
    if( this.oHandling !== null )
    {
      this.__initDefaultSettings( retval );

      jQuery.extend( true, retval['defaultSettings']['processing'], this.oHandling );
    }
    if( this.oRestrictions !== null )
    {
      this.__initDefaultSettings( retval );

      jQuery.extend( true, retval['defaultSettings']['processing'], this.oRestrictions );
    }
    if( this.oTags !== null )
    {
      this.__initDefaultSettings( retval );

      jQuery.extend( true, retval['defaultSettings']['processing'], this.oTags );
    }
    if( this.oSeeAlso !== null )
    {
      this.__initDefaultSettings( retval );

      jQuery.extend( true, retval['defaultSettings']['processing'], this.oSeeAlso );
    }

    if( this.oWorkflowInfo !== null )
    {
      this.__initDefaultSettings( retval );

      jQuery.extend( true, retval['defaultSettings']['processing'], this.oWorkflowInfo );
    }



    if( this.sDeriveFromURN != '' )
    {
      retval['sDeriveFromURN'] = this.sDeriveFromURN;

      retval['deriveSettings'] = {};
      retval['deriveSettings']['processing'] = this.oDeriveProcessing;
    }

    return retval;
  }
};

/**
 * Alias the constructor, thus defining the "instantiable"
 * class.
 */
ContentAddSettings = ContentAddSettings$Class.__construct;
