var scrollPanelPause = 10;	// nb de ms entre deux affichages lors du scroll
var scrollPanelDec = 2;		// nb de points décalés entre deux affichages

var scrollPanelList = new Array();
var scrollPanelInterval;

var DEFAULT_PANEL_SIZE = 4;
var DEFAULT_PANEL_STYLE = "r";

function Panel(id, basePath) {
	this.basePath = (basePath) ? (basePath) : "";

	this.content = document.createElement("SPAN");
	this.content.id = id;
	this.content.style.overflow = "hidden";
	this.content.style.cursor = "default";

	this.innerDiv = document.createElement("SPAN");
	this.innerDiv.style.width = "10000px";
	this.innerDiv.style.overflow = "visible";
	this.innerDiv.style.cursor = "default";
	this.innerDiv.style.textAlign = "left";

	this.content.style.display = "block";
	this.innerDiv.style.display = "block";
	this.content.style.position = "relative";

	this.content.appendChild(this.innerDiv);

	// --

	var table = document.createElement("TABLE");
	var tbody = document.createElement("TBODY");
	var tr = document.createElement("TR");

	table.cellSpacing = 0;
	table.cellPadding = 0;

	tbody.appendChild(tr);
	table.appendChild(tbody);
	this.innerDiv.appendChild(table);

	this.tr = tr;

	this.imgs = new Array();

	// --

	this.color = "FF0808";
	this.padColor = "858585";

	this.SetSizeAndStyle(DEFAULT_PANEL_SIZE, DEFAULT_PANEL_STYLE);

	this.autoScroll = false;
	this.scrollRun = false;
	this.displayType = "text";

	this.speed = 6;
}

Panel.prototype.SetSpeed = function(val) {
	this.speed = val;
}

Panel.prototype.ScrollStart = function() {
	if (this.scrollRun)
		return;

	this.scrollPause = false;
	this.autoScroll = false;
	this.scrollRun = true;
	this.Display(this.value);

	var self = this;

	this.scrollCount = this.speed;

	if (scrollPanelList.length == 0)
		scrollPanelInterval = setInterval(ScrollPanel, scrollPanelPause);

	scrollPanelList.push(this);
}

function ScrollPanel() {
	try {
		for (var i=0; i<scrollPanelList.length; i++)
			scrollPanelList[i].Scroll();
	}
	catch(exception) {}
}

Panel.prototype.ScrollStop = function() {
	if (!this.scrollRun)
		return;

	this.scrollPause = false;
	this.autoScroll = false;
	this.scrollRun = false;
	this.Display(this.value);
	this.content.scrollLeft = 0;

	if (scrollPanelList.length == 1) {
		clearInterval(scrollPanelInterval);
		scrollPanelList.length = 0;
	} else {
		for (var i =0; i<scrollPanelList.length; i++) {
			if (scrollPanelList[i] == this) {
				scrollPanelList[i] = scrollPanelList[scrollPanelList.length-1]
				scrollPanelList.length--;
				break;
			}
		}
	}
}

Panel.prototype.ScrollPause = function() {
	this.scrollPause = true;
}

Panel.prototype.ScrollResume = function() {
	this.scrollPause = false;
}

Panel.prototype.Scroll = function() {
	if (this.scrollPause)
		return;

	if (this.scrollCount == 0) {
		this.content.scrollLeft += this.size * scrollPanelDec;
		if (this.content.scrollLeft >= this.scrollMax)
			this.content.scrollLeft = 0;
	}

	--this.scrollCount;
	if (this.scrollCount < 0)
		this.scrollCount = this.speed-1;
}

Panel.prototype.AddTo = function(container) {
	this.container = container;
	container.appendChild(this.content);
}

Panel.prototype.RemoveFrom = function(container) {
	try {
		container.removeChild(this.content);
	} catch(exception) {}
	this.container = null;
}

Panel.prototype.RemoveFromCurrentContainer = function() {
	this.RemoveFrom(this.container);
}

Panel.prototype.SetUrlEnd = function() {
	this.end = "_" + this.size + "_" + this.style + ".ashx";
	this.end_pad = "_" + this.size + "_" + this.style + "_" + this.padColor + ".ashx";
}

Panel.prototype.SetColor = function(color) {
	if (color.length == 7)
		color = color.substr(1);

	this.color = color;
	this.RefreshDisplay();
}

Panel.prototype.SetPadColor = function(color) {
	if (color.length == 7)
		color = color.substr(1);

	this.padColor = color;
	this.SetUrlEnd();
}

Panel.prototype.SetWidth = function(width) {
	if (isNaN(width))
		return;

	this.width = width;
	this.content.style.width = width + "px";

	// pour forcer un refresh :
	this.todisplay = "";
	if (this.scrollRun)
		this.RefreshDisplay();

	return width;
}

