// newsTicker.js - version 1.0 - ds-develop
//
// Copyright (c) 2008. ds-develop.
// All rights reserved.
//
// This file implements functions which wrap
// the spry functionalities into a generic
// News Ticker. Do implement it, you may use
// html as follows:
//
// <div id="dsDataContainer" spry:region="dsDataContainer">
//		<p spry:state="loading">
//			Loading, please wait...
//		</p>
//		<div spry:repeatchildren="dsDataContainer">
//			<p id="ticker_entry_ID{ds_RowNumber}" style="display: block;" spry:if="{ds_RowNumber} == 0">{title}</p>
//			<p id="ticker_entry_ID{ds_RowNumber}" style="display: none;" spry:if="{ds_RowNumber} != 0">{title}</p>
//		</div>
// </div>
//
// This may be the minimal code for implementing the newsticker.
//
// For instanciating the newsticker, call "js_createNewsTicker(XML_FILE_PATH, NODES)"
// and specify the XML file, that contains the data and the node-path, e.g. "articles/article"
// if your news-data is stored in the xml-file like follows:
//
// <whatever>
// ...
//  <articles>
//   <article>
//		<data>...
//   <article>
//   <article>
//		<data>...
//   <article>
//  <articles>
//
// Enjoy!

// ----------------------------------------------------------------------------

//
// public variables
//
var newsTicker_timeoutSeconds = 5; // in seconds
var newsTicker_fadingTimespan = 0.8; // in seconds * 2
var newsTicker_regionIdentity = 'dsDataContainer'; // string

// ----------------------------------------------------------------------------

//
// private variables
//
var currentDataRow  = 0;
var cntRows = 0;
var timerObj = null;
var dsDataContainer = null;

// ----------------------------------------------------------------------------

//
// constructor: call this function to implement the News Ticker
//

/**
 *
 * Initialiszes the News Ticker.
 *
 * @access public
 * @return void
 **/
function js_createNewsTicker(xml_path, node_path) {
	dsDataContainer = new Spry.Data.XMLDataSet(
		xml_path,
		node_path,
		{ useCache:  true, loadInterval: 60000, filterFunc: js_filterFunc }
	);
}

// ----------------------------------------------------------------------------

//
// misc functions
//

/**
 *
 * Filters data, that came from xml stream (shows only current data row).
 *
 * @access public
 * @return void
 **/
function js_filterFunc(ds, row, rowNumber){
	cntRows = dsDataContainer.getUnfilteredData().length;
	return row;
}

/**
 *
 * Loads a specific entry, setting its style to "display: block";
 *
 * @access public
 * @return void
 **/
function js_loadEntry(row) {
	var objRef = null;
	for (var i = 0; i <= cntRows; i++) {
		objRef = document.getElementById('ticker_entry_ID' + i);
		if (objRef) {
			objRef.style.display = (i == row)? 'block': 'none';
		}
	}
}

/**
 *
 * Implements the effective, periodic change of a single news to the next one
 * inside the queue.
 *
 * @access public
 * @return void
 **/
function js_newsTimer() {
	// the regions with name "newsTicker_regionIdentity"
	var objRef = Spry.Data.getRegion(newsTicker_regionIdentity);

	// check if the regions was fully loaded, if not, start another timer, that
	// waits for a ready regions
	if (!(dsDataContainer && objRef && objRef.getState() == 'ready')) {
		clearTimeout(timerObj);
		timerObj = window.setTimeout('js_newsTimer()', newsTicker_timeoutSeconds * 1000);
		return null;
	}

	// increment row counter to next row
	currentDataRow = (currentDataRow + 1) % cntRows;

	// clear timer
	clearTimeout(timerObj);

	// implement effect: fade out
	var fadeObj = new Spry.Effect.Fade(newsTicker_regionIdentity, {duration: newsTicker_fadingTimespan * 1000, from: 100, to: 0, toggle:true, finish:
	function finish() {
		// after fading out: load new entry
		js_loadEntry(currentDataRow);

		// fade in new entry
		var fadeObj = new Spry.Effect.Fade(newsTicker_regionIdentity, {duration: newsTicker_fadingTimespan * 1000, from: 0, to: 100, toggle:true});

		// begin fading in
		fadeObj.start();
	}});
	// begin fading out
	fadeObj.start();

	// start new timer
	timerObj = window.setTimeout('js_newsTimer()', newsTicker_timeoutSeconds * 1000);

	return null;
}

// ----------------------------------------------------------------------------

//
// Start timer.
//
timerObj = window.setTimeout('js_newsTimer()', newsTicker_timeoutSeconds * 1000);