<!--
// ensure CVS revision number survives minification
// var dummy = '$Id: model_clock.js,v 1.1.10.4 2010-04-15 08:58:30 mcarey Exp $';
//
/*
 * Live Betting Clocks
 */

var clockList = {};
var clockPeriodJS = {};
var serverDate;
var clientTimeOnLoad;
var divName = new Array();
var custLang;

function initPeriodClocks(_year,_month,_day,_hour,_min,_sec,_lang) {
	if(show_bir_clocks == "Y") {
		setClientTimeOnLoad();
		setServerDate(_year,_month,_day,_hour,_min,_sec);
		custLang = _lang;
	}
}

function addClockEvent(event_id, clockn_js, div) {
	if(show_bir_clocks == "Y") {
		var info = clockList[event_id];
		var is_new_ev = !info;

		if (is_new_ev) {
			clockList[event_id] = {seconds: 0, timeout: 0};
		}

		for(var x in clockn_js) {
			id = clockn_js[x].period_id;
			var period = clockPeriodJS[id];
			var is_new_period = !period;
			
			if(is_new_period) {
				clockPeriodJS[id] = clockn_js[x];
			}
		}

		// Don't duplicate div names everytime a clock comes in for a particular div prefix.
		for (var i = 0; i < divName.length; i++) {
			if (divName[i] == div) {
				return;
			}
		}
	
		divName.push(div);
	}
}

function updatePeriodClock(data) {
	if(show_bir_clocks == "Y") {
		var clock = clockList[data.ev_id];
		if(clock) {
			var periodInfo = clockPeriodJS[data.period_id];
			period_start = 0;
			if(typeof periodInfo != undefined && periodInfo != null) {
				if(periodInfo.period_start_time != "") {
					period_start = periodInfo.period_start_time;
				}	
				
				clockList[data.ev_id].active = true;
				setPeriodStartTime(data.curr_period_start_time,period_start,data.ev_id);
				displayClock(periodInfo.period_name, periodInfo.period_xl[custLang], period_start, periodInfo.break_period, periodInfo.period_clock_format, data.ev_id);
			} else {
				turnOff(data.ev_id);
			}
		}
	}
}

function setServerDate(_year,_month,_day,_hour,_min,_sec) {
	serverDate = new Date(_year,_month,_day,_hour,_min,_sec);
}

function increaseServerTime(secs) {
	serverDate.setSeconds(serverDate.getSeconds() + secs);
}

function setClientTimeOnLoad() {
	clientTimeOnLoad = new Date();
}

function getServerTimeNow() {
	now = new Date();

	timeDiff = (now.getTime() - clientTimeOnLoad.getTime());
		
	dateDifference = serverDate.getTime() + ((timeDiff < 0) ? 0 : timeDiff);

	newDate = new Date(dateDifference);
 	   
	return newDate;
}

function setPeriodStartTime(curr_period_start_time, start, ev_id) {
	var re = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
	
	if(curr_period_start_time.match(re)) {
		myDate = curr_period_start_time.split("-").toString().split(":").toString().split(" ").toString().split(",");
		periodStartTime = new Date(myDate[0],myDate[1],myDate[2],myDate[3],myDate[4],myDate[5]);		
		periodStartTime = parseInt((periodStartTime/1000),10);
	} else {
		periodStartTime = 0;
	}
	
	periodStartTime = ((periodStartTime < 0) ? 0 : periodStartTime);

	if(start == "") {
		periodseconds = 0;
	} else {
		periodminutes = start.split(":");
		
		periodseconds = parseInt((periodminutes[0]*60),10);
		periodseconds += parseInt((periodminutes[1]),10); 
	}

	servertimenow = parseInt(getServerTimeNow()/1000);	
	serverPeriodStart = (servertimenow - periodStartTime);
	if(serverPeriodStart < 0) {
		increaseSecs = -serverPeriodStart;
		increaseServerTime(increaseSecs-1);
		servertimenow = parseInt(getServerTimeNow()/1000);
		serverPeriodStart = (servertimenow - periodStartTime);
	}
	clockList[ev_id].seconds = periodseconds+serverPeriodStart;
}

function displayClock(name, translation, start, is_break, format, ev_id) {
	if(name==null || translation==null || start==null || is_break==null || format==null || clockList[ev_id].active == false)  { 
		turnOff(ev_id);
		return; 
	}
		
	totalTime = getClockTime(ev_id, format, start);
	
	if(typeof _lb_evt_event != 'undefined' && _lb_evt_event != null && _lb_evt_event != "" && lb_evt_ev_id == ev_id) { 
		_lb_evt_event.clock = totalTime;
		_lb_evt_event.period_xl = translation;
	}
	
	for(x in divName) {
		var clockElm = document.getElementById(divName[x]+ev_id);
	
		if(clockElm != null) {
			if(is_break == "Y") {
				clockElm.innerHTML = translation;
			} else {
				clockElm.innerHTML = totalTime+" "+translation;
			}
		}
	}
	
	if(is_break == "Y") {
		clearTimeout(clockList[ev_id].timeout);
	} else {
		clearTimeout(clockList[ev_id].timeout);
		clockList[ev_id].timeout = setTimeout('displayClock("'+name+'","'+translation+'","'+start+'","'+is_break+'","'+format+'","'+ev_id+'")', 1000);
	}
}

function getClockTime(ev_id, format, start) {
	var clock = clockList[ev_id];
	totalTime = "";
	if(clock) {	
		totalTime = 0;

		startSeconds = start;

		if(start != 0) {
			myList = String(startSeconds).split(":");

			if(format == "HH:MM:SS") {
				startSeconds = (parseInt(myList[0],10)*60*60)+(parseInt(myList[1],10)*60)+parseInt(myList[2],10)
			} else if(format == "MMM:SS") {
				startSeconds = (parseInt(myList[0],10)*60)+(parseInt(myList[1],10));
			}
		}
		seconds = ((clockList[ev_id].seconds < startSeconds) ? startSeconds : clockList[ev_id].seconds);
		
		minutes = 0;
		hours = 0;
		
		if(seconds >= 60) {
			minutes = Math.floor(seconds / 60);
			seconds = seconds % 60;
			
			if(format == "HH:MM:SS") {
				hours = minutes / 60;
				minutes = minutes % 60;
			}
		}
		
		if(seconds == 60) {
			seconds = 0;	
			minutes += 1;
		}
		
		if(format == "HH:MM:SS") {
			if(minutes == 60) {
				minutes = 0;
				hours += 1;
			}
		}

		if(format == "HH:MM:SS") {
			totalTime = hours + ((minutes <= 9) ? "0"+minutes : minutes) + ":" + ((seconds <= 9) ? "0"+seconds : seconds)
		} else if (format == "MMM:SS") {
			min = minutes;
			
			if(minutes == 0) {
				min = "00";
			} else if(minutes > 1 && minutes < 10) {
				min = "0"+minutes;
			}
			
			totalTime = min + ":" + ((seconds<=9) ? "0" + seconds : seconds);
		}
	}
	
	clockList[ev_id].seconds++;
	
	return totalTime;
}

function turnOff(ev_id) {
	for(x in divName) {
		var clockElm = document.getElementById(divName[x]+ev_id);

		if(clockElm != null) {
			clockElm.innerHTML = "";
		}
	}
	clearTimeout(clockList[ev_id].timeout);
	clockList[ev_id].active = false;
	
	return;
}

//-->
