/* ***** 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.
 *
 *
 * ***** END LICENSE BLOCK ***** */
 
function runMarquee()
{
  div=this;
  if(isNaN(div._marquee_text_height)||div._marquee_text_height==0) div._marquee_text_height=parseInt(getStyleProperty(div,'height'))
  if(isNaN(div._marquee_text_height)) div._marquee_text_height=div.offsetHeight
  // Do we still need to scroll up
  if(
  
    (!div.reverse && div._marquee_top>0-div._marquee_text_height-10)
    ||
    (div.reverse && div._marquee_top<0)
    
    )
  {
    // Move up
    if(div.parentNode._moveup>0) div._marquee_top+= div.reverse ? 1 : -1;
  }
  else
  {
    div._marquee_top=div._marquee_height;
  }
  // Can we use a setter?
  if (div.style.setProperty)
  {
    div.style.setProperty('margin-top',div._marquee_top+"px","")
  }
  // Let's fallback:-(
  else
  {
    div.style.marginTop=div._marquee_top-10+"px"
  }
  // Call this method once again in 100msec
  callwrapper = new CCallWrapper(this, 70, '_marquee');
  CCallWrapper.asyncExecute(callwrapper)
}

function computeStyle(obj)
{
// Try to use W3C method
  if (window.getComputedStyle) return window.getComputedStyle(obj,null)
// Damn it! Let's fallback to M$:-(
  if (obj.currentStyle) return obj.currentStyle;
}

function getStyleProperty(obj,prop)
{
  styl=computeStyle(obj);
  // Let's try W3C getters
  if (styl.getPropertyValue) return styl.getPropertyValue(prop);
  // It appears we do not have getters:-( 
  // Let's fallback to properites
  return eval("styl."+prop);
}

function stopMarqueeMove()
{
  this.parentNode._moveup=0;
}

function startMarqueeMove()
{
  this.parentNode._moveup=1;
}


// Lets seek for marquees
divs=document.getElementsByTagName("ul");
for(i=0;i<divs.length;++i)
{
  div=divs.item(i);
  if (div.className.indexOf("Marquee")>=0)
  {
    // Generate the container
    container=document.createElement('div');
    // Move the div's children to the container
    while(child=div.firstChild) container.appendChild(div.removeChild(child));
    // Add a "hard space" to the div, workaround a bug/feature in Mozilla
    // Without, our container margin value will be applied to it's parent.
    div.appendChild(document.createTextNode("\u00A0"));
    // Add the container to the div
    div.appendChild(container);
    container.className="MarqueeContainer"
    container._marquee_height=parseInt(getStyleProperty(div,'height'))+1;
    container._marquee_text_height=parseInt(getStyleProperty(container,'height'));
    container._marquee_top=0
    container.reverse=false
    container._marquee=runMarquee
    container.onmouseover=stopMarqueeMove
    container.onmouseout=startMarqueeMove
    div._moveup=1;
    // Can we use a setter?
    if (div.style.setProperty)
    {
      div.style.setProperty('visibility',"visible","")
    }
    // Let's fallback:-(
    else
    {
      div.style.visibility="visible"
    }    
    container._marquee();
  }
}

