
var arrOfficeTabs = new Array("Branch","Office Hours","");
var officebatch = [];

function mapOffice(tab1html, tab2html, tab3html, lat, lng, title, icon) {	
	var arrTabContent = new Array(tab1html, tab2html, tab3html);
	var point = new GLatLng(lat, lng);
	officebatch.push(createOfficeMarker(icon, point, arrTabContent, title));
}

function createOfficeMarker(myicon, point, arrTabContent, mytitle) {
	var options = { title: mytitle, icon: myicon };
	var marker = new GMarker(point, options);
	GEvent.addListener(marker, "click", function() {
		if(arrTabContent[1] == '' && arrTabContent[2] == '')
			marker.openInfoWindowHtml(arrTabContent[0]);
		else {
			var infoTabs = new Array();
			if(arrTabContent[1] != '' && arrTabContent[2] == '') {
				for(i = 0; i < 2; i++)
					infoTabs[i] = new GInfoWindowTab(arrOfficeTabs[i], arrTabContent[i]);
			}
			else if(arrTabContent[1] == '' && arrTabContent[2] != '') {
				infoTabs[0] = new GInfoWindowTab(arrOfficeTabs[0], arrTabContent[0]);
				infoTabs[1] = new GInfoWindowTab(arrOfficeTabs[2], arrTabContent[2]);
			}
			else {
				for(i = 0; i < 3; i++)
					infoTabs[i] = new GInfoWindowTab(arrOfficeTabs[i], arrTabContent[i]);
			}
			marker.openInfoWindowTabsHtml(infoTabs);
		}
	});
	return marker;
}

function AddOffice(lat, lng, bkrcode, branchname, address, city, state, zipcode, branchphone, branchfax) {
	
	// Tab 1 - Branch
	var tab1html = '<table><tr><td width="160px"><a href="/corp/office_details.asp?mlsid=1&Office=' + bkrcode + '" target="_top">' +
		'<img src="/images/officephotos/' + bkrcode + '_tn.jpg" style="border:1px solid #000000;" alt="' + 
		branchname + '"></a></td><td valign="top"><a href="/corp/office_details.asp?mlsid=1&Office=' +
		bkrcode + '"  target="_top">' + branchname + '</a><br />' + address + '<br />' + city + ', ' + state + ' ' + 
		zipcode + '<br />Phone: ' + branchphone + '<br />Fax: ' + branchfax + '<br /></td></tr></table>';
	
	// Tab 2 - Office Hours
	var tab2html = '';
		
	// Tab 3 - Unused
	var tab3html = '';
	
	OfficeIcon = icon_Office;
	mapOffice(tab1html, tab2html, tab3html, lat, lng, branchname, OfficeIcon) 		
	
}

function loadOffices() {	
	GDownloadUrl('/_incs/getOffices.asp', function(data, responseCode) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		for (var i = 0; i < markers.length; i++) 
			AddOffice(markers[i].getAttribute("lat"), markers[i].getAttribute("lng"), markers[i].getAttribute("bkrcode"), markers[i].getAttribute("branchname"), markers[i].getAttribute("address"), markers[i].getAttribute("city"), markers[i].getAttribute("state"), markers[i].getAttribute("zipcode"), markers[i].getAttribute("branchphone"),  markers[i].getAttribute("branchfax"));
		var mgr = new GMarkerManager(map);
		mgr.addMarkers(officebatch, 12);
		mgr.refresh();
	});
}

function refreshOffices() {
	var mgr = new GMarkerManager(map);
	mgr.addMarkers(officebatch, 12);
	mgr.refresh();
}