/// the google map object
var map;


/// the centerpoint of the search radius
var groundZero = new GPoint(strLon, strLat);
var pShield;
var pShieldedMap;

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function readjustPShield() 
{		
	pShield.style.top  = findPosY( pShieldedMap ) + 'px';

	pShield.style.left = findPosX( pShieldedMap ) + 'px';		
}

function recenterFix()
{
	map.centerAndZoom( groundZero, zoomLookupTable() );

	var tmpPnt = new GPoint( map.getBoundsLatLng().minX, map.getBoundsLatLng().minY );
	
	map.centerAndZoom( tmpPnt, zoomLookupTable() );
}

function onLoad() 
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap(document.getElementById("g_map"));

		document.getElementById("g_map").onclick = function() {};

		/// center on the new GPoint and set the zoom level.
		map.centerAndZoom( groundZero, zoomLookupTable() );

		/// change the radius of the circle to whatever value is selected.
		changeDrawCircleRadius( parseFloat(document.getElementById('radius_distance').value) );

		/// draw the circle
		drawCircle( groundZero.x,groundZero.y );

		/// add the centerpoint flag.
		map.addOverlay( new GMarker(groundZero) );

		/// setup the pShield!
		myGmapDiv = document.getElementById("g_map");
		myDiv = document.createElement( "div" );

		var myAdjust = 0;

		if( navigator.appName != "Netscape" )
		{
			//myAdjust = 15;
			myDiv.style.cursor = 'hand';
		} 
		else 
		{
			myDiv.style.cursor = 'pointer';
		}

		myDiv.style.position = 'absolute';
		myDiv.style.top  = (findPosY(myGmapDiv)-myAdjust )+'px';
		myDiv.style.left = findPosX(myGmapDiv)+'px';

		myDiv.valign = 'top';
		myDiv.vAlign = 'top';
		//myDiv.innerHTML = "<img src=/review/i/clear.gif width=160px height=112px border=0 \>"
		myDiv.innerHTML = "<img src=/review/i/clear.gif width=160px height=97px border=0 \>"

		myDiv.style.width  = '160px'
		//myDiv.style.height = '112px'
		myDiv.style.height = '97px'

		//myDiv.style.backgroundColor = 'red';


		myDiv.onclick = function() { openChildWindow(); };

		document.getElementsByTagName("body")[0].appendChild( myDiv );

		/// readjust the pshield
		if( document.parentWindow )
		{
			allA = document.getElementsByTagName( "body" );
			if( !allA[0] ) { alert("noes!"); return; }

			allA[0].onresize = function () { readjustPShield(); };
		}
		else
		{
			windObj = document.defaultView.onresize = function () { readjustPShield(); };
		}

		/// globalize them.
		pShield = myDiv;
		pShieldedMap = myGmapDiv;

		hTrigger();

		try{ fLatLonJazz(); } catch(ig){}
		
		try{ fTenAttmpt(); } catch(ig){}
	}
}

var __openChildWindow_run = false;

function wasCustomLocationSet()
{
	return __openChildWindow_run;
}

function openChildWindow() {

	__openChildWindow_run = true;

	radius = document.getElementById( 'radius_distance' ).value;
	addr = document.getElementById( 'radius_address' ).value;

	state = document.getElementById( 'loc_state' ).value;
	city = document.getElementById( 'loc_city' ).value;

	lat = document.getElementById( 'lat' ).value;
	lon = document.getElementById( 'lon' ).value;
	zip = document.getElementById( 'zip' ).value;

	params = "radius="+radius;
	if( lat && lon )
	{
		params+="&lat="+lat+"&lon="+lon;
	}

	if( zip )
	{
		params+="&zip="+zip;
	}

	if( state && city )
	{
		params+="&state="+state+"&city="+city;
	}

	if( addr )
	{
		params+="&addr="+addr;
	}

	try {
		var newWindow = window.open( "/mvc/MAP/?"+params, 'radius_window', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=550,height=465,left = 365,top = 287');
		newWindow.focus();	
		
	} catch( e ) {}
}