// JavaScript Document
var tID;

function stopClock() {
  clearTimeout(gizmo);
}

function yourClock() {
	var nd;
	for (i=0;i<ar.length;i++) {
		nd = ar[i]['dt'];
		nd.setSeconds(nd.getSeconds()+1);
		checkStock(nd, i);
		setClock(nd, i);
	}
	tID = setTimeout("yourClock()", 1000);
}

function setClock(nd, ind) {
	var h, m;
	var s;
	var time = " ";
	h = nd.getHours();
	m = nd.getMinutes();
	s = nd.getSeconds();
	if (h <= 9) h = "0" + h;
	if (m <= 9) m = "0" + m;
	if (s <= 9) s = "0" + s;
	time += h + ":" + m + ":" + s;
	//
	$(ar[ind]['id']+" .clock").html("<p>"+time+"</p>");
}

function checkStock(dt, ind) {
	var isopen = true;
	var str = "";
	// CHECK TIME
	if (!checkOpenTime(dt, ind)) {
		isopen = false;
	}
	// CHECK WEEKEND
	if (!checkWeekEnd(dt, ind)) {
		isopen = false;
	}
	// CHECK HOLIDAYS
	if (!checkHolidays(dt, ind)) {
		isopen = false;
	}
	//
	if (isopen) {
		str = "<p class=\"market-on\">"+stock_open+"</p>";
	} else {
		str = "<p class=\"market-off\">"+stock_close+"</p>";
	}
	if (ar[ind]['isopen'] != isopen) {
		$(ar[ind]['id']+" .stock").html(str);
		ar[ind]['isopen'] = isopen;
	}
	//console.log(ind+"  "+isopen);
}

function checkOpenTime(dt, ind) {
	if (ar[ind]['def_open'] != undefined && ar[ind]['def_close'] != undefined) {
		var openTime = ar[ind]['def_open'].split(":");
		var closeTime = ar[ind]['def_close'].split(":");
		openTime[0] = parseInt(parsTime(openTime[0]));
		openTime[1] = parseInt(parsTime(openTime[1]));
		closeTime[0] = parseInt(parsTime(closeTime[0]));
		closeTime[1] = parseInt(parsTime(closeTime[1]));
		//
		var open_time = openTime[0]*60+openTime[1];
		var close_time = closeTime[0]*60+closeTime[1];
		var nowtime = dt.getHours()*60+dt.getMinutes();
		//
		if (nowtime < open_time || nowtime > close_time) { // Budni
			return false;
		}
		return true;
	}
}

function checkWeekEnd(dt, ind) {
	for (var i=0; i<ar[ind]['day'].length; i++) {
		if (ar[ind]['day'][i].type == "week") {
			if (dt.getDay() == parseInt(ar[ind]['day'][i].date)) {
				return false;
			}
		}
	}
	return true;
}

function checkHolidays(dt, ind) {
	var now;
	for (var i=0; i<ar[ind]['day'].length; i++) {
		if (ar[ind]['day'][i].type == "holiday") {
			now = (dt.getFullYear()+"."+(dt.getMonth()+1)+"."+dt.getDate()).toString();
			if (now == ar[ind]['day'][i].date) {
				if (ar[ind]['day'][i].topen == "") {
					return false;
				} else {
					return checkHolidayOpenTime(dt, ar[ind]['day'][i].topen, ar[ind]['day'][i].tclose);
				}
			}
		}
	}
	return true;
}

function parsTime(str) {
	if (str.indexOf("0") == 0) {
		str = str.substr(1);
	}
	return str;
}

function checkHolidayOpenTime(dt, topen, tclose) {
	if (topen != undefined && tclose != undefined) {
		var openTime = topen.split(":");
		var closeTime = tclose.split(":");
		openTime[0] = parseInt(parsTime(openTime[0]));
		openTime[1] = parseInt(parsTime(openTime[1]));
		closeTime[0] = parseInt(parsTime(closeTime[0]));
		closeTime[1] = parseInt(parsTime(closeTime[1]));
		//
		var open_time = openTime[0]*60+openTime[1];
		var close_time = closeTime[0]*60+closeTime[1];
		var nowtime = dt.getHours()*60+dt.getMinutes();
		//
		if (nowtime < open_time || nowtime >= close_time) { // Budni
			return false; // is closed
		}
		return true; // is open
	}
}

