/* News Scroller version 1.2
A vertical html news scroller for W3C compliant browsers; IE5+, Firefox, Opera, etc.

Created by Stephen Cox


Usage:
Include news_scroller.js and news_scroller.css in the header of the html file
e.g. <style type="text/css">@import url(javascript/news_scroller.css);</style>
     <script language="JavaScript" src="javascript/news_scroller.js"></script>
		 
Where you want to position the news scroller call the function draw()
e.g. <script>draw();</script>

If using cookies to remember the state of the scroller the file Cookie.js must be included
in the html file before the news_scroller.js file.
e.g. <script language="JavaScript" src="javascript/Cookie.js"></script>
     <script language="JavaScript" src="javascript/news_scroller.js"></script>

The speed and size can be adjusted by changing the variables below and 
the content is changed by altering the strings in the messages array.

Issues:
	Firefox support still not very good; irregular scroll speed and CSS for horizontal rule sketchy

Changes for version 1.2:
    Added cookie to remember state when returning to page

Changes for version 1.1:
    Complete rewrite of the code, less than 10% of the original code remaining
    Added ability to pause the scroller when mouse hovers over it

Changes for version 1.0:
	Added support for Firefox
	Refactored the JavaScript
*/

// Configure the variables to change the speed
var scrollSpeed = 18;
var scrollDecrement = 1;
var pause = 5000;

// Configure the variables to change the appearance
var scrollerWidth = 160; 
var scrollerHeight = 260;
var scrollerbgcolor = '#FFFFFF';

// Set to switch on/off the pause when mouse over
var mouseEvents = true;

// Use cookies to keep state of scroller when returning to page?
var useCookies = true;

// Configure the variable below to change the contents
var messages = new Array();



messages[2]="<hr class='news'><div align='center'><span class='headline'>Nuova pubblicazione</span><hr class='news'><br><img src='images/scroller/Lezioni_bellinzonesi_3_Beltraminelli_pi.jpg' width='80'></div><br><span class='news'>&Egrave; stato pubblicato, a cura di Fabio Beltraminelli, il terzo volume delle <a href='http://www.liceobellinzona.ch/argomenti/lezionibellinzonesi03.html'><b>Lezioni bellinzonesi</b></a>.</span>";


messages[1]="<hr class='news'><div align='center'><span class='headline'>Calendario 2010-11</span><hr class='news'><br><img src='images/scroller/calendario.jpg' width='80'></div><br><span class='news'>&Egrave; possibile scaricare il nuovo calendario scolastico: buone vacanze! <a href='http://www.liceobellinzona.ch/pdf/2010-11.pdf'><b>Info...</b></a></span>";

messages[0]="<hr class='news'><div align='center'><span class='headline'>Concerto</span><hr class='news'><br><img src='images/scroller/coro2010.jpg' width='100'></div><br><span class='news'>Il concerto del coro e dell'orchestra dei licei di Bellinzona e Locarno &egrave; visionabile online: <a href='http://www.liceobellinzona.ch/argomenti/concerto2010.html'><b>Info...</b></a></span>";


/**********************************   Main Code    **********************************/

var i = 0;
var firstTop = '1px', secondTop = scrollerHeight + 'px';
var firstDiv, secondDiv;
var intervalId;

if (mouseEvents)
    var eventString = ', onMouseOver="stop()", onMouseOut="start()"';
else
    var eventString = '';

if (useCookies) {
    var scrollerCookie = Cookie.read('ScrollerCookie');
    if (scrollerCookie) {
        var values = scrollerCookie.value.split("::");
        i = parseInt(values[0]);
        firstTop= values[1];
        secondTop = values[2];
    }
    else {
        scrollerCookie = new Cookie('ScrollerCookie', '');
    }
}

function draw() {
	document.writeln('<span id="main" style="position:relative;width:'+scrollerWidth+';height:'+scrollerHeight+';overflow:hidden;background-color:'+scrollerbgcolor+'">');
	document.writeln('<div class="newsdiv" style="width:'+scrollerWidth+';height:'+scrollerHeight+';clip:rect(0 '+scrollerWidth+' '+scrollerHeight+' 0);top:0"'+eventString+'>');
	document.writeln('<div id="first" class="newsdiv" style="width:'+scrollerWidth+'; top:'+firstTop+';">');
	document.write(messages[i]);
	document.writeln('</div>');
	document.writeln('<div id="second" class="newsdiv" style="width:'+scrollerWidth+'; top:'+secondTop+';">');
	document.write(messages[++i % messages.length]);
	document.writeln('</div>');
	document.writeln('</div>');
	document.writeln('</span>');
}

function step() {
    if (parseInt(firstDiv.style.top) >= scrollerHeight * -1) {
        firstDiv.style.top = parseInt(firstDiv.style.top) - scrollDecrement;
        secondDiv.style.top = parseInt(secondDiv.style.top) - scrollDecrement;
    } else {
        i = ++i % messages.length;
        clearInterval(intervalId);
        temp = firstDiv;
        firstDiv = secondDiv;
        secondDiv = temp;
        secondDiv.style.top = scrollerHeight;
        secondDiv.innerHTML = messages[i];
        intervalId = setInterval(restart, pause);
    }
}

function init() {
	if (document.getElementById && messages.length > 0) {
		firstDiv = document.getElementById("first");
        secondDiv = document.getElementById("second");
        if (firstDiv.style.top == '1px')
            intervalId = setInterval(restart, pause);
        else
            intervalId = setInterval(restart, scrollSpeed);
	}
} 

function start() {
    intervalId = setInterval(step, scrollSpeed);
}

function restart() {
    clearInterval(intervalId);
    intervalId = setInterval(step, scrollSpeed);
}

function stop() {
    clearInterval(intervalId);
}

window.onload = init;

if (useCookies) {
    window.onunload = function() {
        var j = i - 1;
        if (j < 0) j = messages.length - 1;
        scrollerCookie.value = j + '::' + firstDiv.style.top + '::' + secondDiv.style.top;
        scrollerCookie.store();
    }
}

var _console = null
function debug(msg) {
    if ((_console == null) || (_console.closed)) {
        _console = window.open("", "console", "width=600,height=300,resizable");
        _console.document.open("text/plain");
    }
    _console.focus();
    _console.document.writeln(msg);
}
    