function updateWorldpage(area_id) {
	return worldmap.update(area_id);
}

var worldmap = {
	//the url to the MapUpdate service
	url: null,
	//the areaId of the area that was last requested
	requestedAreaId: null,
	//the areaId of the area that is being loaded
	loadedAreaId: null,
	//indicates whether the worldmap is loading
	loading: false,
	
	//initializes the worldmap
	init: function() {
		alert('init worldmap');
	} ,
	
	//updates the worldmap for the given areaId
	//returns true when a load was successfull, false when it's not
	update: function(areaId) {
		//don't load the activeAreaId again
		if (areaId == null || isNaN(areaId)) {
			return true;
		}
		worldmap.requestedAreaId = areaId;
		//only load one area at a time
		if (!worldmap.loading) {
			worldmap.loading = true;
			worldmap.loadedAreaId = areaId;
			//load the area
			dojo.xhrGet({
				url: worldmap.url,
				content: {'area_id':areaId},
				handleAs: "xml",
				load: worldmap.callbackLoad,
				error: worldmap.callbackError,
				timeout: 2000
			});
			
		}
	} ,
	
	//callback for succesfull ajax response
	callbackLoad: function(responseObject, ioArgs) {
	    //alert('area ->'+worldmap.requestedAreaId);
		if (worldmap.loadedAreaId != worldmap.requestedAreaId) {
			worldmap.loading = false;
			//if a new areaId was requested, load that area instead
			worldmap.update(worldmap.requestedAreaId);
		} else {
			//update the page for the loaded area
			var url = dojo.byId('url');
			if (url) {
				var nodes = dojo.query('url', responseObject);
				if (nodes.length > 0) {
					url.value = nodes[0].textContent || nodes[0].text || "";
				}
			}
			var breadcrumb = dojo.byId('breadcrumb');
			if (breadcrumb) {
				var nodes = dojo.query('breadcrumb', responseObject);
				if (nodes.length > 0) {
					breadcrumb.innerHTML = nodes[0].textContent || nodes[0].text || "";
				}
			}
			var mapInformation = dojo.byId('mapInformation');
			if (mapInformation) {
				var nodes = dojo.query('description', responseObject);
				if (nodes.length > 0) {
					mapInformation.innerHTML = nodes[0].textContent || nodes[0].text || "";
				}
			}
			worldmap.loading = false;
		}
	} ,
	
	//callback for failed ajax response
	callbackError: function(responseObject, ioArgs) {
		worldmap.loading = false;
	} 
}

//webtrends implementation
	function getClickedText(continent,subcontinent,country,city){
		var clicked = "";
		var title = getTitleValues(continent,subcontinent,country,city,"_");
		if(subcontinent != "" || subcontinent.toString() == "Not available") {
				clicked = title.substring(title.lastIndexOf('_')+1,title.length);
		} else clicked = title; 
		return clicked;
	}
	
	/**
	* Returns the formatted title values after parsing from continent to country url
	*/
	function getTitleValues(continent,subcontinent,country,city,separator){
		var fValues;
		fValues = continent;
		if(subcontinent != "") {
			if(subcontinent.toString() == "Not available") fValues; 
			else fValues = fValues + separator + subcontinent;
			
		} else fValues;
		country != "" ? fValues = fValues + separator + country : fValues;
		city != "" ? fValues = fValues + separator + city : fValues;
		return fValues;
	}
	
	function stringToLowerFirstCap(text_string){
		var new_string= text_string.substring(0,1).toUpperCase() + text_string.substring(1).toLowerCase();
		return new_string;
	}
	
	var parameterTrail = "";
	
	/**
	* Called in the area onclick event of the worldmap flash
	*/
	function measureWorldMapEvent(clickTrail,eventtype){
		//alert("trail ->"+clickTrail);
		parameterTrail = clickTrail;
		getSelectAreaType(eventtype);
	}
	
	/**
	* Get the parameter trail from the flash and reports the values and events to webtrends
	*/
	function getSelectAreaType(eventtype){
		var areaTypeCode = "";
		var areaTypeName = "";
		
		var paramList = [];
		var isSubConAvail = true;
		var continent = "";
		var subcontinent ="";
		var country ="";
		var city = "";
		
		if(parameterTrail != ""){
			areaTypeCode = parameterTrail.substring(parameterTrail.lastIndexOf('_')-1,parameterTrail.length);
			areaTypeCode = areaTypeCode.substring(0,1);
		}
		
		paramList = parameterTrail.split('_');
		if(paramList.length > 4 && paramList[4] == 4){ // check after continent trail
			isSubConAvail = false;
		}
		if(isSubConAvail == false){
			switch (parseInt(areaTypeCode)) {
				case 1: break;
				case 2: continent = paramList[3]; select = "continent"; break;
				case 4: continent = paramList[3]; subcontinent = "Not available"; country = paramList[5]; select = "country"; break;
				case 5: continent = paramList[3]; subcontinent = "Not available"; country = paramList[5]; city = paramList[7];select = "city"; break;
				default:'unknown';
		    }
		}else{
			switch (parseInt(areaTypeCode)) {
				case 1: continent = "World"; break;
				case 2: continent = paramList[3]; select = "continent"; break;
				case 3: continent = paramList[3]; subcontinent = paramList[5]; select = "subcontinent"; break;
				case 4: continent = paramList[3]; subcontinent = paramList[5]; country = paramList[7]; select = "country"; break;
				case 5: continent = paramList[3]; subcontinent = paramList[5]; country = paramList[7]; city = paramList[9];select = "city"; break;
				default:'unknown';
			}
		}
		metron.measureAndOverwriteVariable("z_application", "DG");
		metron.measureAndOverwriteVariable("z_country", countryLocale);
		metron.measureAndOverwriteVariable("z_language", langLocale);
				
		if(continent!="") { 
			metron.measureAndOverwriteVariable("ti",stringToLowerFirstCap(select));
			metron.measureAndOverwriteVariable("dcsuri",getTitleValues(continent,subcontinent,country,city,'/')+".htm");
			
			if(eventtype == "searchBoxClick")	
				measureEvent("Clicked", "box_finddestination", document.getElementById("pageAreaType").value, getClickedText(continent,subcontinent,country,city));
			else measureEvent("Clicked", "navigation_select"+select, document.getElementById("pageAreaType").value, getClickedText(continent,subcontinent,country,city));	
			
		} 
		
		metron.measuresCommit();
		
	}
	
	/**
	* Called when the user moves the budget slider on the worldmap flash
	*/
	function measureBudgetSlider(amount){
		metron.measureVariablesAndCommit(
			{   "dcsuri" : getTitleValues(continent,subcontinent,country,city,'/')+".htm",
				"ti"  : "Worldmap", 
				"z_dg_event" : "Clicked",
				"z_dg_eventtype" : "slider_budget",
				"z_dg_eventplace" : document.getElementById("pageAreaType").value
			} );
	}