/*!
 * CKBE Google Maps Functions
 * http://www.ckbe.com/
 *
 * Copyright 2011, CKBE
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: Sun Jun 06 18:57:26 2010
 */

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Start Load Map Functions.

window.onload=google_loadMap;

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Global Variables.

var map;
var geocoder;
var directionsPanel;
var directions;

var baseIcon = new GIcon(G_DEFAULT_ICON);
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);

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Map Functions.

function google_loadMap() {
	
	if (GBrowserIsCompatible()) {
	
		geocoder = new GClientGeocoder();
		map = new GMap2(document.getElementById('map'));
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GScaleControl());
		map.setCenter(new GLatLng(25.7742657, -80.1936589), 8);
		google_searchLocations();
	
	}
	
}

function google_searchLocations() {
	
	var address = document.getElementById('address').value;
	
	geocoder.getLatLng(address, function(latlng) {
		
		if (!latlng) {
			
			alert('There was an unexpected error. Please try again.');
		
		} else {
			
			google_searchLocationsNear(latlng);
		
		}
		
	});

}

function google_searchLocationsNear(center) {
	
	var radius = document.getElementById('radius').value;
	var searchString = '/frame/frame_map_store_locator.html';
	
	if (document.getElementById('type').value == 'zip') {
		
		searchString = searchString + '?zip=' + document.getElementById('address').value;
		
	} else {
		
		searchString = searchString + '?id=' + document.getElementById('id').value;
		
	}
	
	var searchUrl = searchString + '&lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
	
	GDownloadUrl(searchUrl, function(data) {
									 
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName('marker');
		map.clearOverlays();
		
		var sidebar = document.getElementById('sidebar');
		sidebar.innerHTML = '';
		
		if (markers.length == 0) {
			
			sidebar.innerHTML = '';
			map.setCenter(new GLatLng(40, -100), 4);
			return;
		
		}
		
		var bounds = new GLatLngBounds();
		
		for (var i = 0; i < markers.length; i++) {
			
			var position = markers[i].getAttribute('position');
			var id = markers[i].getAttribute('id');
			var name = markers[i].getAttribute('name');
			var address = markers[i].getAttribute('address');
			var city = markers[i].getAttribute('city');
			var state = markers[i].getAttribute('state');
			var zip = markers[i].getAttribute('zip');
			var phone = markers[i].getAttribute('phone');
			var email = markers[i].getAttribute('email');
			var distance = parseFloat(markers[i].getAttribute('distance'));
			var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
			parseFloat(markers[i].getAttribute('lng')));
			
			var marker = google_createMarker(point, position, id, name, address, city, state, zip, phone, email, distance);
			map.addOverlay(marker);
			
			var sidebarEntry = google_createSidebarEntry(marker, id, name, address, city, state, zip, phone, email, distance);
			sidebar.appendChild(sidebarEntry);
			
			bounds.extend(point);
		
		}
		
		var zoom = 15;
		
		if (markers.length != 1) {
			zoom = map.getBoundsZoomLevel(bounds);
		}
		
		map.setCenter(bounds.getCenter(), zoom);
	
	});

}

function google_createMarker(point, position, id, name, address, city, state, zip, phone, email, distance) {
	
	var letter;
	var letteredIcon = new GIcon(baseIcon);
	
	if (position == 0) {
		
		letter = 0;
		
	} else {
	
		letter = String.fromCharCode("A".charCodeAt(0) + (position-1));
	
	}
	
	letteredIcon.image = "https://media.eldoradofurniture.com/images/websites/www.eldoradofurniture.com/layout/mrk_red_" + letter + ".png";
	
	var markerOptions = { icon:letteredIcon };
	var marker = new GMarker(point, markerOptions);
	
	var html;
	
	if (document.getElementById('type').value == 'zip') {
		
		html = '<img src="https://media.eldoradofurniture.com/images/websites/www.eldoradofurniture.com/content/store_thumb.jpg" style="float:left; width:77px; height:60px; margin-right:10px;"><div style="float:left; width:180px;"><div class="small"><strong>' + name + '</strong></div><div class="small" style="margin-top:5px;">' + address + '<br />' + city + ', ' + state + ' ' + zip + '<br />' + phone + '</div><div class="instruction" style="margin-top:10px;">' + distance.toFixed(1) + ' Miles Away<div/><div class="instruction" style="margin-top:5px;"><a href="/stores/index.html?id=' + id + '" class="instruction">Store Hours & Directions</a></div></div><div class="clear"></div>';
		
	} else {
	
		html = '<img src="https://media.eldoradofurniture.com/images/websites/www.eldoradofurniture.com/content/store_thumb.jpg" style="float:left; width:77px; height:60px; margin-right:10px;"><div style="float:left; width:180px;"><div class="small"><strong>' + name + '</strong></div><div class="small" style="margin-top:5px;">' + address + '<br />' + city + ', ' + state + ' ' + zip + '<br />' + phone + '</div><div class="instruction" style="margin-top:10px;"><a href="mailto:' + email + '" class="instruction">Email this Store</a></div></div><div class="clear"></div>';
	
	}
	
	GEvent.addListener(marker, "click", function() {
												 
		marker.openInfoWindowHtml(html);
	
	});
	
	return marker;

}

function google_createSidebarEntry(marker, id, name, address, city, state, zip, phone, email, distance) {
	
	var div = document.createElement('div');
	var html;
	
	if (document.getElementById('type').value == 'zip') {
		
		html = '<div class="small"><strong>' + name + '</strong></div><div class="small" style="margin-top:5px;">' + address + '<br />' + city + ', ' + state + ' ' + zip + '<br />' + phone + '</div><div class="instruction" style="margin-top:10px;">' + distance.toFixed(1) + ' Miles Away<div/><div class="instruction" style="margin-top:5px;"><a href="/stores/index.html?id=' + id + '" class="instruction">Store Hours & Directions</a></div><div class="spr-hrz" style="margin-top:10px;"></div>';
		
	} else {
		
		// DevNote: The type is "id" in the detail screen but we don't output any list content from the script.
		html = '';
		
	}
	
	div.innerHTML = html;
	//div.style.cursor = 'pointer';
	div.style.marginBottom = '10px'; 
	
	/*GEvent.addDomListener(div, 'click', function() {
												 
		GEvent.trigger(marker, 'click');
	
	});
	
	GEvent.addDomListener(div, 'mouseover', function() {
													 
		div.style.backgroundColor = '#eee';
	
	});
	
	GEvent.addDomListener(div, 'mouseout', function() {
													
		div.style.backgroundColor = '#fff';
	
	});*/
	
	return div;

}

function google_getDirections() {
	
	map = new GMap2(document.getElementById('map'));
	directionsPanel = document.getElementById('directions');
	//map.setCenter(new GLatLng(49.496675,-102.65625), 3);
	directions = new GDirections(map, directionsPanel);
	directions.load('from: ' + document.getElementById('from').value + ' to: ' + document.getElementById('to').value);

}
