/********************************************************
*********************************************************

Bones in Motion - BiM Active Map Tool for Google
Copyright 2006 by Bones in Motion - All Rights Reserved
January 2006 - Spencer Nassar - spencer@bim-inc.com

gmaps_ui.js - ui controls/management

*********************************************************
********************************************************/

var selectColor = '#5ffff6';
var hoverColor = '#c0c0c0';
var normalColor = '#ffffff';

function getObjPos( objID )
{
	var obj = document.getElementById(objID);
	var curwidth = obj.offsetWidth;
	var curheight = obj.offsetHeight;
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop,curwidth,curheight];
}

function setInfoMessage( msg, isTemp )
{
	var thebar = document.getElementById('infobar');
	var thestring = null;

	if ( msg == 'water' ) thestring = waterSelStr;
	else if ( msg == 'water-h' && editMode != 'water' ) thestring = waterHovStr;
	else if ( msg == 'food' ) thestring = foodSelStr;
	else if ( msg == 'food-h' && editMode != 'food' ) thestring = foodHovStr;
	else if ( msg == 'view' ) thestring = viewSelStr;
	else if ( msg == 'view-h' && editMode != 'view' ) thestring = viewHovStr;
	else if ( msg == 'bathroom' ) thestring = toiletSelStr;
	else if ( msg == 'bathroom-h' && editMode != 'bathroom' ) thestring = toiletHovStr;
	else if ( msg == 'parking' ) thestring = parkSelStr;
	else if ( msg == 'parking-h' && editMode != 'parking' ) thestring = parkHovStr;
	else if ( msg == 'other' ) thestring = starSelStr;
	else if ( msg == 'other-h' && editMode != 'other' ) thestring = starHovStr;
	else if ( msg == 'line' ) thestring = lineModeStr;
	else if ( msg == 'tostart' ) thestring = tostartHovStr;
	else if ( msg == 'backtrack' ) thestring = backtrackHovStr;
	else if ( msg == 'remove' ) thestring = undoHovStr;
	else if ( msg == 'marker-open' ) thestring = markerOpenHovStr;
	else if ( msg == 'marker-closed' ) thestring = markerClosedHovStr;
	else if ( msg == 'path-open' ) thestring = routeOpenHovStr;
	else if ( msg == 'path-closed' ) thestring = routeClosedHovStr;
	else if ( msg == 'off' )
		if ( pathEditable && markersEditable )
			thestring = startDualStr;
		else if ( pathEditable )
			thestring = startRouteStr;
		else if ( markersEditable )
			thestring = startMarkerStr;

	// show bar if it is not visible
	if ( thebar.style.display == 'none' )
		thebar.style.display == '';

	if ( !isTemp )
		lastInfoMsgIdent = msg;

	if ( thestring )
		thebar.innerHTML = thestring;
}

function setIconHilite( icon )
{
	if ( icon == 'water' || icon == 'food' || icon == 'view' || icon == 'bathroom' || icon == 'parking' || icon == 'other' ) {
		document.getElementById('iiwater').style.backgroundColor = normalColor;
		document.getElementById('iifood').style.backgroundColor = normalColor;
		document.getElementById('iiview').style.backgroundColor = normalColor;
		document.getElementById('iibathroom').style.backgroundColor = normalColor;
		document.getElementById('iiparking').style.backgroundColor = normalColor;
		document.getElementById('iiother').style.backgroundColor = normalColor;
		document.getElementById('iitostart').style.backgroundColor = normalColor;
		document.getElementById('iibacktrack').style.backgroundColor = normalColor;
		document.getElementById('iiremove').style.backgroundColor = normalColor;
	}

	if ( icon == 'water' ) document.getElementById('iiwater').style.backgroundColor = selectColor;
	if ( icon == 'food' ) document.getElementById('iifood').style.backgroundColor = selectColor;
	if ( icon == 'view' ) document.getElementById('iiview').style.backgroundColor = selectColor;
	if ( icon == 'bathroom' ) document.getElementById('iibathroom').style.backgroundColor = selectColor;
	if ( icon == 'parking' ) document.getElementById('iiparking').style.backgroundColor = selectColor;
	if ( icon == 'other' ) document.getElementById('iiother').style.backgroundColor = selectColor;
}

