//version: 1.110
var map;
var xml;
var a=0;
var markers = new Array();
var words = new Array();
var find = new Array();
var result = 0;
var count;
var video = 'video.flv';
var bounds = new GLatLngBounds();		

var baseIcon = new GIcon();
baseIcon.image = "marker_default.png";
baseIcon.iconSize = new GSize(49, 32);
baseIcon.iconAnchor = new GPoint(15, 32);
baseIcon.infoWindowAnchor = new GPoint(13, 9);
baseIcon.infoShadowAnchor = new GPoint(18, 25);
var icon = new GIcon(baseIcon);	

var mainIcon = new GIcon();
mainIcon.image = "marker_icon.png";
mainIcon.iconSize = new GSize(32, 32);
mainIcon.iconAnchor = new GPoint(15, 15);
mainIcon.infoWindowAnchor = new GPoint(13, 9);
mainIcon.infoShadowAnchor = new GPoint(18, 25);
var flat = new GIcon(mainIcon);

function fakeStopVideo(){//to stop audio playback issue in IE when infowindow is closed
	if(a==1) { //alert("yes"); 
		a=0;
		map.openInfoWindowHtml(map.getCenter(),'<P>');
	}
}			

function zoomfit(){
	var center = bounds.getCenter();
	var newZoom = map.getBoundsZoomLevel(bounds);
	if (map.getZoom() != newZoom){
		map.setCenter(center, newZoom);
	}else{
		map.panTo(center);
	}
	if(map.getZoom() > 16) map.setZoom(16);
}

function show_tooltip(i) {
	if(a == 0) {
	map.panTo(new GLatLng(xml.documentElement.getElementsByTagName("lat")[i].childNodes[0].nodeValue, xml.documentElement.getElementsByTagName("lng")[i].childNodes[0].nodeValue));
	document.getElementById('tooltip').innerHTML = '<span align="center" class="text1">&nbsp;'+xml.documentElement.getElementsByTagName("name")[i].childNodes[0].nodeValue+'&nbsp;</span>';
	document.getElementById('tooltip').style.visibility = 'visible';
	}
}

function hide_tooltip() {
	if(a == 0)
	document.getElementById('tooltip').style.visibility = 'hidden';
}

function show_infowindow(i) {
	hide_tooltip();
	map.panTo(new GLatLng(xml.documentElement.getElementsByTagName("lat")[i].childNodes[0].nodeValue, xml.documentElement.getElementsByTagName("lng")[i].childNodes[0].nodeValue));
	markers[i].openInfoWindowHtml('<div align="left" style="width:300px"><table><tr><td><img src="'+xml.documentElement.getElementsByTagName("image")[i].childNodes[0].nodeValue+'"></td><td><a class="link2" href="'+xml.documentElement.getElementsByTagName("link")[i].childNodes[0].nodeValue+'" target="_blank">'+xml.documentElement.getElementsByTagName("name")[i].childNodes[0].nodeValue+'</a><br><span class="text1">'+xml.documentElement.getElementsByTagName("address")[i].childNodes[0].nodeValue+'<br>'+xml.documentElement.getElementsByTagName("phone")[i].childNodes[0].nodeValue+' | <a class="link1" href="'+xml.documentElement.getElementsByTagName("link")[i].childNodes[0].nodeValue+'" target="_blank">website</a></span></td></tr></table><div id="player" style="width:300px;height:175px"></div><div class="text1">'+xml.documentElement.getElementsByTagName("details")[i].childNodes[0].nodeValue+'</div><div align="center" class="text1"><a class="link1" href="http://maps.google.com/maps?daddr='+xml.documentElement.getElementsByTagName("address")[i].childNodes[0].nodeValue+'" target="_blank">Directions</a> | <a class="link1" href="#" onclick="map.setZoom(18);map.setMapType(G_SATELLITE_MAP);">Zoom In</a></div></div>');
	var so = new SWFObject('player.swf','mpl','300','175','9');
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
//	so.addParam('flashvars','&file='+video+'&bufferlength=2&controlbar=over&autostart=true');
	so.addParam('flashvars','&file='+xml.documentElement.getElementsByTagName("video")[i].childNodes[0].nodeValue+'&bufferlength=2&controlbar=over&autostart=true&screencolor=ffffff');
	so.write('player');		
	a=1;
}		

function set_marker(_category){
	for (var i = 0; i < options_xml.documentElement.getElementsByTagName("category").length; i++) {
		if (options_xml.documentElement.getElementsByTagName("category")[i].childNodes[0].nodeValue == _category) 
			baseIcon.image = options_xml.documentElement.getElementsByTagName("category")[i].getAttribute("marker") ;
		icon = new GIcon(baseIcon);			
	}
}

