// we send the parameters to every page, 
// so we can come back to the same content by clicking on the tab

////////////////////////////////////////////////////////////////////
//--> NGS hosted pages
////////////////////////////////////////////////////////////////////

// !!! this parameter needs to be adjusted depending on what NGS server the application is running under
// for old staging: saltflat.nationalgeographic.com
// for staging: test.nationalgeographic.com
// for production: plasma.nationalgeographic.com
var domainNameNGS = "plasma.nationalgeographic.com"; 

// start: staging environment ....
if (document.domain == "ngistg.esri.com"
	|| document.domain == "test.nationalgeographic.com") {
	domainNameNGS = "test.nationalgeographic.com"; 
}
// end: staging environment ....

var theMainPath = "http://"+ domainNameNGS;
var theAppPath = theMainPath + "/mapmachine";

var theProductPath = theAppPath + "/products";
var theCountryProfilePath = theAppPath + "/profiles"; 
var theHeader = theAppPath + "/ax/header.html";
var theFooter = theAppPath + "/ax/footer.html";
var theTopAd = theAppPath + "/ax/ad.html";
var theTopSearchAd = theAppPath + "/ax/ad_search_728.html";
var theRightAd = theAppPath + "/ax/ad_search_160.html";
var theDefaultSearchAndBrowsePage = theAppPath + "/search.html"; 

// call 'MapMachine Home' tab
function getHomeUrl() {
	return theAppPath + "/index.html?" + getParameters();
}
// call 'Country Profiles' tab
function getCountryProfilesUrl() {
	return theAppPath + "/countryprofiles.html?" + getParameters();
}
// call 'E-Mail this Map' button
function getEMailUrl(mapname) {
	return theAppPath + "/emailmap.html?mapname=" + escape(mapname) + getParameters();
}
// call 'Credits' button
function getCreditsUrl() {
	return theAppPath + "/credits.html?" + getParameters();
}
// call general 'Order' page
function getGeneralOrderUrl() {
	return theAppPath + "/order.html?" + getParameters();
}

////////////////////////////////////////////////////////////////////
//--> ESRI hosted pages
////////////////////////////////////////////////////////////////////

// !!! this parameter needs to be adjusted depending on what ESRI server the application is running under
// for staging: ngistg.esri.com
// for production: mapmachine.nationalgeographic.com
var domainNameESRI = "mapmachine.nationalgeographic.com";

// start: staging environment ....
if (document.domain == "ngistg.esri.com" 
	|| document.domain == "test.nationalgeographic.com") {
	domainNameESRI = "ngistg.esri.com"; 
}
// end: staging environment ....

// start: just for testing within ESRI ....
if (document.domain == "web.esri.com")
	domainNameESRI = "web.esri.com"; 
else if (document.domain == "ngidev.esri.com")
	domainNameESRI = "ngidev.esri.com"; 
// end: just for testing within ESRI ....

var theWebAppPath = "http://"+ domainNameESRI + "/mapmachine";
var theMapSourcePath = theWebAppPath + "/mapsource"; 

// call 'My Saved Maps' tab
function getSavedMapsUrl() {
	return theWebAppPath + "/mysavedmaps.html?task=getSavedMap" + getParameters();
}
// call 'View & Customize' tab
function getViewAndCustomizeUrl() {
	return theWebAppPath + "/viewandcustomize.html?task=getMap" + getParameters();
}
// call 'Search & Browse' tab
function getSearchAndBrowseUrl() {
	// if the user didn't do a search before, we go to the default page
	if (getValue(document.URL,"splace").length == 0) {
		urlParams = getParameters();
		urlParams = removeKey(urlParams,"task");
		return theDefaultSearchAndBrowsePage + "?" + urlParams;
	} else
		return theWebAppPath + "/searchandbrowse.html?task=getSearchMap" + getParameters();
}
// call 'Inside the MapMachine'
function getInsideUrl() {
	return theWebAppPath + "/inside.html?" + getParameters();
}
// link to be place into e-mail
function getMapFromEMailUrl() {
	return theWebAppPath + "/viewandcustomize.html" + getParametersForEMail();
}
// the call behind the Dynamic Map link on the Country Profile pages
function createViewAndCustomizeUrl(themeId,minx,miny,maxx,maxy) {
	urlParams = getParameters();
	urlParams = removeKey(urlParams,"themeId");
	urlParams = removeKey(urlParams,"ext");
	urlParams = removeKey(urlParams,"topten");
	urlParams = removeKey(urlParams,"iext");
	urlParams = removeKey(urlParams,"iplace");
	fullUrlParams = theWebAppPath + "/viewandcustomize.html?task=getMap&themeId=" + themeId + urlParams;
	if (minx && miny && maxx && maxy) {
		fullUrlParams += "&ext=" + (Math.round(minx*1000000)/1000000) +","
			+ (Math.round(miny*1000000)/1000000) +","
			+ (Math.round(maxx*1000000)/1000000) +","
			+ (Math.round(maxy*1000000)/1000000);
	}
	return fullUrlParams;
}

