//<![CDATA[

// created: 06/07/07 19:11:32 UTC - Copyright Alertsite
//  author: Chris Brinegar (cbrinegar), <chris dot brinegar //at// gmail.com>

//detect internet exploder
var ie=false; if (document.all) { ie = true }

var map;

var map_start_position = { "lat":16.0220, "lng":10.2339, "zoom":2 }

var marker_conf = { 
	"std":		{ "imgurl":"/images/as_reddot_bordered-10x10.gif", "w":7, "h":7 },
	"hover":	{ "imgurl":"/images/as_yelanim_10x10.gif", "w":7, "h":7 }
	}

var infowindow = { "show":1 }

var autoScroll = { "enabled":0, "initial_delay":3000, "direction":1, "returnToStartLat":1, "panAmount":.3, "delay":50 }

// alpha - step through location details, panning through progression, a bit laggy for slow-net folks
var autoWalk = { "enabled":0, "initial_delay":30000, "direction":1, "delay":3000 }

// rand_pct +/- percent - e.g. .30 == +/- 30%
var polyLines = {
	"enabled":1,
	"initial_delay":3000,
	"run_duration":13000,
	"delay":200,
	"persist_time":12000,  // disabled
	"rand_pct":.30,
	"line_length":2,
	"color":"#00ff00",
	"base_color":"#ffff00",
	"error_color":"#ff8000",
	"linewidth":1,
	"opacity":0.33,
	"timer":''
};

if (polyLines.enabled && !ie ) { 
	window.setTimeout(
		function(){drawPointLines(polyLines.delay)}, 
		polyLines.initial_delay
	);

	window.setTimeout(
		function(){clearTimeout(polyLines.timer)},
		polyLines.run_duration
	);
}

var side_bar_html = "";

var gmarkers = [];


function mapscroll(bool) { autoScroll.enabled = false /* bool */ }
function maphover(bool) { map.is_hovered = bool }

function appendSideBar(text) { side_bar_html += text }

function mark_click(i)	{ GEvent.trigger(gmarkers[i], "click") }

function mark_hover(i)	{
	clearTimeout(map.timer_pan_reset);
	GEvent.trigger(gmarkers[i], "linkover");

}
function mark_reset(i)	{ 
	if (map.is_hovered){
		return;
	}
	GEvent.trigger(gmarkers[i], "linkout");
	map.timer_pan_reset = setTimeout(function(){ 
		if (map.is_hovered){
			return;
		}
		map.disableInfoWindow(); map.enableInfoWindow();
		map.setMapType(G_SATELLITE_MAP);
		map.setZoom(map_start_position.zoom);
		map.panTo(
			new GLatLng( map_start_position.lat, map_start_position.lng)
		);
	}, 1000);
}

