var points = [];
var markers = [];
var counter = 0;
var sidebar_html = "";
var marker_html = [];
var to_htmls = [];
var from_htmls = [];
var icon = [];
icon[0] = new GIcon();
icon[0].image = "/ajax/images/butt.png";
icon[0].shadow = "/ajax/images/butt.png";
icon[0].shadowSize = new GSize(32,32);
icon[0].iconSize = new GSize(32,32);
icon[0].iconAnchor = new GPoint(16,16);
icon[0].infoWindowAnchor = new GPoint(16,16);
var map = null;

function callLoad() {
	var mstr = document.getElementById('mpjs').innerHTML;
	$marr = mstr.split("//break");
	eval($marr[1]+" onLoad();");
}

function gmZindex(){
	return 1;
}

function createMarker(point, title, html, n, tooltip) {
if(n >= 1) { n = 0; }
var marker = new GMarker(point,{'icon': icon[n], 'title': tooltip, 'zIndexProcess': gmZindex});
var tabFlag = isArray(html);
if(!tabFlag) { html = [{"contentElem": html}]; }
to_htmls[counter] = html[0].contentElem + '<form class="gmapDir" id="gmapDirTo" style="white-space:nowrap;font-size:12px;font-weight:normal;" action="http://maps.google.com/maps" method="get">' +
                     '<span class="gmapDirHead" id="gmapDirHeadTo">Directions: <strong>To here</strong> - <a href="javascript:fromhere(' + counter + ')">From here</a></span>' +
                     '<p class="gmapDirItem" id="gmapDirItemTo"><label for="gmapDirSaddr" class="gmapDirLabel" id="gmapDirLabelTo">Start address: (include addr, city st/region)<br /></label>' +
                     '<input type="text" size="20" maxlength="40" name="saddr" class="gmapTextBox" id="gmapDirSaddr" value="" onfocus="this.style.backgroundColor = \'#e0e0e0\';" onblur="this.style.backgroundColor = \'#ffffff\';" />' +
                     '<span class="gmapDirBtns" id="gmapDirBtnsTo"><input value="Go" type="submit" class="gmapDirButton" id="gmapDirButtonTo" /></span></p>' +
                     '<input type="hidden" name="daddr" value="' +
                     point.y + ',' + point.x + "(" + title.replace(new RegExp(/"/g),'&quot;') + ")" + '" /></form>';
from_htmls[counter] = html[0].contentElem + '<p /><form class="gmapDir" id="gmapDirFrom" style="white-space:nowrap;font-size:12px;font-weight:normal;" action="http://maps.google.com/maps" method="get">' +
                     '<span class="gmapDirHead" id="gmapDirHeadFrom">Directions: <a href="javascript:tohere(' + counter + ')">To here</a> - <strong>From here</strong></span>' +
                     '<p class="gmapDirItem" id="gmapDirItemFrom"><label for="gmapDirSaddr" class="gmapDirLabel" id="gmapDirLabelFrom">End address: (include addr, city st/region)<br /></label>' +
                     '<input type="text" size="20" maxlength="40" name="daddr" class="gmapTextBox" id="gmapDirSaddr" value="" onfocus="this.style.backgroundColor = \'#e0e0e0\';" onblur="this.style.backgroundColor = \'#ffffff\';" />' +
                     '<span class="gmapDirBtns" id="gmapDirBtnsFrom"><input value="Go" type="submit" class="gmapDirButton" id="gmapDirButtonFrom" /></span></p>' +
                     '<input type="hidden" name="saddr" value="' +
                     point.y + ',' + point.x + encodeURIComponent("(" + title.replace(new RegExp(/"/g),'&quot;') + ")") + '" /></form>';
html[0].contentElem = html[0].contentElem + '<p /><div id="gmapDirHead" class="gmapDir" style="white-space:nowrap;font-size:12px;font-weight:normal;">Directions: <a href="javascript:tohere(' + counter + ')">To here</a> - <a href="javascript:fromhere(' + counter + ')">From here</a></div>';

if(!tabFlag) { html = html[0].contentElem; }if(isArray(html)) { GEvent.addListener(marker, "click", function() { marker.openInfoWindowTabsHtml(html); }); }
else { GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); }); }

points[counter] = point;
markers[counter] = marker;
marker_html[counter] = html;
sidebar_html += '<li class="gmapSidebarItem" id="gmapSidebarItem_'+ counter +'"><a href="javascript:click_sidebar(' + counter + ')">' + title + '<\/a><\/li>';
counter++;
return marker;
}
function isArray(a) {return isObject(a) && a.constructor == Array;}
function isObject(a) {return (a && typeof a == 'object') || isFunction(a);}
function isFunction(a) {return typeof a == 'function';}
function click_sidebar(idx) {
  if(isArray(marker_html[idx])) { markers[idx].openInfoWindowTabsHtml(marker_html[idx]); }
  else { markers[idx].openInfoWindowHtml(marker_html[idx]); }
}
function showInfoWindow(idx,html) {
map.centerAtLatLng(points[idx]);
markers[idx].openInfoWindowHtml(html);
}
function tohere(idx) {
markers[idx].openInfoWindowHtml(to_htmls[idx]);
}
function fromhere(idx) {
markers[idx].openInfoWindowHtml(from_htmls[idx]);
}

function driveToNow(obj){
	var address_dest = obj.elements['daddr'].value;
	var address_src = obj.elements['saddr'].value;
	var panel = document.getElementById("panel");
	window.scrollTo(0,0);
	panel.innerHTML = '';
	map.clearOverlays();
	var dir = new GDirections(map, panel);
	dir.load("from: "+address_src+" to: "+address_dest);
	return false;
}

function driveFromNow(obj){
	var address_dest = obj.elements['daddr'].value;
	var address_src = obj.elements['saddr'].value;
	var panel = document.getElementById("panel");	
	window.scrollTo(0,0);
	panel.innerHTML = '';
	map.clearOverlays();
	var dir = new GDirections(map, panel);
	dir.load("from: "+address_src+" to: "+address_dest);	
	return false;
}

function refreshMap(){
	var panel = document.getElementById("panel");
	panel.innerHTML = "";
	ldlSearchInit();
	callLoad();
}
