//listen for flex app to initialize
FABridge.addInitializationCallback('plannerExpress', init);

//object that requests data via http
var xmlhttp;

//setup the object for instantiating and calling our xmlhttp request object
function loadBanner(url)
{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{
		// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

function state_Change()
{
	if (xmlhttp.readyState==4)
	{
		// 4 = "loaded"
		if (xmlhttp.status==200)
		{
			// 200 = "OK"
			//status=xmlhttp.status;
			//statusText=xmlhttp.statusText;
			//document.getElementById('bannerURL').href = xmlhttp.responseText;
			document.getElementById('iaccBannerArea').innerHTML = xmlhttp.responseText;
		}
		else
		{
			alert("Problem retrieving XML data:" + xmlhttp.statusText);
		}
	}
}

// flex app has initialized, let's do some stuff
function init()
{
	// let's determine the root of our flex app
	var flexApp =  FABridge.plannerExpress.root();
	
	//i listen to the country combo and when it changes I inform selectedCountryCallback
	flexApp.getLstCountry().addEventListener("change", selectedCountryCallback);
	
	//i listen to the metro combo and when it changes I inform selectedMetroCallback
	flexApp.getLstMetro().addEventListener("change", selectedMetroCallback);
	
	//i listen for a marker click and when it happens I inform selectedMetroCallback
	flexApp.addEventListener("markerOpened", setDefaultBanner);
}

// when country combo changes, I respond
var selectedCountryCallback = function(event)
{
	// set the countryID based upon the selection in the flex app
	var countryID = event.getTarget().getSelectedItem().CountryID;
	var countryName = event.getTarget().getSelectedItem().Name;
	//document.getElementById('bannerLink').href="";
	//document.getElementById('adType').innerHTML = "Platinum Member Sponsored Ad";
	//document.getElementById('bannerImg').src = "/index.cfm?fuseaction=plx_banner&countryID=" + countryID;
	var curl = "/index.cfm?fuseaction=plx_banner&countryID=" + countryID;
	loadBanner(curl);
}

//when metro combo changes, I respond
var selectedMetroCallback = function(event)
{
	// set the metroID based upon the selection in the flex app
	var metroID = event.getTarget().getSelectedItem().MetroID;
	var metroName = event.getTarget().getSelectedItem().Name;
	//document.getElementById('bannerLink').href="";
	//document.getElementById('adType').innerHTML = "Platinum Member Sponsored Ad";
	//document.getElementById('bannerImg').src = "/index.cfm?fuseaction=plx_banner&metroID=" + metroID;
	var murl = "/index.cfm?fuseaction=plx_banner&metroID=" + metroID;
	loadBanner(murl);
}

//when a marker is clicked, I respond
var setDefaultBanner = function(event)
{
	document.getElementById('bannerLink').href="http://www.iacconline.org/about/index.cfm?fuseaction=ConfCenterDiff";
	document.getElementById('adType').innerHTML = "IACC Sponsored Ad";
	document.getElementById('bannerImg').src = "/images/content/ads/iacc_banner8_v1_468x60.jpg";
}