var lastPoint = new Array();
var thisPoint = new Array();
var trackIn = trackOut = firstPoint = 0;
var trackSpan = 200;
var back = 0;

function checkclick ( point )
{
 	if ( lastclick != point )
 	{
 		lastclick = point ;
 		var zoomlvl = map.getZoom();
		fc( point, zoomlvl);
	}
	if((((pointsave == 4) || (pointsave == 3)) && (llMIndex > 1)) || (pointsave == 5) || (pointsave == 6) || (pointsave == 7) || (pointsave == 8) || (pointsave == 9) || (pointsave == 10) || (pointsave == 11) || (pointsave == 13) || (pointsave == 14)){
		pointTrack();
	}
}

function geocode(){
	if(owner){
		back = back + 1;
		window.open('geocode.php?record=' + idRecord);
	}else{
		alert("Either you are not logged in or you do\nnot have permission to add to this record.\nYou must own this Data Record to add to it.");
	}
}

function help(){
	window.open('loceight_help.html', 'LoceightHelp', 'scrollbars=yes,height=700,width=500', false);
}

function tim(){
	back = back + 1;
	window.open('tim.php?record=1111111111');
	top.close();
}

function flickr(){
	back = back + 1;
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 4) && (pointsave != 5) && (pointsave != 6) && (pointsave != 7) && (pointsave != 8) && (pointsave != 9) && (pointsave != 10) && (pointsave != 11) && (pointsave != 12) && (pointsave != 13)){
		pointsave = 14;
		pointTrack();
	}
}

//function flickr(){
//	back = back + 1;
//	window.open('flickr.php?term=' + wpt_info[wrownum+2]);
//}

function fc( point, zoomlvl )
{
	var html = "";
	var tllS = "";
	var atllP = new Array;
	tllS += tllS + point;
	tllS = tllS.replace(/\(/g, "");
	tllS = tllS.replace(/\)/g, "");
	tllS = tllS.replace(/ /g, "");
	atllP = tllS.split(/,/g);
	var j = atllP[0].search(/\./);
	var lonTemp = atllP[1].substr(0,j+7);
	j = atllP[1].search(/\./);
	var latTemp = atllP[0].substr(0,j+7);
	map.setCenter( point, zoomlvl) ;
	html += html + "<p><center>Point No. " + (llMIndex + 1) + "<BR>";
	html += "Latitude - Longitude<br>" + latTemp + "  " + lonTemp + "</center></p>";

	LLMarker[llMIndex] = new createLLMarker(point, html);

	llString += point + ",";

	map.addOverlay(LLMarker[llMIndex++]);
//map.getCenterLatLng(point)
//	request.send(null);
}
function createLLMarker(point, html) 
{
	var icon = new GIcon(baseIcon);
	icon.image = "http://www.google.com/mapfiles/markerI.png";
	var marker = new GMarker(point);
	llEvent[llEIndex++] = GEvent.addListener(marker, "click",
		function(){marker.openInfoWindowHtml(html);});
	return marker;
}


function eMail(){
	if((owner) || (email)){
		window.open('contact.php?record=' + idRecord, '_blank', 'height=425,width=400');
	}else{
		alert("Either you are not logged in or you do\nnot have permission to Email this record.\nYou must own this Data Record to Email it\nor the Owner must make it public.");
	}
}

function downLoad(){
	if((owner) || (download)){
		window.open('gpx_download.php?record=' + idRecord,'_blank' ,'height=100,width=100');
	}else{
		alert("Either you are not logged in or you do\nnot have permission to download this file.\nYou must own this Data Record to download it\nor the Owner must make it public.");
	}

}
//, '_blank', 'height=300,width=600,scrollbars=yes',_blank','height=10,width=10');
function kmlExport(){
	if((owner) || (download)){
		window.open('kml_download.php?record=' + idRecord, '_blank','height=10,width=10');
	}else{
		alert("Either you are not logged in or you do\nnot have permission to download this file.\nYou must own this Data Record to download it\nor the Owner must make it public.");
	}
}

