/****************************************************************************
 * 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
 *
 *  slidesPanel.js
 *
 *  Description:
 *    Source of Images to use within the workspace
 *
 *  Structure:
 *
 *
 *  Requires:
 *    prototype.js - http://www.prototypejs.org/
 *
 *  Development History:
 *    9-27-2007       - created
 *
 *******************************************************************************/


function SlidesPanel()
{
  // html elements
  this.mContainer = null;
  this.mPanel = null;
  this.mTab = null;
  this.mContent = null;
  this.mHeader = null;
  this.mSlidesHeader = null;
  this.mSlidesContainer = null;
  this.mPresentationPropertiesContainer = null;
  this.mClonePresentaitonButton = null;
  this.mPropertiesPresentationButton = null;
  //this.mKeynoteExportPresentaitonButton = null;
  this.mPptExportPresentaitonButton = null;
  this.mDeletePresentaitonButton = null;
  this.mPropertiesSlideInstructions = null;
  this.mPropertiesSlideDescription = null;
  this.mPresenationSlideShowButton = null;
  this.mPropertiesSubmitButton = null;
  this.mPresentationsList = null;
  this.mClosePresentationButton = null;
  this.mHelpButton = null;
  this.mCloseButton = null;
  this.mDragPlaceHolder = null;
  
  this.mLoadingContainer = null;
  
  // js elements
  this.mSlideInfos;
  this.mManager;
  
  // flags
  this.mHidden = null;
  this.mLoading = null;
  this.mAnimate = true;
  this.mDimensions = null;
  this.mClosedDimensions = null;
  this.mReorderTimeout = null;
  this.mReorderCount = 0;
  this.mPresentations = null;
  this.mCanExport = false;
  
  this.mSlideCompositeClassName = null;
  
  this.mKeynoteExportUrlPrefix = null;
  this.mPptExportUrlPrefix = null;
  this.mPropertiesUrlPrefix = null;
  this.mSlideShowUrlPrefix = null;
  
  // functions
  this.mFunctionRequestSlides = null;
  this.mFunctionRequestCreateSlide = null;
  this.mFunctionRequestEditSlide = null;
  this.mFunctionRequestCloneSlide = null;
  this.mFunctionRequestDeleteSlide = null;
  this.mFunctionRequestClonePresentation = null;
  this.mFunctionRequestDeletePresentation = null;
  this.mFunctionRequestSlideReorder = null;
  
  this.CLASSNAME = 'slidesPanel';
  this.TAB_CLASSNAME = 'tab';
  this.CONTENT_CLASSNAME = 'content';
  this.PROPERTIES_CLASSNAME = 'propertiesContainer';
  this.DROPZONE_CLASSNAME = 'dropzone';
  this.LOADING_CONTAINER_CLASSNAME = 'loadingContainer';
  this.CLOSE_BUTTON_CLASSNAME = 'closeButton';
  this.HELP_BUTTON_CLASSNAME = 'helpButton';
  this.CREATE_SLIDE_BUTTON_CLASSNAME = 'createSlideButton';
  this.PROPERTIES_BUTTON_CLASSNAME = 'propertiesButton';
  this.SAVE_PRESENTATION_CLASSNAME = 'savePresentation';
  this.OPERATIONS_CONTAINER = 'operationsContainer';
  this.SLIDES_CONTAINER_CLASSNAME = 'slidesContainer';
  this.PLACE_HOLDER = 'placeHolder';
  this.CLONE_BUTTON_CLASSNAME = 'cloneButton';
  this.DELETE_BUTTON_CLASSNAME = 'deleteButton';
  this.REORDER_LEFT_BUTTON_CLASSNAME = 'reorderLeftButton';
  this.REORDER_RIGHT_BUTTON_CLASSNAME = 'reorderRightButton';
  this.CURRENT_SLIDE_CLASSNAME = 'currentSlide';
  this.PRESENTATION_PROPERTIES_CONTAINER_CLASSNAME = 'presentaionPropertiesContainer';
  this.PRESENTATION_PROPERTIES_BUTTON_CLASSNAME = 'presentationPropertiesButton';
  this.SLIDE_CLASSNAME = 'slide';
  
  this.OPERATION_CLOSE_BUTTON = 'operationCloseButton';
  this.OPERATION_CLONE_BUTTON = 'operationCloneButton';
  this.OPERATION_DELETE_BUTTON = 'operationDeleteButton';
  this.OPERATION_EDIT_BUTTON = 'operationEditButton';
  //this.OPERATION_KEYNOTE_BUTTON = 'operationKeynoteButton';
  this.OPERATION_PP_BUTTON = 'operationPPButton';
  this.OPERATION_SLIDESHOW_BUTTON = 'operationSlideShowButton';  
  
  this.HEADER_TITLE = 'Presentations:';
  this.SLIDES_HEADER_TITLE = 'Slides:';
  this.INITIAL_PRESENTATION_OPTION = 'Select a presentation...';
  this.CONFIRM_CLOSE_PRESENTATION = 'Are you sure you want to close the presentation, any changes since your last save will be lost.';
  this.HELP_INSTRCUTIONS = 'Opens the help page.';
  this.CLOSE_INSTRCUTIONS = 'Close the panel.'
  this.SAVE_INSTRCUTIONS = 'Click to save the current slide.'
  this.PROPERTIES_INSTRCUTIONS = 'Click to edit the current slide\'s properties';
  this.CREATE_INSTRCUTIONS = 'Click to create a new slide';
  
  this.EDIT_SLIDE_INSTRUCTIONS = 'Slide Description';
  this.CREATE_SLIDE_INSTRUCTIONS = 'Fill in the fields below then click the create button.';  
  
  this.SAVE_BUTTON_TEXT = 'Save';
  this.CREATE_BUTTON_TEXT = 'Create';
  
  this.CLONE_BUTTON_TEXT = 'Copy';
  this.EDIT_BUTTON_TEXT = 'Edit';
  //this.KEYNOTE_EXPORT_BUTTON_TEXT = 'Export to Keynote';
  this.PPT_EXPORT_BUTTON_TEXT = 'Export to PowerPoint'
  this.CLOSE_BUTTON_TEXT = '&nbsp';
  this.DELETE_BUTTON_TEXT = 'Delete';
  this.SLIDE_SHOW_BUTTON_TEXT = 'Play Presentation';
  
  this.PRESENTATION_PROPERTIES_BUTTON = 'Operations';
  
  this.SLIDE_CLONE_BUTTON_INSTRCUTIONS = 'Click to copy slide';
  this.SLIDE_DELETE_BUTTON_INSTRCUTIONS = 'Click to delete slide';  
  this.SLIDE_REORDER_LEFT_BUTTON_INSTRCUTIONS = 'Click once or multiple times to move left';
  this.SLIDE_REORDER_RIGHT_BUTTON_INSTRCUTIONS = 'Click once or multiple times to move right';
  
  
  this.CLONE_BUTTON_INSTRCUTIONS = 'Copy the current presentation.';
  this.EDIT_BUTTON_INSTRCUTIONS = 'Edit presentation properties.';
  //this.KEYNOTE_EXPORT_BUTTON_INSTRCUTIONS = 'Export the current presentation for Keynote.';
  this.PPT_EXPORT_BUTTON_INSTRCUTIONS = 'Export the current presentation for PowerPoint.';
  this.CLOSE_BUTTON_INSTRCUTIONS = 'Close the current presentation without saving.';
  this.DELETE_BUTTON_INSTRCUTIONS = 'Delete the current presentation.';
  this.SLIDE_SHOW_BUTTON_INSTRCUTIONS = 'Playback this presentation.'
  
  this.PRESENTATION_PROPERTIES_INSTRUCTIONS = 'Show/hide Presentation Operations';
  
  this.PROPERTIES_CREATE_MESSAGE = 'Please open a presentation first.';
  this.PROPERTIES_EDIT_MESSAGE = 'Please open a slide first.';
  this.WAIT_MESSAGE = ' This will take some time, stay on this page as we create your PowerPoint export  ';
  
  this.CONFIRM_DELETE_SLIDE = 'Are you sure you want to delete the slide?  This operation can not be undone.';
  this.CONFIRM_DELETE_PRESENTATION = 'Are you sure you want to delete this presentation?  This operation can not be undone.';
  
  this.EXPORT_TARGET = 'Export';
  this.PROPERTIES_TARGET = 'Properties';
  this.SLIDE_SHOW_TARGET = 'SlideShow';
    
  this.TAB_HEIGHT = 22;
  this.TAB_WIDTH = 100;
  this.MAX_LOADING_TIME = 46000; // 46 sec, yes that long
  this.MIN_LOADING_TIME = 800;
  this.MAX_OPEN_DISTANCE = 9 + 25;
  this.MARGIN = 9;
  this.REORDER_TIMEOUT = 700;
  
  
  this.init = function()
  {
    this.mPanel = $( document.createElement( 'div' ) );
    this.mDragPlaceHolder = $(document.createElement( 'div' ));
    this.mTab = $( document.createElement( 'div' ) );
    this.mContent = $( document.createElement( 'div' ) );
    this.mHeader = $( document.createElement( 'h1' ) );
    this.mSlidesHeader = $( document.createElement( 'h2' ) );
    this.mSlidesContainer = $( document.createElement( 'div' ) );
    this.mPresentationPropertiesContainer = $( document.createElement( 'div' ) );
    this.mClonePresentaitonButton = $( document.createElement( 'a' ) );
    this.mPropertiesPresentationButton = $( document.createElement( 'a' ) );
    this.mExportPresentaitonButton = $( document.createElement( 'a' ) );
    //this.mKeynoteExportPresentaitonButton = $( document.createElement( 'a' ) );
    this.mPptExportPresentaitonButton = $( document.createElement( 'a' ) );
    this.mPresenationSlideShowButton = $( document.createElement( 'a' ) ); 
    this.mDeletePresentaitonButton = $( document.createElement( 'a' ) );
    this.mPropertiesSlideInstructions = $( document.createElement( 'p' ) );
    this.mPropertiesSlideDescription = $( document.createElement( 'textarea' ) );
    this.mPropertiesSubmitButton = $( document.createElement( 'a' ) );
    this.mPresentationsList = $( document.createElement( 'select' ) );
    this.mClosePresentationButton = $( document.createElement( 'a' ) );
    this.mHelpButton = $( document.createElement( 'a' ) );
    this.mCloseButton = $( document.createElement( 'a' ) );
    
    this.mLoadingContainer = $( document.createElement( 'div' ) );
    
    this.mContent.appendChild( this.mHeader );
    this.mContent.appendChild( this.mPresentationsList );
    this.mContent.appendChild( this.mHelpButton );
    this.mContent.appendChild( this.mCloseButton );
    this.mPresentationPropertiesContainer.appendChild( this.mClosePresentationButton );
    
    this.mPresentationPropertiesContainer.appendChild( this.mPropertiesPresentationButton );
    this.mPresentationPropertiesContainer.appendChild( this.mClonePresentaitonButton );
    this.mPresentationPropertiesContainer.appendChild( this.mDeletePresentaitonButton );
    
    this.mPresentationPropertiesContainer.appendChild( this.mPptExportPresentaitonButton );
    //this.mPresentationPropertiesContainer.appendChild( this.mKeynoteExportPresentaitonButton );
    this.mPresentationPropertiesContainer.appendChild( this.mPresenationSlideShowButton );
    
    this.mContent.appendChild( this.mPresentationPropertiesContainer );
    //this.mContent.appendChild( this.mSlidesHeader );
    this.mContent.appendChild( this.mSlidesContainer );
    this.mPanel.appendChild( this.mContent );
    this.mPanel.appendChild( this.mTab );
    this.mPanel.appendChild( this.mLoadingContainer );
    this.mContainer.appendChild( this.mPanel );
    
    this.mPanel.addClassName( this.CLASSNAME );
    this.mTab.addClassName( this.TAB_CLASSNAME );
    this.mContent.addClassName( this.CONTENT_CLASSNAME );
    this.mPresentationPropertiesContainer.addClassName( this.PRESENTATION_PROPERTIES_CONTAINER_CLASSNAME );
    this.mHelpButton.addClassName( this.HELP_BUTTON_CLASSNAME );
    this.mCloseButton.addClassName( this.CLOSE_BUTTON_CLASSNAME );
    this.mLoadingContainer.addClassName( this.LOADING_CONTAINER_CLASSNAME );
    this.mSlidesContainer.addClassName( this.SLIDES_CONTAINER_CLASSNAME );
    this.mDragPlaceHolder.addClassName( this.PLACE_HOLDER );
    
    this.mClosePresentationButton.addClassName( this.OPERATION_CLOSE_BUTTON );
    this.mClonePresentaitonButton.addClassName(this.OPERATION_CLONE_BUTTON );
    //this.mKeynoteExportPresentaitonButton.addClassName( this.OPERATION_KEYNOTE_BUTTON );
    this.mPptExportPresentaitonButton.addClassName( this.OPERATION_PP_BUTTON ); 
    this.mDeletePresentaitonButton.addClassName( this.OPERATION_DELETE_BUTTON );  
    this.mPropertiesPresentationButton.addClassName( this.OPERATION_EDIT_BUTTON );
    this.mPresenationSlideShowButton.addClassName( this.OPERATION_SLIDESHOW_BUTTON );
    
    this.mDragPlaceHolder.slidesPanel = this;
    
    // text
    this.mHeader.update( this.HEADER_TITLE );
    this.mSlidesHeader.update( this.SLIDES_HEADER_TITLE );
    this.mClosePresentationButton.href = 'javascript: var closePresentationButton;';
    this.mClosePresentationButton.title = this.CLOSE_PRESENTATION_INSTRCUTION;
    this.mHelpButton.href = 'javascript: var helpButton;';
    this.mHelpButton.title = this.HELP_INSTRCUTIONS;
    this.mCloseButton.href = 'javascript: var closeButton;';
    this.mCloseButton.title = this.CLOSE_INSTRCUTIONS;
    
    this.mClosePresentationButton.update( this.CLOSE_BUTTON_TEXT );
    this.mClonePresentaitonButton.update( this.CLONE_BUTTON_TEXT );
    this.mPropertiesPresentationButton.update( this.EDIT_BUTTON_TEXT );
    this.mPropertiesPresentationButton.target = this.PROPERTIES_TARGET;
    //this.mKeynoteExportPresentaitonButton.update( this.KEYNOTE_EXPORT_BUTTON_TEXT );
    //this.mKeynoteExportPresentaitonButton.target = this.EXPORT_TARGET;
    this.mPptExportPresentaitonButton.update( this.PPT_EXPORT_BUTTON_TEXT );
    //this.mPptExportPresentaitonButton.target = this.EXPORT_TARGET;
    this.mDeletePresentaitonButton.update( this.DELETE_BUTTON_TEXT );
    this.mPresenationSlideShowButton.update( this.SLIDE_SHOW_BUTTON_TEXT );
    this.mPresenationSlideShowButton.title = this.SLIDE_SHOW_BUTTON_INSTRCUTIONS;
    this.mPresenationSlideShowButton.target = this.SLIDE_SHOW_TARGET;
    
    this.mClosePresentationButton.title = this.CLOSE_BUTTON_INSTRCUTIONS;
    this.mClonePresentaitonButton.title = this.CLONE_BUTTON_INSTRCUTIONS;
    this.mPropertiesPresentationButton.title = this.EDIT_BUTTON_INSTRCUTIONS;
    //this.mKeynoteExportPresentaitonButton.title = this.KEYNOTE_EXPORT_BUTTON_INSTRCUTIONS;
    this.mPptExportPresentaitonButton.title = this.PPT_EXPORT_BUTTON_INSTRCUTIONS;
    this.mDeletePresentaitonButton.title = this.DELETE_BUTTON_INSTRCUTIONS;
    
    this.mClosePresentationButton.href = 'javascript: var button;';
    this.mClonePresentaitonButton.href = 'javascript: var button;';
    this.mPropertiesPresentationButton.href = 'javascript: var button;';
    this.mDeletePresentaitonButton.href = 'javascript: var button;';
    //this.mKeynoteExportPresentaitonButton.href = 'javascript: var button;';
    this.mPptExportPresentaitonButton.href = 'javascript: var button;';
    this.mPresenationSlideShowButton.href = 'javascript: var button;';
    
    // events
    var sp = this;
    this.mPresentationsList.onchange = 
      function()
      { 
        var presentation = sp.mManager.getCurrentPresentation();
        var newPresentation = sp.getCurrentlySelectedPresentation();
        if( newPresentation == null )
        {
          // must have selected the inital option or some invalid option
          // either just close the current presentation
          sp.closePresentation( true );
        }
        else if( ( ! presentation ) || ( presentation.id != newPresentation.id ) )
        {
          // close the currently open pres
          var requestSlides = true;
          if( presentation )
          {
            requestSlides = sp.closePresentation( true );
          }
          
          // request the new slides, if told to
          if( requestSlides == true )
          {
            sp.requestSlides( newPresentation.id ); 
          }
          else if( presentation && ( presentation.id > 0 ) )
          {
            // lets change back the presentation list
            jshSetOptionSelected( sp.mPresentationsList, presentation.id );
          }
        }
        
        // clear focus
        sp.mPresentationsList.blur();
      };
    this.mTab.onclick = function(){ sp.toggle(); };
    this.mClosePresentationButton.onclick = function(){ sp.mClosePresentationButton.blur(); sp.closePresentation(); };
    this.mHelpButton.onclick = function(){ sp.mHelpButton.blur(); if(sp.mManager){sp.mManager.showHelpPage();} };
    this.mCloseButton.onclick = function(){ sp.mCloseButton.blur(); sp.toggle(); };
    this.mClonePresentaitonButton.onclick = function(){ sp.requestClonePresentation( sp.getCurrentlySelectedPresentationId() ); };
    this.mDeletePresentaitonButton.onclick = function(){ sp.mDeletePresentaitonButton.blur(); sp.requestDeletePresentation( sp.getCurrentlySelectedPresentationId() ); };
    //this.mKeynoteExportPresentaitonButton.onclick = function(){ sp.mKeynoteExportPresentaitonButton.blur(); return true; };
    //this.mPptExportPresentaitonButton.onclick =  function(){ sp.mPptExportPresentaitonButton.blur(); return true; };
    
	this.mPropertiesPresentationButton.onclick = function(){ sp.mPropertiesPresentationButton.blur(); return true; };
    this.mPresenationSlideShowButton.onclick = function(){ sp.mPresenationSlideShowButton.blur(); return true; };
    
    // stylings
    this.mPanel.style.overflow = 'hidden';
    this.mPanel.style.position = 'absolute';
    this.mPanel.style.zIndex = 1;
    
    this.mDragPlaceHolder.style.backgroundColor = 'transparent';
    this.mDragPlaceHolder.style.display = 'block';
    this.mDragPlaceHolder.style.border = '0 solid white';
    
    this.mTab.style.position = 'absolute';
    this.mTab.style.left = 0 + 'px';
    this.mTab.style.bottom = 1 + 'px';
    jshSetOpacity( this.mTab, .60 );
    
    this.mContent.style.position = 'absolute';
    this.mContent.style.left = 0 + 'px';
    this.mContent.style.top = 0 + 'px';
    this.mContent.style.visibility = 'hidden';
    
    this.mPresentationPropertiesContainer.hide();
    
    this.mHelpButton.style.position = 'absolute';
    this.mHelpButton.style.right = 23 + 'px';
    this.mHelpButton.style.top = 3 + 'px';
    
    this.mCloseButton.style.position = 'absolute';
    this.mCloseButton.style.right = 5 + 'px';
    this.mCloseButton.style.top = 3 + 'px';;
    
    this.mSlidesContainer.style.overflowX = 'auto';
    this.mSlidesContainer.style.overflowY = 'hidden';
    this.mSlidesContainer.style.position = 'absolute';
    this.mSlidesContainer.style.left = 0 + 'px';
    this.mSlidesContainer.style.bottom = 0 + 'px';
    
    //this.mSlidesContainer.style.backgroundColor = 'red';
    
    this.mLoadingContainer.hide();
    this.mLoadingContainer.style.position = 'absolute';
    this.mLoadingContainer.style.left = 1 + 'px';
    this.mLoadingContainer.style.top = 1 + 'px';
    this.mLoadingContainer.style.width = 150 + 'px';
    this.mLoadingContainer.style.height = 500 + 'px';
    this.mLoadingContainer.style.zIndex = 99999;
    jshSetOpacity( this.mLoadingContainer, .75 );
    
    this.mHidden = true;
    this.reposition();
    this.moveTo( 9, -153 );
    this.updatePresentationOperations();
    
  };
  
  this.render = function( element )
  {
    if( element )
    {
      this.mContainer = element;
      this.init();
    }    
  };
  
  this.moveTo = function( x, y )
  {
    if( x && y )
    { 
      //var minLeft = ( this.mTab.getWidth() - this.mPanel.getWidth() );
      //var offset = Position.cumulativeOffset( this.mContainer );
      //x = Math.max( x, minLeft );
      //y = Math.max( y, offset[1] );
      
      if( this.mPanel != null )
      {
        this.mPanel.style.right = x + 'px';
        this.mPanel.style.top = y + 'px';
      }
    }    
  };
  
  this.reposition = function()
  {
    if( this.mContainer )
    {
      var screenSize = jshGetWindowSize();
      var height = this.mPresentationsList.getHeight() + this.mTab.getHeight() + 10;
      if( this.mSlidesContainer.childNodes && this.mSlidesContainer.childNodes[0] )
      {
        // make sure we are as tall as the slides plus room for the scroll bar
        height += this.mSlidesContainer.childNodes[0].getHeight() + 57;
      }
      else
      {
        // default height for slides when none present
        height += 152;
      }
      
      this.mDimensions = [( screenSize[0] - 20 ), height];
      this.resize( this.mDimensions[0], this.mDimensions[1] );
    }
  };
  
  this.resize = function( width, height )
  {
    if( width && height )
    {      
      // resize panel
      this.mPanel.style.width = width + 'px';
      this.mPanel.style.height = height + 'px';
      
      // update the tab's size
      this.mTab.style.width = ( this.TAB_WIDTH ) + 'px';
      this.mTab.style.height = ( this.TAB_HEIGHT ) + 'px';
      
      // update the content size
      var border = jshBorderOffset( this.mContent, [0,0] );
      this.mContent.style.width = ( width - 3 ) + 'px';
      this.mContent.style.height = ( height - border[1] - this.TAB_HEIGHT ) + 'px';
      
      // update prese container
      //this.mPresentationPropertiesContainer.style.width = ( width - 1 ) + 'px';
      if( ( this.mManager ) && ( this.mManager.getCurrentTotalNumberOfSlides() > 0 ) )
      {
        height -= 30; 
      }
      
      // update the slides container
      var offset = [0,0];
      var marginY = 9;
      this.mSlidesContainer.style.width = ( width - border[0] - 3 ) + 'px';
      this.mSlidesContainer.style.height = ( height - ( marginY + border[1] + offset[1] + this.mPresentationsList.getHeight() + this.mTab.getHeight() ) ) + 'px';
    }
  };
  
  this.open = function( forceNoAnimation )
  {
    this.mPanel.style.zIndex = 9888;
    var position = [ this.MARGIN, this.MAX_OPEN_DISTANCE ];
    this.mHidden = false;
    jshSetOpacity( this.mTab, 1 );
    this.mContent.style.visibility = 'visible';
    if( !(forceNoAnimation == true) && this.mAnimate == true )
    {
      var sp = this;
      var postFunction = function(){ sp.reposition(); };
      ElementEffects.animateElementPosition( this.mPanel, position, 55, .40, postFunction );
    }
    else
    {
      this.moveTo( position[0], position[1] );
      this.reposition();
    }
  };
  
  this.close = function( forceNoAnimation )
  {
  	var mPanelHeight = 0;
  	if( this.mPanel != null )
  		mPanelHeight = this.mPanel.getHeight();
  		
    var position = [this.MARGIN, ( this.TAB_HEIGHT + 27 - mPanelHeight )];
    this.mHidden = true;
    
    if( !(forceNoAnimation == true) && this.mAnimate == true )
    {
      var sp = this;
      var postFunction = function(){ jshSetOpacity( sp.mTab, .60 ); sp.mContent.style.visibility = 'hidden'; if( sp.mPanel != null ){ sp.mPanel.style.zIndex = 1; }  };
      ElementEffects.animateElementPosition( this.mPanel, position, 55, .45, postFunction );
    }
    else
    {
  	  /*if( this.mPanel != null )
        this.mPanel.style.zIndex = 1;*/
        
      if( this.mContent != null )
        this.mContent.style.visibility = 'hidden';
        
      this.moveTo( position[0], position[1] );
      jshSetOpacity( this.mTab, .60 );
    }
  };
  
  /**
   * Hides the panel so that it can not be seen
   */
  this.hide = function()
  {
    this.mPanel.hide();
  };
  
  /**
   * Shows the panel so that it can seen
   */
  this.show = function()
  {
    this.mPanel.show();
  };
  
  this.toggle = function()
  {
    if( this.mHidden == true )
    {
      this.open();
    }
    else
    {
      this.close();
    }
  };

  this.cloneSlide = function( sid )
  {
    if( sid > 0 )
    {
      if( this.mFunctionRequestCloneSlide )
      {
        this.startLoadingState();
        
        // request a clone
        this.mFunctionRequestCloneSlide( sid );
      }
      else
      {
        // bad slide id, message user if possible
        if( this.mManager )
        {
          this.mManager.showMessageToUser( 'Unable to copy slide, invalid id.' );
        }
      }
    }
  };
  
  this.deleteSlide = function( sid )
  {
    if( sid > 0 )
    {
      if( this.mFunctionRequestDeleteSlide )
      {
        // request a clone
        if( confirm( this.CONFIRM_DELETE_SLIDE ) )
        {
          var slide = null;
          if( this.mManager )
          {
            slide = this.mManager.getCurrentSlide();
            if( slide && ( sid == slide.id ) )
            {
              this.mManager.closeCurrentSlide();
            }
          }

          this.startLoadingState();
          
          // request delete
          this.mFunctionRequestDeleteSlide( sid );
        }
      }
      else
      {
        // bad slide id, message user if possible
        if( this.mManager )
        {
          this.mManager.showMessageToUser( 'Unable to delete slide, invalid id.' );
        }
      }
    }
  };
  
  this.requestClonePresentation = function( pid, forceClonePresentation )
  {
    if( pid > 0 )
    {
      if( this.mFunctionRequestClonePresentation )
      {
        if( ( forceClonePresentation != true ) &&
            ( this.mManager )  )
        {
          var presId = pid;
          var sp = this;
          this.mManager.addPostSaveOperation( function(){ sp.requestClonePresentation( presId, true ); } );
          this.mManager.saveCurrentSlide();
          return;  
        }
        
        this.startLoadingState();
        
        if( this.mManager )
        {
          this.mManager.closeCurrentPresentation( true );
        }
        
        // request a clone
        this.mFunctionRequestClonePresentation( pid );
      }
    }
    else
    {
      // bad slide id, message user if possible
      if( this.mManager )
      {
        this.mManager.showMessageToUser( 'Unable to copy presentation, invalid id.' );
      }
    }
  };
  
  this.requestDeletePresentation = function( pid )
  {
    if( pid && ( pid > 0 ) )
    {
      if( this.mFunctionRequestDeletePresentation )
      {
        // request a clone
        if( confirm( this.CONFIRM_DELETE_PRESENTATION ) )
        {
          if( this.mManager )
          {
            var presentation = this.getCurrentlySelectedPresentation()
            if( presentation && ( pid == presentation.id ) )
            {
              this.closePresentation( true, true );
            }
          }

          this.startLoadingState();
          
          // request delete
          this.mFunctionRequestDeletePresentation( pid );
        }
      }
      else
      {
        // bad slide id, message user if possible
        if( this.mManager )
        {
          this.mManager.showMessageToUser( 'Unable to delete presentation, invalid id.' );
        }
      }
    }
  };
  
  this.requestSlideReorder = function( sid, newIndexPosition )
  {
    if( this.mFunctionRequestSlideReorder ) 
    {
      this.startLoadingState();
        
      this.mFunctionRequestSlideReorder( sid, newIndexPosition );
    }
  };  
  
  this.setAnimate = function( animate )
  {
    this.mAnimate = animate;
  };
  
  this.setCanExport = function( canExport )
  {
    this.mCanExport = canExport;
  };
  
  this.setKeynoteExportUrlPrefix = function( keynoteExportUrlPrefix )
  {
    this.mKeynoteExportUrlPrefix = keynoteExportUrlPrefix;
  };
  
  this.setPptExportUrlPrefix = function( pptExportUrlPrefix )
  {
    this.mPptExportUrlPrefix = pptExportUrlPrefix;
  };
  
  this.setPresentationPropertiesUrlPrefix = function( propertiesUrlPrefix )
  {
    this.mPropertiesUrlPrefix = propertiesUrlPrefix;    
  };
  
  this.setSlideShowUrlPrefix = function( slideShowUrlPrefix )
  {
    this.mSlideShowUrlPrefix = slideShowUrlPrefix;
  };
  
  this.setRequestSlidesFunction = function( requestSlidesFunction )
  {
    this.mFunctionRequestSlides = requestSlidesFunction;
  };
  
  this.setRequestCreateSlideFunction = function( requestCreateSlide )
  {
    this.mFunctionRequestCreateSlide = requestCreateSlide;    
  };
  
  this.setRequestEditSlideFunction = function( requestEditSlide )
  {
    this.mFunctionRequestEditSlide = requestEditSlide;    
  };
  
  this.setRequestCloneSlideFunction = function( requestCloneSlide )
  {
    this.mFunctionRequestCloneSlide = requestCloneSlide;    
  };
  
  this.setRequestDeleteSlideFunction = function( requestDeleteSlide )
  {
    this.mFunctionRequestDeleteSlide = requestDeleteSlide;    
  };
  
  this.setRequestClonePresentationFunction = function( requestClonePresentation )
  {
    this.mFunctionRequestClonePresentation = requestClonePresentation;    
  };
  
  this.setRequestDeletePresentationFunction = function( requestDeletePresentation )
  {
    this.mFunctionRequestDeletePresentation = requestDeletePresentation;    
  };
  
  this.setFunctionRequestSlideReorder = function( functionRequestSlideReorder )
  {
    this.mFunctionRequestSlideReorder = functionRequestSlideReorder;
  }
  
  this.setManager = function( manager )
  {
    this.mManager = manager;
  };
  
  this.getCurrentlySelectedPresentation = function()
  {
    var toReturn = 
      this.mPresentationsList.options[ 
        this.mPresentationsList.selectedIndex ].presentationInfo;
    
    return toReturn;
  };
  
  this.getCurrentlySelectedPresentationId = function()
  {
    var pres = this.getCurrentlySelectedPresentation();
    if( pres )
    {
      return pres.id
    }
      
    return -1;    
  };
  
  this.setSelectedPresentation = function( presentationId, openSlideIndex )
  {
    if( presentationId && this.mPresentationsList )
    {
      for( var i = 0; i < this.mPresentationsList.options.length; i++ )
      {
        if( ( this.mPresentationsList.options[i].presentationInfo ) && 
            ( this.mPresentationsList.options[i].presentationInfo.id == presentationId ) )
        {
          this.mPresentationsList.options[i].selected = true;
          this.requestSlides( presentationId, openSlideIndex );
          break;
        }
      }
    }
  };
  
  this.getSlideIndex = function( sid )
  {
    if( this.mSlideInfos )
    {
      for( var i = 0; i < this.mSlideInfos.length; i++ )
      {
        if( sid == this.mSlideInfos[i].id )
        {
          return i;
        }
      }
    }
    
    return -1;
  }
  
  this.setPresentations = function ( presentations )
  {
    if( presentations )
    {
      this.mPresentations = presentations;
      
      // clear old ones
      this.mPresentationsList.options.length = 0;
      
      // blank one
      this.mPresentationsList.options[ this.mPresentationsList.options.length ] = 
          new Option( this.INITIAL_PRESENTATION_OPTION, '' );
      
      // add new ones
      var theOption;
      for( var i = 0; i < presentations.length; i++ )
      {    
        if( ! presentations[i].displayName )
          presentations[i].displayName = '';
        
        theOption = new Option( presentations[i].displayName.toString().unescapeHTML(), presentations[i].id );
        theOption.presentationInfo = presentations[i];
        this.mPresentationsList.options[ this.mPresentationsList.options.length ] = theOption;
      }
    }
    
    this.endLoadingState();
  };
  
  this.setSlides = function( slideInfos, openSlideIndex )
  {
    this.mSlideInfos = slideInfos;
    
    if( this.mPresentationsList )
    {
      this.mManager.mInPresentation = true;
      this.mManager.setCurrentPresentation( 
        this.mPresentationsList.options[ 
          this.mPresentationsList.selectedIndex ].presentationInfo );
    }
    
    this.updatePresentationOperations();
    
    // end the loading    
    this.endLoadingState();
    
    this.updateSlides( openSlideIndex );
  };
  
  this.updatePresentationOperations = function()
  {
    if( this.mContainer )
    {
      var cp = this.mManager.getCurrentPresentation();
      if( cp )
      {
        this.mPresentationPropertiesContainer.show();
        //this.mClosePresentationButton.show();
        this.mPropertiesPresentationButton.show();
        this.mClonePresentaitonButton.show();
        this.mDeletePresentaitonButton.show();
        this.mPresenationSlideShowButton.show();
        
        if( this.mCanExport == true )
        {
          //this.mKeynoteExportPresentaitonButton.show();
          this.mPptExportPresentaitonButton.show();
        }
        this.open();
      }
      else
      {
        this.mPresentationPropertiesContainer.hide();
        this.mClosePresentationButton.hide();
        this.mPropertiesPresentationButton.hide();
        this.mClonePresentaitonButton.hide();
        this.mDeletePresentaitonButton.hide();
        this.mPptExportPresentaitonButton.hide();
        //this.mKeynoteExportPresentaitonButton.hide();
        this.mPresenationSlideShowButton.hide();
      }
    }
  };
  
  this.updateSlides = function( openSlideIndex )
  {
    if( this.mContainer )
    {
      // clear any slides we have
      this.mSlidesContainer.update( '' );
      
      if( this.mSlideInfos )
      {
        var innerContainer = $( document.createElement( 'div' ) );
        this.mSlidesContainer.appendChild( innerContainer );
        
        // we have slides! load them up    
        var slideInfo;
        var container;
        var slide;
        var slideContainer;
        var slideNumber;
        var cloneButton;
        var deleteButton;
        var slideRightButton;
        var slideLeftButton;
        var sp = this;
        for( var i = 0; i < this.mSlideInfos.length; i++ )
        {
          slideInfo = this.mSlideInfos[ i ];
          if( slideInfo )
          {
            var theOption = this.mPresentationsList.options[ this.mPresentationsList.selectedIndex ];
            
            container = $( document.createElement( 'div' ) );
            container.addClassName( this.SLIDE_CLASSNAME );
            innerContainer.appendChild( container );
            
            //slides have numbers
            slideNumber = $( document.createElement( 'span' ) );
            slideNumber.update( ( i+1 ) + '.' );
            container.appendChild( slideNumber );
            slideNumber.title = ( i+1 );
            
            if( slideInfo.description )
            {
              slideNumber.title += '. ' + slideInfo.description;
            }
            
            
            slideContainer = $( document.createElement( 'div' ) );
            container.appendChild( slideContainer );
            
            // create a new image with values from the info       
            slide = new SlideComposite();
            slide.setAspectRatio( [ theOption.presentationInfo.width, theOption.presentationInfo.height ] );
            slide.render( slideContainer );
            slide.setSlideInfo( slideInfo );
            this.mSlideCompositeClassName = slide.CLASSNAME;           
            
            slide.mSlide.presentationInfo = theOption.presentationInfo;
            
            slide.slidesPanel = this;
            slide.mSlide.slidesPanel = this;
            slide.mSlide.sid = slideInfo.id;          
             
            // events
            slide.mGlassPlate.onmousedown = Static_SlidesPanel_StartDrag;
            slide.mGlassPlate.onmousemove = Static_SourcePanel_Drag;
            slide.mGlassPlate.onmouseup = Static_SlidesPanel_EndDrag;
            slide.mGlassPlate.onclick  = Static_SlidesPanel_RestoreSlide;
            
            slide.mGlassPlate.style.cursor = 'move';
            
            slideContainer.style.textAlign = 'left';
            slideContainer.style.width = ( slide.mSlide.getWidth() + 5 ) + 'px';
            slideContainer.style.height = ( slide.mSlide.getHeight() + 5 ) + 'px';
            container.style.width = slide.mSlide.getWidth() + 'px';
            container.style.height = ( slideNumber.getHeight() + slide.mSlide.getHeight() + 23 ) + 'px';
            
            // reorder left
            if( i != 0 )
            {
              slideLeftButton = $( document.createElement( 'a' ) );
              container.appendChild( slideLeftButton );
              slideLeftButton.addClassName( this.REORDER_LEFT_BUTTON_CLASSNAME );
              slideLeftButton.update( '&nbsp;' );
              slideLeftButton.href = 'javascript: var button';
              slideLeftButton.title = this.SLIDE_REORDER_LEFT_BUTTON_INSTRCUTIONS;
              
              slideLeftButton.sid = slideInfo.id;
              slideLeftButton.slidesPanel = this;
              slideLeftButton.onclick = Static_SlidesPanel_ReorderLeft;
              slideLeftButton.ondblclick = Static_SlidesPanel_ReorderLeft;
            }
            
            // clone button
            cloneButton = $( document.createElement( 'a' ) );
            container.appendChild( cloneButton );
            cloneButton.addClassName( this.CLONE_BUTTON_CLASSNAME );
            cloneButton.update( '&nbsp;' );
            cloneButton.href = 'javascript: var cloneButton';
            cloneButton.title = this.SLIDE_CLONE_BUTTON_INSTRCUTIONS + ' ' + slideNumber.title + '.';
  
            cloneButton.sid = slideInfo.id;
            cloneButton.slidesPanel = this;
            cloneButton.onclick = Static_SlidesPanel_CloneSlide;
            
            // delete button
            deleteButton = $( document.createElement( 'a' ) );
            container.appendChild( deleteButton );
            deleteButton.addClassName( this.DELETE_BUTTON_CLASSNAME );
            deleteButton.update( '&nbsp;' );
            deleteButton.href = 'javascript: var cloneButton';
            deleteButton.title = this.SLIDE_DELETE_BUTTON_INSTRCUTIONS + ' ' + slideNumber.title + '.';
            
            deleteButton.sid = slideInfo.id;
            deleteButton.slidesPanel = this;
            deleteButton.onclick = Static_SlidesPanel_DeleteSlide;
            
            if( i < (this.mSlideInfos.length - 1) )
            {
              slideRightButton = $( document.createElement( 'a' ) );
              container.appendChild( slideRightButton );
              slideRightButton.addClassName( this.REORDER_RIGHT_BUTTON_CLASSNAME );
              slideRightButton.update( '&nbsp;' );
              slideRightButton.href = 'javascript: var button';
              slideRightButton.title = this.SLIDE_REORDER_RIGHT_BUTTON_INSTRCUTIONS;
              
              slideRightButton.sid = slideInfo.id;
              slideRightButton.slidesPanel = this;
              slideRightButton.onclick = Static_SlidesPanel_ReorderRight;
              slideRightButton.ondblclick = Static_SlidesPanel_ReorderRight;
            }
          }
          
          // set current slide if any
          if( this.mManager && ( this.mManager.getCurrentSlideIndex() == i ) )
          {
            slide.mSlide.addClassName( this.CURRENT_SLIDE_CLASSNAME )
          }
        }
        
        if( slide )
        {
          var margin = jshMarginOffset( container, [10,10] );
          innerContainer.style.width = ( (container.getWidth() + margin[0] ) * this.mSlideInfos.length + 10 ) + 'px';
          innerContainer.style.height = ( container.getHeight() + margin[1] ) + 'px';
        }
        
        this.reposition();
        
        // be nice and open the slide specified, if any
        if( ( openSlideIndex >= 0 ) && ( this.mSlideInfos[openSlideIndex] ) )
        {
          this.restoreSlide( this.mSlideInfos[openSlideIndex], this.getCurrentlySelectedPresentation() );
        } else {
              var slideComposites = this.mSlidesContainer.getElementsBySelector( '.' + this.mSlideCompositeClassName );
	      if( slideComposites  && (this.mManager.getCurrentSlideIndex()>-1))
	        {
	          this.mSlidesContainer.scrollLeft=slideComposites[this.mManager.getCurrentSlideIndex()].offsetLeft;	          
	         }	          
        }        
      } 
    }
  };
  
  this.openSlideForCurrentPresentation = function( index )
  {
    var current = this.getCurrentlySelectedPresentation();
    if( current != null )
    {
      if( ( index < this.mSlideInfos.length ) && ( index >= 0 ) )
      {
        this.restoreSlide( this.mSlideInfos[ index ], current );
      }
    }
  }
  
  this.requestSlides = function( pid, openSlideIndex )
  {
    if( pid && ( pid != NaN ) && this.mFunctionRequestSlides )
    {
      this.startLoadingState();
      this.mFunctionRequestSlides( pid, openSlideIndex );
      
      // lets up date the export urls
      //this.mKeynoteExportPresentaitonButton.href = 
      //  jshAppendParameter( this.mKeynoteExportUrlPrefix, 'pid', pid, true );
        
      //this.mPptExportPresentaitonButton.href = 
      //  jshAppendParameter( this.mPptExportUrlPrefix, 'pid', pid, true );
      var sp = this;
	  var url = jshAppendParameter( this.mPptExportUrlPrefix, 'pid', pid, true );
	  var msg = this.WAIT_MESSAGE;
      this.mPptExportPresentaitonButton.onclick = 
	    function()
		{ 
		  window.setTimeout("showNotify('" + msg + "');", 1000);
		  window.setTimeout("window.location.href='" + url + "';", 0);
		  window.setTimeout("hideNotify();",10000);
		  return true; 
		};
      this.mPropertiesPresentationButton.href = 
        jshAppendParameter( this.mPropertiesUrlPrefix, 'pid', pid, true );
        
      // add pid
      this.mPresenationSlideShowButton.href = 
        jshAppendParameter( this.mSlideShowUrlPrefix, 'pid', pid, true );
        
      // add param to make it editable
      this.mPresenationSlideShowButton.href = 
        jshAppendParameter( this.mPresenationSlideShowButton.href, 'sipc', '1', true );
      
      // make sure the selected on is the right one
      var presentation = this.getCurrentlySelectedPresentation();
      if( !presentation || ( presentation.id != pid ) )
      {
        // lets close the current presentaiton\
        if( presentation )
        {
          this.closePresentation( true, true );
        }
        
        // lets change back the presentation list
        jshSetOptionSelected( this.mPresentationsList, pid );
      }
    }
  };
  
  this.restoreSlide = function( slideInfo, presentationInfo, skipConfirmationOnRestore, addControlsOnRestore )
  {
    // default to current presentation info
    if( ( ! presentationInfo ) && 
        ( this.mPresentationsList.options ) &&
        ( this.mPresentationsList.selectedIndex > 0 ) )
    {
      presentationInfo = this.mPresentationsList.options[ 
        this.mPresentationsList.selectedIndex ].presentationInfo
    }
    
    // restore if we have both slide info and presentation only
    if( slideInfo && presentationInfo )
    {
      this.mManager.restoreWorkspace( slideInfo, presentationInfo, skipConfirmationOnRestore, addControlsOnRestore );
    }
      
    // mark the current slide as selected
    if( this.mSlidesContainer )
    {
      var slideComposites = this.mSlidesContainer.getElementsBySelector( '.' + this.mSlideCompositeClassName );
      for( var i = 0; i < slideComposites.length; i++ )
      {
        if( slideComposites[i] && ( slideComposites[i].sid == slideInfo.id ) )
        {
          slideComposites[i].addClassName( this.CURRENT_SLIDE_CLASSNAME );
          this.mSlidesContainer.scrollLeft=slideComposites[i].offsetLeft;
        }
        else if( slideComposites[i] )
        {
          slideComposites[i].removeClassName( this.CURRENT_SLIDE_CLASSNAME );
        }
      }
    }
    
    // get our selves out of the way
    //this.close();
  }
  
  this.endCurrentAction = function( cancelAction )
  {
    var sourceElement = new Object();
    sourceElement.slidesPanel = this;
    
    if( this.mDragInProgress == true )
    {
      Static_SlidesPanel_EndDrag( jshConstructMockEvent( sourceElement ), cancelAction );
    }
  };
  
  /**
   * Returns true iff the manager was told to close presentation
   * 
   * @param dontResetIndex if true will skip the resetting fo the presentations list
   */
  this.closePresentation = function( dontResetIndex, skipConfirmation )
  {
    // skip reset if told to
    if( !( dontResetIndex == true ) )
    {
      this.mPresentationsList.selectedIndex = 0;
    }      
    
    // clear slides
    this.setSlides( null );
    
    // clear presentaiton buttons urls
    this.mPptExportPresentaitonButton.href = 'javascript: var button;'
    //this.mKeynoteExportPresentaitonButton.href = 'javascript: var button;'
    this.mPropertiesPresentationButton.href = 'javascript: var button;'
    
    // tell manager to close pres
    if( this.mManager )
    {
      this.mManager.closeCurrentPresentation();
      return true;
    }
    
    this.updatePresentationOperations();
    return false;
  };
  
  
  this.startLoadingState = function()
  {
    if( ( this.mHidden == false ) && ( this.mLoading != true ) )
    {
      this.mLoading = true;
      
      //clear any mim timeout currently set
      if( this.mMinimumTimeout )
      {
        clearTimeout( this.mMinimumTimeout );
      }
      
      // some attributes to properly show the loading screen
      this.mLoadingStartTime = new Date();
      this.mLoadingContainer.style.width = ( this.mContent.getWidth() - 1 ) + 'px';
      this.mLoadingContainer.style.height = ( this.mContent.getHeight() - 2 ) + 'px';
      this.mLoadingContainer.show();    
      
      
      // make sure loading state ends
      var sp = this;
      setTimeout( function(){ sp.endLoadingState( true ) }, this.MAX_LOADING_TIME ); 
    } 
  };
  
  /**
   * Ends the loading state
   */
  this.endLoadingState = function( forceClose )
  {
    // close if enough time has elapsed or if we are told to
    if( ( forceClose == true ) || 
        ( ( this.mLoadingStartTime - new Date() ) > this.MIN_LOADING_TIME ) )
    {
      // stop the loading process
      if( this.mLoading != null )
      {
        this.mLoading = null;
        this.mMinimumTimeout = null;
        this.mLoadingStartTime = null;
        this.mLoadingContainer.hide();
      }
    }
    else
    {
      // make sure we dont just flash, give us a few before remving our selves
      var timeout = this.MIN_LOADING_TIME - ( this.mLoadingStartTime - new Date() );
      timeout = Math.min( timeout, this.MIN_LOADING_TIME );    
      
      // call our selves in a bit
      var sp = this;
      this.mMinimumTimeout = setTimeout( function(){ sp.endLoadingState( true ) }, timeout ); 
    }
  };
 
}

