// ***********
// Global Vars
// **********

var startZoom = 13;
var map;
var opt = {};
var markers = [];

// **********

var map_locations = {
  'orchard_lake' : {
    'latitude': 42.577434,
    'longitude': -83.362316,
	'status': '',
	'phone': '248-865-0000',
	'title': 'Orchard Lake',
    'description': '4189 Orchard Lake Road, Orchard Lake Township MI 48323'
  },
  'royal_oak' : {
    'latitude': 42.486419,
    'longitude': -83.145808,
	'status': '',
	'phone': '248-414-7000',
	'title': 'Royal Oak',
    'description': '212 Fifth Avenue, Royal Oak MI 48067'
  },
  'livonia' : {
    'latitude': 42.410657,
    'longitude': -83.412988,
	'status': '',
	'phone': '734-464-8200',
	'title': 'Livonia',
    'description': '37367 6 mile Road, Livonia MI 48152'
  },
  'southfield' : {
    'latitude': 42.4837,
    'longitude': -83.285559,
	'status': '<b> - Coming Soon</b>',
	'phone': '248-350-1800',
	'title': 'Southfield',
    'description': '26555 Telegraph Road, Southfield MI 48033'
  },
  'rochester_hills' : {
    'latitude': 42.693902,
    'longitude': -83.132418,
	'status': '<b> - Coming Soon</b>',
	'phone': '248-601-2121',
	'title': 'Rochester Hills',
    'description': '1470 Rochester Road, Rochester Hills MI 48306'
  }
};

function init() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("loc_map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
	//map.enableGoogleBar(true); // This turns on the google map search bar.
	map.enableScrollWheelZoom(true);
	
	// Below creates an icon specific to Mezza Mediterranean Grille
	var baseIcon = new GIcon();
	baseIcon.image = "/images/locations/mezza_sign.png";
	baseIcon.shadow = "/images/locations/mezza_sign_shadow.png";
	baseIcon.iconSize = new GSize(46, 61);
	baseIcon.shadowSize = new GSize(91, 61); 
	baseIcon.iconAnchor = new GPoint(20,61);
	baseIcon.infoWindowAnchor = new GPoint(20,7);
	opt.icon = baseIcon;

	add_all_markers();
	
	locate_mezza('orchard_lake', '0');
  }
}

function locate_mezza(map_location, loc_id) {
    var loc = map_locations[map_location];
    var loc_pos = new GLatLng(loc.latitude, loc.longitude);
    var center_point = new GLatLng(loc.latitude + 0.01, loc.longitude);
    map.setCenter(center_point, startZoom);
	show_map_location(map_location, createBubbleHtml(loc));

	if (map_location == 'southfield') $("#loc_address_text").css("color", "#502414"); else $("#loc_address_text").css("color", "white");
    $("#loc_address_bar").css("background-position", "0" + " -" + (30 * loc_id) + "px");
    $("#loc_address_text").html("<b>" + loc.title + "</b>" + ": " + loc.description + loc.status);
}

function show_map_location(map_location, bubble_html){
    markers[map_location].openInfoWindowHtml(bubble_html);
}

function add_all_markers (){
	for(var map_location in map_locations){
     	var loc = map_locations[map_location];
		var loc_pos = new GLatLng(loc.latitude, loc.longitude);
		
		if(map_location == 'orchard_lake'){
    		var center_point = new GLatLng(loc.latitude + 0.01, loc.longitude);
    		map.setCenter(center_point, startZoom);
		}

		createMarker(map_location, loc, loc_pos);
	}
}

function createMarker(map_location, loc, loc_pos) {
	var marker = new GMarker(loc_pos, opt);
	markers[map_location] = marker;
	var bubble_html = createBubbleHtml(loc);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(bubble_html);
	});

	map.addOverlay(marker);
}

function createBubbleHtml(loc) {
	var ld_array = loc.description.split(",");
	return "<b>Mezza Mediterranean Grille</b>" + "<br/>" + ld_array[0] + "<br/>" + ld_array[1] + "<br/><br/>" + "Phone: " + loc.phone;
}


window.onload = init;
window.onunload = GUnload;
