/*
 * banner.js
 * $Revision: 1.2 $ $Date: 2003/02/07 16:04:20 $
 */

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Netscape code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Doron Rosenberg <doron@netscape.com>
 *                 Bob Clary <bclary@netscape.com>
 *
 * ***** END LICENSE BLOCK ***** */

function banner(id, height, width, scrollAmount, scrollDelay, html,htmlw)
{
  
  this.scrollAmount  = scrollAmount ? scrollAmount : 6;
  this.scrollDelay   = scrollDelay ? scrollDelay : 160;
  this.name          = 'banner_' + (++banner._name);
  this.runId         = null;
  this.html          = html;
  this.htmlw		 = htmlw;
  this.containerId   =  id;
  this.id            =  id+"element";
  this.parentId      =  id+"parent";

  if (typeof(height) == 'number')
  {
    this.height = height;
    this.heightUnit = 'px';
  }
  else if (typeof(height) == 'string')
  {
    this.height = parseInt('0' + height, 10);
    this.heightUnit = height.toLowerCase().replace(/^[0-9]+/, '');
  }
  else
  {
    this.height = 100;
    this.heightUnit = 'px';
  }

  if (typeof(width) == 'number')
  {
    this.width = width;
    this.widthUnit = 'px';
  }
  else if (typeof(width) == 'string')
  {
    this.width = parseInt('0' + width, 10);
    this.widthUnit = width.toLowerCase().replace(/^[0-9]+/, '');
  }
  else
  {
    this.width = 100;
    this.widthUnit = 'px';
  }

  var markup = '';

  if (document.layers)
  {
    markup = '<ilayer id="' + this.parentId + '" name="' + this.id + '" ' +
             'height="' + height + '" ' +
             'width="' + width + '"  ' +
             'clip="' + width + ', ' + height + '" ' +
             '>' + 
             '<\/ilayer>';
  }
  else 
  {
        markup = '<div id="' + this.parentId + '" name="' + 
             this.parentId + '" ' +
             'style="position: relative; overflow: hidden; ' + 
             'height: ' + this.height + this.heightUnit + '; ' +
             'width: ' + this.width + this.widthUnit + '; ' +
             'clip: rect(0px, ' + this.width + this.widthUnit + ', ' + this.height + this.heightUnit + ', 0px); ' +
             '">' + 
             '<\/div>';
  }

  this.markup=markup;
  this.containerElement=document.getElementById(this.containerId);
  this.containerElement.innerHTML=markup;
  //alert(document.getElementById("banner").nodeType);
  //document.write(markup);
  //alert("marquee ctor");
  window[this.name] = this;

}

// Class Properties/Methods

banner._name = -1;

banner._getInnerSize = function(elm, propName)
{
  var val = 0;

  if (document.layers)
  {
    // navigator 4
    val = elm.document[propName];
  }
  else if (elm.style && typeof(elm.style[propName]) == 'number')
  {
    // opera
    // bug in Opera 6 width/offsetWidth. Use clientWidth
    if (propName == 'width' && typeof(elm.clientWidth) == 'number')
      val = elm.clientWidth;
    else
      val =  elm.style[propName];
  }
  else
  {
    //mozilla and IE
    switch (propName)
    {
    case 'height':
       if (typeof(elm.offsetHeight) == 'number')
         val =  elm.offsetHeight;
       break;
    case 'width':
       if (typeof(elm.offsetWidth) == 'number')
         val = elm.offsetWidth;
       break;
    }
  }

  return val;

};

banner.getElm = function(id)
{
  var elm = null;
  if (document.getElementById)
  {
    elm = document.getElementById(id);
  }
  else
  {
    elm = document.all[id];
  }
  return elm;
}
// Instance properties/methods

