var map = null;
var mapInitialized = false;
/**
 * Makes the map visible
 */
showMap=function(ikeaMapType){
	if(window.dojo){
		dojo.style(dojo.byId('container'), 'display', 'block');
	}else{
		window.mapVisible=ikeaMapType;
	}
	
	switch( ikeaMapType ){
		case "maplocator":
			FramfabControl.mapPosition = 476;
			if (window.map) window.map.removeListeners();
			break;
		case "sys":
		default:
			FramfabControl.mapPosition = 2;
			if (window.map){
				window.map.enableListeners();
				if(ikeaMapType!=window.mapType)
					window.map.resetPosition();
			}
			break;
	}
	window.mapType = ikeaMapType;
//mapDebug("showMap() - ikeaMapType: " + ikeaMapType + ", FramfabControl.mapPosition: " + FramfabControl.mapPosition);
}

/**
 * Hides the map
 */
hideMap=function(){
	// window.mapVisible=false;
	positionMap(-2000, -2000);
	if(dojo){
		dojo.style('spaceLoader','display','none');
		if(window.map)window.map.hideSpaceBox();
	}
//	if(window.dojo){
//		dojo.style(dojo.byId('container'),'display','none');
//		dojo.style(dojo.byId('spacebox'),'display','none');
//	}
}

/**
 * Called by the flash to position the map. 
 */
positionMap=function(xPos,yPos){
	mapDebug("positionMap: xPos,yPos - " + xPos + ", " + yPos);
	if(window.dojo){
		dojo.style(dojo.byId('container'),'left',xPos+'px');
		dojo.style(dojo.byId('container'),'top',yPos+'px');
	}else{
		window.mapX = xPos;
		window.mapY = yPos;
	}
}

/**
 * Called by the flash to resize the map. 
 */
resizeMap=function(width, height){
	mapDebug("resizeMap: (" + width + ", " + height + ") - FramfabControl.mapPosition: " + FramfabControl.mapPosition) ;
	
	if(window.dojo){
		dojo.style(dojo.byId('container'),'width',width+'px');
		dojo.style(dojo.byId('container'),'height',height+'px');
		if (window.map) window.map.gmap.checkResize();
	} else {
		window.mapWidth = width;
		window.mapHeight = height;
	}
}

/**
 * if set to true, the map gets a higher z-index than flash div
 */
mapOnTop=function(state){
	if(window.dojo){
		dojo.style(dojo.byId('container'),'zIndex',state?1:0);
		dojo.style(dojo.byId('spacebox'),'zIndex',state?1:0);
		dojo.style(dojo.byId('swfDIV'),'zIndex',state?0:1);
	}else{
		window.initMapOnTop=state;
	}
}

/**
 * Try to change the zoom level
 */
zoomTo=function(level){
	mapDebug('zoomTo() - level: ' + level);
	window.map.zoom(level);
}
/**
 * Callback to Flash, when zoom level has been changed
 */
zoomed=function(level){
	// Callback to SWF, when zoom
	try{ FramfabControl.getFlashMovie().zoomed(level); } catch(err) {};
}

changeType=function(type){
	// Convert to GMap type
	switch(type){
		case "SPACEMAP_TYPE_HYBRID":
			type = G_HYBRID_MAP;
			break;
		case "SPACEMAP_TYPE_SATELITE":
			type = G_SATELLITE_MAP;
			break;
		default:
			type = G_NORMAL_MAP;
			break;
	}
	// Setting the type of map
	window.map.getGMap().setMapType(type);
}

getSpaces=function() {
	return window.map.getGMAP().getSpaces();
}

/**
 * Have the Flash show detailed space info
 */
viewSpace=function(spaceId){
	var success = false;
	try{success = FramfabControl.getFlashMovie().viewSpace(spaceId);}catch(err){};
	//
}

/**
 * Called to initialize the map. All loading of the map and external
 * javascripts will be loaded. This can take some time.
 * This function returns false until the map has been loaded
 */
initMap=function(options){
	 if(options)window.mapOptions = options;
	 if(!window.dojoLoading){
		 window.dojoLoading ='itIs';
		 include('../js/dojo/dojo.js');
	 }
	 if(window.map!=null)
		 return true;
	 else
		 return false;
}
 