function savePoints(){
	if(pointsave < 2){
		pointsave = 1;
		pointTrack();
	}
}

function kmlSet(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 4) && (pointsave != 5)){
		pointsave = 3;
		pointTrack();
	}
}

function saveTrack(){
	if((pointsave != 1) && (pointsave != 3) && (pointsave != 4) && (pointsave != 5)){
		pointsave = 2;
		pointTrack();
	}
}

function saveFocus(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 5)){
		pointsave = 4;
		pointTrack();
	}
}

function revGcode(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 4)){
		back = back + 1;
		pointsave = 5;
		pointTrack();
	}
}

function revGplace(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 4) && (pointsave != 5)){
		back = back + 1;
		pointsave = 6;
		pointTrack();
	}
}

function realtor(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 4) && (pointsave != 5) && (pointsave != 6)){
		back = back + 1;
		pointsave = 7;
		pointTrack();
	}
}

function megan(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 4) && (pointsave != 5) && (pointsave != 6) && (pointsave != 7) && (pointsave != 8)){
		back = back + 1;
		pointsave = 9;
		pointTrack();
	}
}

function craig(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 4) && (pointsave != 5) && (pointsave != 6) && (pointsave != 7)){
		back = back + 1;
		pointsave = 8;
		pointTrack();
	}
}
function distance(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 4) && (pointsave != 5) && (pointsave != 6) && (pointsave != 7) && (pointsave != 8) && (pointsave != 9) && (pointsave != 10) && (pointsave != 11)){
		back = back + 1;
		pointsave = 12;
		pointTrack();
	}
}
function rv(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 4) && (pointsave != 5) && (pointsave != 6) && (pointsave != 7) && (pointsave != 8) && (pointsave != 9)){
		back = back + 1;
		pointsave = 10;
		pointTrack();
	}
}
function walmart(){
	if((pointsave != 1) && (pointsave != 2) && (pointsave != 3) && (pointsave != 4) && (pointsave != 5) && (pointsave != 6) && (pointsave != 7) && (pointsave != 8) && (pointsave != 9) && (pointsave != 10)){
		back = back + 1;
		pointsave = 11;
		pointTrack();
	}
}

