/* GS: This implements a scrolling marquee that pauses when the mouse moves into the marquee box.  It also includes a GS feature of a Stop/Start button to stop the scrolling after it becomes irritating.  

Parameters:

- marqueewidth (box width: e.g. "300px")
- marqueeheight (box height: e.g. "18px")
- marqueespeed (larger is faster 1-10: e.g. 2)
- marqueebgcolor (e.g. #FFFFFF)
- pauseit (pause marquee onMousever (0=no. 1=yes): e.g. 1)

- marqueecontent (Keep all content on ONE line, backslash any single quotations (e.g. that\'s great), and don't delete the <nobr> tag): e.g. '<nobr><font face="Arial"><span style="color: blue">Please take a moment to complete our</span> <span style="color: purple"><a target="_blank" href="http://www.surveymonkey.com/s.aspx?sm=PJIA8Gw7KInXSwyO1HKi4w_3d_3d" style="color: purple">Survey of NIEC Users</a>. </span><span style="color: blue"></span></font></nobr>'


The code originally comes from:

http://www.dynamicdrive.com/dynamicindex2/cmarquee.htm

Modified by Gary Schankula (GS), December 2007, October 2008

Cross browser Marquee script- © Dynamic Drive (www.dynamicdrive.com)
For full source code, 100's more DHTML scripts, and Terms Of Use, visit http://www.dynamicdrive.com
Credit MUST stay intact
*/

/*************************************************************************/
/*                                                                       */
function IncludeJavaScript2(jsFile) {
/*                                                                       */
/*************************************************************************/
  // Copyright (C) 2006-08 www.cryer.co.uk

  /* Allows one JavaScript file to be included by another.  Copy this entire file, IncludeJavaScript.js, and paste it into the top of the first external JavaScript file, and then insert a statement such as:

         IncludeJavaScript('secondJS.js');

     for any additional JavaScript file you wish to include.  As long as you insert this statement AFTER the IncludeJavaScript function, it will work.  

     You can use this technique to include as many JavaScript files as you like.  What the "IncludeJavaScript" function is doing is generating the HTML to include the JavaScript file. The browser when it comes to render the page will read and execute the JavaScript in the first external file. This includes the line "IncludeJavaScript('secondJS.js');", which generates additional HTML. The browser will then parse the newly generated HTML which is when it picks up the references to the new JavaScript files and so on.

     The reason for having "</scr' + 'ipt>" instead of "< / s c r i p t >" in the document.write in the script is simply that IE 7 would interpret "< / s c r i p t >" as the end of the script, even though it is enclosed in quotes and is part of a string. Breaking "< / s c r i p t >" into two parts gets round this problem.
  */

/* http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm */

  document.write('<script type="text/javascript" src="'
    + jsFile + '"></scr' + 'ipt>'); 
}

IncludeJavaScript2('DetermineBrowser.js');

//---GS

function GetCookie(name) {
  var start, end;
  var cookieValue = "";
  if (document.cookie.length > 0) {
    start = document.cookie.indexOf(name + "=");
    if (start != -1) { 
      start = start + name.length + 1; 
      end = document.cookie.indexOf(";",start);
      if (end == -1)
        end = document.cookie.length;
      cookieValue =  unescape(document.cookie.substring(start,end));
    } 
  }
  return cookieValue;
}

function SetCookie(name, value, expiredays) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  document.cookie = name + "=" + escape(value) +
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function SetScrolling() {
  mode = scrollingMode;
  copyspeed = marqueespeed;
  SetCookie(cookieMode, scrollingMode, 1);
  document.getElementById(marqueeButton).value = "Stop";
}

function SetPause() {
  mode = pauseMode;
  copyspeed = pausespeed;
  SetCookie(cookieMode, pauseMode, 1);
  document.getElementById(marqueeButton).value = "Start";
}

function Startup() {
  if ((mode == null) || (mode == "")) {
    SetScrolling()
  }
  else {
    if (mode == scrollingMode) {
      SetScrolling()
    }
    else {
      SetPause();
    }
  }
}

function Toggle() {
  if ((mode == null) || (mode == "")) {
    SetScrolling()
  }
  else {
    if (mode == scrollingMode) {
      SetPause()
    }
    else {
      SetScrolling();
    }
  }
}

function MouseMove(direction) {
  /* If mouse-over then pause;  otherwise (mouse-out) resume,
     but only if we're not in a stop mode via the Stop button.*/
  if (direction == "mouseover")
    copyspeed=pausespeed
  else {
    if (mode==scrollingMode)
      copyspeed=marqueespeed;
  }
}

//---

function ScrollMarquee(){
  if (iedom){
    if (parseInt(cross_marquee.style.left)>(actualwidth*(-1)+8))
      cross_marquee.style.left=parseInt(cross_marquee.style.left)-copyspeed+"px"
    else
      cross_marquee.style.left=parseInt(marqueewidth)+8+"px";
  }
/*Gary
  else if (document.layers){
    if (ns_marquee.left>(actualwidth*(-1)+8))
      ns_marquee.left-=copyspeed
    else
      ns_marquee.left=parseInt(marqueewidth)+8
  }
*/
}