initMapPh2=function(options){
	
	if(options)window.mapOptions = options;
	if(window.mapX){
		positionMap(window.mapX, window.mapY);
		window.mapX = null;
		window.mapY = null;
	}	
	if(window.mapWidth){
		resizeMap(window.mapWidth, window.mapHeight);
		window.mapWidth = null;
		window.mapHeight = null;
	}
	if(window.mapVisible){
		showMap(window.mapVisible);
		window.mapVisible = null;
	}
	if(window.initMapOnTop){
		mapOnTop(window.initMapOnTop);
		window.initMapOnTop = null;
	}
	
	dojo.require('maps.loader');
	if(!window.mapLoader){
		window.mapLoader = new maps.Loader();
		window.mapLoader.load(window.mapOptions);
		return false;
	}
	if(window.map!=null){
		mapInitialized = true;
		return true;
	} else {
		return false;
	}
}

/**
 * Called by the map application to indicate over for the flash application that a country
 * has been selected by clicking on a country icon
 */
mapsCountrySelected=function(country){
	var success = false;
	try{success = FramfabControl.getFlashMovie().countryChanged(country);}catch(err){};
}

/**
 * Called by the map application to inform the flash application of
 *  a given search result. 
 */
mapsSearchResult=function(count){
	//mapDebug('SEARCH RESULT: ' + count);
	var success = false;
	try{success = FramfabControl.getFlashMovie().setSearchCount(count);}catch(err){};
}

/**
 * Called by the map, when the range of available locations changes.
 * @param locations - Array of Jason locations
 */
locationsChanged=function(locations, count) {
	var success = false;
	try{success = FramfabControl.getFlashMovie().locationsChanged(locations, count);}catch(err){};
}

/**
 * Called by the flash application whenever a search is required. This is done
 * by either changing the tob or country selector. If either of them is either default
 * or no value, null should be supplied
 */
mapsSearch=function(tob, country){
	mapDebug('SEARCH for : ' + tob + ', ' + country);
//	window.map.search(tob, country);
	window.map.updateBusiness(tob);
}
 
mapsShowSpace=function(id){
	window.map.centerSpace(id);
}

showSpaceBox=function(id, show) {
	if (show){
		window.map.showSpaceBox(id);
	} else {
		window.map.hideSpaceBox(id);
	}
}

showLocation=function(location){
	//mapDebug('showLocation() - location: ' + location);
	//mapDebug('showLocation() - geo - lat: ' + location.lat + ', lon: ' + location.lon);
	
	// Convert strings to floats to circumvent ExternalInterface limitation
	// location.lat = parseFloat(location.lat); 
	// location.lon = parseFloat(location.lon); 

	//mapDebug('showLocation() - id: ' + location.id + ', lon: ' + location.lon + ', lat: ' + location.lat + ', name: ' + location.n);
	if (location != null){
		var reply = new Object();
		reply.count=1;
		reply.results=[location];
		// window.map.displaySpaceResult(reply);
		window.map.displayLocation(location);
	}
}
lookupGeoLocation=function(address, country,options){
	//mapDebug("lookupGeoLocation()\n\taddress: " + address + "\n\tcountry: " + country + "\n\toptions: " + options + "\n\tMap: " + window.map );	
	options.removeListeners=true;
	if (!window.map) initMap(options);
	if (window.map){
	// Make sure the map is initialised. If not, the GeoCoder will fail.
		var geocoder = new GClientGeocoder();
		geocoder.setBaseCountryCode(country); 
		geocoder.getLocations(address, geoLocationResponded);
		return true;
	} else {
		return false;
	}
}
geoLocationResponded=function(response){
	mapDebug('GeoLocation responded: ' + response);
	var success = false;
	try{success = FramfabControl.getFlashMovie().locationsLoaded(response);}catch(err){};
	//mapDebug('GeoLoc lookup success: ' + success);
}

include=function(src){
	var script = document.createElement('script');
	script.language='javascript';
	script.type='text/javascript';	
	script.src=src;
	document.getElementsByTagName("head")[0].appendChild(script);
}

mapDebug=function(message){
	try{console.log(message);}catch(err){}
}