/*-----------------------------------------------------------------------#
#  IDV Solutions JavaScript Function Library                             #
#  Copyright (C) 2006 IDV Solutions                                      #
#                                                                        #
#  filename: idvfunctions.js                                             #
#  created : 07.07.2006                                                  #
#  modified: 07.26.2006                                                  #
#-----------------------------------------------------------------------*/

var COUNTRYFRAME = "rightcolumn_iframe";
var COUNTRYURL = "/map/relatedarticles.htm";
var EMERGENCYFRAME = "rightcolumn_iframe";
var EMERGENCYURL = "/map/relatedarticles.htm";
var CRISISTYPECHECKBOX = "document.forms['RightPanel'].elements['Crisis']";	//ref this to the checkbox

/**
 * Called when page is loaded. 
 */
function onLoad() {
	setTimeout('setHostURL("' + COUNTRYFRAME + '","' + COUNTRYURL + '","' + EMERGENCYFRAME + '","' + EMERGENCYURL + '")', 2500);	// setup the URLs and frames used in flash
}

/**
 * called after feature loaded
 */
function featureLoaded() {
//	alert("test: ready");
}


/**
 * Pass host page URLs and frames to flash application
 * if the container page is changed, this part need to be modified.
 */		
function setHostURL(countryFrame,countryURL,EmergencyFrame,EmergencyURL) {
	//thisMovie("Map").setHostPageURL(window.location.href);
	
//	thisMovie("Map").setHostPageURL(window.top.location.href,"target","country.html","target","emergency.html");
	thisMovie("Map").setHostPageURL(window.top.location.href,countryFrame,countryURL,EmergencyFrame,EmergencyURL);
	//para1: hostpageURL
	//para2: target frame to update the country information
	//para3: the url to call for country information, using relative directory
	//		 the final url call will look like "href = 'xxxxxx.html?ab=xxx&cd=xxxx' target = 'xxxx'"
	//para4: target frame to update the emergency information
	//para5: the url to call for emergency information, using relative directory
}

/**
 * this function is called from the flash application to update the country information.
 * @iso, the country iso code
 */
 function showCountryInfo(iso) {
	open(COUNTRYURL + "?iso=" + iso,COUNTRYFRAME);
 }
 
/**
 *  combine the obj (checkbox) array names separated by comma.
 *  If the item is not checked, keep it empty.
 *  eg. ",xxxxsdd,,xxxxx" for 4 items, and item 1 and 3 are not checked.
 *
 * 
 *  finally call the flash function to update the flash view
 */
function updateMapInfo(obj, weatherobj) {
	//Loop through all weather (tsr GDACS) types
	var tsron = false;
	var gdacson = false;
	var tsrdays = 0;
	tsron = weatherobj[0].checked;
	gdacson = weatherobj[1].checked;
	// alert('GDACS:'+gdacson+' TSR:'+tsron);
	if (gdacson == null){
		gdacson = false;
	}
	if (tsron == null){
		tsron = false;
	}
	if (tsron){
		tsrdays = 30;
	}
	var crisistypes = "";

	//Loop through all crisis types
	for(i = 0; i < obj.length; i++) {
		
		/*crisistypes += (
			obj[i].checked == true ?
			"," + obj[i].name : 
			","
		);*/
		
		if (obj[i].checked) {
			crisistypes += "," + obj[i].name;
		}
		else {
			crisistypes += ",";
		}
	}


	//Remove extra comma
	if (crisistypes.length > 0 )
		crisistypes = crisistypes.substr(1);
//alert(crisistypes);
//alert(crisistypes.indexOf("disaster"));
	//Make flash call to update map
//set the flag to see the gdacs feed when disaster layer is on
if (crisistypes.indexOf("disaster")>0)
{
	gdacson =  true;
}
thisMovie("Map").updateMapView(crisistypes, false, 0, false, "",gdacson,tsron,tsrdays,false,"","","","");
//	thisMovie("Map").updateMapView(crisistypes, false, 0, false, "",false,false,0);
	//para1: crisis Types string, separated by comma
	//		eg: "/thenews/emergency/conflicts.htm,/thenews/emergency/food_security.htm,," for 4 crisis types with first two checked and last two unchecked
	//para2: flag for breaking news, 
	//		true, query on breaking news xml file, currently not provided
	//		false, query on hotspots xml file, currently used
}