// call a map from a link, e.g. from home page 
// minx,miny,... don't have to be used, they're optional
function viewMap(id,minx,miny,maxx,maxy) {
	fullUrlParams = theWebAppPath + "/viewandcustomize.html?themeId=" + id;
	if (minx && miny && maxx && maxy) {
		fullUrlParams += "&ext=" + (Math.round(minx*1000000)/1000000) +","
			+ (Math.round(miny*1000000)/1000000) +","
			+ (Math.round(maxx*1000000)/1000000) +","
			+ (Math.round(maxy*1000000)/1000000);
	}
	return fullUrlParams;
}

////////////////////////////////////////////////////////////////////
// start a search
////////////////////////////////////////////////////////////////////

function findAPlace() {

	// get user input
	place = document.search.searchText.value;

	// get rid of leading or ending blanks
	while (place.substring(0,1) == " ")
		place = place.substring(1,place.length);
	while (place.substring(place.length-1,place.length) == " ")
		place = place.substring(0,place.length-1);

	// update user input
	document.search.searchText.value = place;

	// build the URL, if user actually typed in a name
	if (place.length > 0) {
		theCurrentUrl = document.location.search
		refreshUrl = theWebAppPath + "/searchandbrowse.html?task=getSearchPlace"
			+ addParameterToUrl(theCurrentUrl,"themeId")
			+ addParameterToUrl(theCurrentUrl,"ext")
			+ addParameterToUrl(theCurrentUrl,"sext")
			+ addParameterToUrl(theCurrentUrl,"iext")
			+ addParameterToUrl(theCurrentUrl,"size")
			+ addParameterToUrl(theCurrentUrl,"maplayers")
			+ addParameterToUrl(theCurrentUrl,"poilayers")
			+ addParameterToUrl(theCurrentUrl,"topten")
			+ addParameterToUrl(theCurrentUrl,"splace")
			+ addParameterToUrl(theCurrentUrl,"start")
			+ addParameterToUrl(theCurrentUrl,"iplace")
			+ "&place="+escape(place)
			+ addParameterToUrl(theCurrentUrl,"marker");
		top.document.location = refreshUrl;
	}
}


////////////////////////////////////////////////////////////////////
// helper methods
////////////////////////////////////////////////////////////////////

function getParameters() {
	var urlParams = document.location.search
	urlParams = removeKey(urlParams,"task");
	urlParams = removeKey(urlParams,"mapname");
	if (urlParams.substring(0,1) == "?")
		urlParams = urlParams.substring(1,urlParams.length);
	if (urlParams.length > 0 && urlParams.substring(0,1) != "&")
		urlParams = "&"+urlParams;
	return urlParams;
}

function getParametersForEMail() {
	var theCurrentUrl = document.location.search
	if (getValue(theCurrentUrl,"themeId").length > 0) {
		urlParams = "?themeId=" + getValue(theCurrentUrl,"themeId")
			+ addParameterToUrl(theCurrentUrl,"ext")
			+ addParameterToUrl(theCurrentUrl,"maplayers")
			+ addParameterToUrl(theCurrentUrl,"poilayers")
			+ addParameterToUrl(theCurrentUrl,"marker");
		return urlParams;
	}
	return "";
}

function addParameterToUrl(str,search) {
	value = getValue(str,search);
	if (value != "")
		return ("&"+search+"="+value);
	return "";
}

function getValue(str,search) {
	var s = str.indexOf("&"+search+"=",0);
	if (s == -1)
		s = str.indexOf("?"+search+"=",0);
	if (s == -1) return "";
	var e = str.indexOf("&",s+2);
	if (e == -1) e = str.length;
	return str.substring(s+search.length+2,e);
}

function removeKey(string,key) {
	start = string.indexOf("&"+key+"=");
	if (start == -1)
		start = string.indexOf("?"+key+"=");
	if (start == -1)
		return string;
	else {
		end = string.indexOf("&",start+2);
		if (end == -1)
			string = string.substring(0,start);
		else			
			string = string.substring(0,start)+string.substring(end+1,string.length);
	}
	return string;
}

