/****************************************************************************
 * 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
 *
 *  SlideComposite.js
 *
 *  Description:
 *    Composit of a Slide
 *
 *  Structure:
 *
 *
 *  Requires:
 *    prototype.js - http://www.prototypejs.org/
 *
 *  Development History:
 *    6-25-2007    cruiz  - created
 *
 *******************************************************************************/



function SlideComposite()
{
  // memeber variables
  this.mContainer;
  this.mSlide;
  this.mGlassPlate;

  this.mSlideInfo;

  // constants
  this.CLASSNAME = 'slideComposite';

  this.mAspectRatio = $([ 16, 9 ]);
  this.MAX_SIDE_SIZE = 200;



  // methods
  this.init = function()
  {
    this.mSlide = $( document.createElement( 'div' ) );
    this.mGlassPlate = $( document.createElement( 'div' ) );

    this.mSlide.addClassName( this.CLASSNAME );

    this.mContainer.appendChild( this.mSlide );
    this.mSlide.appendChild( this.mGlassPlate );

    this.mSlide.slideComposite = this;
    this.mGlassPlate.slideComposite = this;

    this.mSlide.style.position = 'absolute';
    this.mSlide.style.width = Math.round( this.MAX_SIDE_SIZE / this.mAspectRatio[0] ) + 'px';
    this.mSlide.style.height = Math.round( this.MAX_SIDE_SIZE / this.mAspectRatio[1] ) + 'px';
    this.mSlide.style.zIndex = 5;
    this.mSlide.style.overflow = 'hidden';


    var offset = [0,0];
    this.mGlassPlate.style.position = 'absolute';
    this.mGlassPlate.style.left = offset[0] + 'px';
    this.mGlassPlate.style.top = offset[1] + 'px';
    this.mGlassPlate.style.width = Math.round( this.MAX_SIDE_SIZE / this.mAspectRatio[0] ) + 'px';
    this.mGlassPlate.style.height = Math.round( this.MAX_SIDE_SIZE / this.mAspectRatio[1] ) + 'px';
    this.mGlassPlate.style.zIndex = 6;
    this.mGlassPlate.style.backgroundColor = 'orange';
    jshSetOpacity( this.mGlassPlate, .01 );
  };

  this.render = function( container )
  {
    if( container )
    {
      this.mContainer = container;
      this.init();
    }
  };

  this.setSlideInfo = function( slideInfo )
  {
    this.mSlideInfo = slideInfo;
    this.mSlide.slideInfo = this.mSlideInfo;

    this.updateComposite();
  };

  this.setAspectRatio = function( ratio )
  {
    if( ratio && ratio[0] && ratio[1] )
    {
      this.mAspectRatio[0] = ratio[0];
      this.mAspectRatio[1] = ratio[1];

      if( ( this.mAspectRatio[0] == 16 ) && (this.mAspectRatio[1] == 9  ) )
      {
        this.MAX_SIDE_SIZE = 200;
      }
      else if( ( this.mAspectRatio[0] == 16 ) && (this.mAspectRatio[1] == 10  ) )
      {
        this.MAX_SIDE_SIZE = 200;
      }
      else if( ( this.mAspectRatio[0] == 4 ) && (this.mAspectRatio[1] == 3  ) )
      {
        this.MAX_SIDE_SIZE = 175;
      }
      else
      {
        this.MAX_SIDE_SIZE = 175;
      }
    }
  };

  this.updateComposite = function()
  {
    this.mSlide.update( '' );
    this.mSlide.appendChild( this.mGlassPlate );
    if( this.mSlideInfo )
    {
      // resize slide based on slide info width/height ratio
      if( this.mSlideInfo.width && this.mSlideInfo.height )
        this.mAspectRatio = [ this.mSlideInfo.width, this.mSlideInfo.height ];

      var dimensions = jshCalculateProportionalDimensions( this.mAspectRatio, [this.MAX_SIDE_SIZE, this.MAX_SIDE_SIZE] );
      this.mSlide.style.width = dimensions[0] + 'px';
      this.mSlide.style.height = dimensions[1] + 'px';

      this.mGlassPlate.style.width = dimensions[0] + 'px';
      this.mGlassPlate.style.height = dimensions[1] + 'px';

      // set the title on the slide
      this.mSlide.title = this.mSlideInfo.description;

      if( this.mSlideInfo.slideImages && this.mSlideInfo.slideImages.length )
      {
        // construct the compost
        var slideImage;
        var mediaInfo;
        var derivedState;
        var imageContainer;
        var image;
        var maxStackOrder = 6;
        var offset = $([ 0, 0]);
        var position;
        for( var i = 0; i < this.mSlideInfo.slideImages.length; i++ )
        {
          slideImage = this.mSlideInfo.slideImages[i];
          mediaInfo = slideImage.media;
          slideImage.mediaInfo = mediaInfo;
          if( slideImage && mediaInfo )
          {
            derivedState = Static_ImagePanelManager_DeriveImagePanelState( slideImage, dimensions );

            imageContainer = $( document.createElement( 'div' ) );
            imageContainer.slideComposite = this;
            this.mSlide.appendChild( imageContainer );

            imageContainer.style.position = 'absolute';
            imageContainer.style.left = derivedState.positionX + 'px';
            imageContainer.style.top = derivedState.positionY + 'px';
            imageContainer.style.width = derivedState.panelWidth + 'px';
            imageContainer.style.height = derivedState.panelHeight + 'px';
            imageContainer.style.overflow = 'hidden';
            imageContainer.style.backgroundColor = '#1e1e1e';
            imageContainer.style.zIndex = slideImage.stackOrder + 1;

            image = $( document.createElement( 'img' ) );
            imageContainer.appendChild( image );
            image.derivedState = derivedState;

            //image.onload = function(){ alert(image.width + ', ' + image.height); image.width = image.width * derivedState.scaleFactor[0]; image.height = image.height * derivedState.scaleFactor[1] };
            //image.src = Static_ImagePanel_GetLunaImageUrl( mediaInfo.urlSource, 0, 0, size[0], size[1], derivedState.level );
            image.src = mediaInfo.urlSize2;

            image.width = ( derivedState.actualImageSizeWidth );
            image.height = ( derivedState.actualImageSizeHeight );

            image.style.position = 'relative';
            position = [];
            position[0] = -1 * Math.round( derivedState.centerPoint[0] - ( derivedState.panelWidth / 2 ) );
            position[1] = -1 * Math.round( derivedState.centerPoint[1] - ( derivedState.panelHeight / 2 ) );

            if( derivedState.panelWidth > derivedState.actualImageSizeWidth )
            {
              position[0] = Math.round( ( derivedState.panelWidth / 2 ) - ( derivedState.actualImageSizeWidth / 2  ) );
            }

            if( derivedState.panelHeight > derivedState.actualImageSizeHeight )
            {
              position[1] = Math.round( ( derivedState.panelHeight / 2 ) - ( derivedState.actualImageSizeHeight / 2  ) );
            }

            image.style.left = Math.round( position[0] ) + 'px';
            image.style.top =  Math.round( position[1] ) + 'px';

            maxStackOrder = Math.max( maxStackOrder, imageContainer.style.zIndex );
          }

          this.mGlassPlate.style.zIndex = maxStackOrder + 1;
        }
      }

      // make sure the glass plate is above everything.
      // this is to ensure that dragging is not effected by the slide images
      // stealing events
	  if( maxStackOrder == undefined )
	    maxStackOrder = 0;
      this.mGlassPlate.style.zIndex = maxStackOrder + 10000;
    }
  };
}