/**
 * show and update the breaking news in the flash application.
 * @hours, the period in hours we search back.
 */
function breakingNewsMapInfo(hours){
	var ihours = parseInt(hours);
	thisMovie("Map").updateMapView("", true, ihours, false, "", true, false, 0,false,"","","","");
}

/**
 * show and update the breaking news in the flash application.
 * @statType, the statistic we are searching for
 */
function statisticMapInfo(statType){
	thisMovie("Map").updateMapView("", false, 0, false, statType, false, false, 0,false,"","","","");
}

/**
 * show the NGO view
 */
function ngoMapInfo(){
	thisMovie("Map").updateMapView("", false, 0, true, "", false,false,0,false,"","","","");
}

/**
 * show the Gdacs view
 */
function gdacsMapInfo(){
	thisMovie("Map").updateMapView("", false, 0, false, "", true,false,0,false,"","","","");
}


/**
 * show the Tsr view
 */
function tsrMapInfo(TsrDays){
	thisMovie("Map").updateMapView("", false, 0, false, "", false,true,TsrDays,false,"","","","");
}

/**
 * check all the checkbox.
 */
function chooseAll(obj) {
	for(i = 0; i < obj.length; i++) {
	obj[i].checked = true;
	}
	updateMapInfo(obj);
}

/**
 * set the checkboxes of crisis types
 * @obj, the checkbox group
 * @crisisTypes, the crisis type string deliminated by comma
 */
function setCrisisTypes(obj, crisisTypes) {
	for(i = 0; i < obj.length; i++) {
		if (crisisTypes.indexOf(obj[i].name) >=0)
		{
			obj[i].checked = true;	
		} else
		{
			obj[i].checked = false;	
		}
	}
}

/**
 * flash fscommand listener
 */
function Map_DoFSCommand(command, args) {
//alert("Map_DoFSCommand " + args);
	switch (command) {
	case "params":
		setCrisisTypes(eval(CRISISTYPECHECKBOX), args);
		break;
	case "info":
		if (args == "loaded") {
			featureLoaded();
		}
		break;
	default:
 }
}

/**
 * Pass a country ID, Lat/Long(optional) and tooltip text(optional) to flash application
 * the map will focused to the specified country with tooltip if Lat/Long/nameString provided.
 * @bEmergency, whether the code is the emergency code
 * @code, country iso code if bEmergency = false
 *        emergency code if bEmergency = true
 * @bHightlight, boolean, true=highlighted, default: false
 * @Lat/Long, optional point
 * @nameString, optional string(length <=50), valid if Lat/Long provided
 */

function loadSearchResult(bEmeregency,code, bHighlight, Lat, Long, nameString) {
	thisMovie("Map").SetSearchResult(bEmeregency,code, bHighlight, Lat, Long, nameString);
}


/**
 * Returns reference to flash movie
 * @param movieName name of the embeded SWF file*/
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	}
	else {
		return document[movieName];
	}
} 


/**
 * Writes flash object inside HTML
 * If the flash changed, this part needs to be changed.
 * @flashPath, the flashfile location
 * @flashappXmlPath, the flashapp.xml file location
 */
