// This script is used for the client testimonials google map and to randomize the order of the testimonials on the page.

// chirobase	: ABQIAAAAjXomsDTnRGXd6dvUXWblixSflsVesZSK-NNBZXwTyb-SDWVMvBTY9gMGPOhexpf_MTJ_TKeR-2kX7A
// gen4snapshot	: ABQIAAAAjXomsDTnRGXd6dvUXWblixTRF3guJvUZ-Wrcz_OQdtStF6qlYBRJhb2yX3F0N_0sRbayhOa9ZWwMBg
// localhost	: ABQIAAAAjXomsDTnRGXd6dvUXWblixT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQkqfg7KedANioPviENx1AlSFeQqw
// rickdev01    : ABQIAAAAJiDLp8YZGgSB4Fm5ssFS8RQx-20kGcHPt2dD9fNmmh15s-Q-kRSfnVjn_a8eG43Z0cQAbdUdcDK40g
// vetmatrix    : ABQIAAAAJiDLp8YZGgSB4Fm5ssFS8RTDyN2G2yx1rg2AxvDciQbA2BTNYBR1ZXjLG_IKFmhdHK8wsVrqBbrk5g

//************************************************************************************************************************************
// Random Row Order 
//************************************************************************************************************************************

function randomizeRows(tableid){

	// content array 
	var contents = new Object();

	//array containing references to the participating contents
	contents.ref = new Array();

	//array containing participating contents' contents (innerHTML property)
	contents.text = new Array();

	// get all elements in the table 
	var table = document.getElementById(tableid);  
	var rows = table.getElementsByTagName("div");

	// loop through the rows  
	for (var i=0; i<rows.length; i++){

		// add the reference to the row  
		contents.ref[contents.ref.length]=rows[i];

		// add the html of the row 
		contents.text[contents.text.length]=rows[i].innerHTML;
	}
	
	// randomize the elements 
	contents.text.sort(function() {return 0.5 - Math.random();});

	// if the elements are table rows 
	var tbodyref=contents.ref[0].parentNode;

	// move the contents around 
	for (var i=0; i<contents.ref.length; i++) { 

		// move content 
		contents.ref[i].innerHTML=contents.text[i];

		// make it visible 
		contents.ref[i].style.visibility="visible";
	}
}

//************************************************************************************************************************************
// Build map  
//************************************************************************************************************************************

var map;
function map_initialize() {

	// randomize the table rows 
	randomizeRows('client_testimonials');

	// if the browse is not maps compatible 
	if (!GBrowserIsCompatible()) return; 

	// initialize the map  
    map = new GMap2(document.getElementById("map_canvas"));

	// add zoom and other map controls 
	var mapTypeControl = new GMapTypeControl();
    var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
    var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
    //map.addControl(mapTypeControl, topRight);
    map.addControl(new GSmallMapControl());

    // center United States  
	map.setCenter(new GLatLng(40.565316,-96.461916), 4);
	
	// loop through the rows and add the points 
	var table = document.getElementById('client_testimonials');
	var rows = table.getElementsByTagName("div");
	for (var i=0; i<rows.length; i++){ 
		 map.addOverlay(createMarker(rows[i], i + 1));
	} 
}

//************************************************************************************************************************************
// Creates a marker at the given point  
//************************************************************************************************************************************

function createMarker(row, number) {
	var coordchildno = 7; 

	// in IE, there are fewer child nodes for whatever reason
    if(/MSIE/.test(navigator.userAgent)){ coordchildno = 4; }

	var coords = row.childNodes[coordchildno].innerHTML.split(",");
	var latlng = new GLatLng(coords[0], coords[1]);
	var marker = new GMarker(latlng);
	marker.value = number;
	GEvent.addListener(marker,"click", function() {
    	var myHtml = '<div class="balloon">' + row.innerHTML + '</div>';
    	map.openInfoWindowHtml(latlng, myHtml);
  	});
  	return marker;
}