// .................................... Header Tablo

var TABLO = new Object();
var TabloIteration = 1;
var timeShift = 0;
var index=0;
var index2=0;

function startTablo(freq, temp,davlen) {
    TABLO.St = new Array;
    TABLO.Unit = new Array;

	TABLO.St["-"] = new Array(260,8);
	TABLO.St["+"] = new Array(240,8);
	TABLO.St[":"] = new Array(220,2);
	TABLO.St["*"] = new Array(200,26);
	TABLO.St["%"] = new Array(281,15);
	TABLO.St["@"] = new Array(301,125);
	TABLO.St["u"] = new Array(320,20);
	TABLO.St["y"] = new Array(341,20);
	for(var i=0; i<10; i++) TABLO.St[i] = new Array(i*20,11);

	var bitch=document.createElement('div');
	bitch.className="tablo-unit";
	bitch.style.display = "none";
	bitch.id="tablo_unit";

//	for (var t=4;t>=0;t--){
	for (var t=0;t<=4;t++){
	    TABLO.Unit[t] = bitch.cloneNode(true);
		document.getElementById('tablo').appendChild(TABLO.Unit[t]);
	}
	TABLO.Text = temp;
	
	TABLO.Dav = davlen;
	

	/*if (typeof(Ajax)!='undefined') {
		var myAjax = new Ajax.Request(
			"/servertime/", 
			{
				method: "get",
				onComplete: function (originalRequest) {
					if (originalRequest.responseText) {
						var dNow = new Date();
						var shift = originalRequest.responseText*1000 - dNow.getTime();
						if (!isNaN(shift)) {
							// Difference in mseconds between Novosibirsk time (GMT+6) and local browser time
							//var GMToffset = (-6*60 - dNow.getTimezoneOffset()) * 60 * 1000;
							//timeShift = shift + GMToffset;
							timeShift = shift;
						}
					}
				}
		});
	}
*/
	setInterval("updateTablo()", freq*1000);
}

function updateTablo()
{
	var dNow = new Date();
	dNow.setTime(dNow.getTime()+timeShift);
	var dNowM = dNow.getMinutes(); 
	var dNowH = dNow.getHours(); 
	if(dNowM<10) dNowM = "0" + dNowM;
	if(dNowH<10) dNowH = "0" + dNowH;
	if(TABLO.Time != dNowH+":"+ dNowM) TABLO.Time = dNowH + ":" + dNowM;

    TabloIteration = TabloIteration*(-1);
    
	var t_b=document.getElementById('tablo_unit');

	t_b.style.left='39px';

	if (TabloIteration > 0 && TABLO.Text) {
		if(index==0){
			if(index2==0){
				var strToDisplay = TABLO.Dav;
				index2=1;
			  t_b.style.left='39px';
			}else{
			  t_b.style.left='9px';			
					
				var strToDisplay = "@";
				index2=0;
				 
			}
			index=1;
		}else{
			var strToDisplay = TABLO.Text;
			index=0;
		}
		
		
	}else 
		var strToDisplay = TABLO.Time;

	for(var t=0; t<strToDisplay.length; t++) {
		TABLO.Unit[t].style.backgroundPosition = "0px -" + TABLO.St[strToDisplay.substr(t,1)][0] + "px";
		TABLO.Unit[t].style.width = TABLO.St[strToDisplay.substr(t,1)][1] + "px";
		TABLO.Unit[t].style.display = "inline";
	}

	for(var t=strToDisplay.length; t<5; t++) 
		TABLO.Unit[t].style.display = "none";

	strToDisplay = dNow = dNowH = dNowM = null;
}


function startTablo2(freq, temp,davlen) {
    TABLO.St = new Array;
    TABLO.Unit = new Array;

	TABLO.St["-"] = new Array(260,8);
	TABLO.St["+"] = new Array(240,8);
	TABLO.St[":"] = new Array(220,2);
	TABLO.St["*"] = new Array(200,26);
	TABLO.St["%"] = new Array(281,15);
	TABLO.St["@"] = new Array(301,125);
	TABLO.St["n"] = new Array(321,125);
	TABLO.St["y"] = new Array(341,125);
	for(var i=0; i<10; i++) TABLO.St[i] = new Array(i*20,11);

	var bitch=document.createElement('div');
	bitch.className="tablo-unit";
	bitch.style.display = "none";
	bitch.id="tablo_unit";

//	for (var t=4;t>=0;t--){
	for (var t=0;t<=4;t++){
	    TABLO.Unit[t] = bitch.cloneNode(true);
		document.getElementById('tablo').appendChild(TABLO.Unit[t]);
	}
	TABLO.Text = temp;
	
	TABLO.Dav = davlen;
	

	if (typeof(Ajax)!='undefined') {
		var myAjax = new Ajax.Request(
			"/servertime/", 
			{
				method: "get",
				onComplete: function (originalRequest) {
					if (originalRequest.responseText) {
						var dNow = new Date();
						var shift = originalRequest.responseText*1000 - dNow.getTime();
						if (!isNaN(shift)) {
							// Difference in mseconds between Novosibirsk time (GMT+6) and local browser time
							//var GMToffset = (-6*60 - dNow.getTimezoneOffset()) * 60 * 1000;
							//timeShift = shift + GMToffset;
							timeShift = shift;
						}
					}
				}
		});
	}

	setInterval("updateTablo2()", freq*1000);
}

