﻿var ScrollList = new Array();
function RegisterScroll(sc){	
	var i;
	for(i = 0 ; i < ScrollList.length ; i++)
	{
		if(ScrollList[i] == null)ScrollList[i] = sc;
	}
	ScrollList.push(sc);
	return i;
}
function UnRegisterScroll(id){ScrollList[id] = null;}
function ScrollSwitch(id){ScrollList[id]._OnTick();}

function Scroll(id){
	this.Place = document.getElementById(id);
	this.Place.style.overflow='hidden';
	if(!this.Place.style.width)this.Place.style.width='100%';
	this.Place.style.direction='ltr';
	this.Id = RegisterScroll(this);
	this.Handle = null;
	this.Handle2 = null;
	
	this.Items = new Array();
	this.Wait = 5000;
	this.Delay = 5;
	this.Tr = null;
	this.Index = 0;	
	this.Step = 15;
	this.Mode = 1;
	this.Rev = false;	
}

Scroll.prototype = {
	_GetSize : function(e){
		var w = e.clientWidth;
		var h = e.clientHeight;
		return [w,h]
	},

	_SetPos : function(e,p){
		e.scrollLeft = p[0];
		e.scrollTop = p[1];
		if(e.scrollTo)e.scrollTo(p[0],p[1]);
	},
	_GetPos : function(e){
		var x = 0;
		var y = 0;
		x = e.scrollLeft;
		y = e.scrollTop;
		return [x,y]
	},

	
	_CheckIndex : function(index){	
		if(index >= this.Items.length)return 0;
		else if(index < 0)return this.Items.length - 1;
		else return index;
	},
	
	addItem : function(id){
		var e = document.getElementById(id);
		e.style.display = 'none';
		this.Items.push(e);
	},
	
	_OnTick : function(){
		this.Handle2 = null;
		var Pos = this._GetPos(this.Place);
		Pos[0] += (this.Mode * this.Step);
		var Size = this._GetSize(this.Place);
		var end;
		if(this.Mode > 0){
				if(!this.Rev)end = (Pos[0] >= (Size[0] * 2 - 10));
				else end = (Pos[0] >= Size[0]);
				if(end){
					this.Handle = null;
					if(!this.Rev){
						this.Index= this._CheckIndex(this.Index + this.Mode);
						var td1 = document.createElement("td");
						td1.style.width = "33.33%";
						td1.style.direction = 'rtl';
						td1.innerHTML = this.Items[this._CheckIndex(this.Index + this.Mode)].innerHTML;			
						this.Tr.removeChild(this.Tr.firstChild);
						this.Tr.appendChild(td1);
					}else this.Rev = false;
					this._SetPos(this.Place,[Size[0],Pos[1]]);
					this.Handle2 = setTimeout("ScrollSwitch(" + this.Id + ");",this.Wait);
					return;
				}
		}
		else{
				if(!this.Rev)end = (Pos[0] <= 0);
				else end = (Pos[0] <= Size[0]);
				if(end){
					this.Handle = null;
					if(!this.Rev){
						this.Index= this._CheckIndex(this.Index + this.Mode);
						var td1 = document.createElement("td");
						td1.style.width = "33.33%";
						td1.style.direction = 'rtl';
						td1.innerHTML = this.Items[this._CheckIndex(this.Index + this.Mode)].innerHTML;			
						this.Tr.removeChild(this.Tr.lastChild);
						this.Tr.insertBefore(td1,this.Tr.firstChild);
					}else this.Rev = false;
					this._SetPos(this.Place,[Size[0],Pos[1]]);
					this.Handle2 = setTimeout("ScrollSwitch(" + this.Id + ");",this.Wait);					
					return;
				}
		}
		this._SetPos(this.Place,Pos);
		this.Handle = setTimeout("ScrollSwitch(" + this.Id + ");",this.Delay);
	},
	
	LeftToRight : function(){
		if(this.Mode == 1){
			if(!this.Handle){
				if(this.Handle2)
					clearTimeout(this.Handle2);
				this.Handle2 = null;
				this._OnTick();
			}
		}else{
			this.Mode = 1;
			if(this.Handle)this.Rev = true;
			else{
				if(this.Handle2)clearTimeout(this.Handle2);
				this.Handle2 = null;
				this._OnTick();
			}
		}
	},	
	
	RightToLeft : function(){
		if(this.Mode == -1){
			if(!this.Handle){
				if(this.Handle2)
					clearTimeout(this.Handle2);
				this.Handle2 = null;
				this._OnTick();
			}
		}else{
			this.Mode = -1;
			if(this.Handle)this.Rev = true;
			else{
				if(this.Handle2)clearTimeout(this.Handle2);
				this.Handle2 = null;
				this._OnTick();
			}
		}
	},
	
	Start : function(){	
		var tbl1 = document.createElement("table");
		var tb1 = document.createElement("tbody");
		var tr1 = document.createElement("tr");
		tbl1.appendChild(tb1);
		tbl1.style.width = "300%";
		tbl1.cellPadding = 0;
		tbl1.cellSpacing = 0;
		tb1.appendChild(tr1);
		this.Place.appendChild(tbl1);
		this.Tr = tr1;
		var i,t;
		this.Index = 0;
		t = this._CheckIndex(this.Items.length - 1);
		for(i = 0 ; i < 3 ; i++){
			var td1 = document.createElement("td");
			td1.style.width = "33.33%";
			td1.style.direction = 'rtl';
			td1.innerHTML = this.Items[t].innerHTML;
			this.Tr.appendChild(td1,this.Tr.firstChild);
			t = this._CheckIndex(t + 1);
		}
		var s = this._GetSize(this.Place);
		this._SetPos(this.Place,[s[0],1]);
		this.Handle2 = setTimeout("ScrollSwitch(" + this.Id + ");",this.Wait);
	}
};