function handleMouseOut( icon )
{
	setInfoMessage( lastInfoMsgIdent, true );
	setIconHilite( editMode );
}

function handleMouseOver(  msg, isTemp  )
{
	setInfoMessage( msg, isTemp );

	if ( msg == 'water-h' && editMode != 'water' )
		document.getElementById('iiwater').style.backgroundColor = hoverColor;
	else if ( msg == 'food-h' && editMode != 'food' )
		document.getElementById('iifood').style.backgroundColor = hoverColor;
	else if ( msg == 'view-h' && editMode != 'view' )
		document.getElementById('iiview').style.backgroundColor = hoverColor;
	else if ( msg == 'bathroom-h' && editMode != 'bathroom' )
		document.getElementById('iibathroom').style.backgroundColor = hoverColor;
	else if ( msg == 'parking-h' && editMode != 'parking' )
		document.getElementById('iiparking').style.backgroundColor = hoverColor;
	else if ( msg == 'other-h' && editMode != 'other' )
		document.getElementById('iiother').style.backgroundColor = hoverColor;
	else if ( msg == 'tostart' )
		document.getElementById('iitostart').style.backgroundColor = hoverColor;
	else if ( msg == 'backtrack' )
		document.getElementById('iibacktrack').style.backgroundColor = hoverColor;
	else if ( msg == 'remove' )
		document.getElementById('iiremove').style.backgroundColor = hoverColor;
}

function setupEditButtons( autoSelectRoute)
{
	// markers only, adjust the positions of the marker button and panel
//	if ( markersEditable && !pathEditable ) {
//		document.getElementById('markerButton').style.left = '370px';
//		document.getElementById('markerOptions').style.left = '150px';
//		document.getElementById('markerLabelText').innerHTML = '&lt; Marker';
//	}

	if ( pathEditable )
		document.getElementById('pathButton').style.display = '';
	if ( markersEditable )
		document.getElementById('markerButton').style.display = '';
	if ( pathEditable || markersEditable ) {
		document.getElementById('infobar').style.display = '';
		setEditMode( 'off' );
	}
	if ( autoSelectRoute == '1' ) {
		setPathToolsSelected( true );
	}
}

function setMarkerToolsSelected( setOn )
{
	if ( setOn ) {
		document.getElementById('markerButton').style.display = 'none';
		document.getElementById('markerOptions').style.display = '';
		if ( pathEditable ) {
			document.getElementById('pathButton').style.left = 120 + mapOffsetX + 'px';
			setPathToolsSelected( false );
		}
		setEditMode("water");
	} else {
		document.getElementById('markerButton').style.display = '';
		document.getElementById('markerOptions').style.display = 'none';
		if (pathEditable) document.getElementById('pathButton').style.left = 155 + mapOffsetY + 'px';
		setEditMode("off")
	}
}

function setPathToolsSelected( setOn )
{
	if ( setOn ) {
		document.getElementById('pathButton').style.display = 'none';
		document.getElementById('pathOptions').style.display = '';
		if ( markersEditable ) setMarkerToolsSelected( false );
		setEditMode("line")
	} else {
		document.getElementById('pathButton').style.display = '';
		document.getElementById('pathOptions').style.display = 'none';
		setEditMode("off")
	}
}