function getScrollTop() {
  if ( document.documentElement.scrollTop )
    return document.documentElement.scrollTop;

  return document.body.scrollTop;
}

function scrollHandler() {
   var e = document.getElementById('waitMessageContainer');
   e.style.top = getScrollTop();
}

function showNotify( str ) { 
  var elem = document.getElementById('waitMessageContainer');
  elem.style.display = 'block'; 
  elem.style.visibility = 'visible';

  if ( elem.currentStyle && 
       elem.currentStyle.position == 'absolute' ) 
  {
    elem.style.top = getScrollTop();
    window.onscroll = scrollHandler;
  }

  elem.innerHTML = str;
}

function hideNotify() {
  var elem = document.getElementById('waitMessageContainer');
  elem.style.display = 'none';
  elem.style.visibility = 'hidden';
  window.onscroll = null;
}

/**
 * Marks the state of a panel move operation
 */
function Static_SlidesPanel_StartDrag( e )
{
  //debug( 'Static_SlidesPanel_StartDrag' );
  e = jshGetEvent( e );
  var sourceElement = jshGetSourceElement( e );
  
  // this is mostly for the slides.  
  // if the source element doesnt have a reference to the source images
  // then parent becomes the source
  if( sourceElement && ( ! sourceElement.slidesPanel ) )
  {
    sourceElement = sourceElement.up();
  }
  
  if( e && sourceElement && sourceElement.slidesPanel )
  {    
    var slidesPanel = sourceElement.slidesPanel;
    
    if( slidesPanel.mDragInProgress != true )
    {
      slidesPanel.mDragInProgress = true;      
      slidesPanel.mOgSource = sourceElement;
      slidesPanel.mDragObject = sourceElement.cloneNode( true );
      slidesPanel.mDragObject.slidesPanel = slidesPanel;
      slidesPanel.mDragPlaceHolder.slideInfo = sourceElement.slideInfo;
      slidesPanel.mContainer.appendChild( slidesPanel.mDragObject );
      slidesPanel.mDragObject.hide();
      
      // add events
      slidesPanel.mContainer.slidesPanel = slidesPanel;
      slidesPanel.mContainer.onmousemove = Static_SourcePanel_Drag;
      slidesPanel.mContainer.onmouseup = Static_SlidesPanel_EndDrag;
    }
  }
}