Panel.prototype.VerticalAlign = function() {
	if (this.alignTop) {
		this.innerDiv.style.paddingTop = "0px";
		return;
	}

	var diff = this.height - (this.size * 9);
	if (isNaN(diff) || diff < 0)
		diff = 0;
	if (diff == 0) {
		this.innerDiv.style.paddingTop = "0px";
	} else {
		diff = Math.round((diff >> 1) / this.size) * this.size;
		this.innerDiv.style.paddingTop = diff + "px";
	}
}

Panel.prototype.SetHeight = function(height, alignTop) {
	if (isNaN(height))
		return;

	this.height = height;
	this.alignTop = alignTop;
	this.content.style.height = height + "px";

	this.VerticalAlign();

	return height;
}

Panel.prototype.SetSize = function(size) {
	this.size = (size > 1) ? size : 2;

	this.innerDiv.style.marginTop = size + "px";
	this.innerDiv.style.height = (size * 8) + "px";

	if (this.style)
		this.content.style.background = "url('" + this.basePath + "char/bk_" + this.size + "_" + this.style + ".ashx') repeat";

	this.SetUrlEnd();

	// pour forcer un refresh :
	this.todisplay = "";

	this.VerticalAlign();
}

Panel.prototype.SetStyle = function(style) {
	this.style = style;

	if (this.size)
		this.content.style.background = "url('" + this.basePath + "char/bk_" + this.size + "_" + this.style + ".ashx') repeat";

	this.SetUrlEnd();

	// pour forcer un refresh :
	this.todisplay = "";
}

Panel.prototype.SetSizeAndStyle = function(size, style) {
	this.size = (size > 1) ? size : 2;
	this.style = style;

	this.innerDiv.style.marginTop = size + "px";
	this.innerDiv.style.height = (size * 8) + "px";

	this.content.style.background = "url('" + this.basePath + "char/bk_" + this.size + "_" + this.style + ".ashx') repeat";

	this.SetUrlEnd();

	// pour forcer un refresh :
	this.todisplay = "";

	this.VerticalAlign();
}

Panel.prototype.RefreshDisplay = function() {
	this.Display(this.value);
}

Panel.prototype.Decal = function() {
	this.dec = this.size << 1;

	if (this.width > this.textWidth + 5) {
		switch(this.align) {
			case "center" :
				this.dec = Math.round(((this.width - this.textWidth) >> 1) / this.size) * this.size;
				break;
			case "right" :
				this.dec = Math.round((this.width - this.textWidth) / this.size) * this.size - this.size;
				break;
		}
	}

	this.innerDiv.style.paddingLeft = this.dec + "px";
}

Panel.prototype.SetAutoScroll = function(val) {
	this.autoScroll = val;
}

Panel.prototype.SetAlign = function(val) {
	if (this.displayType == "text") {
		this.align = val;

		switch(this.align) {
			case "center" :
			case "left" :
			case "right" :
				this.ScrollStop();
				break;
			case "rotate" :
				this.ScrollStart();
				break;
		}
	} else {
		this.align = "left";
		this.ScrollStop();
	}

	// pour forcer un refresh :
	this.todisplay = "";

	this.Decal();
}

Panel.prototype.TestAutoScroll = function() {
	if (this.autoScroll) {
		var width = this.GetRealTextWidth();
		var run = (width > this.width);
		if (run)
			this.ScrollStart();
		else
			this.ScrollStop();
		this.autoScroll = true;
		return this.scrollRun;
	}
	return false;
}

Panel.prototype.GetRealTextWidth = function(val) {
	if (!val) val = this.value;

	var result = 0;
	for (var n=0; n<val.length; n++) {
		var _char = charList[val.charAt(n)];
		if (!_char)	_char = charList[' '];
		result += _char[1] * this.size;
	}
	return result;
}

Panel.prototype.DisplayColors = function(value) {
	this.displayType = "graph";
	this.value = value;

	if (this.scrollRun)
		this.ScrollStop();
	this.textWidth = this.width;
	this.Decal();

	var imgs = this.imgs;

	var todisplay = value[0][0];
	var todisplay1 = value[0][1].value.substring(1);
	for (var i=1; i<value.length; i++) {
		todisplay += "-" + value[i][0];
		todisplay1 += "-" + value[i][1].value.substring(1);
	}

	if (imgs.length == 0) {
		var td = document.createElement("TD");
		td.vAlign = "top";

		var span = document.createElement("DIV");
		span.style.pointer = "default";

		if (gecko) {
			imgs[0] = document.createElement("IMG");
			imgs[0].ondragstart = function() {return false;};
			span.appendChild(imgs[0]);
		} else {
			imgs[0] = span;
		}
		td.appendChild(span);

		this.tr.appendChild(td);
	} else {
		imgs[0].style.display = "";
	}

	if ( (this.todisplay != todisplay) || (this.todisplay1 != todisplay1) ) {
		this.todisplay = todisplay;
		this.todisplay1 = todisplay1;

		var charUrl = this.basePath + "gchar/" + todisplay + "_" + todisplay1 + "_" + this.size + "_" + this.style + "_" + this.width + ".ashx";
		imgs[0].lastUrl = charUrl;

		if (gecko) {
			imgs[0].src = charUrl;
			imgs[0].parentNode.style.background = "transparent";

			imgs[0].style.width = null;
			imgs[0].style.height = null;
		} else {
			imgs[0].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + charUrl + "';sizingMethod='image');";
			imgs[0].style.background = "transparent";
		}
	}

	for (var n=1; n<imgs.length; n++)
		imgs[n].style.display = "none";
}