banner.prototype.start = function ()
{
  var markup = '';

  this.stop();

  if (!this.dirsign)
  {
    if (!document.layers)
    {
      //recupero l'elemento creato nel costruttore
	  this.containerDiv = banner.getElm(this.parentId);
	  //alert(document.body.innerHTML);
      if (typeof(this.containerDiv.innerHTML) != 'string')
      {
        return;
      }

      // adjust the container size before inner div is filled in
      // so IE will not hork the size of percentage units 
      var parentNode    = null;
      if (this.containerDiv.parentNode)
        parentNode = this.containerDiv.parentNode;
      else if (this.containerDiv.parentElement)
        parentNode = this.containerDiv.parentElement;

      if (parentNode && 
          typeof(parentNode.offsetHeight) == 'number' && 
          typeof(parentNode.offsetWidth) == 'number')
      {
        if (this.heightUnit == '%')
        {
          this.containerDiv.style.height = 
          parentNode.offsetHeight * (this.height/100) + 'px';
        }

        if (this.widthUnit == '%')
        {
          this.containerDiv.style.width = 
          parentNode.offsetWidth * (this.width/100) + 'px';
        }
      }

      markup += '<div id="' + this.id + '" name="' + this.id + '" ' +
        'style="position:relative; visibility: hidden;width:0px;"><nobr>' + this.html + '<\/nobr><\/div>';

	  //alert(markup);
	  //alert(this.containerDiv.id);
      this.containerDiv.innerHTML = markup;
      this.div                    = banner.getElm(this.id);
      this.styleObj     = this.div.style;

    }
    else /* if (document.layers) */
    {
      this.containerDiv = document.layers[this.parentId];
      markup = 
        '<layer id="' + this.id + '" name="' + this.id + '" top="0" left="0" ><nobr>' + this.html + '<\/nobr><\/layer>';


      this.containerDiv.document.write(markup);
      this.containerDiv.document.close();
      this.div          = this.containerDiv.document.layers[this.id];
      this.styleObj     = this.div;
    }

    // Start must not run until the page load event has fired
    // due to Internet Explorer not setting the height and width of 
    // the dynamically written content until then

	this.dirsign = -1;
	
	if (this.widthUnit == '%')
	this.startAt = this.width * banner._getInnerSize(this.containerDiv, 'width') / 100;
	else  
	this.startAt = 2;//this.width
	
	this._setLeft(this.startAt);
	this.stopAt  = this.dirsign*(this.htmlw/2+1);//-banner._getInnerSize(this.div,'width');

    this.newPosition          = this.startAt;
    this.styleObj.visibility = 'visible'; 
  }

  this.newPosition += this.dirsign * this.scrollAmount;
  if ((this.newPosition+(this.htmlw/2))<0) {
	//alert(this.newPosition +  ' '  + (this.newPosition+(this.htmlw/2)) + ' ' + this.width);
	this.newPosition=this.newPosition+(this.htmlw/2);
	//alert('repos');
  }

  if ( this.newPosition < this.stopAt )
  {
      this.newPosition = this.startAt;
  }
  this._setLeft(this.newPosition);
  this.runId = setTimeout(this.name + '.start()', this.scrollDelay);
};

banner.prototype.stop = function ()
{
  if (this.runId)
    clearTimeout(this.runId);
    
  this.runId = null;
};

banner.prototype.setInnerHTML = function (html)
{
  if (typeof(this.div.innerHTML) != 'string')
  {
    return;
  }

  var running = false;
  if (this.runId)
  {
    running = true;
    this.stop();
  }
  this.html = html;
  this.dirsign = null;
  if (running)
  {
    this.start();
  }
};

// fixes standards mode in gecko
// since units are required

if (document.layers)
{
  banner.prototype._setLeft = function (left)
  {
    this.styleObj.left = left;
  };

  banner.prototype._setTop = function (top)
  {
    this.styleObj.top = top;
  };
}
else
{
  banner.prototype._setLeft = function (left)
  {
    this.styleObj.left = left + 'px';
  };

  banner.prototype._setTop = function (top)
  {
    this.styleObj.top = top + 'px';
  };
}


