// ********************************************
// **        Flashmaps DB Mapper 1.0         **
// **       JavaScript API Functions         **
// ********************************************
// ** Please DO NOT make any changes here.   **
// ** You may want to chenge fmDBMClient.js  **
// ********************************************
// **      (c)2005 Flashmaps Geospatial      **
// **        http://www.flashmaps.com        **
// ********************************************


// ********************************************
// **  Functions called from the map, events **
// ********************************************

// The map is ready to receive commands
function fmMapIsReady() {
	return fmMapObj1.GetVariable(fmMcPath + "map_mc.outMapIsReady");
}

// A POI event was triggered
function fmPOIEvent(eventName, fmPOIID) {
	switch (eventName) {
		case 'onrollover':
			// The visitor placed the mouse over a POI
			fmCustomPOIOnRollOver(fmPOIID);
			break;
			
		case 'onrollout':
			// The visitor moved the mouse out of a POI
			fmCustomPOIOnRollOut(fmPOIID);
			break;
			
		case 'onrelease':
			// The visitor clicked on a POI
			fmCustomPOIOnRelease(fmPOIID);
			break;
						
		default:
			// Should not happen
			alert('Event ' + event + ' was triggered over POI ID ' + fmPOIID);
			break;
	}
}

// A set of POIs has been loaded
function fmPOIURLLoaded(strPOIsURL, iPOIsNumber) {
	fmPOIsLoaded = true;
	fmPOIsURL = strPOIsURL;
	fmCustomPOIURLLoaded(strPOIsURL, iPOIsNumber);
}

// ********************************************
// **             Map properties             **
// ********************************************

// Get map latitude
function fmGetMapLat() {
	return fmMapObj1.GetVariable(fmMcPath + "map_mc.outLat");
}

// Get map longitude
function fmGetMapLon() {
	return fmMapObj1.GetVariable(fmMcPath + "map_mc.outLon");
}

// Get map scale
function fmGetMapScale() {
	return fmMapObj1.GetVariable(fmMcPath + "map_mc.outScale");
}

// Get current view's minimum latitude
function fmGetMapViewMinLat() {
	return fmMapObj1.GetVariable(fmMcPath + "map_mc.outViewMinLat");
}

// Get current view's maximum latitude
function fmGetMapViewMaxLat() {
	return fmMapObj1.GetVariable(fmMcPath + "map_mc.outViewMaxLat");
}

// Get current view's maximum longitude
function fmGetMapViewMaxLon() {
	return fmMapObj1.GetVariable(fmMcPath + "map_mc.outViewMaxLon");
}

// Get current view's minimum longitude
function fmGetMapViewMinLon() {
	return fmMapObj1.GetVariable(fmMcPath + "map_mc.outViewMinLon");
}

// ********************************************
// **  Functions to send commands to the map **
// ********************************************

// Highlight a POI
function fmPOIHighlight(fmPOIID, strMethod) {
	if (fmPOIsLoaded) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsidePOIID", fmPOIID); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "highlightPOI"); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideHighlightMethod", strMethod); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Unhighlight a POI
function fmPOIUnhighlight(fmPOIID) {
	if (fmPOIsLoaded) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsidePOIID", fmPOIID);
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "unhighlightPOI"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Highlight and focus on a POI
function fmPOIHighlightAndFocusOn(fmPOIID, strMapScale, strMethod){
	if (fmPOIsLoaded) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsidePOIID", fmPOIID); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideScale", strMapScale); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "highlightAndFocusOnPOI"); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideHighlightMethod", strMethod); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Focus on a certain point, lat/lon.
