addLoadListener(initGoogleMap);

function initGoogleMap()
{
	var gmapLink = document.getElementById("google-map-link");
	gmapLink.onclick = createGoogleForm;

	return true;
}

function mySubmit(event)
{
	var fromAddress = document.getElementById('drive-from-address');
	var url = 'local.google.com/local?f=d&hl=en&saddr=&hl=en';
	//var myFrom = document.getElementById('fromaddress');
	var myTo = document.getElementById('toaddress');
	if (typeof event == "undefined")
	{
		event = window.event;
	}

	var target = getEventTarget(event);

	while (target.nodeName.toLowerCase() != "input")
	{
		target = target.parentNode;
	}

	var value = target.getAttribute("value");

	var dialog = target;

	while (dialog.className != "customDialog")
	{
		dialog = dialog.parentNode;
	}

	var resultFrom = myTo.value.replace(/[\f\n\r\t\v]|,/,'+');
	var goUrl = url + '&daddr='  + fromAddress.value + '&saddr=' + resultFrom + '&om=1';
	var external = window.open('http://' + goUrl);
	//makePopup( 'http://' + goUrl, 800, 600, 'scroll');
	closeDialog(dialog);
	return true;	
}

function createGoogleForm()
{
	try
	{
		var body = document.getElementById("google-div");

		var dialog = document.createElement("div");
		dialog.className = "customDialog";
		dialog.style.visibility = "hidden";
		dialog.style.position = "relative";

		var dialogTitle = document.createElement("h1");
		dialogTitle.appendChild(document.createTextNode("Get Driving Directions:"));
		dialog.appendChild(dialogTitle);

	//	var dialogInput1 = document.createElement("input");
	//	dialogInput1.setAttribute("id", "fromaddress");
	//	dialogInput1.setAttribute("type", "text");
	//	dialogInput1.setAttribute("size", "40");
	//	dialogInput1.setAttribute("nowrap", "nowrap");
	//	var directFromAddress = document.getElementById('drive-from-address');
	//	dialogInput1.setAttribute("value", directFromAddress.value );
		//attachEventListener(dialogButton1, "click", dialogClick, false);
	//	dialog.appendChild(dialogInput1);

		var dialogInput2 = document.createElement("input");
		dialogInput2.setAttribute("id", "toaddress");
		dialogInput2.setAttribute("type", "text");
		dialogInput2.setAttribute("size", "40");
		dialogInput2.setAttribute("value", "Your Address City,State Zip");
		attachEventListener(dialogInput2, "click", checkLink, false);
		dialog.appendChild(dialogInput2);

		var dialogButton = document.createElement("input");
		dialogButton.setAttribute("type", "button");
		dialogButton.setAttribute("value", "Get Directions");
		attachEventListener(dialogButton, "click", mySubmit, false);
		dialog.appendChild(dialogButton);

		body.appendChild(dialog);

		var scrollingPosition = getScrollingPosition();
		var viewportSize = getViewportSize();

		dialog.style.left = 0;//scrollingPosition[0] + parseInt(viewportSize[0] / 2) - parseInt(dialog.offsetWidth / 2) + "px";
		dialog.style.top = 0;//scrollingPosition[1] + parseInt(viewportSize[1] / 2) - parseInt(dialog.offsetHeight / 2) + "px";
		dialog.style.visibility = "visible";
		var gmapLink = document.getElementById("google-map-link");
		gmapLink.onclick = null;

		dialogButton.focus();
	}
	catch(err)
	{
		for (var i in err)
		{
			alert(i + ': ' + err[i]);
		}
		return true;
	}

	return false;
}
function checkLink()
{
	var toAddress = document.getElementById('toaddress');
	if( toAddress.value == 'Your Address City,State Zip')
	{
		toAddress.value = '';
	}
	return false;
}
function makePopup(url, width, height, overflow)
{
  if (width > 640) { width = 640; }
  if (height > 480) { height = 480; }

  if (overflow == '' || !/^(scroll|resize|both)$/.test(overflow))
  {
    overflow = 'both';
  }

  var win = window.open(url, '',
      'width=' + width + ',height=' + height
      + ',scrollbars=' + (/^(scroll|both)$/.test(overflow) ? 'yes' : 'no')
      + ',resizable=' + (/^(resize|both)$/.test(overflow) ? 'yes' : 'no')
      + ',status=yes,toolbar=yes,menubar=yes,location=yes'
  );

  return win;
}
function closeDialog(dialog)
{
	dialog.parentNode.removeChild(dialog);

	var gmapLink = document.getElementById("google-map-link");
	gmapLink.onclick = createGoogleForm;

	return true;
}

function getScrollingPosition()
{
	//array for X and Y scroll position
	var position = [0, 0];

	//if the window.pageYOffset property is supported
	if (typeof window.pageYOffset != 'undefined')
	{
		//store position values
		position = [
			window.pageXOffset,
			window.pageYOffset
				];
	}

	//if the documentElement.scrollTop property is supported
	//and the value is greater than zero
	if (typeof document.documentElement.scrollTop != 'undefined'
			&& document.documentElement.scrollTop > 0)
	{
		//store position values
		position = [
			document.documentElement.scrollLeft,
			document.documentElement.scrollTop
				];
	}

	//if the body.scrollTop property is supported
	else if(typeof document.body.scrollTop != 'undefined')
	{
		//store position values
		position = [
			document.body.scrollLeft,
			document.body.scrollTop
				];
	}

	//return the array
	return position;
}

function getViewportSize()
{
	var size = [0,0];

	if (typeof window.innerWidth != 'undefined')
	{
		size = [
			window.innerWidth,
			window.innerHeight
				];
	}
	else if (typeof document.documentElement != 'undefined'
			&& typeof document.documentElement.clientWidth != 'undefined'
			&& document.documentElement.clientWidth != 0)
	{
		size = [
			document.documentElement.clientWidth,
			document.documentElement.clientHeight
				];
	}
	else
	{
		size = [
			document.getElementsByTagName('body')[0].clientWidth,
			document.getElementsByTagName('body')[0].clientHeight
				];
	}

	return size;
}
