if (window.offscreenBuffering)
  window.offscreenBuffering = true;

var deltaX = 2;
var xspeed = 20;
var deltaY = 1;
var yspeed = 50;

var rows = 8;
var spacing = 2;
var startXPos = 4;
var startYPos = 0;

//if (navigator.userAgent.indexOf("Firefox")!=-1)
//  startYPos = -8;

var LEDonColor = '#FAF875';

var alphabet = new Array(59);
var messageArray;

var time, ticksTime;

function LEDSetup()
{
	alphabet[0] = new Array("00000100","00101010","00101010","00101010","00011110"); //a ok
	alphabet[1] = new Array("11111110","00100010","00100010","00100010","00011100"); //b ok
	alphabet[2] = new Array("00011100","00100010","00100010","00100010","00100010"); //c ok
	alphabet[3] = new Array("00011100","00100010","00100010","00100010","11111110"); //d ok
	alphabet[4] = new Array("00011100","00101010","00101010","00101010","00011000"); //e ok
	alphabet[5] = new Array("00010000","01111110","10010000","10010000","01000000"); //f ok
	alphabet[6] = new Array("00011000","00100101","00100101","00100101","00111110"); //g ok
	alphabet[7] = new Array("11111110","00010000","00100000","00100000","00011110"); //h ok
	alphabet[8] = new Array("00000000","00100010","10111110","00000010","00000000"); //i ok
	alphabet[9] = new Array("00000010","00000001","00000001","10111110","00000000"); //j ok
	alphabet[10] = new Array("11111110","00010000","00101000","01000100","00000010"); //k ok
	alphabet[11] = new Array("00000000","11111110","00000010","00000000","00000000"); //l ok
	alphabet[12] = new Array("00111110","00100000","00011000","00100000","00011110"); //m ok
	alphabet[13] = new Array("00111110","00010000","00100000","00100000","00011110"); //n ok
	alphabet[14] = new Array("00011100","00100010","00100010","00100010","00011100"); //o ok
	alphabet[15] = new Array("00111111","00100100","00100100","00100100","00011000"); //p ok
	alphabet[17] = new Array("00111110","00010000","00100000","00100000","00010000"); //r ok
	alphabet[18] = new Array("00010010","00101010","00101010","00101010","00000100"); //s ok
	alphabet[19] = new Array("00100000","11111100","00100010","00000010","00000100"); //t ok
	alphabet[20] = new Array("00111100","00000010","00000010","00000100","00111110"); //u ok
	alphabet[21] = new Array("00111000","00000100","00000010","00000100","00111000"); //v ok
	alphabet[26] = new Array("00000100","00101010","10101010","00101010","00011110"); //å ok
	alphabet[28] = new Array("00011100","10100010","00100010","10100010","00011100"); //ö ok

	alphabet[30] = new Array("11111110","01000000","00100000","01000000","11111110"); // M
	alphabet[31] = new Array("10000000","10000000","11111110","10000000","10000000"); // T
	alphabet[32] = new Array("01111100","10000010","10000010","10000010","01111100"); // O
	alphabet[33] = new Array("11111110","10010000","10010000","10000000","10000000"); // F
	alphabet[34] = new Array("11111110","00000010","00000010","00000010","00000010"); // L
	alphabet[35] = new Array("01100100","10010010","10010010","10010010","01001100"); // S

	alphabet[36] = new Array("01111100","10001010","10010010","10100010","01111100"); //0
	alphabet[37] = new Array("00000000","01000010","11111110","00000010","00000000"); //1
	alphabet[38] = new Array("01100010","10000110","10001010","10010010","01100010"); //2
	alphabet[39] = new Array("01000100","10000010","10010010","10010010","01101100"); //3
	alphabet[40] = new Array("00011000","00101000","01001000","11111110","00001000"); //4
	alphabet[41] = new Array("11110100","10010010","10010010","10010010","10001100"); //5
	alphabet[42] = new Array("01111100","10010010","10010010","10010010","01001100"); //6
	alphabet[43] = new Array("10000000","10000000","10001110","10010000","11100000"); //7
	alphabet[44] = new Array("01101100","10010010","10010010","10010010","01101100"); //8
	alphabet[45] = new Array("01100100","10010010","10010010","10010010","01111100"); //9

	alphabet[46] = new Array("00000110","00000110","00000000","00000000","00000000"); //.
	alphabet[47] = new Array("00000000","00000000","00100100","00000000","00000000"); //:

	alphabet[58] = new Array("00000000","00000000","00000000","00000000","00000000"); //space

	v = 1;
	while (v <= 2)
	{
		messageArray = new Array();

		if (v == 1)
		{
			msg = msgDate;
			//dateLayer = new getObj("dateLayer");
		}
		else
		{
			msg = msgTime;
			//timeLayer = new getObj("timeLayer");
		}

		columnCount=0;

		for (i = 0; i < msg.length; i++)
		{
			letterNum = msg.charCodeAt(i);

			if ((letterNum>=97) && (letterNum<=122))
				temp=letterNum-97; //letters

			else if (letterNum==46) temp=46;      //.

			else if (letterNum==58) temp=47;      //:

			else if (letterNum==32) temp=58;      //space

			else if (letterNum==33) temp=28;      //!

			else if ((letterNum>=48) && (letterNum<=57))
				temp=letterNum-12; //[0-9]

			else if (letterNum==63) temp=43;      //?

			else if (letterNum==44) temp=38;      //,

			else if (letterNum==64) temp=39;      //pause

			else if (letterNum==35) temp=40;      //flash effect

			else if (letterNum==94) temp=41;      //scroll up effect

			else if (letterNum==95) temp=42;      //sunrise effect

			else if (letterNum==36) temp=44;      //split effect

			else if (letterNum==229) temp=26;      //å

			else if (letterNum==246) temp=28;      //ö

			else if (letterNum==77) temp=30;      //M

			else if (letterNum==84) temp=31;      //T

			else if (letterNum==79) temp=32;      //O

			else if (letterNum==70) temp=33;      //F

			else if (letterNum==76) temp=34;      //L

			else if (letterNum==83) temp=35;      //S

			else continue;

			for (ii=0; ii < alphabet[temp].length; ii++)
			{
				messageArray[columnCount] = alphabet[temp][ii];
				columnCount++;
			}

			messageArray[columnCount] = "0000000";
			columnCount++;
		}

		str = '';

		for (var q = 0; q < messageArray.length; q++)
		{
			aCol = messageArray[q];
			for (var x = 0; x < rows; x++)
			{
				if (aCol.charAt(x) == "1")
				{
					idstr = "";
					if (v == 2 && q == 50) // vilken kolumn i hela raden av ledkolumner
					{
						if (x == 2) // vilken rad i kolumnen finns pricken i kolonet på
							idstr = "id='colonUp'";
						else if (x == 5)
							idstr = "id='colonDown'";
					}

					str += '<div ' + idstr + ' style="position:absolute; left:' + (startXPos+q*spacing) + 'px; top:' + (startYPos+x*spacing) + 'px; background-color:' + LEDonColor +'; height:1px; width:1px; font-size:1px; overflow:hidden"></div>';
				}
			}
		}

		lyr = v == 1 ? new getObj("dateLayer"): new getObj("timeLayer");
		
		if (!(document.all))
		{
			//lyr.style.top = (parseInt(lyr.style.top) - 8) + 'px';
			//lyr.style.height = (parseInt(lyr.style.height) - 8) + 'px';
		}

		lyr.obj.innerHTML = str;
		lyr.style.clip = 'rect(' + lyr.style.height + ', auto, auto, auto)';
		lyr.startLeft = lyr.style.left;
		lyr.startTop = lyr.style.top;
		v++;
	}

	BeginScroll();
	//jsDate();
}

