var google_map, google_geo, google_map_visual, google_geo_visual;

function google_map_init_visual(id, addr, txt) {
	google_geo_visual = new google.maps.Geocoder(); google_geo_visual.geocode( { 'address': addr }, function(results, status) {
		var vmap = document.getElementById('vmap'); if(status == google.maps.GeocoderStatus.OK) {
			var panoramaOptions = { position: results[0].geometry.location, pov: { heading: 34, pitch: 10, zoom: 1 } }, panorama = new google.maps.StreetViewPanorama(vmap, panoramaOptions), sv = new google.maps.StreetViewService(panorama);
			sv.getPanoramaByLocation(results[0].geometry.location, 10, function(data, st) { if(st == "OK") { google_map_visual.setStreetView(panorama); } else { clearVmap(); } });
			if(txt != '') { google_add_marker(results[0].geometry.location, txt); }
		} else { clearVmap(); return false; }
	});
}

function clearVmap() { var vmap = document.getElementById('vmap'), vmapErr = document.getElementById('vmapErr'), vmapImgErr = document.getElementById('vmapImgErr'), imgErr = document.getElementById('imgErr'); vmap.style.display = 'none'; vmapErr.style.display = 'none'; vmapImgErr.style.display = 'block'; imgErr.style.display = 'block'; return false; }

function google_map_init(id, addr, txt) {
	google_geo = new google.maps.Geocoder();
	var container = document.getElementById(id),latlng = new google.maps.LatLng(-52.290, -1.520), myOptions = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }
	google_map = new google.maps.Map(container, myOptions);
	
	if(google_code_address(addr, txt) != true) { container.style.display = 'none'; }
}

function google_add_marker(latlng, txt, ico) {
	var marker = new google.maps.Marker({ position: latlng, map: google_map, title: txt, icon: ico });//orig





/*
  GEvent.addListener(marker, "click", function() {
    var myHtml = "<b>" + txt + "</b><br/>";
    map.openInfoWindowHtml(latlng, myHtml);
  });
*/
}

function google_code_address(addr, txt) {
	google_geo.geocode( { 'address': addr, region: 'UK' }, function(results, status) {
		if(status == google.maps.GeocoderStatus.OK) {
			google_map.setCenter(results[0].geometry.location);
			if(txt != '') { google_add_marker(results[0].geometry.location, txt); }
		} else { return false; }
	});
	return true;
}


function innerHTMLFromURL(v, url) {
	var el = v; if(window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { el.innerHTML = xmlhttp.responseText; } }
	xmlhttp.open("GET", url, true); xmlhttp.send();
}

function $selected(f) { var txt = '';
	if(window.getSelection) { txt = window.getSelection(); }
	else if (document.getSelection) { txt = document.getSelection(); }
	else if (document.selection) { txt = document.selection.createRange().text; }
	else return; return txt;
}

function $id(v) { return document.getElementById(v); }
function $class(p, classname){var node=(p)?p:document.getElementsByTagName("body")[0];var a=[];var re=new RegExp('\\b'+classname+'\\b');var els=node.getElementsByTagName("*");for(var i=0,j=els.length;i<j;i++)if(re.test(els[i].className))a.push(els[i]);return a;}
function $rel(p, relname){var node=(p)?p:document.getElementsByTagName("body")[0];var a=[];var re=new RegExp('\\b'+relname+'\\b');var els=node.getElementsByTagName("*");for(var i=0,j=els.length;i<j;i++)if(re.test(els[i].rel))a.push(els[i]);return a;}

function htmlspecialchars (string, quote_style, charset, double_encode) {
	var optTemp = 0, i = 0, noquotes= false;
	if(typeof quote_style === 'undefined' || quote_style === null) { quote_style = 2; } string = string.toString(); if(double_encode !== false) { string = string.replace(/&/g, '&amp;'); } string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');
	var OPTS = { 'ENT_NOQUOTES': 0, 'ENT_HTML_QUOTE_SINGLE' : 1, 'ENT_HTML_QUOTE_DOUBLE' : 2, 'ENT_COMPAT': 2, 'ENT_QUOTES': 3, 'ENT_IGNORE' : 4 }; if(quote_style === 0) { noquotes = true; }
	if(typeof quote_style !== 'number') { quote_style = [].concat(quote_style); for(i = 0; i < quote_style.length; i++) { if(OPTS[quote_style[i]] === 0) { noquotes = true; } else if (OPTS[quote_style[i]]) { optTemp = optTemp | OPTS[quote_style[i]]; } } quote_style = optTemp; }
	if(quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) { string = string.replace(/'/g, '&#039;'); }
	if(!noquotes) { string = string.replace(/"/g, '&quot;'); }
	return string;
}
