//-----------------------------------------------------------
// MyList Cookie Function End
//-----------------------------------------------------------
//-----------------------------------------------------------
// TickerComponent Start
//-----------------------------------------------------------
function Inst(id, nm, fnm, isin, ch, chp, lsp,
			  incBg, fallBg, unchBg, 
			  incImg, fallImg, unchImg,
			  fontFace, fontSize) {
	this.id = id;
	this.nm = nm;
	this.fnm = fnm;
	this.isin = isin;
	this.ch = ch;
	this.chp = chp;
	this.lsp = lsp;
	this.attributes = [id, nm, fnm, isin, ch, chp, lsp];
	this.incBg = isDefault(incBg, "0000FF");
	this.fallBg = isDefault(fallBg, "FF0000");
	this.unchBg = isDefault(unchBg, "000000");
	this.incImg = incImg;
	this.fallImg = fallImg;
	this.unchImg = unchImg;
	this.fontFace = isDefault(fontFace, "Verdana");
	this.fontSize = isDefault(fontSize, 2);
}

Inst.prototype.getImage = function() {
	if (this.chp > 0) return this.incImg;
	else if (this.chp < 0) return this.fallImg;
	else return this.unchImg;
}

Inst.prototype.getColor = function() {
	if (this.chp > 0) return this.incBg;
	else if (this.chp < 0) return this.fallBg;
	else return this.unchBg;
}

Inst.prototype.getId = function() {return this.id;}
Inst.prototype.getNM = function() {return this.nm;}
Inst.prototype.getFNM = function() {return this.fnm;}
Inst.prototype.getISIN = function() {return this.isin;}
Inst.prototype.getCH = function() {return this.ch;}
Inst.prototype.getCHP = function() {return this.chp;}
Inst.prototype.getLSP = function() {return this.lsp;}
Inst.prototype.getIncBg = function() {return this.incBg;}
Inst.prototype.getFallBg = function() {return this.fallBg;}
Inst.prototype.getUnchBg = function() {return this.unchBg;}
Inst.prototype.getIncImg = function() {return this.incImg;}
Inst.prototype.getFallImg = function() {return this.fallImg;}
Inst.prototype.getUnchImg = function() {return this.unchImg;}
Inst.prototype.getFontFace = function() {return this.fontFace;}
Inst.prototype.getFontSize = function() {return this.fontFace;}

function Ticker(id, width, height, bgColor, sleep, steps, separator) {
	this.ref = document.getElementById(id);
	this.width = width = isDefault(width, 710);
	this.height = height = isDefault(height, 20);
	clip(this.ref, 0, width, height, 0);
	this.ref.style.backgroundColor = "#" + isDefault(bgColor, "FFFFFF");
	this.left = this.ref.offsetLeft;
	this.top = this.ref.offsetTop;
	this.width = this.ref.offsetWidth;
	this.sleep = isDefault(sleep, 40) // thread sleep, ms
	this.steps = isDefault(steps, 1); // xpos steps for child layers
	this.separator = separator;
	this.lastOut = null;// reference to the last layer that went out of bounds a container rect perspective(xpos)
	this.timer = null;
	this.block = false;
	this.containerIsLoaded = false;	
	this.contentContainers = null;// array for container div elements
	this.calculateBugg = false;
}

Ticker.prototype.tick = function() {
	if (!this.block) {
		var ctx = this;
		this.timer = window.setTimeout(
			function() {
				var length = ctx.contentContainers.length;
				for (var i = 0; i < length; i++) {
					var con = ctx.contentContainers[i];
					var x = con.offsetLeft;
					con.style.left = x - ctx.steps + "px";
					if (!ctx.calculateBugg) {
						if (con.offsetLeft + (con.offsetWidth / 2) <= ctx.left) {
							if (i == length -1) {
								var tmp = ctx.contentContainers[i-1];
								var x = tmp.offsetLeft + tmp.offsetWidth;
								con.style.left = x + "px";				
								ctx.calculateBugg = true;
							}
						}
					}
					if (con.offsetLeft + con.offsetWidth <= ctx.left) {						
						var lastOut;
						if (ctx.lastOut != null) {// find the last elm that was out of bounds or just take the last one from our list
							lastOut = ctx.lastOut;
						} else {
							lastOut = ctx.contentContainers[length -1];
						}
						x = lastOut.offsetLeft + lastOut.offsetWidth;// calculate the x-pos
						con.style.left = x - ctx.steps + "px";// move it beyond the last one
						ctx.lastOut = con;// store this item for next out of bounds.					
					}
				}
				ctx.tick.apply(ctx, [""]);
			}, ctx.sleep
		);
	}
}