function BeginScroll()
{
	lyr = new getObj("dateLayer");
	var top = parseInt(lyr.style.top);
	var clipTop = parseInt(lyr.style.height);
	var fc = YScroll(lyr, top, clipTop);
	setTimeout(fc, 0);
}

function YScroll(lyr, top, clipTop)
{
	return (function()
	{
		top += deltaY;
		clipTop -= deltaY;

		lyr.style.clip = 'rect(' + clipTop + ', auto, auto, auto)';
		lyr.style.top = top + 'px';

		// If we have reached 0px (downwards from -13px), then we stop scrolling
		if (top == 0)
		{
			if (time)
				clearTimeout(time);

			if (lyr.id == "dateLayer")
			{
				fc = RestoreAndClipY(lyr);
				setTimeout(fc, 3000);
			}
			else if (lyr.id == "timeLayer")
			{
				setTimeout("Tick(0)", 500);
			}

			return;
		}

		fc = YScroll(lyr, top, clipTop);
  	time = setTimeout(fc, yspeed);
	});
}

function Tick(ticks)
{
	colonU = new getObj("colonUp");
	colonD = new getObj("colonDown");

	if (ticks % 2 == 0)
	{
		colonU.style.visibility = 'hidden';
		colonD.style.visibility = 'hidden';
	}
	else
	{
		colonU.style.visibility = 'visible';
		colonD.style.visibility = 'visible';
	}

	ticks++;

	if (ticks == 15)
	{
		lyr = new getObj("timeLayer");
		var left = parseInt(lyr.style.left);
		var clipLeft = 0;
		fc = XScroll(lyr, left, clipLeft);
		setTimeout(fc, 0);
	}


	ticksTime = setTimeout("Tick(" + ticks + ")", 500);
}

