var interval = 1 * 60 * 1000;
var months = new Array(12);
months[0] = "January";
months[1] = "February";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "June";
months[6] = "July";
months[7] = "August";
months[8] = "September";
months[9] = "October";
months[10] = "November";
months[11] = "December";

$(document).ready(function() {
	queryPT();
	setInterval("queryPT()", interval);
});

function queryPT() {
	var mt = getParameter("mt");
	setTime(mt);
	var from = getParameter("from");
	var to = getParameter("to");
	var operator = getParameter("operator");
	var radius = getParameter("radius");
	var time = getParameter("time");
	var nret = getParameter("option");
	var routeIDList = getParameter("routes");
	var rtpi = getParameter("rtpi");
	//alert("routes: " + routeIDList);
	var url = "querypt.action?ts=" + new Date().getTime() + "&request_locale=en_US";
	if (from != null)
		url += "&from=" + from;
	if (to != null)
		url += "&to=" + to;
	if (operator != null)
		url += "&operator=" + operator;
	if (radius != null)
		url += "&radius=" + radius;
	if (time != null)
		url += "&time=" + time;
	if (nret != null)
		url += "&option=" + nret;
	if (mt != null)
		url += "&militaryTime=" + mt;
	if(routeIDList != null)
		url += "&routeList="+routeIDList;
	if(rtpi != null)
		url += "&rtpi="+rtpi;
	//alert(url);
	$.getJSON(url, function(data, status) {
		if (status == "success") {
			var html = "";
			if (data.rs.c == 200) {
				if (data.rs.s != null && data.rs.s.length > 0) {
					$.each(data.rs.s, function(i, service) {
						html += "<tr>";
						html += "<td colspan=\"3\" class=\"origin\">" + escapeQuote(service.f) + "</td>";
						html += "</tr>";
						html += "<tr class=\"title\">";
						html += "<td>DEPARTURE</td>";
						html += "<td>DESTINATION</td>";
						html += "<td>ROUTE</td>";
						html += "</tr>";
						$.each(service.i, function(j, item) {
							html += "<tr class=\"depart\">";
							if (item.d != null && item.d != "" && item.d != 0) {
								if (item.d > 0) {
									if (item.d == "1")
										html += "<td>" + item.t + " (1 min delay)</td>";
									else
										html += "<td>" + item.t + " (" + item.d + " mins delay)</td>";
								}
								else {
									if (item.d == "1")
										html += "<td>" + item.t + " (1 min early)</td>";
									else
										html += "<td>" + item.t + " (" + (0 - item.d) + " mins early)</td>";
								}
							}
							else {
								html += "<td>" + item.t + "</td>";								
							}
							html += "<td>" + escapeQuote(item.a) + "</td>";
							html += "<td>" + escapeQuote(item.r) + "</td>";
							html += "</tr>";
						});
					});
				}
				else {
					html += "<tr>";
					html += "<td colspan=\"3\" class=\"depart\">No Service Found</td>";
					html += "</tr>";
				}
			}
			else if (data.rs.c == 400) {
				html += "<tr>";
				html += "<td colspan=\"3\" class=\"error\">Bad Request</td>";
				html += "</tr>";
			}
			else if (data.rs.c == 500) {
				html += "<tr>";
				html += "<td colspan=\"3\" class=\"error\">Server Error</td>";
				html += "</tr>";
			}
			else if (data.rs.c == 601) {
				html += "<tr>";
				html += "<td colspan=\"3\" class=\"error\">Missing Query</td>";
				html += "</tr>";
			}
			else if (data.rs.c == 602) {
				html += "<tr>";
				html += "<td colspan=\"3\" class=\"error\">Unknown Address</td>";
				html += "</tr>";
			}
			$("#tblService").html(html);
		}
	});
}

function getParameter(key, default_) {
	key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if (qs == null)
		return default_;
	else
		return qs[1];
}