/****************************************************************************
 * Copyright (c) 1998-2007 Luna Imaging, Inc.  All Rights Reserved.
 *
 * This software is confidential and proprietary information of
 * Luna Imaging, Inc.  ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Luna Imaging, Inc.
 *
 * The software may not be copied, reproduced, translated or reduced to
 * any electronic medium or machine-readable form without
 * the prior written consent of Luna Imaging.
 *
 * You are not allowed to distribute the binary and source code
 * (if released) to third parties, without the prior written consent from
 * Luna Imaging.
 *
 * You are not allowed to reverse engineer, disassemble or decompile
 * code, or make any modifications of the binary or source code, remove
 * or alter any trademark, logo, copyright or other proprietary notices,
 * legends, symbols, or labels in the Software.
 *
 * You are not allowed to sub-license the Software or any derivative
 * work based on or derived from the Software.
 *
 * LUNA IMAGING MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
 * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, LUNA IMAGING SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 *  cruiz10020@yahoo.com
 *
 *  QuickSlideControls.js
 *
 *  Description:
 *    Quickly access functionality of a presentaiton's slides
 *
 *  Structure:
 *
 *
 *  Requires:
 *    prototype.js - http://www.prototypejs.org/
 *
 *  Development History:
 *    5-14-2007       - created
 *
 *******************************************************************************/