/**
 * The actual work of a Dragging
 */
function Static_SourcePanel_Drag( e )
{
  //debug( 'Static_SourcePanel_Drag' );
  e = jshGetEvent( e );
  var sourceElement = jshGetSourceElement( e );
  
  // this is mostly for the slides.  
  // if the source element doesnt have a reference to the source images
  // then parent becomes the source
  if( sourceElement && ( ! sourceElement.slidesPanel ) )
  {
    sourceElement = sourceElement.up();
  }
  
  if( e && sourceElement && sourceElement.slidesPanel )
  {
    // move the panel if we are in move mode
    var slidesPanel = sourceElement.slidesPanel;
    if( slidesPanel.mDragInProgress == true )
    {
      slidesPanel.mDragObject.show();
      slidesPanel.mOgSource.style.visibility = 'hidden';
      slidesPanel.mDragObject.style.position = 'absolute';
      slidesPanel.mDragObject.style.zIndex = 9999;
      
      var x = Event.pointerX( e );
      var y = Event.pointerY( e );
      
      // are we over the target?
      if( ( Position.within( slidesPanel.mContainer, x, y ) ) && 
          ( ! Position.within( slidesPanel.mPanel, x, y ) ) )
      {
        // delete any exising placeholder caused by a reorder
        if( slidesPanel.mDragPlaceHolder.parentNode == slidesPanel.mSlidesContainer )
          slidesPanel.mSlidesContainer.removeChild( slidesPanel.mDragPlaceHolder );
        
        if( ! slidesPanel.mContainer.hasClassName( slidesPanel.DROPZONE_CLASSNAME ) )
          slidesPanel.mContainer.addClassName( slidesPanel.DROPZONE_CLASSNAME );
      }
      else
      {       
        // not in drop zone so remove classname
        slidesPanel.mContainer.removeClassName( slidesPanel.DROPZONE_CLASSNAME );
      }
      
      // update position
      slidesPanel.mDragObject.style.left = x - ( slidesPanel.mDragObject.getWidth() / 2 ) + 'px';
      slidesPanel.mDragObject.style.top = y - ( slidesPanel.mDragObject.getHeight() / 2 ) + 'px';
    }
  }
}