function writeFlashObject_orig(flashPath, flashappXmlPath) {
	
	//the parameters in the url
	var UrlParameters=window.top.location.search;
	var configPath='';
	
	//remove the '?' sign if exists
	if (UrlParameters[0]='?') {
		UrlParameters=UrlParameters.substr(1, UrlParameters.length-1);
		//UrlParameters=UrlParameters.replace(/,/g,"&");
	}
	
	if (flashappXmlPath == null || flashappXmlPath == "") 
	{
		configPath = '';
	} 
	else
	{
		configPath = '__config=' + flashappXmlPath + '&';
	}
	
	var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

	// Hook for Internet Explorer.
	if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
		document.write('<script language=\"VBScript\"\>\n');
		document.write('On Error Resume Next\n');
		document.write('Sub Map_FSCommand(ByVal command, ByVal args)\n');
		document.write('	Call Map_DoFSCommand(command, args)\n');
		document.write('End Sub\n');
		document.write('</script\>\n');
	}

	//write flash object
	document.write(
		' <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"' +
		' width="656" height="400" id="Map" align="middle" VIEWASTEXT>\n' +
		'	<param name="allowScriptAccess" value="sameDomain" />\n' +
		'	<param name="movie" value="' + flashPath + '" />\n' +
		'	<param name="quality" value="high" />\n' +
		'	<param name="bgcolor" value="#000000" />\n' +
		'	<param name=FlashVars VALUE="' + configPath + UrlParameters + '">\n' +
		'	<embed src="' + flashPath + '" \n' +
		'		FlashVars="' + configPath + UrlParameters + '"\n' +
		'		quality="high" bgcolor="#000000" \n' +
		'		width="656" height="400" name="Map" align="middle" \n' +
		'		allowScriptAccess="sameDomain" type="application/x-shockwave-flash" \n' +
		'		pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>'
	);
}

function writeFlashObject(flashPath, flashappXmlPath, extraparams) {
	
	//the parameters in the url
	var UrlParameters=window.top.location.search;
	var configPath='';
	
	//remove the '?' sign if exists
	if (UrlParameters[0]='?') {
		UrlParameters=UrlParameters.substr(1, UrlParameters.length-1);
		//UrlParameters=UrlParameters.replace(/,/g,"&");
	}
	if (extraparams){
		if (UrlParameters.length > 0){
			UrlParameters = UrlParameters + '&';
		}
		UrlParameters = UrlParameters + extraparams;
/*		alert('asdf:'+UrlParameters);*/
	}
	
	if (flashappXmlPath == null || flashappXmlPath == "") 
	{
		configPath = '';
	} 
	else
	{
		configPath = '__config=' + flashappXmlPath + '&';
	}
	
	var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

	// Hook for Internet Explorer.
	if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
		document.write('<script language=\"VBScript\"\>\n');
		document.write('On Error Resume Next\n');
		document.write('Sub Map_FSCommand(ByVal command, ByVal args)\n');
		document.write('	Call Map_DoFSCommand(command, args)\n');
		document.write('End Sub\n');
		document.write('</script\>\n');
	}
	
	//write flash object
/*	document.write(
		' <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"' +
		' width="468" height="370" id="Map" align="middle" VIEWASTEXT>\n' +
		'	<param name="allowScriptAccess" value="sameDomain" />\n' +
		'	<param name="movie" value="' + flashPath + '" />\n' +
		'	<param name="quality" value="high" />\n' +
		'	<param name="bgcolor" value="#000000" />\n' +
		'	<param name=FlashVars VALUE="' + configPath + UrlParameters + '">\n' +
		'	<embed src="' + flashPath + '" \n' +
		'		FlashVars="' + configPath + UrlParameters + '"\n' +
		'		quality="high" bgcolor="#000000" \n' +
		'		width="468" height="370" name="Map" align="middle" \n' +
		'		allowScriptAccess="sameDomain" type="application/x-shockwave-flash" \n' +
		'		pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>'
	);
*/
	var flashhtml = ' <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"' +
	' width="685" height="385" id="Map" align="middle" VIEWASTEXT>\n' +
	'	<param name="allowScriptAccess" value="sameDomain" />\n' +
	'	<param name="movie" value="' + flashPath + '" />\n' +
	'	<param name="quality" value="high" />\n' +
	'	<param name="bgcolor" value="#000000" />\n' +
	'	<param name=FlashVars VALUE="' + configPath + UrlParameters + '">\n' +
	'	<embed src="' + flashPath + '" \n' +
	'		FlashVars="' + configPath + UrlParameters + '"\n' +
	'		quality="high" bgcolor="#000000" \n' +
	'		width="684" height="389" name="Map" align="middle" \n' +
	'		allowScriptAccess="sameDomain" type="application/x-shockwave-flash" \n' +
	'		pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
/*	alert(flashhtml);*/
	document.write(flashhtml);
}