function updateTablo2()
{
	var dNow = new Date();
	dNow.setTime(dNow.getTime()+timeShift);
	var dNowM = dNow.getMinutes(); 
	var dNowH = dNow.getHours(); 
	if(dNowM<10) dNowM = "0" + dNowM;
	if(dNowH<10) dNowH = "0" + dNowH;
	if(TABLO.Time != dNowH+":"+ dNowM) TABLO.Time = dNowH + ":" + dNowM;

    TabloIteration = TabloIteration*(-1);
    
	var t_b=document.getElementById('tablo_unit');

	t_b.style.left='39px';
    //var index=0;
    
	if (TabloIteration > 0 && TABLO.Text) {
			  t_b.style.left='9px';			
			  if(index==0){
				var strToDisplay = "@";
				//index=1;
    		  //}else{
				//var strToDisplay = "n";
				index=0;
    		  }
	}else{ 
	    //if(index2==0){
		//var strToDisplay = "y";
		//index2=1;
		//}else{
		var strToDisplay =TABLO.Time;
		index2=0;
		//}
	}

	for(var t=0; t<strToDisplay.length; t++) {
		TABLO.Unit[t].style.backgroundPosition = "0px -" + TABLO.St[strToDisplay.substr(t,1)][0] + "px";
		TABLO.Unit[t].style.width = TABLO.St[strToDisplay.substr(t,1)][1] + "px";
		TABLO.Unit[t].style.display = "inline";
	}

	for(var t=strToDisplay.length; t<5; t++) 
		TABLO.Unit[t].style.display = "none";

	strToDisplay = dNow = dNowH = dNowM = null;
}


function startTablo3(freq, temp,davlen) {
    TABLO.St = new Array;
    TABLO.Unit = new Array;

	TABLO.St["-"] = new Array(260,8);
	TABLO.St["+"] = new Array(240,8);
	TABLO.St[":"] = new Array(220,2);
	TABLO.St["*"] = new Array(200,26);
	TABLO.St["%"] = new Array(281,15);
	TABLO.St["@"] = new Array(301,125);
	TABLO.St["n"] = new Array(321,125);
	TABLO.St["y"] = new Array(341,125);
	for(var i=0; i<10; i++) TABLO.St[i] = new Array(i*20,11);

	var bitch=document.createElement('div');
	bitch.className="tablo-unit";
	bitch.style.display = "none";
	bitch.id="tablo_unit";

//	for (var t=4;t>=0;t--){
	for (var t=0;t<=4;t++){
	    TABLO.Unit[t] = bitch.cloneNode(true);
		document.getElementById('tablo').appendChild(TABLO.Unit[t]);
	}
	TABLO.Text = temp;
	
	TABLO.Dav = davlen;
	

	if (typeof(Ajax)!='undefined') {
		var myAjax = new Ajax.Request(
			"/servertime/", 
			{
				method: "get",
				onComplete: function (originalRequest) {
					if (originalRequest.responseText) {
						var dNow = new Date();
						var shift = originalRequest.responseText*1000 - dNow.getTime();
						if (!isNaN(shift)) {
							// Difference in mseconds between Novosibirsk time (GMT+6) and local browser time
							//var GMToffset = (-6*60 - dNow.getTimezoneOffset()) * 60 * 1000;
							//timeShift = shift + GMToffset;
							timeShift = shift;
						}
					}
				}
		});
	}

	setInterval("updateTablo3()", freq*1000);
}

function updateTablo3()
{
	var dNow = new Date();
	dNow.setTime(dNow.getTime()+timeShift);
	var dNowM = dNow.getMinutes(); 
	var dNowH = dNow.getHours(); 
	if(dNowM<10) dNowM = "0" + dNowM;
	if(dNowH<10) dNowH = "0" + dNowH;
	if(TABLO.Time != dNowH+":"+ dNowM) TABLO.Time = dNowH + ":" + dNowM;

    TabloIteration = TabloIteration*(-1);
    
	var t_b=document.getElementById('tablo_unit');

	t_b.style.left='39px';
    //var index=0;
    
	if (TabloIteration > 0 && TABLO.Text) {
			  t_b.style.left='9px';			
			  if(index==0){
				var strToDisplay = "@";
				index=1;
    		  }else{
				var strToDisplay = "n";
				index=0;
    		  }
	}else{ 
	    if(index2==0){
		var strToDisplay = "y";
		index2=1;
		}else{
		var strToDisplay =TABLO.Time;
		index2=0;
		}
	}

	for(var t=0; t<strToDisplay.length; t++) {
		TABLO.Unit[t].style.backgroundPosition = "0px -" + TABLO.St[strToDisplay.substr(t,1)][0] + "px";
		TABLO.Unit[t].style.width = TABLO.St[strToDisplay.substr(t,1)][1] + "px";
		TABLO.Unit[t].style.display = "inline";
	}

	for(var t=strToDisplay.length; t<5; t++) 
		TABLO.Unit[t].style.display = "none";

	strToDisplay = dNow = dNowH = dNowM = null;
}
