var flashMap;
var mapIsReady = 0;


// initializeFlash()
// --------------------------------------------------------------------------------
// Define the hook to the flash and test for rendering
//
function initializeFlash() {
	
	// <![CDATA[
	var ie = navigator.appName.indexOf("Microsoft") != -1;
	flashMap = (ie) ? window['vayamaFlightMap'] : document['vayamaFlightMap'];
	// ]]>

	if(flashMap == null) {
		document.getElementById("mappedResult").style.display='none';
	}	
}

// LoadmapX()
// --------------------------------------------------------------------------------
// Load the map
//
function LoadmapX(targetPath) {

	// <![CDATA[
	var flashObj;
	flashObj = new SWFObject("http://cdn-us.vayama.com/flash/flightmap.swf", "vayamaFlightMap", "685", "375", "8.0.23", "#ffffff");
	
	flashObj.useExpressInstall("/flash/expressinstall.swf");
	flashObj.addParam("wmode", "opaque");
	flashObj.addParam("allowScriptAccess", "always");
	flashObj.addVariable("pageMode", "FindFlights");
	flashObj.write("vayamaFlightMapDiv");

	// ]]>

	initializeFlash();
}



// setMapReady()
// --------------------------------------------------------------------------------
// Called when the map is fully rendered and is available to take commands
//
function setMapReady() {
	mapIsReady = 1;
	if(flashMap) {
		if(mapItSearchId > 0)  {
			LoadAjaxCallFunction("/results/include/displayOnMap.jsp", "&searchId=" + mapItSearchId + "&itineraryId=" + mapItItinId, displayOnMapCallback);
		}
		else {
			drawSearchOnMap();
		}
	}
}


// drawSearchOnMap()
// --------------------------------------------------------------------------------
// Constructs the array of airports and passes it to the function to generate
// XML for the map.
//
function drawSearchOnMap() {

	if(flashMap) {

		var mode	= document.getElementById("tripType").value;
		var fromMt1 = document.getElementById("deptPlace1hidden").value;
		var ToMt1 	= document.getElementById("arrivalPlace1hidden").value;
		var fromMt2 = document.getElementById("deptPlace2hidden").value;
		var ToMt2 	= document.getElementById("arrivalPlace2hidden").value;
		var fromMt3 = document.getElementById("deptPlace3hidden").value;
		var ToMt3 	= document.getElementById("arrivalPlace3hidden").value;
		var Mtarray = [];
	
		if(fromMt1.length > 0) { Mtarray = [fromMt1, "-"]; }
		if((fromMt1.length > 0) &&(ToMt1.length > 0) ) { Mtarray = [fromMt1,ToMt1]; }
		if((fromMt1.length > 0)&& (ToMt1.length > 0) && (fromMt2.length > 0)) {	Mtarray = [fromMt1,ToMt1,fromMt2, "-"]; }
		if((fromMt1.length > 0) && (ToMt1.length > 0) && (fromMt2.length > 0) && (ToMt2.length > 0)) {	Mtarray = [fromMt1,ToMt1,fromMt2,ToMt2]; }
		if((fromMt1.length > 0) && (ToMt1.length > 0) && (fromMt2.length > 0) && (ToMt2.length > 0) && (fromMt3.length > 0)) {	Mtarray = [fromMt1,ToMt1,fromMt2,ToMt2,fromMt3, "-"]; }
		if((fromMt1.length > 0) && (ToMt1.length > 0) && (fromMt2.length > 0) && (ToMt2.length > 0) && (fromMt3.length > 0) && (ToMt3.length > 0)) {	Mtarray = [fromMt1,ToMt1,fromMt2,ToMt2,fromMt3,ToMt3]; } 
		
		if(Mtarray.length > 0) { 
			generateSearchXMLForMap(Mtarray, mode); 
		}
	}
}


// generateSearchXMLForMap()
// --------------------------------------------------------------------------------
// Passes the search O/D array to a AJAX function to generate map XML
//
function generateSearchXMLForMap(strArray, mode) {

	var tempDiv;
	var pop2=false;

	if(strArray[0].length>3)
	{	
		pop2=true;

		var country = new String(strArray[0]);
		airportsInArea(country);
	}
	else {

		if(strArray[0].length > 0) {
			
			var airportList = "";
			for (var i=0; i < strArray.length; i++) {
				airportList += strArray[i];
				
				if (i < strArray.length - 1) airportList += ",";
			}
			//Safety check .. just to be sure airports are being passed.
			if(airportList != "") {
				var arguments = "airports=" + airportList + "&searchMode=" + mode;
				LoadAjaxCallFunction('/modules/map/generateMapSearchXML.jsp', arguments, generateSearchXMLForMapCallback);
			}
		}
	}
}


// generateSearchXMLForMapCallback()
// --------------------------------------------------------------------------------
// Gets the search XML from the map and instruct the map to display
//
function generateSearchXMLForMapCallback(resultObj) {

	if (flashMap && mapIsReady) {
		flashMap.SendCommand(trim(resultObj));
	}
}


// displayWizardPosition()
// --------------------------------------------------------------------------------
// Display the wizard with position defined
//
function displayWizardPosition(title, description, position) {
	var X = 20;
	var Y = 250;
	
	if(position == "Center") {
			X = 150;
			Y = 140;
	} 
	else if (position == "BottomLeft") {
			X = 20;
			Y = 250;
	}
	else if (position == "BottomRight") {
			X = 290;
			Y = 270;
	}
	else if (position == "TopLeft") {
			X = 20;
			Y = 20;
	}
	else if (position == "TopRight") {
			X = 280;
			Y = 20;
	}

	var tosendString = "<DisplayWizard><caption><![CDATA[ " + description + "]]></caption><X>"+X+"</X><Y>"+Y+"</Y><Width>400</Width><Height>90</Height></DisplayWizard>";

	if(flashMap && mapIsReady){
		flashMap.SendCommand(tosendString);
	}
}