Ticker.prototype.load = function(items) {
	if (!this.containerIsLoaded) {
		var ctx = this;
		window.setTimeout(function() {ctx.load.apply(ctx, [items]);},1000);
	} else {
		if (this.timer != null) {
			window.clearTimeout(this.timer);		
		}		
		this.contentContainers = this.ref.getElementsByTagName("div");
		var clength = this.contentContainers.length;
		var length = items.length;
		var childs = new Array();
		for (var i = 0; i < clength; i++) {
			childs[i] = new Array();			
			for (var j = 0; j < length; j++) {				
				childs[i][j] = getTable(this, items[j]);
			}
		}
		for (var i = 0; i < clength; i++) {
			var con = this.contentContainers[i];
			var table = createElement("table");
			initTable(table)
			var tr = table.insertRow(table.rows.length);
			disableSelectHandler(tr, this);
			for (var j = 0; j < length; j++) {
				var td = getTD(tr);
				td.appendChild(childs[i][j]);
			}
			con.appendChild(table);
			con.style.left = "0px";
			con.style.top = "0px";			
		}
		this.tick();
	}
}

function getTable(ticker, inst) {
	var color = inst.getColor();
	var face = inst.fontFace;
	var size = inst.fontSize;
	var table = createElement("table");
	initTable(table);	
	var tr = table.insertRow(table.rows.length);
	var nobr = createElement("nobr");
	nobr.appendChild(getFont(face, size, inst.fnm + " ", "#000000"));
//	var chp = inst.chp > 0 ? "+" + inst.chp : inst.chp;
	var chp = inst.chp > 0 ? "- " + inst.chp : inst.chp;
//	var info = inst.nm + " " + chp + " ";
	var info = inst.nm + chp + " ";
	nobr.appendChild(getFont(face, size, info, color));
	nobr.appendChild(getImage(inst.getImage()));
	nobr.appendChild(getFont(face, size, "  ", color));
	var td = getTD(tr);
	disableSelectHandler(td, ticker);
	addMouseHandler(td, ticker);
	td.appendChild(nobr);	
	td = getTD(tr);
	disableSelectHandler(td, ticker);
	var sep = "";// this was the only way i found working..
	for (var i = 0; i < ticker.separator; i++) {
		sep += "&nbsp;";
	}
	td.innerHTML = sep;
	return table;
}

function initTable(table) {
	table.cellpadding = "0";
	table.cellspacing = "0";
	table.border = "0";
	table.margin = "0";
}

function getTD(tr) {
	var td = tr.insertCell(tr.cells.length);
	td.valign = "top";	
	return td;
}

function addMouseHandler(elm, ticker) {
	elm.ticker = ticker;
	elm.onmouseover = function() {
		this.ticker.block = true;
		if (this.ticker.timer != null) {
			window.clearTimeout(this.ticker.timer);
			this.ticker.timer = null;
		}
	}
	elm.onmouseout = function() {
		this.ticker.block = false;
		this.ticker.tick();
	}
	elm.style.cursor = "pointer";
}

function disableSelectHandler(elm) {
	if (typeof elm.onselectstart != "undefined") {
		elm.onselectstart = function() {return false};
	} else if (typeof elm.style.MozUserSelect!= "undefined") {
		elm.style.MozUserSelect = "none";
	} else {
		elm.onmousedown = function() {return false};
	}
	elm.style.cursor = "default";	
}

function getImage(src) {
	var img = createElement("img");
	img.src = src;
	return img;
}

function getFont(face, size, tx, color) {
	var font = createElement("font");
	font.style.font = size + "px " + face;
	font.color = "#" + color;
	font.appendChild(document.createTextNode(tx));
	return font;
}

function createElement(tag) {
	return document.createElement(tag);
}

function clip(layer, t, r, b, l) {
	layer.style.clip = "rect("+t+" "+r+" "+b+" "+l+")";
	layer.style.width = r + "px";
	layer.style.height = b + "px";
}

function isDefault(param, value) {
	if (param == null || param == "Undefined") {
		return value;
	}
	return param;
}

function isLoaded(ref) {
	if (typeof ref != "undefined") {
		ref.containerIsLoaded = true;
	} else {
		window.setTimeout("isLoaded(" + ref + ")", 1000);
	}	
}
//-----------------------------------------------------------
// TickerComponent End
//-----------------------------------------------------------