var map_business = null;
var map_training = null;
var geocoder = null;
var searchType = null;
var BusinesMarker = null;
var TrainingMarker = null;
var country_city = null;
var country_city_street = null;
var CountryZoom = 4;
var CityZoom = 9;
var StreetZoom = 13;
var test = 5;


function address_control() {
		document.getElementById('country_business').value = "-1";
		document.getElementById('city_business').disabled = true;
		document.getElementById('street_business').disabled = true;
		document.getElementById('date_business').disabled = true;
		document.getElementById('country_training').value = "-1";
		document.getElementById('city_training').disabled = true;
		document.getElementById('street_training').disabled = true;
		document.getElementById('date_training').disabled = true;
                document.getElementById('drop_time_h').disabled = true;
                document.getElementById('drop_time_m').disabled = true;
		$('booking_agree').checked = false;
		}	

function check_map_div() {
	 if (document.getElementById('map_business') == null){
                        dom.query.ajax({url:'/map/',success:function(html){
                                document.getElementById('middle').innerHTML = html;
                                initialize();
                                if (dom.query('#current_language').html()!= '') {
                                      var transleted_lng = dom.query('#current_language').html();
                                      dom.query('#middle').translate(transleted_lng.slice(0,2));
                                      watermark();
                                }
                                }});
		}
	}


function initialize()     
{    
    if(GBrowserIsCompatible()) {
    	 
         map_business = new GMap2(document.getElementById("map_business"));
		 geocoder = new GClientGeocoder();
    	 map_business.addControl(new GSmallMapControl());
		 map_business.setCenter(new GLatLng (51.0 ,10.45) ,2);
		 
		 map_training = new GMap2(document.getElementById("map_training"));
		 map_training.addControl(new GSmallMapControl());
		 map_training.setCenter(new GLatLng (51.0 ,10.45) ,2);
 }  
}


function CreateMarker(map_id ,point,address,zoom ) {
	var OBIcon = new GIcon();
	OBIcon.image = '/site_media/img/map_icons/pointer.png';
	OBIcon.shadow = "";
	OBIcon.iconSize = new GSize(24,24);
	OBIcon.iconAnchor = new GPoint(24,24);
	
	markerOptions = {icon:OBIcon};
	var marker = new GMarker(point ,markerOptions);
	GEvent.addListener(marker, "click", function() {
		ShowAddress(map_id ,address ,zoom );
	});
	if (map_id == "business"){
		map_business.addOverlay(marker);
	}else{
		map_training.addOverlay(marker);
	}
	return marker;

}

function ShowAddress(map_id,address,zoom) {
	if (geocoder) {
	geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
      	if (map_id == 'business') {
        map_business.setCenter(point, zoom);
        map_business.addOverlay(CreateMarker(map_id,point,address ,zoom));
		}else{
		map_training.setCenter(point ,zoom );
		map_training.addOverlay(CreateMarker(map_id,point,address ,zoom));
		}
	ShowLatLng(point, map_id);
    }
   }
  );
}
}

function ShowLatLng(point, map_id) {
      if ($('street_'+map_id).value != "") {

	var lat_number = point.lat();
	var lat_string = Math.floor(Math.abs(lat_number));
	var lat_sign = "";
	lat_sign = (lat_number > 0) ? " N":" S";
	lat_number = Math.abs(lat_number) - lat_string;
	lat_number *= 3600;
	lat_string = lat_string + "&deg; " + ((lat_number-(lat_number % 60)) / 60) + "' " + Math.round(lat_number % 60) + "''" + lat_sign;

	var lng_number = point.lng();
	var lng_string = Math.floor(Math.abs(lng_number));
	var lng_sign = "";
	lng_sign = (lng_number > 0) ? " E":" W";
	lng_number = Math.abs(lng_number) - lng_string;
	lng_number *= 3600;
	lng_string = lng_string + "&deg; " + ((lng_number-(lng_number % 60)) / 60) + "' " + Math.round(lng_number % 60) + "''" + lng_sign;

	$(map_id+'_prev_latitude').innerHTML = lat_string;
	$(map_id+'_prev_longitude').innerHTML = lng_string;
      }else{
	$(map_id+'_prev_latitude').innerHTML = "--";
	$(map_id+'_prev_longitude').innerHTML = "--";
      }
}


function ShowCountry(map_id ,country) {
	ShowAddress(map_id , country ,5);
	document.getElementById('city_'+map_id).disabled = false;
	document.getElementById('city_'+map_id).value = "";
	document.getElementById('street_'+map_id).value = "";
	document.getElementById('street_'+map_id).disabled = true;
	document.getElementById('date_'+map_id).disabled = true;
}

function ShowCity(map_id ,city) {
	if (map_id =='business') {
	country = document.getElementById("country_business").value;
	}else{
	country = document.getElementById("country_training").value;
	}
	country_city = country +","+ city;
	ShowAddress(map_id , country_city , 13);
	document.getElementById('street_'+map_id).disabled = false;
	document.getElementById('street_'+map_id).value = "";
}


function ShowStreet(map_id , street){
	if (map_id == 'business') {
		street = document.getElementById("street_business").value;
	}else{
		street = document.getElementById("street_training").value;
	}
	country_city_street = country_city +","+ street;
	ShowAddress(map_id ,country_city_street ,16);
	document.getElementById('date_'+map_id).disabled =false;
        if (map_id=='training'){
                document.getElementById('drop_time_h').disabled = false;
                document.getElementById('drop_time_m').disabled = false;
        }
}