/**
 * Marks the end of a panel move operation
 */
function Static_SlidesPanel_EndDrag( e, cancelAction )
{
  //debug( 'Static_SlidesPanel_EndDrag: ' + cancelAction);
  
  e = jshGetEvent( e );
  var sourceElement = jshGetSourceElement( e );
  
  //debug( 'sourceElement: ' + sourceElement );
  
  // this is mostly for the slides.  
  // if the source element doesnt have a reference to the source images
  // then parent becomes the source
  if( sourceElement && ( ! sourceElement.slidesPanel ) )
  {
    sourceElement = sourceElement.up();
  }
  
  //debug( 'ending move cancelAction:' + cancelAction );
  if( e && sourceElement && sourceElement.slidesPanel )
  {
    var slidesPanel = sourceElement.slidesPanel;
    
    if( slidesPanel.mDragInProgress == true )
    { 
      // reset variables
      slidesPanel.mDragInProgress = null;
      slidesPanel.mDragStartingX = null;
      slidesPanel.mDragStartingY = null;
      
      // reset events
      slidesPanel.mContainer.slidesPanel = null;   
         
      slidesPanel.mContainer.onmousemove = null;
      slidesPanel.mContainer.onmouseup = null;
      
      // did we cancel
      if( ( cancelAction != true ) )
      {
        var x = Event.pointerX( e );
        var y = Event.pointerY( e );
        
        // are we putting it back
        if( ( ! Position.within( slidesPanel.mPanel, x, y ) ) &&
                Position.within( slidesPanel.mContainer, x, y ) )
        {
          // add the image to workspace
          slidesPanel.mOgSource.startingPosition = [ Event.pointerX( e ), Event.pointerY( e ) ];
          Static_SlidesPanel_RestoreSlide( jshConstructMockEvent( slidesPanel.mOgSource ), true );
        }
      }
      
      // clean up
      slidesPanel.mDragPlaceHolder.newIndex = null;
      slidesPanel.mDragPlaceHolder.slideInfo = null;
      if( slidesPanel.mDragPlaceHolder.parentNode == slidesPanel.mSlidesContainer )
        slidesPanel.mSlidesContainer.removeChild( slidesPanel.mDragPlaceHolder );
      
      slidesPanel.mContainer.removeClassName( slidesPanel.DROPZONE_CLASSNAME );
      slidesPanel.mContainer.removeChild( slidesPanel.mDragObject );
      slidesPanel.mDragObject = null;
      slidesPanel.mOgSource.style.display = '';
      slidesPanel.mOgSource.style.visibility = '';
    }
  }
}

