//<![CDATA[
var map;
var geocoder;
	
// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
var cm_baseIcon = new GIcon();
cm_baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
cm_baseIcon.iconSize = new GSize(20, 34);
cm_baseIcon.shadowSize = new GSize(27, 24);
cm_baseIcon.iconAnchor = new GPoint(9, 34);
cm_baseIcon.infoWindowAnchor = new GPoint(9, 2);
cm_baseIcon.infoShadowAnchor = new GPoint(18, 25);

var tooltip;
var tooltip_status='N';

// START OF ZOOM IN AND ZOOM OUT GRAPHICS ICONS

function TextualZoomControl() {
}
TextualZoomControl.prototype = new GControl();
TextualZoomControl.prototype.initialize = function(map) {
var container = document.createElement("div");
var zoomInDiv = document.createElement("img");
zoomInDiv.src = "/images/zoom_in.png";
zoomInDiv.style.zIndex = -1;
container.appendChild(zoomInDiv);
GEvent.addDomListener(zoomInDiv, "click", function() {
map.zoomIn();
});
var br = document.createElement("br");
container.appendChild(br);
var zoomOutDiv = document.createElement("img");
zoomOutDiv.src = "/images/zoom_out.png";
zoomInDiv.style.zIndex = -1;
container.appendChild(zoomOutDiv);
GEvent.addDomListener(zoomOutDiv, "click", function() {
map.zoomOut();
});
map.getContainer().appendChild(container);
return container;
}
TextualZoomControl.prototype.getDefaultPosition = function() {
return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}

// END OF ZOOM IN AND ZOOM OUT GRAPHICS ICONS

// START OF PAN DIRECTION UP GRAPHICS ICONS

function TextualPanDirectionUp() {
}
TextualPanDirectionUp.prototype = new GControl();
TextualPanDirectionUp.prototype.initialize = function(map) {
var container = document.createElement("div");
var zoomInDiv = document.createElement("img");
zoomInDiv.src = "/images/map_arrow_up.png";
zoomInDiv.style.zIndex = -1;
container.appendChild(zoomInDiv);
GEvent.addDomListener(zoomInDiv, "click", function() {
map.panDirection(0,1);
});
map.getContainer().appendChild(container);
return container;
}
TextualPanDirectionUp.prototype.getDefaultPosition = function() {
return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(137, 7));
}

// END OF PAN DIRECTION UP GRAPHICS ICONS

// START OF PAN DIRECTION DOWN GRAPHICS ICONS

function TextualPanDirectionDown() {
}
TextualPanDirectionDown.prototype = new GControl();
TextualPanDirectionDown.prototype.initialize = function(map) {
var container = document.createElement("div");
var zoomInDiv = document.createElement("img");
zoomInDiv.src = "/images/map_arrow_down.png";
zoomInDiv.style.zIndex = -1;
container.appendChild(zoomInDiv);
GEvent.addDomListener(zoomInDiv, "click", function() {
map.panDirection(0,-1);
});
map.getContainer().appendChild(container);
return container;
}
TextualPanDirectionDown.prototype.getDefaultPosition = function() {
return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(137, 7));
}

// END OF PAN DIRECTION DOWN GRAPHICS ICONS

// START OF PAN DIRECTION LEFT GRAPHICS ICONS

function TextualPanDirectionLeft() {
}
TextualPanDirectionLeft.prototype = new GControl();
TextualPanDirectionLeft.prototype.initialize = function(map) {
var container = document.createElement("div");
container.style.zIndex = 1; 
container.style.position = 'absolute'; 
var zoomInDiv = document.createElement("img");
zoomInDiv.src = "/images/map_arrow_left.png";
zoomInDiv.style.zIndex = 1;
zoomInDiv.style.position = 'absolute'; 
container.appendChild(zoomInDiv);
GEvent.addDomListener(zoomInDiv, "click", function() {
map.panDirection(1,0);
});
map.getContainer().appendChild(container);
return container;
}
TextualPanDirectionLeft.prototype.getDefaultPosition = function() {
return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 100));
}

// END OF PAN DIRECTION LEFT GRAPHICS ICONS

// START OF  PAN DIRECTION RIGHT GRAPHICS ICONS