Panel.prototype.Display = function(value, nbDigits) {
	if (typeof(value) == "object") {
		this.DisplayColors(value);
		return;
	}
	this.displayType = "text";

	if (typeof(value) == "undefined")
		value = "-";
	else if (typeof(value) == "number")
		value += '';

	this.value = value;
	this.todisplay = "";

	var padNumber = 0;
	if (nbDigits) {
		// Reset end_pad if not set
		if ((typeof(this.end_pad) == "undefined") || (this.end_pad == null))
			this.SetPadColor("#858585");

		// Test if I should add padding
		padNumber = nbDigits - value.length;
	}

	this.TestAutoScroll();
	if (this.scrollRun) {
		var margin = this.width - this.GetRealTextWidth(value);
		var spaceNumber = (margin > 0) ? Math.round(margin/(4*this.size)) : 6;
		if (spaceNumber < 6)
			spaceNumber = 6;

		for (var i=0; i<spaceNumber; i++)
			value += " ";
		value += value;
	}

	var self = this;
	this.textWidth = 0;

	var imgs = this.imgs;

	var _char = charList['0'];
	var todisplay =_char[0];
	var charUrl = this.basePath + "char/" + todisplay + this.end_pad;
	var img_width = _char[1] * this.size;

	for (var n=0; n<padNumber; n++)	{
		if (n >= imgs.length) {
			var td = document.createElement("TD");
			td.vAlign = "top";

			var span = document.createElement("DIV");
			span.style.pointer = "default";

			if (gecko) {
				imgs[n] = document.createElement("IMG");
				span.appendChild(imgs[n]);
			} else {
				imgs[n] = span;
			}
			td.appendChild(span);

			this.tr.appendChild(td);
		} else {
			imgs[n].style.display = "";
		}

		if (imgs[n].lastUrl != charUrl) {
			if (gecko) {
				imgs[n].src = charUrl;
			} else {
				imgs[n].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + charUrl + "';sizingMethod='image');";
				imgs[n].style.height = (this.size*9) + "px";
			}

			imgs[n].style.width = img_width + "px";
			imgs[n].lastUrl = charUrl;
		}

		if (gecko)
			imgs[n].parentNode.style.background = "#" + this.color;
		else
			imgs[n].style.background = "#" + this.color;

		this.textWidth += img_width;
	}

	var imgCount = (padNumber > 0) ? padNumber : 0;
	for (var n=0; n<value.length; n++) {
		_char = charList[value.charAt(n)];
		if (!_char)	_char = charList[' '];
		todisplay = _char[0];
		img_width = _char[1] * this.size;

		if (imgCount >= imgs.length) {
			var td = document.createElement("TD");
			td.vAlign = "top";

			var span = document.createElement("DIV");
			span.style.pointer = "default";

			if (gecko) {
				imgs[imgCount] = document.createElement("IMG");
				imgs[imgCount].ondragstart = function() {return false;};
				span.appendChild(imgs[imgCount]);
			} else {
				imgs[imgCount] = span;
			}
			td.appendChild(span);

			this.tr.appendChild(td);
		} else {
			imgs[imgCount].style.display = "";
		}

		charUrl = this.basePath + "char/" + todisplay + this.end;

		if (imgs[imgCount].lastUrl != charUrl) {
			if (gecko) {
				imgs[imgCount].src = charUrl;
			} else {
				imgs[imgCount].style.visibility = "visible";
				imgs[imgCount].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + charUrl + "';sizingMethod='image');";
				imgs[imgCount].style.height = (this.size*9) + "px";
			}

			imgs[imgCount].style.width = img_width + "px";
			imgs[imgCount].lastUrl = charUrl;
		}

		if (gecko)
			imgs[imgCount].parentNode.style.background = "#" + this.color;
		else
			imgs[imgCount].style.background = "#" + this.color;

		this.textWidth += img_width;

		imgCount++;
	}

	for (var n=imgCount; n<imgs.length; n++)
		imgs[n].style.display = "none";


	this.scrollMax = this.textWidth >> 1;

	this.Decal();
}

Panel.prototype.DisplayNumber = Panel.prototype.Display;