var gglMapInfoData = null;

function fillGoogleMap(gglMapInfo) {
    try {
        gglMapInfoData = JSON.parse(gglMapInfo);       
    }
    catch (Error) {
        alert(Error.message);
    }

    if (!jQuery("#googlemapsscr")) {
        embedGApiScript();
    }
    else {
        jQuery("#googlemapsscr").val("");
        embedGApiScript();
    }

    if (typeof (GoogleMapping) == 'undefined') {
        Type.createNamespace('ecoAmerica.FindNature');
        GoogleMapping.createClass('GoogleMapping');
    }


    GoogleMapping.set_userAddress(gglMapInfoData.UserAddress);

    var gglMapSearches = gglMapInfoData.GGLMapSearches;
    var searches = [];
    var allKmlFiles = [];
  
    var searches_string = "";
    
    for (var i = 0; i < gglMapSearches.length; i++) {
        var search = gglMapSearches[i].SearchString;
		//alert(search);
		if(i == 0) {
			searches_string = search;
		} else {
			searches_string = searches_string + ","+search;
		}
		searches.push(search);

        if (typeof (gglMapSearches[i].KmlFiles) != 'undefined' && gglMapSearches[i].KmlFiles != null  && gglMapSearches[i].KmlFiles.length != null) {
            var kmlFiles = gglMapSearches[i].KmlFiles;
            for (var k = 0; k < gglMapSearches[i].KmlFiles.length; k++) {

                var j = allKmlFiles.length;
                var exists = false;
                while (j--) {
                    if (allKmlFiles[j] == gglMapSearches[i].KmlFiles[k]) {
                        exists = true;
                    }
                }

                if (!exists) {
                    allKmlFiles.push(gglMapSearches[i].KmlFiles[k]);
                }
            }
        }        
    }
    
    searches_string = searches_string.replace("(", ",");
	searches_string = searches_string.replace(")", ",");
	searches_string = searches_string.replace(" or ", ",");
	searches_string = searches_string.replace("category:", "");
	searches_string = searches_string.replace("&amp;", "&");
	searches_string = searches_string.replace(",,", ",");

    GoogleMapping.set_searches(searches);
    GoogleMapping.set_kmlFiles(allKmlFiles);
    
    GoogleMapping.set_contentDivName("fngooglepmaps");
    GoogleMapping.set_resultsDivName("fngooglepmapsresults");
    GoogleMapping.set_fakeGoogleSearchBox("fakegoogletextbox");
    GoogleMapping.set_mapContainerHeight("800px");
    GoogleMapping.set_mapContainerWidth("560px");
    GoogleMapping.set_controlContainerWidth("560px");
    GoogleMapping.set_zoomLevel(10);
}

function embedGApiScript() {
    var script = document.createElement("script");
    script.src = "http://www.google.com/jsapi?key=ABQIAAAA0lrsNlGtKRb72tzScbEdshQc-ymnnuqSYVuLGwbRksJx7G_0oRRZyRL-aUpn6ZwxKSLRDeJJLLjSUw&callback=GoogleMapping.initializeMap";
	//script.src = "http://www.google.com/jsapi?key=ABQIAAAA0lrsNlGtKRb72tzScbEdshQPwB3bTZ_tnIqCCnzYWOUZxuCQKxRWPdxxo8itvrxFkvvlsaFAeN0R-g&callback=GoogleMapping.initializeMap";

// Key for QA - ABQIAAAA0lrsNlGtKRb72tzScbEdshQPwB3bTZ_tnIqCCnzYWOUZxuCQKxRWPdxxo8itvrxFkvvlsaFAeN0R-g

	
	
    //script.src = "http://www.google.com/jsapi?key=ABQIAAAA6C6pxdk6fuyjY0mwQGgQdxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxTXfyeNOheE_qmZVBKXyqVSHMXWvg&callback=GoogleMapping.initializeMap";
    script.type = "text/javascript";
    script.id = "googlemapsscr";
    document.body.appendChild(script);
}