// JavaScript Document
var centerLatitude = -23.504895;
var centerLongitude = 133.769531;
var startZoom = 4;
var map;

var http_site="";




 


function init() 
{

	if (GBrowserIsCompatible())
	{

		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		
		map.removeMapType(G_HYBRID_MAP);
	
		
        var mapControl = new GMapTypeControl();
        map.addControl(mapControl);
		
		map.clearOverlays();
		
		add_form();
		
		if(typeof(markers) !== 'undefined')
		{
			set_center_create_info_tabs();
		}
		else
			map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

	}
	
	
}


function copy_lat_lang(message,latitude,longitude)
{
	if(confirm(message))
	{
		window.opener.document.getElementById("longitude").value=longitude;
		window.opener.document.getElementById("latitude").value=latitude;
		self.close();
	}
	else
	return false;
}


function add_form()
{
	GEvent.addListener(map, "click", function(overlay, latlng) {
			if(typeof(latlng) !== 'undefined')
			{
				//create an HTML DOM form element
				var inputForm = document.createElement("div");
				var cfm_message="'Are you sure?'";
				var lng = latlng.lng();
				var lat = latlng.lat();
				inputForm.innerHTML = '<form name="frm1" id="frm1" method="post" action="">'
				+ '<fieldset style="width:150px;border:solid 1px #CCCCCC">'
				+ '<legend>New Location</legend>'
			+ '<input type="submit" name="SaveAddress" onclick="return copy_lat_lang('+cfm_message+','+lat+','+lng+')"  value="Copy Lat and Lan"/>'
				
				+ '<input type="hidden" id="longitude" name="longitude" value="' + lng + '"/>'
				+ '<input type="hidden" id="latitude" name="latitude" value="' + lat + '"/>'
				+ '</fieldset></form>';
				map.openInfoWindow (latlng,inputForm);
			}
		});
}


function set_center_create_info_tabs()
{
		var theSW = new GLatLng(minX, minY);
		var theNE = new GLatLng(maxX, maxY);																								
		
		var gLatLngBound 	= new GLatLngBounds();//theSW, theNE); 								
		gLatLngBound.extend(theSW); 	
		gLatLngBound.extend(theNE); 	
		
		var centerPoint 	= new GLatLng();
		centerPoint 	= gLatLngBound.getCenter();	

		var newZoom = map.getBoundsZoomLevel(gLatLngBound); 
		
		map.setCenter(centerPoint,newZoom);	
		var i=0;
		for(id in markers) 
		{

				var cfm_message="'Are you sure?'";

				var form1= '<form name="frm1" id="frm1" method="post" action="">'
				+ '<fieldset style="width:150px;border:solid 1px #CCCCCC">'
				+ '<legend>New Location</legend>'
			+ '<input type="submit" name="SaveAddress" onclick="return copy_lat_lang('+cfm_message+','+markers[id].latitude+','+ markers[id].longitude+')"  value="Copy Lat and Lan"/>'
				
				+ '<input type="hidden" id="longitude" name="longitude" value="' + markers[id].longitude + '"/>'
				+ '<input type="hidden" id="latitude" name="latitude" value="' + markers[id].latitude + '"/>'
				+ '</fieldset></form>';
				
			var infoTabs = [
						new GInfoWindowTab("Address", markers[id].address+"<br>"+form1+"<br>"+markers[id].longitude+"<br>"+markers[id].latitude)
						];

			addMarker(markers[id].latitude ,markers[id].longitude, infoTabs);
			i++;
		}

}

function addMarker(latitude , longitude, infoTabs)
{
	
	
	if(typeof(iconImage) !== 'undefined')
	{
	var icon = new GIcon();
	icon.image = iconImage;
	icon.iconSize = new GSize(35, 35);
	icon.iconAnchor = new GPoint(14, 35);
	icon.infoWindowAnchor = new GPoint(14, 14);
	
	var marker = new GMarker(new GLatLng(latitude, longitude),icon);
	}
	else
	var marker = new GMarker(new GLatLng(latitude, longitude));

	GEvent.addListener(marker, 'mouseover',
	function() {
	
				marker.openInfoWindowTabsHtml(infoTabs,{
				selectedTab:0,
				maxWidth:350
				});
	}
	);
	map.addOverlay(marker);
}


function locate_address(latitude,longitude,info,infotab_name) 
{

	if (GBrowserIsCompatible())
	{
		
	
       
		
		
		 
		map = new GMap2(document.getElementById("map"));

		var customUI = map.getDefaultUI();
        map.setUI(customUI);
		
		//map.addControl(new PromoControl(http_site));
		
		map.setCenter(new GLatLng(latitude, longitude), 16);

		var infoTabs = [
						new GInfoWindowTab(infotab_name, info)
						];
		addMarker(latitude ,longitude, infoTabs);

		
	}
	
	
}

/*var PromoControl = function(url) {
this.url_ = url;
};
PromoControl.prototype = new GControl(true);
PromoControl.prototype.initialize = function(map) {
var container = document.createElement("div");
container.innerHTML = '<marquee><b>This is test</b></marquee>';
container.style.width='91px';
container.style.height='46px';

//url = this.url_;
//GEvent.addDomListener(container, "click", function() {
//document.location = url;
//});

map.getContainer().appendChild(container);
return container;
};
PromoControl.prototype.getDefaultPosition = function() {
return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(70, 0));
};*/


window.onunload = GUnload;
