// Global variables
var counter = 0;
var lastVisitorTimestamp = '';

$(document).ready(function() {
	
	// Stupid IE
	if ($.browser.msie) {
		$('#ie_map').show();
		setTimeout("init();", 2000);
	}
	else
		init();
	
	// Hourly refresh
	setTimeout("window.location.reload(true);", 3600000);
});


function init() {
	initialize_map();
	updateAll();
	
	// Stupid IE
	if ($.browser.msie)
		$('#ie_map').fadeOut();
}

function updateAll() {
	
	// Stupid IE strikes again
	// Pass along arbitrary parameter, so the response doesn't get cached
	$.getJSON('data/cache_today_total.php?t=' + (new Date()).getTime(), function(data) {
		
		// Server is slow
		if (data == null || !data.has_data) {
			timeoutPeriod = 60000;
		}
		
		// Update BIG5
		else if (lastVisitorTimestamp != data.visitors[0].visit_time_stamp) {
			timeoutPeriod = 3000;
			lastVisitorTimestamp = data.visitors[0].visit_time_stamp;
			
			var loc = data.visitors[0].country;
			if (data.visitors[0].city) {
				loc = data.visitors[0].city + ', ' + loc;
			}
			addNextMarker(data.visitors[0].latitude, data.visitors[0].longitude, counter++, loc, data.visitors[0].site_tracking_id);
			
			// Update visitor list
			var h = '';
			var a = {
				2: 'decision',
				3: 'discipleship',
				4: 'visitor'
			};
			
			for (var i in data.visitors) {
				var v = data.visitors[i];
				var type = a[v.site_tracking_id];
				
				// Update HTML
				h += '<div class="vd_item">';
				h += '<div id="vd_cover_' + type + '" class="vd_highlight_cover"></div>';
				
				// Visitor Type
				h += '<div class="container_response">';
				h += '<p><span class="response_type response_type_' + type + '"></span>' + v.site_tracking_name + '</p>';
				h += '</div>';
				
				// Website
				h += '<div class="container_url">';
				h += '<span class="vd_divider"></span><p>' + v.website + '</p>';
				h += '</div>';
				
				// Time
				h += '<div class="container_time">';
				h += '<span class="vd_divider"></span>';
				h += '<p class="time">' + dateConvert(v.visit_time_stamp) + '</p>';
				h += '</div>';
				
				// Location
				h += '<div class="container_location">';
				h += '<span class="vd_divider"></span>';
				h += '<p class="location">' + v.country;
				if (v.city)
					h += ' <span>&raquo;</span> ' + v.city;
				h += '</p></div>';
				h += '<img class="flag" src="country_flags/' + v.iso2.toLowerCase() + '.gif" />';
				h += '</div>';
			}
			$('#visitor_list').html(h);
			$('.vd_highlight_cover').fadeOut(800);
			
			// Update BIG5
			var h = '<div id="v">' + addCommas(data.big5.gospel) + '</div>';
			h += '<div id="id">' + addCommas(data.big5.decision) + '</div>';
			h += '<div id="if">' + addCommas(data.big5.email) + '</div>';
			h += '<div id="di">' + addCommas(data.big5.discipleship) + '</div>';
			$('#visitor_count').html(h);
		}
	});
	
	setTimeout("updateAll();", timeoutPeriod);
}

function dateConvert(str) {
	var h = str.substr(11,2);
	var i = str.substr(14,2);
	var s = str.substr(17,2);
	var a = (h < 12) ? "am" : "pm";
	if (h == 0) {
		h = 12;
	}
	return parseInt(h,10) + ":" + i + ":" + s + " " + a;
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