function gmap_init() {

	if (GBrowserIsCompatible()) {
		
		var i = 0;

		var icon = new GIcon();

		if (ie) { marker_conf.std.imgurl = marker_conf.std.imgurl.replace(/png$/,'gif') }

		icon.image = marker_conf.std.imgurl;
		icon.iconSize = new GSize(marker_conf.std.w, marker_conf.std.h)
		icon.iconAnchor = new GPoint((marker_conf.std.w/2), (marker_conf.std.h/2))
		icon.infoWindowAnchor = new GPoint(marker_conf.std.w - (marker_conf.std.w /2), marker_conf.std.h - (marker_conf.std.h /2))

		//function to add markers to map and populate side_bar html --cb
		function createMarker(point,name,html) {

			var marker = new GMarker(point, icon, !infowindow.show);

			GEvent.addListener(marker, "mouseover", function() {
				marker.setImage(marker_conf.hover.imgurl);
				if (infowindow.show) {
					clearTimeout(infowindow.timer);
					infowindow.opening = 1;
					marker.openInfoWindowHtml(html);
					window.setTimeout(function(){ infowindow.opening = 0 }, 40);
				}
				mapscroll(1)
			});

			GEvent.addListener(marker, "mouseout", function() {
				if ( ! infowindow.opening ) {
					if (infowindow.show) {
						infowindow.timer = window.setTimeout(function(){ 
							map.disableInfoWindow(); map.enableInfoWindow();
							marker.setImage(marker_conf.std.imgurl);
							infowindow.opening = 0
						}, 40);
					}
					mapscroll(1)
				}
				else {
					infowindow.timer = window.setTimeout(function(){ 
						map.disableInfoWindow(); map.enableInfoWindow();
						infowindow.opening = 0
					}, 2000);
					marker.timer = window.setTimeout(function(){ 
						marker.setImage(marker_conf.std.imgurl);
					}, 2000);
				}
			});

			GEvent.addListener(marker, "linkover", function() {
				mapscroll(0)
				marker.setImage(marker_conf.hover.imgurl);

				// remove map-anim-stop handler
				if (map.event_moveend_handler)
					GEvent.removeListener(map.event_moveend_handler);

				// add map-anim-stop handler
				map.event_moveend_handler = GEvent.addListener(map, "moveend", function() {
					GEvent.removeListener(map.event_moveend_handler);

					map.setZoom(map_start_position.zoom + 3);
					map.setMapType(G_HYBRID_MAP);

					if (infowindow.show) {
						marker.openInfoWindowHtml(html);
					}
				});

				// pan to marker
				map.panTo( marker.getPoint() );

 	  		});

			GEvent.addListener(marker, "linkout", function() {
				marker.setImage(marker_conf.std.imgurl);
				if (infowindow.show) {
					map.disableInfoWindow(); map.enableInfoWindow();
				}
				mapscroll(1)
			});

			GEvent.addListener(marker, "click", function() {
				if (infowindow.show) {
					clearTimeout(infowindow.timer);
					infowindow.opening = 1;
					marker.openInfoWindowHtml(html);
					window.setTimeout(function(){ infowindow.opening = 0 }, 10000);
				}
			});

			gmarkers[i] = marker;
			gmarkers[i].connections = 0

			side_bar_html += '<span><a href="#" onclick="javascript:return false;" onmouseover="javascript:mark_hover(' + i + ')" onmouseout="mark_reset('+i+')">' + name + "</a></span><br>\n";
			i++;
			return marker;
		}

		function panMap(msDelay,panAmount) {
			var center = map.getCenter();
			if (autoScroll.enabled) {
				if (autoScroll.returnToStartLat) {
					center = new GLatLng(map_start_position.lat, center.lng())
				}
				map.panTo( new GLatLng( center.lat(), center.lng() - (panAmount * autoScroll.direction) ))
			}
			window.setTimeout(function(){ panMap(autoScroll.delay,autoScroll.panAmount) }, autoScroll.delay);
		}


		// create the map
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng( map_start_position.lat, map_start_position.lng ),map_start_position.zoom);
		//map.setMapType(G_HYBRID_MAP);
		map.setMapType(G_SATELLITE_MAP);
		map.addControl(new GSmallMapControl());
//		map.disableDragging();
//		map.enableScrollWheelZoom();

		window.setTimeout(function(){ panMap(autoScroll.delay,autoScroll.panAmount) }, autoScroll.initial_delay);

		//load the location data --cb
		var locs = eval("(" + json_locs + ")");

		//populate sidebar and markers --cb
		var c,d;
		while (c = locs.shift() ) {
			appendSideBar('<span class="cat_header">' + c.country + "</span>");
			var m;
			while (m = c.locations.shift() ) {
				map.addOverlay(createMarker(new GLatLng(m.lat, m.lon),m.citystate, m.descrip));
			}
			appendSideBar('<br>');
		}

		// put the assembled side_bar_html contents into the side_bar div
		document.getElementById("column_section_content").innerHTML = side_bar_html;

	}
	else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}


// connect each point to every other
function spiderWeb() {
	for ( var m in gmarkers ) {
		for ( var n in gmarkers ) {
			var linePoints = [];
			linePoints.push(gmarkers[m].getPoint());
			linePoints.push(gmarkers[n].getPoint());
			linePoints.sort(function(p1, p2) {return p1.lng() - p2.lng();});
			newLine = new GPolyline(linePoints, polyLines.base_color, polyLines.linewidth, polyLines.opacity);
			map.addOverlay(newLine);
		}
	}
}

function trafficSim(visibleMarkers) {

	var linePoints = [];

	var newLine;

	function randIndex(array) { return (Math.floor((Math.random()*array.length))) }

//	linePoints.push(gmarkers[1].getPoint());
	for ( var i=0; i < polyLines.line_length; i++ )
		linePoints.push(gmarkers[visibleMarkers[randIndex(visibleMarkers)]].getPoint());

	linePoints.sort(function(p1, p2) {return p1.lng() - p2.lng();});

	var color = (Math.floor(Math.random()*3000) > 2500) ? polyLines.error_color : polyLines.color;

	newLine = new GPolyline(linePoints, color, polyLines.linewidth, polyLines.opacity);

	map.addOverlay(newLine);

	/*
	window.setTimeout(function(){ 
		map.removeOverlay(newLine) 
		}, 
		Math.floor(
		rand_pct(polyLines.rand_pct, polyLines.persist_time)
			+ polyLines.persist_time
		)
	);
	*/
}

//qnd
var index = 0;
function drawPointLines(msDelay) {

	var visibleMarkers = [];

	for ( var q in gmarkers ) {
//		if ( bounds.contains(gmarkers[q].getPoint()) ) {
			visibleMarkers.push(q);
//		}
	}

	if (polyLines.enabled) {
		trafficSim(visibleMarkers)
	}
	if (autoWalk.enabled) {
		mark_reset( index - 1 < 0 ? 0 : index - 1 );
		mark_hover(index++);
	}

	polyLines.timer = window.setTimeout(function(){
		drawPointLines(msDelay)
		}, 
		rand_pct(polyLines.rand_pct, msDelay) + msDelay
	);
}

// +/- "pct" percent
function rand_pct(pct,value) {
	return Math.floor((Math.random()-.5)*2*100*pct) / 100 * value
}

//]]>
