/* 
Copyright 2009 Expedia, Inc. All rights reserved. 
*/

/**
 * Utility for loading the dynamic map
 * @namespace YAHOO.cx.exp.map.loader
 */
YAHOO.namespace("cx.exp.map.loader");

/**
* Constant used for specifying the script location
* @type String
*/
var MAPPER_SCRIPT_LOCATION = "/static/default/default/scripts/exp/map/display";

/**
* Constant used for specifying the location of the mapper factory script.
* @type String
*/
var MAPPER_FACTORY_SRC = MAPPER_SCRIPT_LOCATION + "/expediaMap.js";

/**
* The MapLoader class is used for loading the mapping APIs and rendering the dynamic map.
* This class loads the APIs before renders the specified map type
* @param {String} mapType The map type, either Google or Microsoft
* @param {String} staticSrc The location of the static proprietary script.
* @param {String} dynamicSrc The location of the dynamic proprietary script.
* @param {String} params The map parameters
* @class YAHOO.cx.exp.map.loader.mapLoader
*/
YAHOO.cx.exp.map.loader.MapLoader = function (mapType /*:String */, staticSrc /*:String */, dynamicSrc /*:String */, params /* parameters */){
	
	// validate the input, do nothing if it fails
	if(arguments.length < 3 ||
		!(YAHOO.lang.isString(mapType) && (mapType.length > 0)) ||
		!(YAHOO.lang.isString(staticSrc) && (staticSrc.length > 0)) ||
		!(YAHOO.lang.isString(dynamicSrc) && (dynamicSrc.length > 0)) ){
		return;
	}
	
	// instance variables
	var _static_external_src = staticSrc;	
	var _dynamic_external_src = dynamicSrc;	
	var _mapType = mapType;
	var _adapter_src = MAPPER_SCRIPT_LOCATION + "/mapApi_" + _mapType + ".js";
	var _params = "";
	if (YAHOO.lang.isString(params)) 
		_params = params;	
	
		
	/**
	* Loads the script for the external proprietary APIs with the callback set to loadAdapterScript
	*/
	YAHOO.cx.exp.map.loader.MapLoader.loadExternalScript = function(){
		if(_mapType == "Google") {
			_dynamic_external_src = _dynamic_external_src + "?callback=YAHOO.cx.exp.map.loader.MapLoader.loadAdapterScript";
		}
		YAHOO.cx.exp.util.loadScript(_dynamic_external_src);
	};

	/**
	* Loads the script for the adapter APIs with a callback set to loadMapperScript
	*/
	YAHOO.cx.exp.map.loader.MapLoader.loadAdapterScript = function(){
		YAHOO.cx.exp.util.loadScript(_adapter_src,{callback: YAHOO.cx.exp.map.loader.MapLoader.loadMapperScript});		
	}

	/**
	 * Loads the script for the mapper factory APIs with a callback set to render
	 */
	YAHOO.cx.exp.map.loader.MapLoader.loadMapperScript = function(){
		YAHOO.cx.exp.map.loader.registerAPILoadEvent.subscribe(render);
		YAHOO.cx.exp.util.loadScript(MAPPER_FACTORY_SRC);		
	}	

	/**
	* This method will load the  map with the additional map parameters
	* @param {String} renderType The render type
	* @param {String} additional map parameters
	*/
	this.loadMap = function(renderType /*: String*/, params  /*: String*/){	
		if (renderType == "Static") {
			// construct the query string 
			var query = _params;
			if (YAHOO.lang.isString(params)) {
				if (query.length > 0)
					query  =  query + "&" + params
				else
					query = params;
			}			
			var src = _static_external_src;
			if (query.length > 0)
				var src = src + "?" + query;				

			// load the static image
			this.googleImage = YAHOO.util.Dom.get(JSONMapConfig.Container + "_image");
			this.googleImage.src = src ;	
			this.googleImage.style.cursor = 'hand';	
			this.googleImage.style.cursor = 'pointer';	
		} else {
			this.googleImage = null;
			// load all the API scripts, this will execute the render() callback method
			YAHOO.cx.exp.map.loader.MapLoader.loadExternalScript();	
		}
	}
		
	/**
	 * Renders the map
	 */
	var render = function(){
		// create the map	
		YAHOO.cx.exp.map.display.map.createMap(_params);
	}	
};

/**
* This method will trigger all the registerd listeners after the map APIs have been loaded
*/
YAHOO.cx.exp.map.loader.registerAPILoadEvent = function(){
	var obj = new YAHOO.util.CustomEvent("YAHOO.cx.exp.map.loader.registerAPILoadEvent");
	return obj;
}();
