function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}

function popUp(URL,width,height)
{
	day = new Date();
	id = day.getTime();
	var winLeft = (screen.width - width) / 2;
	var winUp = (screen.height - height) / 2;
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+width+',height='+height+',left='+winLeft+',top='+winUp);");
}

/* START: google_maps.js.php */
function showMaps(location) {
	
	var popupObj = document.getElementById('googleMaps_popup');
	var addresses = popupObj.getElementsByTagName('*');
	
	for(var a=0; a<addresses.length; a++) {
		var element = addresses[a];
		
		if(element.className == "address" && element.id == "address_" + location) {
			element.style.display = "block";
			var address = element.getElementsByTagName('p')[0].innerHTML;
				address = address.replace(/<span>.*<\/span>/g, ''); // Others
		}
		else if(element.className == "address") {
			element.style.display = "none";
		}
	}
	
	$('#googleMaps_popup').slideDown();
	
	// Initialize google maps
	setTimeout("initializeMap('" + address + "')", 100);
}

function closeMaps() {
	$('#googleMaps_popup').slideUp();
}

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

var map = null;
var geocoder = null;

// Function: To init google maps
function initializeMap(address) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		//map.setCenter(new GLatLng(37.4419, -122.1419), 15);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		// To show the address
		geocoder = new GClientGeocoder();
		showAddress(address);
	}
}
 
// Function: To show the address of on the map
function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					map.setCenter(point, 15);
					
					var newIcon = new GIcon(G_DEFAULT_ICON);
			        newIcon.image = "/sites/site-1/themes/ttynl/images/maps_marker.gif";
					
					// Set up our GMarkerOptions object
					markerOptions = { icon:newIcon };
					
					var marker = new GMarker(point,markerOptions);
					map.addOverlay(marker);
					
					//GEvent.addListener(marker,"click", function() {
					//	marker.openInfoWindowHtml(document.getElementById('addressArea').innerHTML);
					//});
				}
			}
		);
	}
}

/* END: google_maps.js.php */