function Static_SlidesPanel_RestoreSlide( e )
{
  e = jshGetEvent( e );
  var sourceElement = jshGetSourceElement( e );
  
  //debug( 'Static_SlidesPanel_RestoreSlide:slidesPanel: ' + slidesPanel );
  
  // this is mostly for the slides.  
  // if the source element doesnt have a reference to the source images
  // then parent becomes the source
  if( sourceElement && ( ! sourceElement.slidesPanel ) )
  {
    sourceElement = sourceElement.up();
  }
  
  if( e && sourceElement && sourceElement.slidesPanel )
  {
    var slidesPanel = sourceElement.slidesPanel;
    
    if( sourceElement.mediaInfo )
    {
      var x = null;
      var y = null;
      if( sourceElement.startingPosition )
      {
        x = sourceElement.startingPosition[0];
        y = sourceElement.startingPosition[1];
      }
      
      slidesPanel.mManager.createImagePanel( sourceElement.mediaInfo, x, y );
    }
    else if( sourceElement.slideInfo && sourceElement.presentationInfo )
    {
      slidesPanel.restoreSlide( sourceElement.slideInfo, sourceElement.presentationInfo );
    }
  }
}

function Static_SlidesPanel_CloneSlide( e )
{ 
  e = jshGetEvent( e );
  var sourceElement = jshGetSourceElement( e );
  
  if( sourceElement && sourceElement.sid && sourceElement.slidesPanel )
  {
    sourceElement.blur(); 
    sourceElement.slidesPanel.cloneSlide( sourceElement.sid ); 
  }
}