function fmFocusOnLatLon(strLat, strLon, strMapScale) {
	if (fmMapIsReady()) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideLat", strLat); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideLon", strLon);
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideScale", strMapScale); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "FocusOnLatLon"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Focus on the POIs of a particular category or on all of them
function fmFocusOnAllPOIs(strCategory) {
	if (fmPOIsLoaded) {
		if (strCategory == "") {strCategory = "*";}
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideCategory", strCategory);
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "FocusOnAllPOIs"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

//Focus on an address
function fmFocusOnAddress(strAddress, strCity, strState, strZipCode, strCountry) {
	if (fmMapIsReady()) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAddress", strAddress);
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideCity", strCity);
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideState", strState);
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideZipCode", strZipCode);
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideCountry", strCountry);
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "FocusOnAddress"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}
		

// Show category (* for all)
function fmPOIShowCategory(strCategory) {
	if (fmPOIsLoaded) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideCategory", strCategory); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "showCategory"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Hide category (* for all)
function fmPOIHideCategory(strCategory) {
	if (fmPOIsLoaded) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideCategory", strCategory); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "hideCategory"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Show cross hair
function fmShowCrossHair() {
	if (fmMapIsReady()) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "showCrossHair"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Hide cross hair
function fmHideCrossHair() {
	if (fmMapIsReady()) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "hideCrossHair"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Load a set of POIs
function fmPOILoadURL(strPOIsURL) {
	if (fmMapIsReady()) {
		fmPOIsURL = strPOIsURL;
		fmMapObj1.SetVariable("_root.map_mc.fromOutsidePOIsURL", strPOIsURL); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "loadPOIsURL"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Load a set of POIs
function fmPOILoadURLAndFocus(strPOIsURL) {
	if (fmMapIsReady()) {
		fmPOIsURL = strPOIsURL;
		fmMapObj1.SetVariable("_root.map_mc.fromOutsidePOIsURL", strPOIsURL); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "loadPOIsURLAndFocus"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
	}
}

// Remove all POIs
function fmPOIRemoveAll() {
	if (fmPOIsLoaded) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "removeAllPOIs"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");		
		fmPOIsLoaded = false;
	}
}

// Remove all POIs of a category
function fmPOIRemoveCategory(strCategory) {
	if (fmPOIsLoaded) {
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideCategory", strCategory); 
		fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "removeCategory"); 
		fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");		
	}
}

// Load an set of POIs for the administration tool
function fmAdminPOILoadURL(strFile)  {
	if (fmMapIsReady()) {
		fmMapObj1.SetVariable("_root.mcAddPlugging.fromOutsideXML", strFile); 
		fmMapObj1.TCallLabel("_root.mcAddPlugging.adminFromOutside_mc", "doAction");
	}
}

// Initial view of the map
function fmInitialView() {
	fmMapObj1.SetVariable("_root.map_mc.fromOutsideAction", "initialView");
	fmMapObj1.TCallLabel("_root.map_mc.fromOutside_mc", "doAction");
}


function fmMaximize(fmToolbar, fmWidth, fmHeight, fmLat, fmLon, fmScale) {
//do: enlarge map

	//STEP 1 - browser detection
	if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
		var isIE = (navigator.appName.indexOf("Microsoft")!=-1)?1:0;
		var isNN = (navigator.appName=="Netscape")?1:0;
		var isSF = (fmCheckString("Safari", navigator.appVersion)!=-1)?1:0;
	}

	//STEP 2 - toolbar object to open
	var toolbar_str;
	var pois_str;
	
	if (isIE) {
		toolbar_str = fmMapObj1.movie; 
	} else {
		toolbar_str = fmMapObj1.data; 
	}
	
	//extract pois url
	if (toolbar_str.indexOf("poisURL") != -1) {
		pois_str = toolbar_str.substr(toolbar_str.indexOf("poisURL"));
		if (pois_str.indexOf("&") != -1){
			pois_str = pois_str.substr(0, pois_str.indexOf("&"));
		}
	}

	if (toolbar_str.indexOf("?") != -1) {
		toolbar_str = toolbar_str.substr(0, toolbar_str.indexOf("?"));
	}
	
	toolbar_str = toolbar_str + "?focusLat=" + fmGetMapLat() + "&focusLon=" + fmGetMapLon() + "&initialScale=" + fmGetMapScale() + "&" + pois_str;

	//STEP 3 - window size calculation
	factorW = (fmWidth * 100) /screen.width;
	factorH = (fmHeight * 100)/screen.height;
	if (factorW > factorH) {
		newW = screen.width;
		newH = (((screen.width*100)/fmWidth)/100)*fmHeight;
		posX = 0;
		posY = (screen.height - newH) / 2;
	} else {
		if (factorW < factorH) {
			newW = (((screen.height*100)/fmHeight)/100)*fmWidth;
			newH = screen.height;
			posX = (screen.width - newW) / 2;
			posY = 0;
		} else {
			newW = screen.width;
			newH = screen.height;
			posX = 0;
			posY = 0;
		}
	}
	
	//STEP 4 - variables for open the new window
	var optIE = 'scrollbars=no, menubar=0,  width=' + newW + ', height=' + newH + ', left=' + posX + ', top=' + posY+'';
	var optNN = 'scrollbars=no, menubar=no, width=' + newW + ', height=' + newH + ', left=' + posX + ', top=' + posY +'';
	
	//STEP 5 - open the window
	if (isIE) { newWindow = window.open('fmMaximize.asp?toolbar=' + escape(toolbar_str) + '&w=' + newW + '&h=' + newH + '', '', optIE); 
	} else    { newWindow = window.open('fmMaximize.asp?toolbar=' + escape(toolbar_str) + '&w=' + newW + '&h=' + newH + '', '', optNN); }


}

// ********************************************
// **              Miscellaneous             **
// ********************************************

// Check string, for navigator version
function fmCheckString(str_find, str) {
	var result=-1;
	var re = new RegExp(str_find) ;

	if (str.search(re) != -1)
		result = 0;
	else
		result = -1;
	return result;
} 