function TextualPanDirectionRight() {
}
TextualPanDirectionRight.prototype = new GControl();
TextualPanDirectionRight.prototype.initialize = function(map) {
var container = document.createElement("div");
var zoomInDiv = document.createElement("img");
zoomInDiv.src = "/images/map_arrow_right.png";
zoomInDiv.style.zIndex = -1;
container.appendChild(zoomInDiv);
GEvent.addDomListener(zoomInDiv, "click", function() {
map.panDirection(-1,0);
});
map.getContainer().appendChild(container);
return container;
}
TextualPanDirectionRight.prototype.getDefaultPosition = function() {
return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 100));
}

// END OF  PAN DIRECTION RIGHT GRAPHICS ICONS

// START OF LOAD GOOGLE MAP

function load() {
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();
map = new GMap2(document.getElementById('map'));
map.addControl(new TextualZoomControl());
map.addControl(new TextualPanDirectionUp());
map.addControl(new TextualPanDirectionDown());
map.addControl(new TextualPanDirectionLeft());
map.addControl(new TextualPanDirectionRight());
//map.addControl(new GSmallMapControl());
// map.addControl(new GMapTypeControl());

var latitude = document.getElementById('latitude').value;
var longitude = document.getElementById('longitude').value;
map.setCenter(new GLatLng(latitude, longitude), 11);
}
}

function initMap() {
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();
map = new GMap2(document.getElementById('map'));
map.addControl(new TextualZoomControl());
map.addControl(new TextualPanDirectionUp());
map.addControl(new TextualPanDirectionDown());
map.addControl(new TextualPanDirectionLeft());
map.addControl(new TextualPanDirectionRight());
//map.addControl(new GSmallMapControl());
// map.addControl(new GMapTypeControl());

var latitude = document.getElementById('latitude').value;
var longitude = document.getElementById('longitude').value;
map.setCenter(new GLatLng(latitude, longitude), 11);
}
}

// END OF LOAD GOOGLE MAP

//  get current page address latitude and longitude from google map

function searchLocations(subcatid, lid, listtype) {
/*var address = document.getElementById('addressInput').value;
geocoder.getLatLng(address, function(latlng) {
if (!latlng) {
//alert(address + ' not found');
} else {
searchLocationsNear(latlng, subcatid, lid, listtype);
}
});*/
var latitude = document.getElementById('latitude').value;
var longitude = document.getElementById('longitude').value;
searchLocationsNear(latitude, longitude,  subcatid, lid, listtype);
}

// search nearby listings

function searchLocationsNear(latitude,longitude,subcatid,lid,listtype) {
//Set the radius 
var curlid = lid;
var radius = 25;
var searchUrl = '/googlemap_xml.php?lat=' + latitude + '&lng=' + longitude + '&radius=' + radius + '&subcatid='+ subcatid +'&lid=' + lid +'&listtype=' + listtype;
GDownloadUrl(searchUrl, function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName('marker');
var currentmarker = xml.documentElement.getElementsByTagName('currentmarker');
map.clearOverlays();

var sidebar = document.getElementById('sidebar-data');
sidebar.innerHTML = '';
if (markers.length == 1) {
if(subcatid == '1')	{
sidebar.innerHTML = '&nbsp;&nbsp;No nearby hotels found.';
}else if(subcatid == '2'){
sidebar.innerHTML = '&nbsp;&nbsp;No nearby attractions found.';
}else if(subcatid == '3'){
sidebar.innerHTML = '&nbsp;&nbsp;No nearby restaurants found.';	
} else if(subcatid == '11'){
sidebar.innerHTML = '&nbsp;&nbsp;No nearby business & education found.';	
}else if(subcatid == '12'){
sidebar.innerHTML = '&nbsp;&nbsp;No nearby community found.';	
}else if(subcatid == '13'){
sidebar.innerHTML = '&nbsp;&nbsp;No nearby music found.';	
}
//map.setCenter(new GLatLng(latitude, longitude), 11);
//return;
}

var bounds = new GLatLngBounds();

// near by listings

for (var i = 0; i < markers.length; i++) {
var urllistname = markers[i].getAttribute('urllistname');
var name = markers[i].getAttribute('name');
var address = markers[i].getAttribute('address');
var distance = parseFloat(markers[i].getAttribute('distance'));
var lid = parseFloat(markers[i].getAttribute('lid'));
var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
parseFloat(markers[i].getAttribute('lng')));

var rank = "pin_"+ parseInt(i+1);
var dot = "dot_"+ parseInt(i+1);
var imgid =  parseInt(i+1)
var div = document.createElement('div');


if(i % 2 == 0)
var bgcolor = "#FFFFFF";
else
var bgcolor = "#f1ede0";

var html = '<table width="100%" border="0" cellspacing="2" cellpadding="3" bgcolor="'+ bgcolor +'"><tr><td width="10%" align="left"><img src="/images/'+ dot +'.png" border=0 id="img'+ imgid +'" /></td><td width="70%" align="left" style="font:11px Verdana, Arial, Helvetica, sans-serif;"><div align="left"><a href="/listings/detail/'+ lid +'/'+ urllistname +'">'+ name +'</a></div></td><td width="20%" align="right" style="font:11px Verdana, Arial, Helvetica, sans-serif;">'+ distance.toFixed(1) +' mi</td></tr></table>';

div.innerHTML = html;
div.style.cursor = 'pointer';
div.style.marginBottom = '0px'; 
div.style.border = '1px solid #FFFFFF';

if(lid == curlid) {
div.style.display = 'none';
}
	  
var marker = createMarker(point, name, address, rank, div,imgid, lid, urllistname,curlid);
map.addOverlay(marker);
var sidebarEntry = createSidebarEntry(marker, name, address, distance, div, imgid, lid, curlid);
sidebar.appendChild(sidebarEntry);
bounds.extend(point);
}
//map.setCenter(bounds.getCenter(), 11);
map.setCenter(new GLatLng(latitude, longitude), 11);
});
}
	  
