/* 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[5]="<hr class='news'><div align='center'><span class='headline'>Le bici del LiBe</span><hr class='news'><br><img src='images/scroller/bicip.jpg' width='120'></div><br><span class='news'>Conosci il nuovo servizio biciclette del LiBe? Tutte le informazioni qui: <a href='argomenti/bici.html'><b>Info...</b></a></span>";

messages[4]="<hr class='news'><div align='center'><span class='headline'>Scopri le partiture musicali del LiBe!</span><hr class='news'><br><img src='images/scroller/partiturep.jpg' width='140'></div><br><span class='news'>Suoni il pianoforte o ti piacerebbe incominciare a suonare? Visita il settore di partiture didattiche nella biblioteca del liceo: troverai quello che fa per te! <a href='argomenti/partiture.html'><b>Info...</b></a></span>";

messages[3]="<hr class='news'><div align='center'><span class='headline'>Calendario 2010</span><hr class='news'><br><img src='images/scroller/icofebbraio.jpg' width='100'></div><br><span class='news'>I 12 mesi del 2010 illustrati con opere di allievi del corso di arti visive del prof. Fontanesi: <a href='http://www.fontanesi.ch/libe/2010/mesi.html'><b>Info...</b></a></span>";

messages[2]="<hr class='news'><div align='center'><span class='headline'>Mostra</span><hr class='news'><br><img src='images/copertina/maschera_1.jpg' width='80'></div><br><span class='news'>Lavori di maturit&agrave; in Arti visive, blocco 2: <a href='http://www.liceobellinzona.ch/argomenti/mostra_maschere.html'><b>Info...</b></a></span>";


messages[1]="<hr class='news'><div align='center'><span class='headline'>Orientamento</span><hr class='news'><br><img src='images/argomenti/bussola.gif' width='80'></div><br><span class='news'>Visita la nuova pagina dell'orientamento alle scuole universitarie: <a href='http://www.liceobellinzona.ch/servizi/orientamento.html'><b>Info...</b></a></span>";

messages[0]="<hr class='news'><div align='center'><span class='headline'>Giornate culturali</span><hr class='news'><br><img src='images/scroller/grecia.jpg' width='120'></div><br><span class='news'>Pagina informativa delle giornate culturali: <a href='http://www.liceobellinzona.ch/argomenti/culturali.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);
}
    