function Static_SlideComposite_CalculateDimensions( maxSideSize, ratio )
{
  var dimensions = $(new Array());
  if( maxSideSize && ratio )
  {
    if( ratio[0] > ratio[1] )
    {
      dimensions[0] = maxSideSize;
      dimensions[1] = Math.round( ratio[1] * ( dimensions[0] / ratio[0] ) );
    }
    else
    {
      dimensions[1] = maxSideSize;
      dimensions[0] = Math.round( ratio[0] * ( dimensions[1] / ratio[1] ) );
    }
  }

  return dimensions;
}

function Static_SlideComposite_TranslatePoint( point, dimensions, lunaLevel, baseProportion )
{
  var toReturn = $( new Array() );

  if( point && dimensions && lunaLevel && baseProportion )
  {
    var newDimensions = Static_ImagePanel_CalculateRatio( baseProportion, lunaLevel );

      /*debug(
       ' point: ',point,
       ' dimensions: ', dimensions,
       ' lunaLevel: ', lunaLevel,
       ' baseProportion: ', baseProportion,
       ' newDimensions: ', newDimensions
      );*/

    // translate x
    toReturn[0] = Math.round( dimensions[0] * ( point[0] / newDimensions[0] ) );

    // translate y
    toReturn[1] = Math.round( dimensions[1] * ( point[1] / newDimensions[1] ) );
  }

  //debug( 'toReturn: ', toReturn );

  return toReturn;
}







































