/**
 * The Parameters class implements the CustomisationAPI. A Parameters object can be initialised by a parameters
 * object. Then parameters are retrieved by name calling the get(name) function. If the parameter is not
 * found in the supplied parameters, a default value is returned.
 * 
 * @param parameters - object containing parameters set by the client
 */
function BOM_AWRIS_Parameters(parameters) {
	
	var UNDEF = 'undefined';
	this.parameters = parameters;
	
	this.defaults = new Array();
	
	// be very careful with these, they must match the region.type and map.map_type
	// in the data retrieved from the server
	this.defaults.regionTypeCountry = 'country';
	this.defaults.regionTypeState = 'state';
	this.defaults.regionTypeDD = 'drainage';
	this.defaults.regionTypeCC = 'city';
	
	this.defaults.width = undefined;
	this.defaults.titleVisible = true;
	this.defaults.regionNameVisible = true;
	this.defaults.regionMeasurementVisible = true;
	this.defaults.defaultBoundary = this.defaults.regionTypeCC;
	this.defaults.defaultRegionId = 'urn:bom.gov.au:awris:common:codelist:region.country:australia';
	this.defaults.dropDownVisible = true;
	this.defaults.goToLinkVisible = true;
	this.defaults.goToLinkText = undefined;
	this.defaults.goToLinkURL = "http://www.nwc.gov.au";
	
	this.defaults.localDomain = false;
	this.defaults.baseURL = 'http://water.bom.gov.au/widgets/waterstorage/'; 
	this.defaults.mapBaseURL = "maps/";
	this.defaults.serverURL = "resources/data";		
	this.defaults.chartBaseURL = "resources/chart/";
	this.defaults.location = "divWidget";
	this.defaults.widgetStyle = undefined;
	this.defaults.aboutURL = 'http://www.bom.gov.au/water/';
	this.defaults.mapTabSelected = 'true'; //passed in params as string 
	
	this.defaults.nationalRegionId = 'urn:bom.gov.au:awris:common:codelist:region.country:australia';
	
	// added configuration text
	this.defaults.title = 'Water Storage';
	this.defaults.lastRecordedText = 'Last recorded';
	this.defaults.aboutText = 'About';
	this.defaults.stateButtonAlt = 'States';
	this.defaults.cityButtonAlt = 'Cities';
	this.defaults.drainageButtonAlt = 'Drainage Divisions';
	this.defaults.goToAWRISLink = 'http://www.bom.gov.au/water';
	this.defaults.goToAWRISLinkText = 'Go to AWRIS';
	this.defaults.mapTabText = "Map";
	this.defaults.chartTabText = "Graph";
	this.defaults.percentageFullText = " full";
	this.defaults.goButtonText = "Go";
	this.defaults.stateSelectText = "Select a state...";
	this.defaults.citySelectText = "Select a city...";
	this.defaults.drainageSelectText = "Select a drainage division...";
	this.defaults.backToNational = "Back to National";
		
	if(typeof this.parameters.location == UNDEF ) {
		alert('You must provide a div id as location in the parameters');
		return;
	}
	
	//replace the region and map type parameters if passed in URL
	var regionId = getParamsFromUrl('defaultRegionId');
	if (regionId != 'UNDEFINED'){
		this.parameters['defaultRegionId'] = regionId;
	}
	
	//get and parse the map tab selected to a boolean
	var mapTabSelected = getParamsFromUrl('mapTabSelected');
	if (mapTabSelected == 'UNDEFINED'){
		this.parameters['mapTabSelected'] = this.defaults.mapTabSelected;
	} else {
		this.parameters['mapTabSelected'] = mapTabSelected;
	} 
	
	var mapType = getParamsFromUrl('defaultBoundary');
	if (mapType != 'UNDEFINED'){
		this.parameters['defaultBoundary'] = mapType;
	}
	
	function getParamsFromUrl(name){
		if (window.location.hash.length > 0) {
	    	var paramString = window.location.hash.substring(1);
	    	var paramArray = paramString.split("&");
	    	
	    	for (var i=0; i < paramArray.length; i++){
	    		var param = paramArray[i];
	    		var paramName = param.substring(0, paramArray[i].indexOf("=")); 
	    	    var paramValue = param.substring(param.indexOf("=") + 1, param.length); 
	    	    if (paramName == name){
	    	    	return paramValue;
	    	    }
	    	}
	    }
	    return 'UNDEFINED';
	}
	
	/**
	 * Returns  been pthe parameter with the given name, if it has notassed by the user of the api
	 * the default parameter is returned. If there is no parameter by the given name null is returned.
	 */
	this.get = function(name) {
		if( typeof this.parameters[name] == UNDEF ) {
		    return this.defaults[name];	
		}
		else {
		    return this.parameters[name];	
		}
	};
	
	/**
	 * Returns the parameter making sure it always has a trailing slash, this is used
	 * for URL parameters
	 */
	this.getWithForwardSlash = function(name) {
	    var url = this.get(name);
	    if( typeof url == UNDEF ) return url;
	    if( url.length > 1 && url.charAt(url.length-1) != "/" ) {
	        url += "/";	
	    }
	    return url;
	};
}
