/*-----------------------------------------------------------------
This is a news ticker handler which manages one to many instances
of the NewsTicker class. The uniqueness of each instance depends
on the name of the class.
-----------------------------------------------------------------*/
var NewsTickerHandler = {
	all			        : {},
	intCountLists   : 0,
	
	AddList : function(p_objList) {
	    this.all[p_objList.ide] = p_objList;
        this.intCountLists++;
	},

	Initialize : function() {
	    if (this.intCountLists == 0) {
	        alert("List Error!");
        }
        else {
	        for (var strKey in this.all) {
	          this.all[strKey].GetArray();
	        }            
        }	
	}
};
//-----------------------------------------------------------------

/*-----------------------------------------------------------------
This is a class called NewsTicker with 
Properties like : ide, ImageElementName etc...
Methods: GetArray, ReadArray
-----------------------------------------------------------------*/
function NewsTicker (p_strIde, p_strSource, p_intInterval) {

  // List properties
    if (!p_strIde) {
      alert('Missing id / unique name for Ticker.');
      return;
    }
    this.ide          = p_strIde;
    this.Interval     = (!p_intInterval)? 1000 : p_intInterval;
    this.Source       = (!p_strSource)? '' : p_strSource;

    // List internal variables
    this.Counter            = 0;
    NewsTickerHandler.AddList(this);
}
//--------------------------------------------------------------

//--------------------------------------------------------------
//This method loads the array file
//--------------------------------------------------------------
NewsTicker.prototype.GetArray = function() {
  if (this.Source.length == 0) return;
  this.ReadArray();
  window.setInterval("NewsTickerHandler.all['" + this.ide + "'].ReadArray();", this.Interval);
}
//--------------------------------------------------------------

/*--------------------------------------------------------------
This method reads the Array of elements passed to it
--------------------------------------------------------------*/
NewsTicker.prototype.ReadArray = function() {
  var arrElements = this.Source.split(',');
  if (arrElements.length == 0) return;

  // if the end of the array is reached, reset the counter to start from the beginning
  if (arrElements.length == this.Counter) {
    if (document.getElementById(arrElements[this.Counter - 1])) document.getElementById(arrElements[this.Counter - 1]).style.display = 'none';
    this.Counter = 0;
  }

  // hide the previous element if it exists
  if (document.getElementById(arrElements[this.Counter - 1])) document.getElementById(arrElements[this.Counter - 1]).style.display = 'none';
  // display the next element if it exists
  if (document.getElementById(arrElements[this.Counter])) document.getElementById(arrElements[this.Counter]).style.display = '';
  this.Counter++;

};
//--------------------------------------------------------------
