google.load("maps", "3", {other_params:"sensor=false"});
var city = "Virginia";
var state = "";
var industry = "Law Firm";
var filename = "cache/" + city + " " + state + " " + industry + ".xml";
var cache = [];
var $j = jQuery.noConflict();
var myloc = window.location.href;
var locarray = myloc.split("/");
delete locarray[(locarray.length-1)];
var arraytext = locarray.join("/");
var domain = arraytext.match(/:\/\/(.[^/]+)/)[1]
arraytext = "http://" + domain;
var dirurl = arraytext + "/" + filename;
$j(document).ready(function($)
{
	//Build list items and points
	if ($("#localdirectory").length != 0)
	{
		var directoryItem;
		var i=0;
		var highlightCircle = null;
		var currentMarker = null;
		
		var xmlDoc;
		if (window.XMLHttpRequest)
		{
			xhttp=new XMLHttpRequest();
		}
		else // for IE 5/6
		{
			xhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xhttp.open("GET",dirurl,false);
		xhttp.send();
		xmlDoc = xhttp.responseXML;
				
		//Prepare the map
		$("#contact-form").after('<div id="map-area" style="position: relative;"><div id="map-canvas" style="position: absolute; left: 17px; top: 26px; width: 302px; height: 302px"></div></div>');
		$(".mapboxplaceholder").after('<div id="mapbox"><div id="map-canvas"></div></div>');
		var bounds = new google.maps.LatLngBounds();
		var geocoder = new google.maps.Geocoder();
		var mapOptions = 
		{
			mapTypeControl: false,
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			mapTypeId: google.maps.MapTypeId.ROADMAP      
		};
		var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
		
		var items = xmlDoc.getElementsByTagName("item");
		for(var count=0;count<10;count++)
		{
			directoryItem = new DirectoryItem();
			directoryItem.name = items[count].getElementsByTagName("name")[0].childNodes[0].nodeValue;
			directoryItem.street = items[count].getElementsByTagName("address")[0].childNodes[0].nodeValue;
			directoryItem.address = items[count].getElementsByTagName("address")[0].childNodes[0].nodeValue + ' ';
			directoryItem.city = items[count].getElementsByTagName("city")[0].childNodes[0].nodeValue;
			directoryItem.address += items[count].getElementsByTagName("city")[0].childNodes[0].nodeValue + ', ';
			directoryItem.state = items[count].getElementsByTagName("state")[0].childNodes[0].nodeValue;
			directoryItem.address += items[count].getElementsByTagName("state")[0].childNodes[0].nodeValue + ' ';
			directoryItem.zipcode = items[count].getElementsByTagName("zipcode")[0].childNodes[0].nodeValue;
			directoryItem.address += items[count].getElementsByTagName("zipcode")[0].childNodes[0].nodeValue;
			directoryItem.phone = items[count].getElementsByTagName("phone")[0].childNodes[0].nodeValue;
			if (items[count].getElementsByTagName("clickurl")[0].childNodes[0] != null)
			{
				directoryItem.url = items[count].getElementsByTagName("clickurl")[0].childNodes[0].nodeValue;
			}
			directoryItem.buildMarker(map,count+1,bounds,geocoder);
		}
	}

	preLoadImages("/images/directory/option1_selected.jpg", "/images/directory/option2_selected.jpg", "/images/directory/option3_selected.jpg");

	if ($("#contact-content").length != 0)
	{
	       $(".option-button:first-child").attr('src','/images/directory/option1_selected.jpg');
	       $(".option-pane:first-child").siblings().hide();

	       $(".option-button").css('cursor','pointer')
               .click(function()
               {
                       var div = $(this).attr('name');
                       $(div).fadeIn('fast').siblings().hide();
                       $(this).siblings().attr('src',function(){
                           return $(this).attr('src').replace('_selected','_norm');
                       });
                       $(this).attr('src',function(){
                           return $(this).attr('src').replace('_norm','_selected');
                       });
               });
	}
});
function FileExists(strURL)
{
    oHttp = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();    
    oHttp.open("GET", strURL, false);
    oHttp.send();
    return oHttp.responseText.indexOf("404 - File not found") > 0 ? false : true;
}

//DirectoryItem object


function DirectoryItem()
{
	var prevmark = null;
	this.name = '';
	this.street = '';
	this.address = '';
	this.city = '';
	this.state = '';
	this.zipcode = '';
	this.phone = '';
	this.url = '';
	
	this.buildMarker = function(map,localppccount,bounds,geocoder)
	{
		var name = this.name;
		var street = this.street;
		var city = this.city;
		var state = this.state;
		var zipcode = this.zipcode;
		var phone = this.phone;
		var url = this.url;
		if (url != '')
		{
			url = ' - <a href="' + url + '" target="_blank">Web Site</a>';
		}
		var address = this.address;
		infowindow = new google.maps.InfoWindow({
			content: "loading..."
		});
		geocoder.geocode(
		{'address': this.address},
		function(results, status)
		{
			if (status != google.maps.GeocoderStatus.OK) 
			{
				//alert('Sorry, we were unable to geocode that address.');
			} 
			else 
			{
				point = results[0].geometry.location;
				var num = localppccount;
				var image = 'images/map marker/map-marker-' + num + '.png';
				var contentString = 
					'<div class="mapcontent">' +
					name + '<br />' +
					street + '<br />' +
					city + ', ' +
					state + ' ' +
					zipcode + '<br />' +
					'P: ' + phone.substr(0,3) + '-' + phone.substr(3,3) + '-' + phone.substr(6,4) +
					url +
					'<br>Directions: <b>To here</b><br />Start address:<form action="directions.php" method="post">' + '<input type="text" name="saddr" id="saddr" value="" /><br>' + '<INPUT value="Get Directions" TYPE="SUBMIT">' + '<input type="hidden" name="daddr" value="' + address + '"/>' +
					'</div>';
				var marker = new google.maps.Marker({
					position: point, 
					map: map,
					icon: image,
					title: name,
					zIndex: num, 
					html: contentString
				});
				bounds.extend(point);
				map.setCenter(bounds.getCenter());
				map.fitBounds(bounds);
				google.maps.event.addListener(marker, 'click', function() {
					if(infowindow) { infowindow.close(); } 
					infowindow.setContent(this.html);
					infowindow.open(map,marker);
				});
 				var imgTag = document.getElementById("marker" + num);
				imgTag.onclick=function(){
					google.maps.event.trigger(marker,"click")
				};
			}
		});
	};
}

function preLoadImages()
{
	var args_len = arguments.length;
	for (var i = args_len; i--;)
	{
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		cache.push(cacheImage);
	}
}