function pointTrack(){
	if((llOn) && ((owner) || (pointsave == 13) || (pointsave == 12) || (pointsave == 14) || (pointsave == 5) || (pointsave == 6) || (pointsave == 7) || (pointsave == 8) || (pointsave == 9) || (pointsave == 10) || (pointsave == 11))){
		GEvent.removeListener(llEvent[0]);
		map.closeInfoWindow();
		lastclick = "";
		llOn = 0;
		var llTemp = "";
		var latTemp = "";
		var lonTemp = "";
		var j = 0;
		var llPoints = new Array;
		if(llString != ""){
			llString = llString.replace(/\(/g, "");
			llString = llString.replace(/\)/g, "");
			llString = llString.replace(/ /g, "");
			llPoints = llString.split(/,/g);
			for(i = 0;i < (llMIndex + llMIndex);i+=2){
				j = llPoints[i].search(/\./);
				latTemp = llPoints[i].substr(0,j+7);
				j = llPoints[i+1].search(/\./);
				lonTemp = llPoints[i+1].substr(0,j+7);
				llTemp += latTemp + "," + lonTemp + "<BR>";
			}
		}
		else{
			llTemp = "null";
		}
		if(pointsave == 1){
			window.open('point_save.php?record=' + idRecord + '&point=' + llTemp);
		}
		else if(pointsave == 2){
			window.open('point_save.php?record=' + idRecord + '&track=' + llTemp);
		}else if(pointsave == 3){
			llTemp = llTemp.replace(/<BR>$/, "");
			window.open('lookat.php?record=' + idRecord + '&point=' + llTemp);
		}else if(pointsave == 4){
			window.open('focus.php?record=' + idRecord + '&point=' + llTemp);
		}
		else if(pointsave == 5){
			window.open('revgcode.php?record=' + idRecord + '&point=' + llTemp);
		}
		else if(pointsave == 6){
			window.open('revgplace.php?record=' + idRecord + '&point=' + llTemp, '_blank' ,'height=200,width=400');
//			window.open('revgplace.php?record=' + idRecord + '&point=' + llTemp);
		}
		else if(pointsave == 7){
			window.open('realtor.php?record=' + idRecord + '&point=' + llTemp);
		}
		else if(pointsave == 8){
			window.open('craig.php?point=' + llTemp);
		}
		else if(pointsave == 9){
			window.open('megan.php?point=' + llTemp);
		}
		else if(pointsave == 10){
			window.open('rv.php?point=' + llTemp);
		}
		else if(pointsave == 11){
			window.open('walmart.php?point=' + llTemp);
		}
		else if(pointsave == 12){
			window.open('distance.php?point=' + llTemp, '_blank', 'height=100,width=200');
			numPoints = 0;
		}
		else if(pointsave == 14){
			window.open('flickr.php?point=' + llTemp + '&term=' + wpt_info[wrownum+2]);
		}
		pointsave = 0;
		weather = 0;
	}
	else if(!llOn){
		if(llEIndex){
			for(var i = 1;i < llEIndex;i++){
				GEvent.removeListener(llEvent[i]);
			}
		}
		if(llMIndex){
			for(i = 0;i < llMIndex;i++){
				map.removeOverlay(LLMarker[i]);
			}
		}
		llString = "";
		llEIndex = 0;
		llMIndex = 0;
		llEvent[llEIndex++] = GEvent.addListener(map, 'click', function(overlay, point){if (overlay){} else if (point){checkclick( point );}});
		llOn = 1;
	}
	else{
		map.closeInfoWindow();
		if(llEIndex){
			for(var i = 0;i < llEIndex;i++){
				GEvent.removeListener(llEvent[i]);
			}
		}
		if(llMIndex){
			for(i = 0;i < llMIndex;i++){
				map.removeOverlay(LLMarker[i]);
			}
		}
		lastclick = "";
		llOn = 0;
		llEIndex = 0;
		llMIndex = 0;
		pointsave = 0;
	}
}
function toggleColor(){
	if(llOn){return;}
	if(tColor){
		tColor = 0;
		clearTimeout(flashTimer);
	}else{
		if((notrack) && (trackState)){
			tColor=1;
			color_index++;
			for(var i = 0;i< notrack;i++){
				map.removeOverlay(_track[i]);
			}
			makeTrack();
			flashTimer = setTimeout("advanceColor()", 1000);
		}
	}
}

function advanceColor(){

	for(var i = 0;i< notrack;i++){
		map.removeOverlay(_track[i]);
	}
	color_index++;
	if(color_index == 10){color_index = 0;}
	makeTrack();
//	flashTimer = setTimeout("advanceColor()", 1000);
}

function track_first(){
	red_row = wrownum - notrack + trackAdd;
	if (red_row != wrownum){
		for(var i = red_row;i < wrownum;i++){
			markers[i] = createMarker(wpoints[i], wpt_info[i], 0, 2);
			map.addOverlay(markers[i]);
		}
	}
}

function addPoints(){
track_first();
var size = 1;
var icolor = 0;
var yellowIcon = (parseInt(w_lat_x[wrownum]));

for (var i = 0; i < wrownum-notrack+trackAdd; i++) {
	if (i == 1) {
		size = 0;
	}
	if(yellowIcon){
		if(i == yellowIcon){
			icolor = 1;
		}
	}
	else{
		if(i == 1){
			icolor = 1;
		}
	}
//	if ((i == (wrownum - notrack + trackAdd)) && (i)){
//		icolor = 2;
//	}
        markers[i] = createMarker(wpoints[i], wpt_info[i], size, icolor);
	map.addOverlay(markers[i]);
     }
}
function centerData(){
	if(noData){
		map.setCenter(wpoints[0], 17);
//	}else if(idRecord == '1111111111'){
//		map.setCenter(wpoints[0], 12);
	}else{
		if(wrownum > 1){
		// center and zoom on the bounds. 
			myCenterAndZoomOnBounds(); 
		}
		else{
			if(totrows > 3000){
				map.setCenter(wpoints[0], 4);
			}
			else if(totrows > 2000){
				map.setCenter(wpoints[0], 7);
			}
			else if(totrows > 1000){
				map.setCenter(wpoints[0], 10);
			}
			else{
				map.setCenter(wpoints[0], 17);
			}
		}
	}
}
function doTracks(){
	if(trackState){removeTracks();}
	else{remakeTrack();}
}