// create marker icons and openwindow messages

function createMarker(point, name, address, rank, div, imgid, lid, urllistname,curlid) {
var markerOpts = {};
var nIcon = new GIcon(cm_baseIcon);
if(lid == curlid) {
nIcon.imageOut = "/images/pin_solo.png"
nIcon.imageOver = "/images/pin_solo_select.png"
} else {
nIcon.imageOut = "/images/" + rank + ".png"
nIcon.imageOver = "/images/" + rank + "_select.png"
}  
nIcon.image = nIcon.imageOut;
markerOpts.icon = nIcon;
markerOpts.title = name;
markerOpts.zIndex = 500;
  
var addressSplit =   address.split(",");
var openWinMssgs = '<a href="/listings/detail/'+ lid +'/'+ urllistname +'"<b>' + name + '</b></a><br>' + addressSplit[0] +'<br>'+ addressSplit[1] + ',' + addressSplit[2]; 

var marker = new PdMarker(point, markerOpts);
	
GEvent.addListener(marker, "click", function() {
marker.setTooltip(openWinMssgs);
});

GEvent.addListener(marker, "mouseover", function() {
marker.setTooltip(openWinMssgs);
div.style.border = '1px solid #000000';
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '_select.png'
marker.setImage(marker.getIcon().imageOver);
marker.topMarkerZIndex();
marker.setOpacity(100); // not transparent
var id = marker.getId();

var previousid = document.getElementById('previousid').value
if(previousid != "" && id != previousid)
//document.getElementById("ttobj" + previousid).style.display = 'none'
document.getElementById('previousid').value = id
marker.setTooltipHiding(false); // never hide tooltip
});

GEvent.addListener(marker, "mouseout", function() {

var id = marker.getId();
tooltipObj = document.getElementById("ttobj"+id);
GEvent.addDomListener(tooltipObj, 'mouseover', function() {
marker.setImage(marker.getIcon().imageOver);
marker.topMarkerZIndex();														
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '_select.png'
div.style.border = '1px solid #000000';
marker.setOpacity(100); // not transparent
tooltip_status = 'Y';
});

GEvent.addDomListener(tooltipObj, 'mouseout', function() {
tooltip_status = 'N';
//selectTimeout(div,imgid,marker);
div.style.border = '1px solid #FFFFFF';												
marker.setOpacity(0);
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '.png'
marker.setImage(marker.getIcon().imageOut);
marker.restoreMarkerZIndex();

});
		
if(tooltip_status == 'N'){
//	div.style.border = '0px solid #000000';												
//	marker.setOpacity(0);
//selectTimeout(div,imgid,marker);
div.style.border = '1px solid #FFFFFF';												
marker.setOpacity(0);
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '.png'
marker.setImage(marker.getIcon().imageOut);
marker.restoreMarkerZIndex();
}

});

GEvent.addListener(marker, "infowindowopen", function() {
div.style.border = '1px solid #000000';
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '_select.png'
marker.setImage(marker.getIcon().imageOver);
marker.setMouseOutEnabled(false); 
marker.setOpacity(100); // not transparent
});
GEvent.addListener(marker, "infowindowclose", function() {
div.style.border = '1px solid #FFFFFF';
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '.png'
marker.setImage(marker.getIcon().imageOut);
marker.setMouseOutEnabled(true); 
marker.setOpacity(100); // 30% transparent
});
return marker;
}