function load_marker(i){
	var point = new GLatLng(xml.documentElement.getElementsByTagName("lat")[i].childNodes[0].nodeValue, xml.documentElement.getElementsByTagName("lng")[i].childNodes[0].nodeValue);
	set_marker(xml.documentElement.getElementsByTagName("category")[i].childNodes[0].nodeValue);
	markers[i] = new GMarker(point,{icon:icon,title: xml.documentElement.getElementsByTagName("name")[i].childNodes[0].nodeValue});
	map.addOverlay(markers[i]);
	bounds.extend(point);
	zoomfit();	
	GEvent.addListener(markers[i], "click", function() { show_infowindow(i); });
	GEvent.addListener(markers[i],"infowindowclose", function() {fakeStopVideo();});
}

function show_map(_location, _community, _category) {
	map.clearOverlays();
	var panel_html = '';
	document.getElementById('search_input').value = '';
	document.getElementById('select_location').value = _location;
	document.getElementById('select_community').value = _community;
	bounds = new GLatLngBounds();
	for (var i = 0; i < xml.documentElement.getElementsByTagName("item").length; i++){		
		if((xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue == _location || _location == 'all') && (xml.documentElement.getElementsByTagName("category")[i].childNodes[0].nodeValue == _category || xml.documentElement.getElementsByTagName("category2")[i].childNodes[0].nodeValue == _category || _category == 'all') && (xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue == document.getElementById("select_community").value || document.getElementById("select_community").value == 'all')) {
			load_marker(i);
			panel_html += '<div style="border-bottom:1px dashed #888;padding:5px;background:#ffffff" onmouseover="this.style.background=\'#ffeeee\';show_tooltip('+i+');" onmouseout="this.style.background=\'#ffffff\';hide_tooltip();" onclick="show_infowindow('+i+');a=1;"><table><tr><td><img src="'+xml.documentElement.getElementsByTagName("image")[i].childNodes[0].nodeValue+'"></td><td><span class="link2">'+xml.documentElement.getElementsByTagName("name")[i].childNodes[0].nodeValue+'</span><br><span class="text1">'+xml.documentElement.getElementsByTagName("address")[i].childNodes[0].nodeValue+'<br>'+xml.documentElement.getElementsByTagName("phone")[i].childNodes[0].nodeValue+'</span></td></tr></table></div>';
		}	
	}
	document.getElementById('panel').innerHTML = panel_html;	
	GEvent.addListener(map,"infowindowopen", function() { if(a==0) { map.closeInfoWindow();} });
}

function load_search(count){//load markers and panel with most word matches first
	var panel_html = '';
	var match;
	bounds = new GLatLngBounds();
	for(var k=count;k>0;--k){
		match = 0;
		for (var i = 0; i < xml.documentElement.getElementsByTagName("item").length; i++){
			if((xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue == document.getElementById('select_location').value || document.getElementById('select_location').value == 'all' ) && (xml.documentElement.getElementsByTagName("category")[i].childNodes[0].nodeValue == document.getElementById('select_category').value || document.getElementById('select_category').value == 'all') && (xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue == document.getElementById("select_community").value || document.getElementById("select_community").value == 'all')) {
			if(find[i] == k){
				match = 1;
				load_marker(i);
				panel_html += '<div style="border-bottom:1px dashed #888;padding:5px;background:#ffffff" onmouseover="this.style.background=\'#ffeeee\';show_tooltip('+i+');" onmouseout="this.style.background=\'#ffffff\';hide_tooltip();" onclick="show_infowindow('+i+');a=1;"><table><tr><td><img src="'+xml.documentElement.getElementsByTagName("image")[i].childNodes[0].nodeValue+'"></td><td><span class="link2">'+xml.documentElement.getElementsByTagName("name")[i].childNodes[0].nodeValue+'</span><br><span class="text1">'+xml.documentElement.getElementsByTagName("address")[i].childNodes[0].nodeValue+'<br>'+xml.documentElement.getElementsByTagName("phone")[i].childNodes[0].nodeValue+'</span></td></tr></table></div>';
			}
			}
		}
		if (match == 1 && k != 1) {// add spacer between results
			panel_html += '<div style="border-bottom:1px dashed #888;padding:4px;"></div>';
		}	
	}
	document.getElementById('panel').innerHTML = panel_html;
}

function search_node(node_value, count, i){//find number of word match in a node
	var word_start = 0;
	var word_end = 0;
	var word;
	var found = 0;
	find[i]= 0;
	for(var k=0;k<count;k++){//compare xml word with all search words	
		word_start = 0;
		for (var j=0;j<node_value.length+1;j++){//seperate words in xml
			if ((node_value.charAt(j) == " ") || (node_value.charAt(j) == "\"") || (node_value.charAt(j) == ".") || (node_value.charAt(j) == ":") || (node_value.charAt(j) == "'") || (node_value.charAt(j) == ",") || j == node_value.length){
				word_end = j;
				word = node_value.substring(word_start,word_end);
				word = word.toLowerCase();
				if (words[k] == word)	{
					result = 1;
					find[i] = ++found; //no. of search words found in a node
					j = node_value.length + 1; //if one word match found in node, then move to next word search in the node
				}
				word_start = j + 1;
			}	
		}
	}
}

