var m1 = null;
var m2 = null;
var map = null;
//var title1 = "Your Name";
var desc  = "Location confirmed <br\/>Press <b>Submit<\/b> or Reset <br\/> <em>Plotter<\/em> to locate again !";
var dB = zzx('businessname');
var geo = new google.maps.Geocoder();
//function gP(pos) {
//geo.geocode({latLng: pos},function(responses) {
//if (responses && responses.length > 0) {
//uMA(responses[0].formatted_address);
//}else{uMA('Not Found for this location.');}
//});}
function uMS(str){zz('mS').innerHTML = str;}

function uMP(latLng) {
	lat_= latLng.lat();
	lng_ = latLng.lng();
	zm_= map.getZoom();
 zz('inf').innerHTML= "Latitude <br/>"+lat_+"<br/>Langitude <br/>"+lng_;
}

function uMA(str){zz('add').innerHTML = str;}
//var ll = new google.maps.LatLng(lat_,lng_);
//function HomeControl(controlDiv, map) {
//controlDiv.style.padding = '5px';
//var controlUI = document.createElement('span');
//controlUI.className = 'mycss';
//controlUI.title = 'Click to Get Plotter Pin';
//controlDiv.appendChild(controlUI);
//controlUI.appendChild(document.createTextNode("Reset Plotter"));
//google.maps.event.addDomListener(controlUI, 'click', function() {if(infowindow){infowindow.close();}
//if(m2){m2.setMap(null);}
//m1.setVisible(true);
//m1.setPosition(map.getCenter());
////uMS('Location Not Confirmed');
//});
//var sV = document.createElement('span');
//sV.className = 'mycss';
//sV.title = 'Confirm The Location';
//controlDiv.appendChild(sV);
//sV.appendChild(document.createTextNode("Save Position"));
//google.maps.event.addDomListener(sV, 'click', function() {
//if (m1){m1.setVisible(false);
//uMP(m1.getPosition());
//zzx('Lat').value = lat_; 
//zzx('Lng').value = lng_;
//zzx('Zmm').value = zm_;
//cM(m1.getPosition());
//gP(m1.getPosition());
//uMS('Location Confirmed');
//}
//});
//}

function M_start() {
  var ll = new google.maps.LatLng(lat_,lng_);
	map = new google.maps.Map(zz('map'), {
    zoom: zm_,
    center: ll,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true,
    navigationControl: true,
    navigationControlOptions: {style: google.maps.NavigationControlStyle.ANDROID}
  });
	m1 = new google.maps.Marker({
    position: ll,
    title: 'Drag me to your Exact Location',
    map: map,
    draggable: true
  });
  uMP(ll);
  zz("Notex").style.visibility = "visible";
  //var homeControlDiv = document.createElement('DIV');
  //var homeControl = new HomeControl(homeControlDiv, map);

  //homeControlDiv.index = 1;
  //map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);
  // Add dragging event listeners.
  google.maps.event.addListener(m1, 'dragstart', function() {
    uMS('Dragging...');
	zz("Ftx").style.display='block';
  });
  google.maps.event.addListener(m1, 'dragend', function(e) {
    uMS('Drag ended');uMP(m1.getPosition());
	//map.setCenter(m1.getPosition());
    //gP(m1.getPosition());
	});

 google.maps.event.addListener(map, 'bounds_changed', function() {
    //uMS('Boundaries changed');
	//m1.setPosition(e.latLng);
	setTimeout('m1.setPosition(map.getCenter())', 500);
	
  }); 
  
}

function showAddress(a) {
if (geo) {
geo.geocode({'address':a}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
//alert(results[0].geometry.bounds);
map.setCenter(results[0].geometry.location);
map.setZoom(12);
uMP(results[0].geometry.location);
//uMA(results[0].geometry.location);
} else {
//alert("No results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}

var contentString = desc ;
var infowindow = new google.maps.InfoWindow({ 
    content: contentString 
});

function cM(p) {
map.setCenter(p);
m2 = new google.maps.Marker({
position: p,
title: 'Saved Position/Location',
map: map,
draggable: false,
icon:'/images/green-dot.png'
});
infowindow.content ='<b>' + dB.value + '</b><br/>' + desc ;
infowindow.open(map,m2); 
}
function rM(){
if(infowindow){infowindow.close();}
if(m2){m2.setMap(null);}
m1.setVisible(true);
m1.setPosition(map.getCenter());
uMS('Plotter Reset');
zz("Ftx").style.display='block';} 

function sM(){
if (dB.value.length<3){alert ("Please Fill your Information First ! ");dB.focus();return true;}
if (m1){m1.setVisible(false);
uMP(m1.getPosition());
zzx('Lat').value = lat_; 
zzx('Lng').value = lng_;
zzx('Zmm').value = zm_;
cM(m1.getPosition());
//gP(m1.getPosition());
uMS('<b class="mycss">Congratulation!<br/>'+dB.value+' Location Saved<br/>Go to Next Procedure of Form</b>');
zz("Ftx").style.display='none';
}
}
function zz(sd){ if (!document.getElementById(sd)){//alert("Error : element  "+sd+" not found");
return '';} return document.getElementById(sd);}
function zzx(sd){ if (!parent.document.getElementById(sd)){//alert("Error : element "+sd+" not found in parent document");
return '';}return parent.document.getElementById(sd);}
