// Driver for Countdown Auctions

var cddebug = 0;


function echoD(what) {
	if (cddebug == 1) 
		if (!confirm(what + " ### (Weiter debuggen?)"))
			cddebug = 0;
}



var cdInit = 0;
var cdReq = 0;
var cdCnt = 0;
var cdLCnt = 0;

var diffTS = 0;

var cAId = new Array();
var cAL = new Object();
cAL.length = 0;
var cAD = new Object();

var buyAck = 100;

var lastFinal = "";


function cdLoader() {
//	if (cdReq == 0 && (cdInit == 0 || cdLCnt == 0)) {
	if (cdReq == 0 && cdLCnt == 0) {
		msg = "";
		for (iB = 0; iB < cAL.length; iB++)
			msg += cAId[iB]; 
		msg = "init" + (iB<10 ? "0"+iB : iB) + msg;
		cdReq = 1;
echoD("ch=ajapi&c=" + msg);
		sendRequest("/api/call.php", processResponse, "ch=ajapi&c=" + msg);
	}
	cdLCnt++;
	if (cdLCnt >= 5) cdLCnt = 0;
}


function setCdTimes() {

	

	for (iA = 0; iA < cAId.length; iA++) {
		id = cAId[iA];

		cAD = cAL[id];

		sta = cAD['status'];

if (sta != 'wait') 
	echoD(id + ": " + sta);

		switch (cAD['status']) {

			case "active":
				ts = tL[id] - Math.round((new Date()).getTime() / 1000);
				if (ts > 0) {
					day = Math.floor(ts / 86400);
					ts -= day * 86400;
					hour = Math.floor(ts / 3600);
					ts -= hour * 3600;
					min = Math.floor(ts / 60);
					ts -= min * 60;
					timer = (day > 0) ? day + "T" : "";
					timer += hour < 10 ? "0" + hour : hour;
					timer += ":";
					timer += min < 10 ? "0" + min : min;
					timer += ":";
					timer += ts < 10 ? "0" + ts : ts;
					document.getElementById("t" + id).innerHTML = timer;
				}
			break;
			
			case "finished":
//alert("POSI");
				document.getElementById("t" + id).innerHTML = "Beendet";
				endCountdown();
			break;
		
			case "final":
				document.getElementById("t" + id).innerHTML = "Finale";
			break;
		
		}

	}
	cdCnt++;
	if (cdCnt >= 2) cdCnt = 0;
}


function processResponse(req) {
	cdReq = 0;
	txt = req.responseText;
echoD(txt);
	cmds = txt.split("\n");
	for (j1 = 0; j1 < cmds.length; j1++) {
		txt = cmds[j1];
//echoD(j1 + ": " + txt);
		cmd = txt.substr(0, 4);
		switch (cmd) {
			case "clie":
				if (txt.indexOf("__err") > 0) {
					err = txt.substr(10); 
					if (err == "nocredit") top.location.href='/?ch=payment';
				} else {
					dd = (txt.substr(4)).split("#");
//echoD(txt);
					setCreditAmount(parseInt(dd[0]));
// nicht unterstützt			setCreditBids(parseInt(dd[1]));
					if (dd[2] == detailId) {
						lABidAmo = parseInt(dd[3]);
						lABidNum = parseInt(dd[4]);
						lMBidNum = parseInt(dd[5]);
						lMBidAmo = lMBidNum * 50;
						setCalculation();
					}
				}
			break;
			case "init":
			case "upda":
				ts = Math.round((new Date()).getTime() / 1000);
				aucts = (txt.substr(4)).split("id");
				for (i1 = 0; i1 < aucts.length; i1++) 
					if (i1 == 0) 
				{
					diffTS = aucts[i1] - ts;
				} else {
					a_d = aucts[i1].split("#");

					id = a_d[0];

					cAD = cAL[id];
					
/*
					if (a_d[4] == "active" && detailId == a_d[0])
						document.getElementById('nbi').innerHTML = "Auktion endet nach Ablauf der Auktionsrestzeit.";
*/
					
					cAD['status'] = a_d[4];

					if (a_d[4] == 'active') {
						fTs = a_d[1] - diffTS;
						tTs = tL[a_d[0]] - fTs;
						if (tTs > 10 || tTs < -10 || fTs - ts < 30) tL[a_d[0]] = fTs;
					} 
//echoD(cAL[id]['id'] + ": " + a_d[0] + "," + cAD['status']);
						
//					if (lAmount.toString() != a_d[2]) {
					if ("undefined" == typeof cAD['amount'] || cAD['amount'] != a_d[2]) {
//alert(cAD['amount'] + " = " + a_d[2]);
						cAD['amount'] = a_d[2];
						setAmountField(a_d[0], a_d[2], 'countdown');
						setFinalAmountField(a_d[0], a_d[5]);
						if (!isNaN(a_d[2])) {
							setUsername(a_d[0], a_d[3]);
							setHistory(a_d[0]);
						}
					}


					cAL[a_d[0]] = cAD;


//echoD(a_d[4]);
					if (a_d[4] == "final" || a_d[4] == "finished") {
						if (document.getElementById("b" + a_d[0])) {
							b = document.getElementById("b" + a_d[0]);
//echoD("b" + a_d[0]);
							b.src = detailId == a_d[0] ? "/img/de/button_bid_big_standard_gray.png" : "/img/de/button_bid_small_standard_gray.png";
							b.onmousedown = "";
							b.onmouseover = "";
							b.onmouseout = "";
							b.onmouseup = "";
						}
					}

				}
				cdInit = 1;
			break;
			case "fina":
				dt = txt.substr(4);
//alert(lastFinal + " - " + dt);
				if (lastFinal != dt && !box.opened) {
					loadFinal(dt);
					lastFinal = dt;
				}
			
			break;
		}
	}
}


function sendCommand(cmd, id) {
	id = cAId[id];
	msg = cmd + id;
	sendRequest("/", processResponse, "ch=ajapi&c=" + msg);
}

//alert(cddebug);