function search_xml(count){
	result = 0;
	var xml_string;
	for (var i = 0; i < xml.documentElement.getElementsByTagName("item").length; i++) {//search node, if found in node move to next
		xml_string = xml.documentElement.getElementsByTagName("name")[i].childNodes[0].nodeValue + ' ' + xml.documentElement.getElementsByTagName("details")[i].childNodes[0].nodeValue + ' ' + xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue+ ' ' + xml.documentElement.getElementsByTagName("category")[i].childNodes[0].nodeValue+ ' ' + xml.documentElement.getElementsByTagName("address")[i].childNodes[0].nodeValue;
		search_node(xml_string, count, i);
	}
	load_search(count);
	if (!result) {
		document.getElementById('panel').innerHTML = '<center class="text1">Search not found...<center>';
	}	
}

function search(string){	
	if (string.length != 0){		
		map.clearOverlays();
		document.getElementById('panel').innerHTML += '<hr size="1">';
		var string_start = 0;
		var string_end = 0;
		count = 0;
		for (var j=0;j<string.length+1;j++){//seperate words in search string and store in array
			if((string.charAt(j) == " ") || j == string.length){ //seperate words in search
				string_end = j; //alert (string_start);
				if(string_end - string_start != 0){//if not blank or space
					words[count] = string.substring(string_start,string_end); 
					words[count] = words[count].toLowerCase();//alert(words[count]);
					count++;
				}
				string_start = j + 1;
			}
		}//alert (count);

		document.getElementById('panel').innerHTML = '';
		search_xml(count);
	}		
	return false;
}

function load_community_markers(i) {
	var point = new GLatLng(options_xml.documentElement.getElementsByTagName("community")[i].getAttribute("lat"), options_xml.documentElement.getElementsByTagName("community")[i].getAttribute("lng"));
	markers[i] = new GMarker(point,{icon:flat,title: options_xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue});
	map.addOverlay(markers[i]);
	bounds.extend(point);
	zoomfit();	
	GEvent.addListener(markers[i], "click", function() { 
		load_page(0,options_xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue);
	});			

}

function load_location_markers(i) {
	var point = new GLatLng(options_xml.documentElement.getElementsByTagName("location")[i].getAttribute("lat"), options_xml.documentElement.getElementsByTagName("location")[i].getAttribute("lng"));
	markers[i] = new GMarker(point,{icon:flat,title: options_xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue});
	map.addOverlay(markers[i]);
	bounds.extend(point);
	zoomfit();	
	GEvent.addListener(markers[i], "click", function() { 
		load_page(options_xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue, 0);
	});			

}

function load_community(_location) {
		var select_community = '';
		var isCommunity = 0;
		select_community += '<select style="width:160px" id="select_community" onchange="load_page(0, document.getElementById(\'select_community\').value)"><option value="all">---Select Community---</option>';
		for (var i = 0; i < options_xml.documentElement.getElementsByTagName("community").length; i++) {
			if(options_xml.documentElement.getElementsByTagName("community")[i].getAttribute("location") == _location) {
				isCommunity = 1;
				select_community += '<option value="'+options_xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue+'">'+options_xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue+'</option>';			
			}
		}
		select_community += '</select>';	
		if (isCommunity == 1) {
			document.getElementById('options_community').style.visibility = 'visible';
			document.getElementById('options_community').innerHTML = select_community;
		}
		else {
			document.getElementById('options_community').style.visibility = 'hidden';
			document.getElementById('select_community').value = 'all';
		}	
}

function load_page(_location, _community) {
	if (_location == 'all') location = 'index.html';
	if (_community == 'all') _location = document.getElementById('select_location').value;
	for (var i = 0; i < options_xml.documentElement.getElementsByTagName("location").length; i++) {
		if(options_xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue == _location)
			location = options_xml.documentElement.getElementsByTagName("location")[i].getAttribute("page");
	}
	for (var i = 0; i < options_xml.documentElement.getElementsByTagName("community").length; i++) {
		if(options_xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue == _community)
			location = options_xml.documentElement.getElementsByTagName("community")[i].getAttribute("page");
	}	
}

