/* Javascript by Daniel Cohen Gindi (c) danielgindi@gmail.com 054-5655765 */
/* Version: 2009-04-27 */

function createScrollingNewsObject(divName, boxWidth, boxHeight, speed, direction, newsLinger, stopOnHover, highlightOnHover, textColor, textColorHover, textDirection, textAlign, arrTexts, divClip, endSpacer, addStyleStr)
{ // Backwards compatibility
	var news = new scrollingNews();
	news.element=divName;
	if (boxWidth) news.width=boxWidth;
	if (boxHeight) news.height=boxHeight;
	news.speed=speed;
	news.direction=direction;
	news.linger=newsLinger;
	news.stopOnHover=stopOnHover;
	news.underlineOnHover=highlightOnHover;
	news.textColor=textColor;
	news.colorOnHover=textColorHover;
	news.textDirection=textDirection;
	news.textAlign=textAlign;
	news.arrNews=arrTexts;
	if (divClip) news.divClip=parseInt(divClip);
	if (endSpacer) news.endSpacer=endSpacer;
	if (addStyleStr && ((typeof addStyleStr) == 'string')) news.addStyleStr=';' + addStyleStr;
	news.create();
	return news;
}

function scrollingNews() { };

scrollingNews.prototype = {
	
  created: false,
  arrNews: null,
  element: null,
  speed: 30,
  speedSteps: 1,
  linger: 2000,
  stopOnHover: true,
  underlineOnHover: true,
  colorOnHover: '',
  textColor: '',
  textAlign: '',
  textDirection: '',
  addStyleStr: null,
  direction: 'left',
  endSpacer: 8,
  _el: null, _elAbs: null,
  _arrDivs1: null, _arrDivs2: null,
  _div1: null, _div2: null,
  _zeroPoint: 10,
  _divsDiff: null,
  _divLength: null,
  _scrollPos: null,
  _int: null,
  _size: null,
  _divsDeltas: null,
  _nextLinger: 0,
  width: null, height: null,
  _parentLength: null,
  divClip: 2,
  _mouseStop: false,
  _vert: true,
  
  create: function() {
    var _thisObj = this;
    this._thisObj = this;
    this._FdoScroll = function() { return _thisObj._doScroll(); };
    this._FendLinger = function() { return _thisObj._endLinger(); };
    this._FmouseOver = function(e) { return _thisObj._mouseOver(e || event); };
    this._FmouseOut = function(e) { return _thisObj._mouseOut(e || event); };
    this._create();
  },
  _create: function() {
    if (this.created) return;
    
    if (this.arrNews==null) this.arrNews=new Array();
    else this.arrNews=this.arrNews.slice(); // duplicate array as we may be modifying
    
    this.direction = this.direction + '';
  	switch (this.direction)
		{
			default:
			case '1': case 'up': this.direction=1; this._vert=true; break;
			case '2': case 'down': this.direction=2; this._vert=true; break;
			case '3': case 'left': this.direction=3; this._vert=false; break;
			case '4': case 'right': this.direction=4; this._vert=false; break;
		}
		
		if (this.direction==2 || this.direction==4) this.arrNews.reverse();

		this._div1=document.createElement('div');
		this._div2=document.createElement('div');
		this._div1.style.position='absolute';
		this._div2.style.position='absolute';
		this._arrDivs1 = new Array();
		this._arrDivs2 = new Array();
		
    this.created = true;
    if (this.element!=null) this._int=setInterval(this._FdoScroll, this.speed);
  },
  _prepareSubDiv: function(div)
  {
		if (typeof this.addStyleStr == 'string') div.style.cssText += this.addStyleStr;

		try{
			if (this.direction==1) div.style.paddingBottom=this.endSpacer + 'px';
			else if (this.direction==2) div.style.paddingTop=this.endSpacer + 'px';
			else if (this.direction==3) div.style.paddingRight=this.endSpacer + 'px';
			else if (this.direction==4) div.style.paddingLeft=this.endSpacer + 'px';
		} catch(E){};
		try{div.style.color=this.textColor;}catch(E){};
		try{div.style.cursor='pointer'}catch(E){};
		try{div.style.textAlign=this.textAlign;}catch(E){};
  	try{div.style.direction=this.textDirection;}catch(E){};
  	div.style.position='absolute';
  	div.style.overflow='hidden';
		if (this._vert) div.style.width=this.width + 'px';
		else div.style.height=this.height + 'px';
			
		registerEvent(div, 'mouseover', this._FmouseOver);
		registerEvent(div, 'mouseout', this._FmouseOut);
	},
  _prepare: function()
  {
  	if (this._el!=null) return true;
  	
		if (this.element) this._el=$find(this.element);
		if (this._el==null) return false;

		if (this.width==null) this.width=$width(this._el);
		if (this.height==null) this.height=$height(this._el);
		
		var el;
		for (var i=0; i<this.arrNews.length; i++)
		{
			el=document.createElement('div');
			el.innerHTML=this.arrNews[i];
			this._prepareSubDiv(el);
			this._arrDivs1.push(el);
			this._div1.appendChild(this._arrDivs1[i]);
			
			el=document.createElement('div');
			el.innerHTML=this.arrNews[i];
			this._prepareSubDiv(el);
			this._arrDivs2.push(el);
			this._div2.appendChild(this._arrDivs2[i]);
		}
				
		this._calcSizes();

		// absolute container
		el=document.createElement('div');
		el.style.width=this.width+'px';
		el.style.height=this.height+'px';
		el.style.position='absolute';
		el.style.overflow='hidden';
		this._el.appendChild(el);
		this._elAbs=el;

		// inner relative container
		el=document.createElement('div');
		el.style.width=this.width+'px';
		el.style.height=this.height+'px';
		el.style.position='relative';
		this._elAbs.appendChild(el);
		this._el=el;
		
		if (this._vert) this._scrollPos = 0; else this._scrollPos = 0;
		
		if (this._vert)
		{
			this._div2.style.height=this._div1.style.height=this._divLength+'px';
			this._div2.style.width=this._div1.style.width=this.width+'px';
			this._divsDiff=this.height-this._divLength;
			this._parentLength = this.height;
		}
		else
		{
			this._div2.style.width=this._div1.style.width=this._divLength+'px';
			this._div2.style.height=this._div1.style.height=this.height+'px';
			this._divsDiff=this.width-this._divLength;
			this._parentLength = this.width;
		}
		if (this._divsDiff<this.endSpacer) this._divsDiff=this.endSpacer;

		if (this.direction==1)
		{
			this._div1.style.top=this._parentLength - this._scrollPos + 'px';
			this._div2.style.top=(this._parentLength - this._scrollPos + this._divsDiff + this._divLength) + 'px';
		}
		else if (this.direction==2)
		{
			this._div1.style.top=-this._divLength+this._scrollPos + 'px';
			this._div2.style.top=(-this._divLength+this._scrollPos - this._divsDiff - this._divLength) + 'px';
		}
		else if (this.direction==3)
		{
			this._div1.style.left=this._parentLength - this._scrollPos + 'px';
			this._div2.style.left=(this._parentLength - this._scrollPos + this._divsDiff + this._divLength) + 'px';
		}
		else if (this.direction==4)
		{
			this._div1.style.left=-this._divLength+this._scrollPos + 'px';
			this._div2.style.left=(-this._divLength+this._scrollPos - this._divsDiff - this._divLength) + 'px';
		}

		this._el.appendChild(this._div1);
		this._el.appendChild(this._div2);

		switch (this.direction) {
		case 1:
		case 2:
		    this._elAbs.style.clip = "rect(" + this.divClip + "px " + this.width + "px " + (this.height - ($ver.ie?(this.divClip*2):this.divClip)) + "px 0px)";
		    break;
		case 3:
		case 4:
		    this._elAbs.style.clip = "rect(0px " + (this.width - ($ver.ie?(this.divClip*2):this.divClip)) + "px " + this.height + "px " + this.divClip + "px)";
		    break;
		}
		
		return true;
  },
	_calcSizes: function() {
  	this._size = new Array();
  	this._divsDeltas = new Array();

    var elParent = document.createElement('div');
    var elText = document.createElement('div');

    if (this._vert) elParent.style.width = this.width + 'px';
    else elParent.style.height = this.height + 'px';

    elParent.style.visiblity = 'hidden';
    elParent.style.position = 'relative';
    elText.style.position = 'absolute';
    try{elText.style.textAlign=this.textAlign;}catch(E){};
    try{elText.style.direction=this.textDirection;}catch(E){};
    document.body.appendChild(elParent);
    elParent.appendChild(elText);

  	if (typeof this.addStyleStr == 'string') elText.style.cssText += this.addStyleStr;

		var sum=0, s;
    for (var i = 0; i < this.arrNews.length; i++) {
      if (this._vert) elText.style.width = this.width + 'px';
      else elText.style.height = this.height + 'px';
      elText.innerHTML = this.arrNews[i];
      if (this._vert) s=$height(elText); else s=$width(elText);
  		if (this._vert) 
  		{
  			this._arrDivs1[i].style.top=this._arrDivs2[i].style.top=sum+'px';
  			this._arrDivs1[i].style.height=this._arrDivs2[i].style.height = s+'px';
  		}
  		else 
			{
  			this._arrDivs1[i].style.left=this._arrDivs2[i].style.left=sum+'px';
  			this._arrDivs1[i].style.width=this._arrDivs2[i].style.width = s+'px';
			}
      s+=this.endSpacer;
      this._divsDeltas.push(sum);	this._size.push(s);
 			sum+=s;
    }
    this._divLength = sum;
    elParent.removeChild(elText);
    document.body.removeChild(elParent);

    delete elText;
    delete elParent;
	},
	_doScroll: function()
	{
		if (this._el==null) {	if (!this._prepare()) return;	}
		if (this._mouseStop) return;

		var bLinger=false;

		if (this._scrollPos+this.speedSteps>=this._divLength+this._parentLength) 
		{
			this._scrollPos -= this._divLength + this._divsDiff;
			if (this._nextLinger==this._arrDivs1.length) this._nextLinger=0;
		}
		
		if (this._parentLength - (this._scrollPos+this.speedSteps) + this._divsDeltas[this._nextLinger]<=this._zeroPoint)
		{
			bLinger=true;
			this._scrollPos=this._parentLength+this._divsDeltas[this._nextLinger]-this._zeroPoint;
		}
		else this._scrollPos+=this.speedSteps;

		if (this.direction==1)
		{
			this._div1.style.top=this._parentLength - this._scrollPos + 'px';
			this._div2.style.top=(this._parentLength - this._scrollPos + this._divsDiff + this._divLength) + 'px';
		}
		else if (this.direction==2)
		{
			this._div1.style.top=-this._divLength+this._scrollPos + 'px';
			this._div2.style.top=(-this._divLength+this._scrollPos - this._divsDiff - this._divLength) + 'px';
		}
		else if (this.direction==3)
		{
			this._div1.style.left=this._parentLength - this._scrollPos + 'px';
			this._div2.style.left=(this._parentLength - this._scrollPos + this._divsDiff + this._divLength) + 'px';
		}
		else if (this.direction==4)
		{
			this._div1.style.left=-this._divLength+this._scrollPos + 'px';
			this._div2.style.left=(-this._divLength+this._scrollPos - this._divsDiff - this._divLength) + 'px';
		}
			
		if (bLinger) { clearInterval(this._int); setTimeout(this._FendLinger, this.linger);	}
	},
	_endLinger: function()
	{
		this._nextLinger++;
		this._int = setInterval(this._FdoScroll, this.speed);
	},
	_mouseOver: function(evt)
	{
		if (this.stopOnHover) this._mouseStop = true;
		var el = evt.srcElement || evt.toElement;
		if (el) 
		{
			try{el.style.color=this.colorOnHover;}catch(E){};
			try{if (this.underlineOnHover) el.style.textDecoration='underline';}catch(E){};
		}
	},
	_mouseOut: function(evt)
	{
		this._mouseStop = false;
		var el = evt.srcElement || evt.toElement;
		if (el) 
		{
			try{el.style.color=this.textColor;}catch(E){};
			try{el.style.textDecoration='none';}catch(E){};
		}
	}
}