// we send the parameters to every page,
// so we can come back to the same content by clicking on the tab

var domainNameNGS = document.domain;

var theAppPath = "http://"+ domainNameNGS + "/maps/civilwar";
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 'Home' tab
function getHomeUrl() {
	return theAppPath + "/index.html?" + getParameters();
}
// call 'Printable Wall Maps' tab
function getPrintPdfUrl() {
	return theAppPath + "/printpdf.html?" + getParameters();
}
// call 'About' tab
function getAboutUrl() {
	return theAppPath + "/about.html?" + getParameters();
}
// call 'Credits' button
function getCreditsUrl() {
	return theAppPath + "/credits.html?" + getParameters();
}
// call 'My Saved Maps' tab
function getSavedMapsUrl() {
	return theAppPath + "/mysaved.html?task=getSavedMap" + getParameters();
}
// call 'View & Customize' tab
function getViewAndCustomizeUrl() {
    var params = getParameters();
    if (params.indexOf("poilayers") == -1) {
        params += "&poilayers=" + escape("all::T");
    }
	return theAppPath + "/viewandcustomize.html?task=getMap" + params;
}
// link to be place into e-mail
function getMapFromEMailUrl() {
        var theCurrentUrl = document.location.search;
        if (getValue(theCurrentUrl,"ext").length == 0){ 
          return theAppPath + 
                 "/searchandbrowse.html?task=getSearchPlace&place="+
                 getValue(theCurrentUrl,"place"); 
        }
	return theAppPath + "/viewandcustomize.html" + getParametersForEMail();
}

// link to Event of the Day  -- dani tam 6/23
function getEventofDayUrl() {
	return theAppPath + "/eod.html?" + getParametersForEMail();
}

function getSearchAndBrowseUrl() {
	return theAppPath + "/search.html?" + getParametersForEMail();
}


// 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) {
    if (id) {
    	var fullUrlParams = theAppPath + "/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);
    	}
    	loadUrl(fullUrlParams);
    }
}
function viewPoiTheme(id) {
    if (id) {
        var fullUrlParams = theAppPath + "/viewandcustomize.html?";
        fullUrlParams += "poilayers=" + escape(id + "::T");
        if (id == "countryprofile" || id == "xpeditions") {
            fullUrlParams +="&ext=-19,-53,180,90";
        } else if (id == "food" || id == "history" || id == "language" || id == "naturalworld" || id == "peoples" || id == "religion") {
            fullUrlParams +="&ext=31,10.8,179.9,83.5";
        }
        loadUrl(fullUrlParams);
    }
}

////////////////////////////////////////////////////////////////////
// start a search
////////////////////////////////////////////////////////////////////

function findAPlace() {

	// get user input
	var 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 = theAppPath + "/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.href = refreshUrl;
	}
}

////////////////////////////////////////////////////////////////////
// helper methods
////////////////////////////////////////////////////////////////////

function loadUrl(newUrl) {
    top.document.location.href = newUrl;
}

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;
}