function QuickSlideControls()
{
  // html elements
  this.mContainer = null;
  this.mPanel = null;

  this.mSaveCurrentSlideButton = null;
  this.mEditCurrentSlide = null;
  this.mCreateSlide = null;

  this.mClonePresentationButton = null;
  this.mJumpToFirstSlideButton = null;
  this.mJumpToLastSlideButton = null;

  this.mSlideDialogContainer = null;
  this.mSlideDialogInstructions = null;
  this.mDescriptionInput = null;
  this.mSlideDialogSubmit = null;
  this.mSlideDialogCancel = null;

  this.mNextSlideButton = null;
  this.mPreviousSlideButton = null;

  this.mConfirmationKeyListener = null;

  this.mDimmer = null;

  // flags
  this.mAnimate = true;
  this.mSkipConfirmationOnRestore = false;
  this.mAddControlsOnRestore = true;
  this.mShowClonePresentation = false;

  // js objects
  this.mManager;

  // passed functions
  this.mFunctionRequestCreateSlide = null;
  this.mFunctionRequestEditSlide = null;

  this.mClonePresentationUrl = null;

  this.DIMMER_OPACITY = .6;

  this.CLASSNAME = 'presentationControls';
  this.SAVE_BUTTON_CLASSNAME = 'saveButton';
  this.EDIT_BUTTON_CLASSNAME = 'editButton';
  this.CREATE_BUTTON_CLASSNAME = 'createButton';
  this.SLIDE_DIALOG_CONTAINER_CLASSNAME = 'slideDialogContainer';
  this.DIMMER_CLASSNAME = 'dimmerContainer';
  this.NEXT_BUTTON_CLASSNAME = 'nextButton';
  this.PREVIOUS_BUTTON_CLASSNAME = 'previousButton';
  this.CLONE_BUTTON_CLASSNAME = 'cloneButton';
  this.FIRST_SLIDE_BUTTON_CLASSNAME = 'jumpToFirstSlide';
  this.LAST_SLIDE_BUTTON_CLASSNAME = 'jumpToLastSlide';


  this.SAVE_BUTTON_INSTRUCTIONS = 'Click to save the current slide.';
  this.EDIT_BUTTON_INSTRUCTIONS = 'Click to edit the slide description.';
  this.CREATE_BUTTON_INSTRUCTIONS = 'Click to create a new slide for this presentation.';
  this.NEXT_BUTTON_INSTRUCTIONS = 'Click to view the next slide.';
  this.PREVIOUS_BUTTON_INSTRUCTIONS = 'Click to view the previous slide.';

  this.CREATE_DIALOG_INSTRCUTIONS = 'Fill in the description field below then click the create button or click cancel to abort.';
  this.EDIT_DIALOG_INSTRCUTIONS = 'Edit the description field below then click the save button or click cancel to leave the slide unedited.';
  this.CLONE_PRESENTATION_INSTRUCTIONS = 'Click to copy this presentation.';
  this.JUMP_FIRST_SLIDE_INSTRUCTIONS = 'Click to jump to the first slide';
  this.JUMP_LAST_SLIDE_INSTRUCTIONS = 'Click to jump to the last slide';

  this.SAVE_CURRENT_SLIDE = 'Save Slide';
  this.CREATE_SLIDE = 'New Slide';
  this.EDIT_SLIDE = 'Slide desc';

  this.CREATE_BUTTON = 'Create';
  this.EDIT_BUTTON = 'Save';
  this.CANCEL_BUTTON = 'Cancel';

  this.init = function()
  {
    this.mPanel = $( document.createElement( 'div' ) );
    this.mSaveCurrentSlideButton = $( document.createElement( 'a' ) );
    this.mEditCurrentSlide = $( document.createElement( 'a' ) );
    this.mCreateSlide = $( document.createElement( 'a' ) );
    this.mNextSlideButton = $( document.createElement( 'a' ) );
    this.mPreviousSlideButton = $( document.createElement( 'a' ) );

    this.mDimmer = $( document.createElement( 'div' ) );
    this.mSlideDialogContainer = $( document.createElement( 'div' ) );
    this.mSlideDialogInstructions = $( document.createElement( 'p' ) );
    this.mDescriptionInput = $( document.createElement( 'textarea' ) );
    this.mSlideDialogSubmit = $( document.createElement( 'a' ) );
    this.mSlideDialogCancel = $( document.createElement( 'a' ) );
    this.mClonePresentationButton = $( document.createElement( 'a' ) );
    this.mJumpToFirstSlideButton = $( document.createElement( 'a' ) );
    this.mJumpToLastSlideButton = $( document.createElement( 'a' ) );

    this.mSlideDialogContainer.appendChild( this.mSlideDialogInstructions );
    this.mSlideDialogContainer.appendChild( this.mDescriptionInput );
    this.mSlideDialogContainer.appendChild( this.mSlideDialogSubmit );
    this.mSlideDialogContainer.appendChild( this.mSlideDialogCancel );

    this.mPanel.appendChild( this.mJumpToFirstSlideButton );
    this.mPanel.appendChild( this.mPreviousSlideButton );
    this.mPanel.appendChild( this.mSaveCurrentSlideButton );
    this.mPanel.appendChild( this.mEditCurrentSlide );
    this.mPanel.appendChild( this.mCreateSlide );
    this.mPanel.appendChild( this.mClonePresentationButton );
    this.mPanel.appendChild( this.mNextSlideButton );
    this.mPanel.appendChild( this.mJumpToLastSlideButton );
    this.mContainer.appendChild( this.mSlideDialogContainer );
    this.mContainer.appendChild( this.mDimmer );

    this.mSlideDialogSubmit.href = 'javascript: var button';
    this.mSlideDialogSubmit.title = '';

    this.mSlideDialogCancel.href = 'javascript: var button';
    this.mSlideDialogCancel.title = '';

    this.mSaveCurrentSlideButton.update( this.SAVE_CURRENT_SLIDE );
    this.mSaveCurrentSlideButton.href = 'javascript: var saveButton';
    this.mSaveCurrentSlideButton.title = this.SAVE_BUTTON_INSTRUCTIONS;

    this.mEditCurrentSlide.update( this.EDIT_SLIDE );
    this.mEditCurrentSlide.href = 'javascript: var editButton';
    this.mEditCurrentSlide.title = this.EDIT_BUTTON_INSTRUCTIONS;

    this.mCreateSlide.update( this.CREATE_SLIDE );
    this.mCreateSlide.href = 'javascript: var createButton';
    this.mCreateSlide.title = this.CREATE_BUTTON_INSTRUCTIONS;

    this.mNextSlideButton.update( '&nbsp;' );
    this.mNextSlideButton.href = 'javascript: var button';
    this.mNextSlideButton.title = this.NEXT_BUTTON_INSTRUCTIONS;

    this.mPreviousSlideButton.update( '&nbsp;' );
    this.mPreviousSlideButton.href = 'javascript: var button';
    this.mPreviousSlideButton.title = this.PREVIOUS_BUTTON_INSTRUCTIONS;

    this.mClonePresentationButton.update( '&nbsp;' );
    this.mClonePresentationButton.href = 'javascript: var button';
    this.mClonePresentationButton.title = this.CLONE_PRESENTATION_INSTRUCTIONS;

    this.mJumpToFirstSlideButton.update( '&nbsp;' );
    this.mJumpToFirstSlideButton.href = 'javascript: var button';
    this.mJumpToFirstSlideButton.title = this.JUMP_FIRST_SLIDE_INSTRUCTIONS;

    this.mJumpToLastSlideButton.update( '&nbsp;' );
    this.mJumpToLastSlideButton.href = 'javascript: var button';
    this.mJumpToLastSlideButton.title = this.JUMP_LAST_SLIDE_INSTRUCTIONS;

    this.mPanel.addClassName( this.CLASSNAME );
    this.mSaveCurrentSlideButton.addClassName( this.SAVE_BUTTON_CLASSNAME );
    this.mEditCurrentSlide.addClassName( this.EDIT_BUTTON_CLASSNAME );
    this.mCreateSlide.addClassName( this.CREATE_BUTTON_CLASSNAME );
    this.mSlideDialogContainer.addClassName( this.SLIDE_DIALOG_CONTAINER_CLASSNAME );
    this.mDimmer.addClassName( this.DIMMER_CLASSNAME );
    this.mNextSlideButton.addClassName( this.NEXT_BUTTON_CLASSNAME );
    this.mPreviousSlideButton.addClassName( this.PREVIOUS_BUTTON_CLASSNAME );
    this.mClonePresentationButton.addClassName( this.CLONE_BUTTON_CLASSNAME );
    this.mJumpToFirstSlideButton.addClassName( this.FIRST_SLIDE_BUTTON_CLASSNAME );
    this.mJumpToLastSlideButton.addClassName( this.LAST_SLIDE_BUTTON_CLASSNAME );

    this.mContainer.appendChild( this.mPanel );

    // events
    var qsc = this;
    this.mSaveCurrentSlideButton.onclick = function(){ qsc.mManager.saveCurrentSlide(); };
    this.mCreateSlide.onclick = function(){ qsc.mCreateSlide.blur(); qsc.showCreateSlideDialog() };
    this.mEditCurrentSlide.onclick = function(){ qsc.mEditCurrentSlide.blur(); qsc.showEditSlideDialog() };
    this.mNextSlideButton.onclick = function(){ qsc.mNextSlideButton.blur(); qsc.mManager.restoreWorkspaceByIndex( qsc.mManager.getCurrentSlideIndex() + 1, qsc.mSkipConfirmationOnRestore, qsc.mAddControlsOnRestore );  qsc.update(); };
    this.mPreviousSlideButton.onclick = function(){ qsc.mPreviousSlideButton.blur(); qsc.mManager.restoreWorkspaceByIndex( qsc.mManager.getCurrentSlideIndex() - 1, qsc.mSkipConfirmationOnRestore, qsc.mAddControlsOnRestore ); qsc.update(); };
    this.mClonePresentationButton.onclick = function(){ if( qsc.mClonePresentationUrl != null ){ document.location.href = qsc.mClonePresentationUrl + '/' + qsc.mManager.getCurrentPresentation().id } };
    this.mJumpToFirstSlideButton.onclick = function(){ qsc.mManager.restoreWorkspaceByIndex( 0, qsc.mSkipConfirmationOnRestore, qsc.mAddControlsOnRestore );  qsc.update(); };
    this.mJumpToLastSlideButton.onclick = function(){ qsc.mManager.restoreWorkspaceByIndex( qsc.mManager.getCurrentTotalNumberOfSlides() - 1, qsc.mSkipConfirmationOnRestore, qsc.mAddControlsOnRestore );  qsc.update(); };



    // esc and return character listeners
    new YAHOO.util.KeyListener(
    this.mDescriptionInput,
    { keys:27 }, // esc key
    { fn:function(){ qsc.hideSlideDialog(); }, correctScope:false },
    "keyup" ).enable();

    // stylings
    this.mPanel.style.position = 'absolute';
    this.moveTo( 5, 2 );
    this.mPanel.style.zIndex = 99999;

    jshSetOpacity( this.mSlideDialogContainer, 0 );
    this.mSlideDialogContainer.hide();
    this.mSlideDialogContainer.style.position = 'absolute';
    this.mSlideDialogContainer.style.left = 0 + 'px';
    this.mSlideDialogContainer.style.top = 0 + 'px';
    this.mSlideDialogContainer.style.zIndex = 99999;

    jshSetOpacity( this.mDimmer, 0 );
    this.mDimmer.hide();
    this.mDimmer.style.position = 'absolute';
    this.mDimmer.style.left = 0 + 'px';
    this.mDimmer.style.top = 0 + 'px';
    this.mDimmer.style.zIndex = 99998;

    this.hide();
    this.update();
  };

  this.render = function( element )
  {
    if( element )
    {
      this.mContainer = element;
      this.init();
    }
  };

  this.update = function()
  {
    this.mSaveCurrentSlideButton.hide();
    this.mEditCurrentSlide.hide();
    this.mCreateSlide.hide();
    //this.mNextSlideButton.hide();
    this.mNextSlideButton.style.visibility = 'hidden';
    //this.mPreviousSlideButton.hide();
    this.mPreviousSlideButton.style.visibility = 'hidden';
    this.mClonePresentationButton.hide();
    //this.mJumpToFirstSlideButton.hide();
    this.mJumpToFirstSlideButton.style.visibility = 'hidden';
    //this.mJumpToLastSlideButton.hide();
    this.mJumpToLastSlideButton.style.visibility = 'hidden';

    if( this.mManager )
    {
      if( this.mShowClonePresentation == true )
      {
        this.mClonePresentationButton.show();
      }

      var cp = this.mManager.getCurrentPresentation();
      var cs = this.mManager.getCurrentSlide();

      if( cp && cs )
      {
        if( this.mFunctionRequestEditSlide )
        {
          this.mSaveCurrentSlideButton.show();
          this.mEditCurrentSlide.show();
        }
      }

      if( cp )
      {
        if( this.mFunctionRequestCreateSlide )
        {
          this.mCreateSlide.show();
        }

        var totalSlides = this.mManager.getCurrentTotalNumberOfSlides();
        var currentIndex = this.mManager.getCurrentSlideIndex();
        if( currentIndex > 0 )
        {
          //this.mJumpToFirstSlideButton.show();
          this.mJumpToFirstSlideButton.style.visibility = '';
          //this.mPreviousSlideButton.show();
          this.mPreviousSlideButton.style.visibility = '';
        }

        if( ( currentIndex < (totalSlides -1) ) )
        {
          //this.mJumpToLastSlideButton.show();
          this.mJumpToLastSlideButton.style.visibility = '';
          //this.mNextSlideButton.show();
          this.mNextSlideButton.style.visibility = '';
        }
      }
    }
  };
  this.hideSlideDialog = function()
  {
    if( this.mAnimate == true )
    {
      var qsc = this;
      var postFunction = function(){ qsc.mDimmer.hide(); qsc.mSlideDialogContainer.hide(); };
      ElementEffects.fadeElement( this.mDimmer, 0, 55, .2, null );
      ElementEffects.fadeElement( this.mSlideDialogContainer,  0, 55, .2, postFunction );
    }
    else
    {
      jshSetOpacity( this.mDimmer, 0 );
      jshSetOpacity( this.mSlideDialogContainer, 0 );
      this.mDimmer.hide();
      this.mSlideDialogContainer.hide();
    }
  };

  this.showCreateSlideDialog = function()
  {
    this.mSlideDialogInstructions.update( this.CREATE_DIALOG_INSTRCUTIONS );
    this.mDescriptionInput.value = '';
    this.mSlideDialogSubmit.update( this.CREATE_BUTTON );
    this.mSlideDialogCancel.update( this.CANCEL_BUTTON );

    // events
    var qsc = this;
    this.mSlideDialogCancel.onclick = function(){ qsc.mSlideDialogCancel.blur(); qsc.hideSlideDialog(); };
    this.mSlideDialogSubmit.onclick = function(){ qsc.mSlideDialogSubmit.blur(); qsc.requestCreateSlide( qsc.mDescriptionInput.value, qsc.mManager.getCurrentSlideIndex() ); };

    this.mConfirmationKeyListener = new YAHOO.util.KeyListener(
    this.mDescriptionInput,
    { keys:13 }, // return key
    { fn:function(e){ qsc.mConfirmationKeyListener.disable(); qsc.requestCreateSlide( qsc.mDescriptionInput.value, qsc.mManager.getCurrentSlideIndex()); }, correctScope:false },
    "keyup" );
    this.mConfirmationKeyListener.enable();

    this.showSlideDialog();
  };

  this.showEditSlideDialog = function()
  {
    var slideInfo = this.mManager.getCurrentSlide();
    this.mSlideDialogInstructions.update( this.EDIT_DIALOG_INSTRCUTIONS );
    this.mSlideDialogSubmit.update( this.EDIT_BUTTON );
    this.mSlideDialogCancel.update( this.CANCEL_BUTTON );

    this.mDescriptionInput.value = '';
    if( slideInfo && slideInfo.description )
    {
      this.mDescriptionInput.value = slideInfo.description;
    }

    // events
    var qsc = this;
    this.mSlideDialogCancel.onclick = function(){ qsc.mSlideDialogCancel.blur(); qsc.hideSlideDialog(); };
    this.mSlideDialogSubmit.onclick = function(){ qsc.mSlideDialogSubmit.blur(); qsc.requestEditCurrentSlide( qsc.mDescriptionInput.value ); };

    this.mConfirmationKeyListener = new YAHOO.util.KeyListener(
    this.mDescriptionInput,
    { keys:13 }, // return key
    { fn:function(e){ qsc.mConfirmationKeyListener.disable(); qsc.requestEditCurrentSlide( qsc.mDescriptionInput.value ); }, correctScope:false },
    "keyup" );
    this.mConfirmationKeyListener.enable();

    this.showSlideDialog();
  };

  this.showSlideDialog = function()
  {
    var screenSize = jshGetWindowSize();
    //var offset = Position.positionedOffset( this.mPanel );
    var offset = [0, 0];
    var centerOffset = [];
    centerOffset[0] = ( ( screenSize[0] / 2 ) - ( offset[0] + ( this.mSlideDialogContainer.getWidth() / 2 ) ) );
    centerOffset[1] = ( ( screenSize[1] / 2 ) - ( offset[1] + ( this.mSlideDialogContainer.getHeight() / 2 ) ) );
    centerOffset[1] /= 3;

    this.mSlideDialogContainer.style.left = Math.round( centerOffset[0] ) + 'px';
    this.mSlideDialogContainer.style.top = Math.round( centerOffset[1] ) + 'px';

    this.mDimmer.style.width = screenSize[0] + 'px';
    this.mDimmer.style.height = screenSize[1] + 'px';

    this.mDimmer.style.left = ( offset[0] * -1 ) + 'px';
    this.mDimmer.style.top = ( offset[1] * -1 ) + 'px';

    // show it
    this.mDimmer.show();
    this.mSlideDialogContainer.show();

    if( this.mAnimate == true )
    {
      var qsc = this;
      var postFunction = function(){ qsc.mDescriptionInput.focus(); };
      ElementEffects.fadeElement( this.mDimmer, this.DIMMER_OPACITY, 55, .2, null );
      ElementEffects.fadeElement( this.mSlideDialogContainer,  1, 55, .2, postFunction );
    }
  };

  this.requestCreateSlide = function( description, index )
  {
    this.hideSlideDialog();
    //debug( 'requestCreateSlide: [' + description  + ']' );
    if( this.mFunctionRequestCreateSlide )
    {
      var attributes = {};
      attributes['description'] = '';

      if( description )
      {
        attributes['description'] = description;
      }

      if( this.mManager )
       this.mManager.startLoadingState();

      this.mFunctionRequestCreateSlide( attributes, index );
    }
    else
    {
      if( this.mManager )
      {
        this.mManager.showMessageToUser( 'Unable to create slide, no current slide information available.' );
      }
    }
  };

  this.requestEditCurrentSlide = function( description )
  {
    this.hideSlideDialog();

    var slideInfo = this.mManager.getCurrentSlide();
    if( slideInfo && this.mFunctionRequestEditSlide )
    {
      var attributes = {};
      attributes['id'] = slideInfo.id;
      attributes['description'] = '';

      if( description )
      {
        slideInfo.description = description;
        attributes['description'] = description;
      }

      this.mManager.startLoadingState();

      this.mFunctionRequestEditSlide( attributes );
    }
    else
    {
      if( this.mManager )
      {
        this.mManager.showMessageToUser( 'Unable to edit slide, no current slide information available.' );
      }
    }
  };

  this.moveTo = function( x, y )
  {
    this.mPanel.style.right = x + 'px';
    this.mPanel.style.top = y + 'px';
  };

  this.show = function()
  {
    this.update();

    this.mPanel.show();
  };

  this.hide = function()
  {
    this.mPanel.hide();
  };

  this.setManager = function( manager )
  {
    this.mManager = manager;
  };

  this.setRequestCreateSlideFunction = function( functionRequestCreateSlide )
  {
    this.mFunctionRequestCreateSlide = functionRequestCreateSlide;
  };

  this.setRequestEditSlideFunction = function( requestEditSlide )
  {
    this.mFunctionRequestEditSlide = requestEditSlide;
  };

  this.setAnimate = function( animate )
  {
    this.mAnimate = animate;
  };
}