/*
 * @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_newsScroller.js 20 2010-04-15 02:28:47Z kuroi $
 * Major credit for this add-on module is due to Joshua Dechant
 */

function newsScroller(TheID, TheHeight, TheWidth, TheScrollAmount, TheScrollDelay, TheHtml) {
  ScrollerID = TheID;
  ScrollerAmount = (TheScrollAmount ? TheScrollAmount : 5);
  ScrollerDelay = (TheScrollDelay ? TheScrollDelay : 50);
  ScrollerName = 'newsScroller1';
  ScrollerHtml  = TheHtml;

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

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

  ScrollerCopyAmount = ScrollerAmount;
  ScrollerPauseAmount = 0;

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

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

  ScrollerCombinedCSS = 'width: ' + ScrollerWidth + ScrollerWidthUnit + '; height: ' + ScrollerHeight + ScrollerHeightUnit + ';';

  if (ScrollerIEDOM == true) {
    document.write('<div id="' + ScrollerID + '" style="visibility: hidden; position: absolute; top: -100px; left: -10000px;">' + ScrollerHtml + '</div>');
  }

  ScrollerActualHeight = '';

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

function ScrollerPopulate() {
  ScrollerContainerDiv = ScrollerGetElm(ScrollerID + 'Container');

  ScrollerDiv = ScrollerGetElm(ScrollerID);

  ScrollerContainerDiv.style.top = ScrollerHeight + 8 + ScrollerHeightUnit;

  if (ScrollerGetCookie('newsScrollerLastPos') != '') {
    ScrollerRememberLastMsg();
  }

  ScrollerContainerDiv.innerHTML = ScrollerHtml;

  ScrollerActualHeight = ScrollerDiv.offsetHeight;

  ScrollerTime = setInterval("ScrollerScroll()", ScrollerDelay);
}

function ScrollerGetElm(id) {
  var elm = null;

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

  return elm;
}

function ScrollerGetCookie(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 ScrollerSaveLastMsg() {
  document.cookie = 'newsScrollerLastPos=' + ScrollerContainerDiv.style.top;
}

function ScrollerRememberLastMsg() {
  var lastPosition;

  lastPosition = parseInt(ScrollerGetCookie("newsScrollerLastPos"));

  ScrollerContainerDiv.style.top = parseInt(lastPosition) + 'px';
}

function ScrollerScroll() {
  if (parseInt(ScrollerContainerDiv.style.top) > (ScrollerActualHeight*(-1)-ScrollerHeight-8)) {
    ScrollerContainerDiv.style.top = parseInt(ScrollerContainerDiv.style.top) - ScrollerCopyAmount + 'px';
  } else {
    ScrollerContainerDiv.style.top = parseInt(ScrollerHeight) + ScrollerCopyAmount + 8 + ScrollerHeightUnit;
  }
}

function newsScroller_init() {
  if (ScrollerIEDOM) {
    with (document) {
      write('<div style="position: relative; overflow: hidden; '+ ScrollerCombinedCSS +'" onmouseover="ScrollerCopyAmount=ScrollerPauseAmount" onmouseout="ScrollerCopyAmount=ScrollerAmount">');
      write('<div id="' + ScrollerID + 'Container" style="position: absolute; left: 0px; top: 0px;"></div>');
      write('</div>');
    }
  }
}