function remakeTrack(){
	if ((!trackState) && (notrack)) {
		makeTrack();
		trackState = 1;
	}
}

function removeTracks(){
	if(llOn){return;}
	for(var i = 0;i< notrack;i++){
		map.removeOverlay(_track[i]);
	}
	trackState = 0;
}

function removeTracks2(){
	if(llOn){return;}
	if(trackOut > trackSpan){
		var polys = trackOut - trackSpan;
	}else{
		polys = 0;
	}
	for(var i = polys;i< trackOut;i++){
		map.removeOverlay(track_[i]);
	}
}

function playTrack(){
	if(llOn){return;}
		
		if (trackState){
			removeTracks();

			trackOut = trackIn= firstPoint = 0;
			myTimer = setTimeout("doTrack()", 500);
			map.panTo(wpoints[wrownum-1]);
		}
		else{

			clearTimeout(myTimer);
			removeTracks2();

			makeTrack();
			trackState = 1;
			removePoints();
			addPoints();
			pointState = 1;
			map.panTo(wpoints[wrownum-1]);

		}
}

function doTrack(){
	var mpoints = new Array();

	if(trackIn >= lrownum0){
		clearTimeout(myTimer);
		removeTracks2();
		makeTrack();
		trackState = 1;

		removePoints();
		addPoints();
		pointState = 1;
		map.panTo(wpoints[wrownum-1]);
//		centerData();
	}
	else{
		makeTrack2();

		map.removeOverlay(markers[wrownum-1]);

		mpoints.push(thisPoint[0]);
		markers[wrownum-1] = createMarker(mpoints[0], wpt_info[wrownum-1],0,3);
		map.addOverlay(markers[wrownum-1]);
		map.panTo(mpoints[0]);
		myTimer = setTimeout("doTrack()", 100);
	}
}

function removePoints(){
	for(var i = 0;i< wrownum;i++){
		map.removeOverlay(markers[i]);
	}
	pointState = 0;
	if(llEIndex){
		for(var i = 1;i < llEIndex;i++){
			GEvent.removeListener(llEvent[i]);
		}
	}
	if(llMIndex){
		for(i = 0;i < llMIndex;i++){
			map.removeOverlay(LLMarker[i]);
		}
	}
}

function doPoints(){
	if(llOn){return;}
	if(pointState){removePoints();}
	else if ((pointState == 0) && (wrownum)) {
		addPoints();
		pointState = 1;
	}
}

function makeTrack2(){
	points = new Array();
	if(!firstPoint){
		lastPoint[0] = new GLatLng(superx[0][trackIn],supery[0][trackIn]);
		trackIn++;
		thisPoint[0] = new GLatLng(superx[0][trackIn],supery[0][trackIn]);
		points.push(lastPoint[0]);
		points.push(thisPoint[0]);
		firstPoint = 1;
	}
	else{
		lastPoint[0] = thisPoint[0];
		thisPoint[0] = new GLatLng(superx[0][trackIn],supery[0][trackIn]);
		points[0] = lastPoint[0];
		points[1] = thisPoint[0];
	}
	if(trackOut> trackSpan){
		map.removeOverlay(track_[trackOut - trackSpan]);
	}
	trackIn+=4;
	map.addOverlay(track_[trackOut++] = new GPolyline(points, my_color[color_index], 3, .75));
}

function makeTrack(){

	for(var i = 0; i < notrack;i++){
		var points = new Array();
		for (var j = 0; j < superrow[i]; j++) {
			points.push(new GLatLng(superx[i][j],supery[i][j]));
		}
		map.addOverlay(_track[i] = new GPolyline(points, my_color[color_index], 3, .75));
	}
}

