/*
 * @package News and Articles Manager
 * @copyright Copyright 2009-2010 Kuroi Web Design
 * @copyright Copyright 2003-2005 Joshua Dechant
 * @copyright Copyright 2003 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: jscript_newsTicker.js 20 2010-04-15 02:28:47Z kuroi $
 * Major credit for this add-on module is due to Joshua Dechant
 */

function newsTicker(TheID, TheHeight, TheWidth, TheScrollAmount, TheScrollDelay, TheHtml) {
  TickerID = TheID;
  TickerAmount = (TheScrollAmount ? TheScrollAmount : 5);
  TickerDelay = (TheScrollDelay ? TheScrollDelay : 50);
  TickerName = 'newsTicker1';
  TickerHtml  = '<nobr>' + TheHtml + '</nobr>';

  TickerIEDOM = ((document.all || document.getElementById) ? true : false);

  //slow speed down by 1 for NS
  TickerAmount = ((document.all) ? TickerAmount : Math.max(1, TickerAmount - 1));

  TickerCopyAmount = TickerAmount;
  TickerPauseAmount = 0;

  if (typeof(TheHeight) == 'number') {
    TickerHeight = TheHeight;
    TickerHeightUnit = 'px';
  } else if (typeof(TheHeight) == 'string') {
    TickerHeight = parseInt('0' + TheHeight, 10);
    TickerHeightUnit = TheHeight.toLowerCase().replace(/^[0-9]+/, '');
  } else {
    TickerHeight = 200;
    TickerHeightUnit = 'px';
  }

  if (typeof(TheWidth) == 'number') {
    TickerWidth = TheWidth;
    TickerWidthUnit = 'px';
  } else if (typeof(TheWidth) == 'string') {
    TickerWidth = parseInt('0' + TheWidth, 10);
    TickerWidthUnit = TheWidth.toLowerCase().replace(/^[0-9]+/, '');
  } else {
    TickerWidth = 100;
    TickerWidthUnit = 'px';
  }

  TickerCombinedCSS = 'width: ' + TickerWidth + TickerWidthUnit + '; height: ' + TickerHeight + TickerHeightUnit + ';';

  if (TickerIEDOM == true) {
    document.write('<span id="' + TickerID + '" style="visibility: hidden; position: absolute; top: -100px; left: -10000px;">' + TickerHtml + '</span>');
  }

  TickerActualWidth = '';

  if (window.addEventListener) {
    window.addEventListener('load', TickerPopulate, false);
    window.addEventListener('unload', TickerSaveLastMsg, false);
  } else if (window.attachEvent) {
    window.attachEvent('onload', TickerPopulate);
    window.attachEvent('onunload', TickerSaveLastMsg);
  } else if (document.all || document.getElementById) {
    window.onload = TickerPopulate;
    window.onunload = TickerSaveLastMsg;
  }
}

function TickerPopulate() {
  TickerContainerDiv = TickerGetElm(TickerID + 'Container');

  TickerDiv = TickerGetElm(TickerID);

  TickerContainerDiv.style.left = TickerWidth + 8 + TickerWidthUnit;

  if (TickerGetCookie('newsTickerLastPos') != '') {
    TickerRememberLastMsg();
  }

  TickerContainerDiv.innerHTML = TickerHtml;

  TickerActualWidth = TickerDiv.offsetWidth;

  TickerTime = setInterval("TickerScroll()", TickerDelay);
}

function TickerGetElm(id) {
  var elm = null;

  if (document.getElementById) {
    elm = document.getElementById(id);
  } else {
    elm = document.all[id];
  }

  return elm;
}

function TickerGetCookie(Name) {
  var search = Name + '=';
  var returnvalue = '';
  var offset;
  var end;

  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);

    if (offset != -1) {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);

      if (end == -1) {
        end = document.cookie.length;
      }

      returnvalue = unescape(document.cookie.substring(offset, end));
    }
  }

  return returnvalue;
}

function TickerSaveLastMsg() {
  document.cookie = 'newsTickerLastPos=' + TickerContainerDiv.style.left;
}

function TickerRememberLastMsg() {
  var lastPosition;

  lastPosition = parseInt(TickerGetCookie("newsTickerLastPos"));

  TickerContainerDiv.style.left = parseInt(lastPosition) + 'px';
}

function TickerScroll() {
  if (parseInt(TickerContainerDiv.style.left) > (TickerActualWidth*(-1)+8)) {
    TickerContainerDiv.style.left = parseInt(TickerContainerDiv.style.left) - TickerCopyAmount + 'px';
  } else {
    TickerContainerDiv.style.left = parseInt(TickerWidth) + TickerCopyAmount + 8 + TickerHeightUnit;
  }
}

function newsTicker_init() {
  if (TickerIEDOM) {
    with (document) {
      write('<div style="position: relative; overflow: hidden; '+ TickerCombinedCSS +'" onmouseover="TickerCopyAmount=TickerPauseAmount" onmouseout="TickerCopyAmount=TickerAmount">');
      write('<div id="' + TickerID + 'Container" style="position: absolute; left: 0px; top: 0px;"></div>');
      write('</div>');
    }
  }
}