function load(_location,_community) {
	document.getElementById('mainmap').innerHTML = '<div id="map" style="width:100%; height:100%;min-width:300px"></div><div id="tooltip" style="position:absolute;top:50%;left:50%;z-index:99999;visibility:hidden; border:1px solid #888; background: #ffeeee;"></div>';

  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GHierarchicalMapTypeControl());
	map.addMapType(G_PHYSICAL_MAP);
	map.addControl(new GSmallZoomControl());
	map.setCenter(new GLatLng(39,-96), 4); 
	map.enableGoogleBar(); 		

	map.openInfoWindowHtml(new GLatLng(33,-100), '<div id="player"></div>');
	map.closeInfoWindow();	

	document.getElementById('panel').innerHTML = '<center class="text1">Loading...<center>';//show loading while parsing xml		

	GDownloadUrl("options.xml", function(data, responseCode) {
		options_xml = GXml.parse(data);		
		
		//create option list for locations and category
		var select_location = '';
		select_location += '<select style="width:160px" id="select_location" onchange="load_page(document.getElementById(\'select_location\').value, 0)"><option value="all">---Select Location---</option>';
		for (var i = 0; i < options_xml.documentElement.getElementsByTagName("location").length; i++) {
			select_location	+= '<option value="'+options_xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue+'">'+options_xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue+'</option>';
		}
		select_location	+= '</select>';	
		
		var select_category = '';
		select_category += '<select style="width:200px" id="select_category" onchange="show_map(document.getElementById(\'select_location\').value,document.getElementById(\'select_community\').value,document.getElementById(\'select_category\').value)"><option value="all">---Select Category---</option>';
		for (var i = 0; i < options_xml.documentElement.getElementsByTagName("category").length; i++) {
			select_category	+= '<option value="'+options_xml.documentElement.getElementsByTagName("category")[i].childNodes[0].nodeValue+'">'+options_xml.documentElement.getElementsByTagName("category")[i].childNodes[0].nodeValue+'</option>';
		}
		select_category	+= '</select>';	

		document.getElementById('options_category').innerHTML = select_category;
		document.getElementById('options_location').innerHTML = select_location;
		document.getElementById('options_community').innerHTML = '<select style="width:160px" id="select_community" disabled><option value="all">---Select Community---</option></select>';
		document.getElementById('options_community').style.visibility = 'hidden';
		document.getElementById('options_search').innerHTML = '<form name="searchform" action="#" method="POST" onsubmit="return search(document.getElementById(\'search_input\').value);"><input id="search_input" name="search_input" type="text" size="20"> <input type="submit" value="Search"></form>';
		
		
		GDownloadUrl("data.xml", function(data, responseCode) {
			xml = GXml.parse(data);
			
			if (_location == 0 && _community == 0) {//load page with location markers
				panel_html = '';	
				for (var i = 0; i < options_xml.documentElement.getElementsByTagName("location").length; i++) {
					load_location_markers(i);
					panel_html += '<div style="border-bottom:1px dashed #888;padding:5px;background:#ffffff" onmouseover="this.style.background=\'#ffeeee\'" onmouseout="this.style.background=\'#ffffff\'" onclick="load_page(\''+options_xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue+'\', 0);"><table><tr><td><img src="'+mainIcon.image+'"></td><td><a class="link2" href="#" onclick="load_page(\''+options_xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue+'\', 0);">'+options_xml.documentElement.getElementsByTagName("location")[i].childNodes[0].nodeValue+'</a><br><span class="text1">'+options_xml.documentElement.getElementsByTagName("location")[i].getAttribute("state")+'</span></td></tr></table></div>';
				}
				document.getElementById('panel').innerHTML = panel_html;
			}
			else if (_community == 0) {//load page with community markers for the 
				panel_html = '';	
				for (var i = 0; i < options_xml.documentElement.getElementsByTagName("community").length; i++) {
					if(options_xml.documentElement.getElementsByTagName("community")[i].getAttribute("location") == _location){
						load_community_markers(i);
						panel_html += '<div style="border-bottom:1px dashed #888;padding:5px;background:#ffffff" onmouseover="this.style.background=\'#ffeeee\'" onmouseout="this.style.background=\'#ffffff\'" onclick="load_page(0,\''+options_xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue+'\');"><table><tr><td><img src="'+mainIcon.image+'"></td><td><a class="link2" href="#" onclick="load_page(\''+options_xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue+'\', 0);">'+options_xml.documentElement.getElementsByTagName("community")[i].childNodes[0].nodeValue+'</a><br><span class="text1">'+options_xml.documentElement.getElementsByTagName("community")[i].getAttribute("location")+'</span></td></tr></table></div>';
					}
				}
				document.getElementById('panel').innerHTML = panel_html;
				document.getElementById('select_location').value = _location;
				load_community(_location);
				document.getElementById('select_community').value = _community;
			} 			
			else {//load page with clent markers based on location and community				
				document.getElementById('select_location').value = _location;
				load_community(_location);
				document.getElementById('select_community').value = _community;
				show_map(_location,_community, 'all');
			}
		});	
	});
	}
}