function RestoreAndClipY(lyr)
{
	return (function()
	{
		lyr.style.clip = 'rect(' + lyr.style.height + ', auto, auto, auto)';
		lyr.style.top = lyr.startTop;

		lyr2 = new getObj("timeLayer");

		var top = parseInt(lyr2.style.top);
		var clipTop = parseInt(lyr2.style.height);

		fc = YScroll(lyr2, top, clipTop);
		setTimeout(fc, 0);
	});
}

function XScroll(lyr, left, clipLeft)
{
	return (function()
	{
		left -= deltaX;
		clipLeft += deltaX;

		lyr.style.clip = 'rect(auto, auto, auto, ' + clipLeft + ')';
		lyr.style.left = left + 'px';

		// If clipLeft has increased it's clip to equal its width then we stop scrolling
		if (clipLeft >= parseInt(lyr.style.width))
		{
			if (time)
				clearTimeout(time);

			if (ticksTime)
			{
				colonU = new getObj("colonUp");
				colonD = new getObj("colonDown");

				colonU.style.visibility = 'visible';
				colonD.style.visibility = 'visible';

				clearTimeout(ticksTime);
			}

			setTimeout("ReadNewDate()", 0);
			return;
		}

	fc = XScroll(lyr, left, clipLeft);
  	time = setTimeout(fc, xspeed);
	});
}

function ReadNewDate()
{
	//TODO: Here we need to get the new date from the server...
	//And without reposting, we need to keep track of the time using the client's clock

	lyr = new getObj("timeLayer");
	fc = RestoreAndClipX(lyr);
	setTimeout(fc, 0);
}

function RestoreAndClipX(lyr)
{
	return (function()
	{
		lyr.style.clip = 'rect(' + lyr.style.height + ', auto, auto, auto)';
		lyr.style.left = lyr.startLeft;
		lyr.style.top = lyr.startTop;

		lyr2 = new getObj("dateLayer");

		var top = parseInt(lyr2.style.top);
		var clipTop = parseInt(lyr2.style.height);

		fc = YScroll(lyr2, top, clipTop);
		setTimeout(fc, 0);
	});
}

function getObj(name)
{
  if (document.getElementById)
  {
    this.obj = document.getElementById(name);
    this.style = document.getElementById(name).style;
    this.startLeft = "0px";
    this.startTop = "-15px";
    this.id = name;
  }
  else if (document.all)
  {
    this.obj = document.all[name];
    this.style = document.all[name].style;
    this.startLeft = "0px";
    this.startTop = "-15px";
    this.id = name;
  }
  else if (document.layers)
  {
    this.obj = document.layers[name];
    this.style = document.layers[name];
    this.startLeft = "0px";
    this.startTop = "-15px";
    this.id = name;
  }
}

function jsDate()
{
	var now = new Date();
	var day = now.getDay();
	var month = now.getMonth();
	var year = now.getYear();
	var date = now.getDate();
	var hour = now.getHours();
	var minutes = now.getMinutes();
	var secs = now.getSeconds();

	if (year < 2000)
	{
		year += 1900;
	}

	if (hour < 10)
	{
		hour = "0" + hour;
	}

	if (minutes < 10)
	{
		minutes = "0" + minutes;
	}

	if (secs < 10)
	{
		secs = "0" + secs;
	}

	obj = new getObj("date");
	obj.obj.innerHTML = hour + ":" + minutes + ":" + secs;

	setTimeout("jsDate()", 1000);
}