function Static_SlidesPanel_DeleteSlide( e )
{ 
  e = jshGetEvent( e );
  var sourceElement = jshGetSourceElement( e );
  
  if( sourceElement && sourceElement.sid && sourceElement.slidesPanel )
  {
    sourceElement.blur(); 
    sourceElement.slidesPanel.deleteSlide( sourceElement.sid ); 
  }
}

function Static_SlidesPanel_ReorderRight( e )
{ 
  e = jshGetEvent( e );
  var sourceElement = jshGetSourceElement( e );
  
  // brower specific fix.  
  // ie counts 2 consecutive clicks as one event while ff
  // see it as 2 event.  this causes a mis count for reordering.  
  // so 3 clicks resulted in 4 in ie while 3 in ff.
  if( jshIsIE6Or7() || ( e.type != 'dblclick' ) )
  {
    if( sourceElement && sourceElement.sid && sourceElement.slidesPanel )
    {
      Event.stop( e );
      
      sourceElement.blur();
      var sid = sourceElement.sid;
      var slidesPanel = sourceElement.slidesPanel;
      
      if( ( slidesPanel.mReorderCount ) || ( slidesPanel.mReorderCount == 0 ) )
       slidesPanel.mReorderCount++;
      else
        slidesPanel.mReorderCount = 1;
      
      if( slidesPanel.mReorderTimeout )
      {
        clearTimeout( slidesPanel.mReorderTimeout );
      }
      
      slidesPanel.mReorderTimeout = setTimeout( function(){ sourceElement.blur(); if( slidesPanel.mReorderCount != 0 ){ slidesPanel.requestSlideReorder( sid, slidesPanel.getSlideIndex( sid ) + slidesPanel.mReorderCount ); slidesPanel.mReorderCount = 0; } }, slidesPanel.REORDER_TIMEOUT ); 
    }
  }
}