// initialize icons for display purposes
function setupIcons()
{
	baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	startIcon = new GIcon(baseIcon);
	startIcon.image = "http://www.google.com/mapfiles/dd-start.png";
	endIcon = new GIcon(baseIcon);
	endIcon.image = "http://www.google.com/mapfiles/dd-end.png";
	pauseIcon = new GIcon(baseIcon);
	pauseIcon.image = "images/gicons/pause.png";
	resumeIcon = new GIcon(baseIcon);
	resumeIcon.image = "images/gicons/resume.png";
	startendIcon = new GIcon(baseIcon);
	startendIcon.image = "images/gicons/startstop.png";

	bimIcon = new GIcon();
	waterIcon = new GIcon(bimIcon);
	waterIcon.iconSize = new GSize(18, 22);
	waterIcon.iconAnchor = new GPoint(9, 11);
	waterIcon.image = "images/gicons/water.gif";
	viewIcon = new GIcon(bimIcon);
	viewIcon.iconSize = new GSize(28, 22);
	viewIcon.iconAnchor = new GPoint(14, 11);
	viewIcon.image = "images/gicons/view.gif";
	foodIcon = new GIcon(bimIcon);
	foodIcon.iconSize = new GSize(19, 22);
	foodIcon.iconAnchor = new GPoint(9, 11);
	foodIcon.image = "images/gicons/food.gif";
	bathroomIcon = new GIcon(bimIcon);
	bathroomIcon.iconSize = new GSize(30, 22);
	bathroomIcon.iconAnchor = new GPoint(15, 11);
	bathroomIcon.image = "images/gicons/toilet.gif";
	parkingIcon = new GIcon(bimIcon);
	parkingIcon.iconSize = new GSize(18, 22);
	parkingIcon.iconAnchor = new GPoint(9, 11);
	parkingIcon.image = "images/gicons/parking.gif";
	otherIcon = new GIcon(bimIcon);
	otherIcon.iconSize = new GSize(22, 22);
	otherIcon.iconAnchor = new GPoint(11, 11);
	otherIcon.image = "images/gicons/other.gif";
}

// move map position at page draw based on php set params in shared_header.tpl
function adjustMapPosition()
{
	var theObj;
	var colALoc;
	var screenFactor = 0;

	// fires function from template header, writes php set values to blobals mapOffsetX and mapOffsetY
	getMapOffset();

	if ( doGMapDynamOffset == 1 )
	{
		colALoc = getObjPos('colA');
		screenFactor = colALoc[0];
	}

	// funky stuff - unable to read the value of left and top, but able to set it
	// if the css defs for these items change, they must be changed here too

	theObj = document.getElementById('map');
	theObj.style.left = 13 + mapOffsetX + screenFactor + 'px';
	theObj.style.top = 193 + mapOffsetY + 'px';

	theObj = document.getElementById('markerOptions');
	theObj.style.left = 190 + mapOffsetX + screenFactor + 'px';
	theObj.style.top = 574 + mapOffsetY + 'px';

	theObj = document.getElementById('pathOptions');
	theObj.style.left = 64 + mapOffsetX + screenFactor + 'px';
	theObj.style.top = 574 + mapOffsetY + 'px';

	theObj = document.getElementById('markerButton');
	theObj.style.left = 226 + mapOffsetX + screenFactor + 'px';
	theObj.style.top = 576 + mapOffsetY + 'px';

	theObj = document.getElementById('pathButton');
	theObj.style.left = 155 + mapOffsetX + screenFactor + 'px';
	theObj.style.top = 576 + mapOffsetY + 'px';

	theObj = document.getElementById('infobar');
	theObj.style.left = 13 + mapOffsetX + screenFactor + 'px';
	theObj.style.top = 613 + mapOffsetY + 'px';
	//theObj.style.top = (613 + mapOffsetY - 17) + 'px';

	theObj = document.getElementById('infobarbottom');
	theObj.style.left = 13 + mapOffsetX + screenFactor + 'px';
	theObj.style.top = 629 + mapOffsetY + 'px';
	//theObj.style.top = (629 + mapOffsetY - 17) + 'px';

	theObj = document.getElementById('savebutton');
	theObj.style.left = 379 + mapOffsetX + screenFactor + 'px';
	theObj.style.top = 618 + mapOffsetY + 'px';
	//theObj.style.top = (618 + mapOffsetY - 17) + 'px';
}

function addMileMarker( theMileNum, theLat, theLon )
{
	// determine zoom threshold for display of this marker
//	if ( theMileNum div 100 == 0 )
//		pointZoomThreshold = 6;
//	else if ( theMileNum div 10 == 0 )
//		pointZoomThreshold = 5;
//	else if ( theMileNum div 5 == 0 )
//		pointZoomThreshold = 4;
//	else pointZoomThreshold = 3;

}