function OutputMarquee() {
  var cs; //GS

/*Gary*//*if (iedom||document.layers) {*/ if (iedom) {

/*Gary (This WITH is needed for all the "write"s without a "document.". */
    with (document) {

/*      document.write('<table border="0" cellspacing="0" cellpadding="0"><td>') //Add 'align="center"'.*/

      document.write('<table align="center" border="0" cellspacing="0" cellpadding="0"><td>');

      if (iedom) {

/*        write('<div style="position:relative;width:'+marqueewidth+';height:'+marqueeheight+';overflow:hidden">') //Add "text-align:left;".*/

        write('<div style="text-align:left; position:relative;width:'+marqueewidth+';height:'+marqueeheight+';overflow:hidden">');

        write('<div style="position:absolute; BORDER: 1PX DOTTED #0C6483; width:'+marqueewidth+'; height:'+marqueeheight+'; background-color:'+marqueebgcolor+'" onMouseover="MouseMove(\'mouseover\')" onMouseout="MouseMove(\'mouseout\')">');

        write('<div id="iemarquee"></div>');

        write('</div></div>');

        //------ Inserted by GS:
//cs = (copyspeed==pausespeed)?'"Stop"':'"Start"'
        if (mode == pauseMode)
          cs = '"Stop"'
        else
          cs = '"Start"';

        /* GS: Added id=marqueeButton below, otherwise the marquee doesn't work at all in non-IE browsers because getElementById() is actually looking for an ID, not a NAME (IE thinks a NAME is the same as an ID) -- http://www.codingforums.com/showthread.php?t=94475. */

        write('<div align="right"><input type="button" name="'+marqueeButton+'" id="'+marqueeButton+'" value="' + cs + '" alt="' + cs + '" align="right" style="font-family: Arial; font-size: 6pt; font-weight: bold;" onclick="Toggle();"></div>');

      } //end if (iedom)

/*Gary (note that all this document.layers, <ilayer>, etc. is for Netscape 4, which is not supported anymore, so I'm in the process of removing this code)
      else {
        if (document.layers) {
          write('<ilayer width='+marqueewidth+' height='+marqueeheight+' name="ns_marquee" bgColor='+marqueebgcolor+'>')
          write('<layer name="ns_marquee2" left=0 top=0 onMouseover="MouseMove(\'mouseover\')" onMouseout="MouseMove(\'mouseout\')"></layer>')
          write('</ilayer>')
        } //end if (document.layers)
      } //end else
*/

      document.write('</td></table>');
    } //end with
  } //end if (iedom||document.layers)
}

function Populate() {
  if (iedom){
    cross_marquee=document.getElementById? document.getElementById("iemarquee") : document.all.iemarquee;
    cross_marquee.style.left=parseInt(marqueewidth)+8+"px";
    cross_marquee.innerHTML=marqueecontent
actualwidth=document.all? temp.offsetWidth : document.getElementById("temp").offsetWidth;
  }
/*Gary
  else if (document.layers){
    ns_marquee=document.ns_marquee.document.ns_marquee2
    ns_marquee.left=parseInt(marqueewidth)+8
    ns_marquee.document.write(marqueecontent)
    ns_marquee.document.close()
    actualwidth=ns_marquee.document.width
  }
*/
  lefttime=setInterval("ScrollMarquee()",20);
}

function InitMarquee() {

/*Gary  marqueespeed=(document.all)? marqueespeed : Math.max(1, marqueespeed-1) //slow speed down by 1 for NS*/

  copyspeed = marqueespeed;
  scrollingMode = "scrollingMode";
  pauseMode = "pauseMode";
  cookieMode = "marqueeMode";
  marqueeButton = "ToggleButton";
  mode = GetCookie(cookieMode);
  pausespeed=(pauseit==0)? copyspeed: 0;

/*Gary*//*iedom=document.all||document.getElementById*/
  if (iedom)
    document.write('<span id="temp" style="visibility:hidden;position:absolute;top:-100px;left:-9000px">'+marqueecontent+'</span>');

  actualwidth='';

  var exp = new Date();
  //"(D * 24 * 60..." where D = #days cookie to last:
  var oneYearFromNow = exp.getTime() + (1 * 24 * 60 * 60 * 1000);
  exp.setTime(oneYearFromNow);
}

function RunMarquee() {
  var browserName = browserIE; //Default
//  browserName = DetermineBrowser();

  InitMarquee();
  OutputMarquee();
  Startup();

  if (browserName == browserIE)
    window.onload=Populate();
  else {
    if (window.addEventListener) // For Firefox, Opera & Safari
      window.addEventListener("DOMContentLoaded", Populate(), false);
    }
}

/* GLOBAL VARIABLE DECLARATIONS */

var mode, scrollingMode, pauseMode, cookieMode;
var copyspeed, pausespeed;
var cross_marquee, ns_marquee;
var marqueeButton;
var actualwidth;
var iedom = true;
var browserIE = "Microsoft Internet Explorer";