function Static_SlidesPanel_ReorderLeft( e )
{ 
  e = jshGetEvent( e );
  var sourceElement = jshGetSourceElement( e );
  
  // brower specific fix.  
  // ie counts 2 consecutive clicks as one event while ff
  // see it as 2 event.  this causes a mis count for reordering.  
  // so 3 clicks resulted in 4 in ie while 3 in ff.
  if( jshIsIE6Or7() || ( e.type != 'dblclick' ) )
  {
    if( sourceElement && sourceElement.sid && sourceElement.slidesPanel )
    {
      Event.stop( e );
      
      sourceElement.blur();
      var sid = sourceElement.sid;
      var slidesPanel = sourceElement.slidesPanel;
      
      if( ( slidesPanel.mReorderCount ) || ( slidesPanel.mReorderCount == 0 ) )
       slidesPanel.mReorderCount--;
      else
        slidesPanel.mReorderCount = -1;
      
      if( slidesPanel.mReorderTimeout )
      {
        clearTimeout( slidesPanel.mReorderTimeout );
      }
      
      slidesPanel.mReorderTimeout = setTimeout( function(){ sourceElement.blur(); if( slidesPanel.mReorderCount != 0 ){ slidesPanel.requestSlideReorder( sid, slidesPanel.getSlideIndex( sid ) + slidesPanel.mReorderCount ); slidesPanel.mReorderCount = 0; } }, slidesPanel.REORDER_TIMEOUT );  
    }
  }
}