// right nearby listings view

function createSidebarEntry(marker, name, address, distance, div, imgid, lid, curlid) {

if(lid == curlid) {
div.style.display = 'none';
marker.topMarkerZIndex();

GEvent.addDomListener(document.getElementById("currentlist"), 'mouseover', function() {
GEvent.trigger(marker, 'mouseover');
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '_select.png'
div.style.border = '1px solid #000000';
marker.setOpacity(100); // not transparent

var id = marker.getId();

tooltipObj = document.getElementById("ttobj"+id);

GEvent.addDomListener(tooltipObj, 'mouseover', function() {
tooltipObj.style.position = "absolute";
tooltipObj.style.zIndex = "6000005";

marker.setImage(marker.getIcon().imageOver);
marker.topMarkerZIndex();
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '_select.png'
div.style.border = '1px solid #000000';
marker.setOpacity(100); // not transparent
tooltip_status = 'Y';
});

GEvent.addDomListener(tooltipObj, 'mouseout', function() {
tooltip_status = 'N';
div.style.border = '1px solid #FFFFFF';												
marker.setOpacity(0);
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '.png'
marker.setImage(marker.getIcon().imageOut);
marker.restoreMarkerZIndex();
//selectTimeout(div,imgid,marker);
});
});

GEvent.addDomListener(document.getElementById("currentlist"), 'mouseout', function() {
if(tooltip_status == 'N'){
div.style.border = '1px solid #FFFFFF';												
marker.setOpacity(0);
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '.png'
marker.setImage(marker.getIcon().imageOut);
marker.restoreMarkerZIndex();
//selectTimeout(div,imgid,marker);
}
//selectTimeout(div,imgid,marker);
//marker.setTooltipHiding(true);
});
} else {
GEvent.addDomListener(div, 'click', function() {
GEvent.trigger(marker, 'click');
});

GEvent.addDomListener(div, 'mouseover', function() {
GEvent.trigger(marker, 'mouseover');
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '_select.png'
div.style.border = '1px solid #000000';
marker.setOpacity(100); // not transparent

var id = marker.getId();

tooltipObj = document.getElementById("ttobj"+id);

GEvent.addDomListener(tooltipObj, 'mouseover', function() {
tooltipObj.style.position = "absolute";
tooltipObj.style.zIndex = "6000005";

marker.setImage(marker.getIcon().imageOver);
marker.topMarkerZIndex();
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '_select.png'
div.style.border = '1px solid #000000';
marker.setOpacity(100); // not transparent
tooltip_status = 'Y';
});

GEvent.addDomListener(tooltipObj, 'mouseout', function() {
tooltip_status = 'N';
div.style.border = '1px solid #FFFFFF';												
marker.setOpacity(0);
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '.png'
marker.setImage(marker.getIcon().imageOut);
marker.restoreMarkerZIndex();
//selectTimeout(div,imgid,marker);
});
});

GEvent.addDomListener(div, 'mouseout', function() {
if(tooltip_status == 'N'){
div.style.border = '1px solid #FFFFFF';												
marker.setOpacity(0);
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '.png'
marker.setImage(marker.getIcon().imageOut);
marker.restoreMarkerZIndex();
//selectTimeout(div,imgid,marker);
}
//selectTimeout(div,imgid,marker);
//marker.setTooltipHiding(true);
});

}
return div;
}
//]]>


function selectTimeout(div, imgid, marker) {
setTimeout(function(){Select(div,imgid,marker)}, 1000);
}

function Select(div,imgid,marker) {

if(tooltip_status == 'N'){
//div.style.border = '1px solid #FFFFFF';
document.getElementById('img' + imgid).src = '/images/dot_' + imgid + '.png'
marker.setImage(marker.getIcon().imageOut);
marker.restoreMarkerZIndex();
//var setting = marker.getTooltipHiding();
var id = marker.getId();
//document.getElementById("ttobj" + id).style.display = 'none'
div.style.border = '1px solid #000000';												
marker.setOpacity(0);
}
}
