var map;

function doGenerateMarkerHtmlCallback(marker,html,result) {
    document.getElementById("coordinates").value = marker.getLatLng();
    return html;
}

function place(lat,lng) {
	map.clearOverlays();
	var point = new GLatLng(lat,lng);
	var placemarker = new GMarker(point);
	map.setCenter(point,14); 
	map.addOverlay(placemarker);
	document.getElementById("coordinates").value = point;
}

function initialize() {
 	var lat = '43.342159';
 	var lon = '142.383156';
 	var zoom = 14;
 	
 	var editLat = document.getElementById('edit_lat');
    var editLng = document.getElementById('edit_lng');
 	if(editLat) {
		lat = editLat.value;
		lon = editLng.value;
	}
 	
 	if (GBrowserIsCompatible()) {
    	   
        // Create and center map on passed cooridinates
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(lat,lon), zoom);
		
		// Show small zoom controls
		var mapControl = new GMapTypeControl();
		map.addControl(new GSmallZoomControl());
		
		// Bind a search control to the map, suppress result list
		map.addControl(new google.maps.LocalSearch(
            {onGenerateMarkerHtmlCallback : doGenerateMarkerHtmlCallback, resultList : document.getElementById("results")}), 
            new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(0,-30)));
       	
       	if(editLat) {
			place(lat, lon);
		}
    	
      	GEvent.addListener(map,"click",function(overlay, point) {
       		if(point) {
       			map.clearOverlays();
       			var mymarker = new GMarker(point, {draggable: false});
       			map.addOverlay(mymarker);
 				document.getElementById("coordinates").value = point;
 				//alert(point);
    		}
    	});
	}
}

function initialize2() {
	var lat = document.getElementById("show_latitude").value;
 	var lon = document.getElementById("show_longitude").value;
 	var zoom = 14;
 	
 	var editLat = document.getElementById('edit_lat');
    var editLng = document.getElementById('edit_lng');
 	if(editLat) {
		lat = editLat.value;
		lon = editLng.value;
	}
 	
 	if (GBrowserIsCompatible()) {
    	   
        // Create and center map on passed cooridinates
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(lat,lon), zoom);
		
		// Show small zoom controls
		var mapControl = new GMapTypeControl();
		map.addControl(new GSmallZoomControl());
		
		place(lat, lon);
	}
}

// Avoid using body tag for load/unload
function addLoadEvent(func, lat, lon, zoom) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
       			oldonload();
     		}
    		func(lat, lon, zoom);
   		}
 	}
}

// arrange for our onunload handler to 'listen' for onunload events
if (window.attachEvent) {
       window.attachEvent("onunload", function() {
               GUnload();      // Internet Explorer
       });
} else {

       window.addEventListener("unload", function() {
               GUnload(); // Firefox and standard browsers
       }, false);

}


Array.prototype.findIndex = function(value) {
	var ctr = "";
	for(var i=0; i < this.length; i++) {
		if(this[i] == value) {
			return i;
		}
	}
	return ctr;
};

function autoFill(value, type) {
  if(value != "") {
 	if(type == 'name') {
 		var index = name_list.findIndex(value);
 		if(index !== "") {
			document.getElementById("reading").value = reading_list[index];
 			document.getElementById("english").value = english_list[index];
 			place(latitude_list[index],longitude_list[index]);
 		}
 	} 
 	if(type == 'reading') {
 		var index = reading_list.findIndex(value);
 		if(index !== "") {
			document.getElementById("name").value = name_list[index];
 			document.getElementById("english").value = english_list[index];
 			place(latitude_list[index],longitude_list[index]);
 		}
 	} 
 	if(type == 'english') {
 		var index = english_list.findIndex(value);
 		if(index !== "") {
 			document.getElementById("name").value = name_list[index];
			document.getElementById("reading").value = reading_list[index];
			place(latitude_list[index],longitude_list[index]);
 		}
 	} 
  }
}

var counterText = 0;
var counterSelect = 0;
function addAllInputs(divName, inputType){
     var newdiv = document.createElement('div');
     switch(inputType) {
          case 'day':
          	   newdiv.innerHTML = '<input type="text" name="holi[]" />DD-MM-YYYY';
               counterText++;
               break;
          case 'weekly':
               newdiv.innerHTML = "<select name='holi[]'><option value='' selected> なし<option value='Sunday'> 日曜日 <option value='Monday'> 月曜日<option value='Tuesday'> 火曜日<option value='Wednesday'> 水曜日<option value='Thursday'> 木曜日<option value='Friday'> 金曜日<option value='Saturday'> 土曜日</select>";
               counterSelect++;
               break;
     }
     document.getElementById(divName).appendChild(newdiv);
}

var counter = 1;
function addInput(divName){
     var newdiv = document.createElement('div');
     newdiv.innerHTML = '<input type="text" name="working_day[]" size="12">DD-MM-YYYY';
     document.getElementById(divName).appendChild(newdiv);
     counter++;
}
