/****************************************************************************
 * 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
 *
 *  ShareThis.js
 *
 *  Description:
 *    ShareThis
 *
 *  Structure:
 *
 *
 *  Requires:
 *    prototype.js - http://www.prototypejs.org/
 *
 *  Development History:
 *    8-29-2007       - created
 *
 *******************************************************************************/

function ShareThis()
{
  this.mSource = null;
  this.mContainer = null;
  this.mUrlContainer = null;
  this.mCloseButton = null;
  
  this.mIsClosed = true;
  
  this.CLASSNAME = 'shareThis';  
  this.INSTRCUTION_TEXT = 'Copy and paste this link into an email or instant message';
  this.OPACITY = .9;
  
  this.init = function()
  {    
    this.mContainer = $( document.createElement( 'div' ) );
    this.mUrlContainer = $( document.createElement( 'input' ) );
    this.mCloseButton = $( document.createElement( 'a' ) );
        
    this.mContainer.addClassName( this.CLASSNAME );
    this.mContainer.hide();
    this.mUrlContainer.type = 'text';
    this.mCloseButton.href = 'javascript: var closeButton;'
    
    this.mContainer.appendChild( this.mUrlContainer );
    new Insertion.Top( this.mContainer, '<p>' + this.INSTRCUTION_TEXT + '</p>' );    
    this.mContainer.appendChild( this.mCloseButton );
    
    document.body.appendChild( this.mContainer );
    
    // events
    var st = this;
    YAHOO.util.Event.addListener( this.mSource, 'click', function(){ st.toggle(); } );
    this.mCloseButton.onclick = function(){ st.toggle(); };
    this.mUrlContainer.onfocus = function(){ Form.Element.select( st.mUrlContainer ) };
    
    
    // stylings
    //var windowSize = jshGetWindowSize();
    //var offset = Position.cumulativeOffset( this.mSource );
    this.mContainer.style.position = 'absolute';
    /*if( windowSize[0] > ( offset[0] + this.mContainer.getWidth() ) )
    {
      this.mContainer.style.left = ( offset[0] ) + 'px';  
    }
    else
    {
      this.mContainer.style.left = ( windowSize[0] - ( this.mContainer.getWidth() + 5 ) ) + 'px';
    }
    
    this.mContainer.style.top = Math.round( offset[1] + this.mSource.getHeight() ) + 'px';
    */
    this.mCloseButton.style.position = 'absolute';
    this.mCloseButton.style.right = 5 + 'px';
    this.mCloseButton.style.top = 5 + 'px';
    
    this.updatePosition();
  };
  
  this.updatePosition = function()
  {
    var windowSize = jshGetWindowSize();
    var offset = Position.cumulativeOffset( this.mSource );
    
    if( windowSize[0] > ( offset[0] + this.mContainer.getWidth() ) )
    {
      this.mContainer.style.left = ( offset[0] ) + 'px';  
    }
    else
    {
      this.mContainer.style.left = ( windowSize[0] - ( this.mContainer.getWidth() + 5 ) ) + 'px';
    }
    
    this.mContainer.style.top = Math.round( offset[1] + this.mSource.getHeight() ) + 'px';
    
  };
  
  this.show = function()
  {
    this.mIsClosed = false;
    this.updatePosition();
    this.mContainer.show();
    
    this.mSource.blur();
    this.mCloseButton.blur();
    this.mUrlContainer.focus();
  };
  
  this.hide = function()
  {
    this.mIsClosed = true;
    this.mContainer.hide();
    
    this.mSource.blur();
    this.mCloseButton.blur();
    //this.mUrlContainer.blur();
  }
  
  this.toggle = function()
  {
    if( this.mIsClosed == true )
    {
      this.show();
    }
    else
    {
      this.hide();
    }    
  }
  
  this.render = function( source, url )
  {
    if( source && url )
    {
      if( ! this.mSource )
      {
        this.mSource = source; 
        this.init();
      }
      
      this.setUrl( url );
    } 
  };
  
  this.setUrl = function( url )
  {
    this.mUrlContainer.value = url;
  };  
}