tinyMCEPopup.requireLangPack();

var GoogleMapsDialog = {
    
	init : function() {
		var f = document.forms[0];
	},

	insert : function() {
		// Insert the contents from the input into the document
		
		var strHtml = '';
		var akey      = document.forms[0].akey.value;
		var divnaam      = document.forms[0].divnaam.value;
		var coords      = document.forms[0].coords.value;
		var width      = document.forms[0].width.value;
		var height      = document.forms[0].height.value;
		var html      = document.forms[0].html.value;

		var browserName = navigator.appName;
		if (browserName == "Microsoft Internet Explorer") 
			var re = new RegExp("\r\n","g");
		else if (browserName = "Netscape")
			var re = new RegExp("\n","g");
		var newHtml = html.replace(re, "<br>");
		if (width == "") width = 100;
		if (height == "") height = 100;
		if (divnaam == "") divnaam = 'map';
		
		if (akey == "" || coords == ""){
			alert(tinyMCEPopup.getLang('googlemaps_dlg.missing_stuff'));
		} else {
			if (tinymce.EditorManager.settings.language == 'en'){
				var langcode = tinymce.EditorManager.settings.language + '-US';
			} else { 
				var langcode = tinymce.EditorManager.settings.language + '-' + tinymce.EditorManager.settings.language.toUpperCase();
			}
			strHtml = '<span class="spangooglemaps">\n';
			strHtml += '<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='+akey+'&hl=' + langcode + '" type="text/javascript"></script>';
			strHtml += '<script type="text/javascript">\n';
			strHtml += '      function setDirections(fromAddress) {\n';
			strHtml += '          gdir.clear()\;\n';
			strHtml += '          gdir.load("from: " + fromAddress + " to: ' + coords+'")\;\n';
			strHtml += '        }\n';
			strHtml += '        function handleErrors(){\n';
			strHtml += '    	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)\n';
			strHtml += '    	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\\nError code: " + gdir.getStatus().code)\;\n';
			strHtml += '    	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)\n';
			strHtml += '    	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\\n Error code: " + gdir.getStatus().code)\;\n';  	   
			strHtml += '    	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)\n';
			strHtml += '    	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\\n Error code: " + gdir.getStatus().code)\;\n';
			strHtml += '    	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)\n';
			strHtml += '    	     alert("The given key is either invalid or does not match the domain for which it was given. \\n Error code: " + gdir.getStatus().code);\n';
			strHtml += '    	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)\n';
			strHtml += '    	     alert("A directions request could not be successfully parsed.\\n Error code: " + gdir.getStatus().code);\n';    	    
			strHtml += '    	   else alert("An unknown error occurred.");\n';
			strHtml += '    	}\n';
			strHtml += '    	function onGDirectionsLoad(){\n'; 
			strHtml += '    	}\n';
			
			strHtml += 'function load() \n';
		    strHtml += '{\n';
		    strHtml += '  if (GBrowserIsCompatible())\n'; 
		    strHtml += '  {\n';
		    strHtml += '  var map = new GMap2(document.getElementById("'+divnaam+'"))\;\n';
		    strHtml += '  var center = new GLatLng('+coords+')\;\n';
		    strHtml += '  var html = \'' + newHtml + '\'\;\n';
		    strHtml += '  var marker = new GMarker(center)\;\n';
		    strHtml += '  map.setCenter(center, 13)\;\n';
		    strHtml += '  GEvent.addListener(marker, "click", function() {\n';
		    strHtml += '          marker.openInfoWindowHtml(html)\;\n';
			strHtml += '        })\;\n';
		    strHtml += '  map.addOverlay(marker)\;\n';
		    strHtml += '  var mapControl = new GMapTypeControl()\;\n';
		    strHtml += '  map.addControl(mapControl)\;\n';
		    strHtml += '  map.addControl(new GLargeMapControl())\;\n';
		    strHtml += '  gdir = new GDirections(map, document.getElementById("'+divnaam+'directions"))\;\n';
			strHtml += '  GEvent.addListener(gdir, "load", onGDirectionsLoad)\;\n';
			strHtml += '  GEvent.addListener(gdir, "error", handleErrors)\;\n';
		    strHtml += '  }\n';
		    strHtml += '}\n';
				
			strHtml += 'function addLoadEvent(func) \n';
		    strHtml += '{\n';
		    strHtml += 'var oldonload = window.onload;\n';
		    strHtml += '  if (typeof window.onload != \'function\') \n';
		    strHtml += '  {\n';
		    strHtml += '  window.onload = func;\n';
		    strHtml += '  } \n';
		    strHtml += '  else \n';
		    strHtml += '  {\n';
		    strHtml += '  window.onload = function() \n';
		    strHtml += '    {\n';
		    strHtml += '    if (oldonload) \n';
		    strHtml += '    {\n';
		    strHtml += '    oldonload();\n';
		    strHtml += '    }\n';
		    strHtml += '    func();\n';
		    strHtml += '}\n';
		    strHtml += '}\n';
		    strHtml += '}\n';
			strHtml += 'addLoadEvent(load)\;\n';
		    strHtml += 'if (window.attachEvent) {\n';
		    strHtml += '  window.attachEvent("onunload", function() {\n';
		    strHtml += '  GUnload();      // Internet Explorer\n';
		    strHtml += '        });\n';
		    strHtml += '} else {\n';
		    strHtml += 'window.addEventListener("unload", function() {\n';
		    strHtml += 'GUnload(); // Firefox and standard browsers\n';
		    strHtml += '    }, false);\n';
		    strHtml += '}\n';
			strHtml += '</script>\n';
		    strHtml += '<div id="'+divnaam+'" style="width: '+width+'px; height: '+height+'px;">&nbsp;</div>\n';
		    strHtml += '<div id="'+divnaam+'_directions"></div>\n';
			strHtml += '<label class="from-address" for="from-address">From address:</label>\n';
			strHtml += '<input class="from-address" id="'+divnaam+'_from-address" name="address" type="text">\n';
			strHtml += '<button id="'+divnaam+'_from-address-submit" class="from-address-submit" type="submit" onclick="setDirections(document.getElementById(\''+divnaam+'_from-address\').value); return false;">Get directions</button>\n';
			strHtml += '<div id="'+divnaam+'_directions"></div>\n';
			strHtml += '</span>\n';
			tinyMCEPopup.editor.execCommand('mceInsertContent', false, strHtml);
			tinyMCEPopup.close();
		}
	}
};

tinyMCEPopup.onInit.add(GoogleMapsDialog.init, GoogleMapsDialog);
