//*******************************************
// GLOBAL DATA TO HANDLE ALL THE DATA PROCESSING STUFF
// DO NOT EDIT THIS FILE.  IT IS GENERATED BY TOOLS
//*******************************************

//*********************************************

// DOUBLE POINT CLASS

function DoublePoint(x,y) {
	this.x = x;
	this.y = y;
}

// SORT FUNCTIONS

// numeric sort function
function num_sf (i1, i2) {
	//alert('i1 ->' + i1 +' i2 ->' + i2);
	var n1 = parseInt(i1); n2 = parseInt(i2);
	
	//alert('n1 ->' + n1 +' n2 ->' + n2);
	if (n1==n2) return 0;
	if (n1<n2) return -1;
	else return 1;
}


// SORT FUNCTIONS

// numeric sort function
function num_sf_2 (i1, i2) {

	var n1 = i1; n2 = i2;
	
	//alert('n1 ->' + n1 +' n2 ->' + n2);
	if (n1==n2) return 0;
	if (n1<n2) return -1;
	else return 1;
}


// case-insensitive sort function
function cis_sf (s1, s2) {
	var n1 = s1.toUpperCase(); var n2= s2.toUpperCase();
	if (n1==n2) return 0;
	if (n1<n2) return -1;
	else return 1;
}

// DATA CONTAINER CLASS

// Construtor
function MapDatabase (index, name) {

	this.index = index;
	this.name = name;
	// Identifica a informação
	this.idData = new Array();

	// Descreve a informação
	this.descData = new Array();

	// Tema da informação
	this.data = new Array();

	this.meta = new Array();
	//methods
	this.getName = getName;
	this.setName = setName;
	
	this.addIdData = addIdData;
	this.getIdValue = getIdValue;
	this.getNumIdData = getNumIdData;
	this.copyIdDataSet = copyIdDataSet;

	this.addDescData = addDescData;
	this.getDescValue = getDescValue;
	this.getNumDescData = getNumDescData;

	this.addDataSet = addDataSet;
	this.getDataSet = getDataSet; // only get's "a reference", CAUTION - DO NOT sort on this value
	this.copyDataSet = copyDataSet; // get's copy FOR SORTING
	this.getValue = getValue;
	this.exportXML = exportXML;

	this.setMeta = setMeta;
	this.getMeta = getMeta;
	this.getMappedValue = getMappedValue;
	this.getMappedValueFromData = getMappedValueFromData;
	this.getNumDataSets = getNumDataSets;
}

// DATASET META CLASS

function DataSetMeta(datatype, description, themetype, vmap) {
	this.dataType = datatype;
	this.description = description;
	this.themeType = themetype,
	this.vmap = vmap;
	// add your custom attributes here
}

// get number of datasets
function getName() {
	return this.name;
}

// get number of datasets
function setName(name) {
	this.name = name;
}

function addIdData(dsname, ds) {
	this.idData[dsname] = ds;
	//this.idLength++;
}

// get value from the named Desccription dataset
function getIdValue(dsname, id) {
	var i = this.index[id];
//	alert ('id: '+id +'  i: ' + i + 'dsn: '+ dsname);
	return this.idData[dsname][i];
}

// get number of datasets
function getNumIdData() {
	// return this.idLength;
	var i=0;
	for (key in this.idData) i++;
	return i;
}

function copyIdDataSet(dsname, dest) {
	var ds = (dest==null) ? new Array(this.data[dsname].length) : dest;
	var src = this.idData[dsname];
	for (var i=0; i<src.length; i++) {
		dest[i] = src[i];
	}
	return ds;
}

function addDescData(dsname, ds) {
	this.descData[dsname] = ds;
	//this.descLength++;
}

// get value from the named Desccription dataset
function getDescValue(dsname, id) {
	var i = this.index[id];
	var value = this.descData[dsname][i];
	try {
		var m = this.meta[dsname];
		if ('link' == m.dataType) {
			return '<A href="' + m.vmap.replace('%s', value) + '">Click Here</A>';
		} else {
			return m.vmap[value];
		}
	} catch (e) {
		return value;
	}
}


// get number of datasets
function getNumDescData() {
	//return this.descLength;
	var i=0;
	for (key in this.descData) i++;
	return i;

}

// add named dataset
function addDataSet(dsname, ds) {
	this.data[dsname] = ds;
	//this.dataLength++;
}

// get named dataset
function getDataSet(dsname) {
	return this.data[dsname];
}

function copyDataSet(dsname, dest) {
	var ds = (dest==null) ? new Array(this.data[dsname].length) : dest;
	var src = this.data[dsname];
	for (var i=0; i<src.length; i++) {
		dest[i] = src[i];
	}
	return ds;
}

// get value from the named dataset
function getValue(dsname, id) {
	var i = this.index[id];
	return this.data[dsname][i];
}

// get value from the named dataset
function getMappedValue(dsname, id) {
	var i = this.index[id];
	var value = this.data[dsname][i];
	try {
		return this.meta[dsname].vmap[value];
	} catch (e) {
		return null;
	}
}

// get value from the named dataset
function getMappedValueFromData(dsname, value) {
	try {
		return this.meta[dsname].vmap[value];
	} catch (e) {
		return null;
	}
}

// encode dataset as XML
function exportXML(dsname) {
	var ds = this.data[dsname];
	var vm = this.meta[dsname].vmap;
	var ix = this.index;
	var vmap_mode = (vm!=null);
	var xmlString = '';
	for (i in ix) {
		xmlString += '<d x="'+i+'" y="'+ (vmap_mode) ? ds[ix[i]] : ds[ix[i]] +'"/>';
	}
	return xmlString
}
	
// get number of datasets
function getNumDataSets() {
	var i=0;
	for (key in this.data) i++;
	return i;
}

// set metadata
function setMeta(dsname, metadata) {
	this.meta[dsname] = metadata;
}

// get metadata
function getMeta(dsname) {
	return this.meta[dsname];
}


////////////////////////////////////////
// COLOR STUFF
///////////////////////////////////////
// COLOR SCHEME CLASS

// constructor
function ColorScheme(name, colors, road_color, label_color, sys) {
	this.name = name;
	this.colors = colors;
	this.road_color = road_color;
	this.label_color = label_color;
	this.getColors = getColors;
	this.writeToPicker = writeToPicker;
	this.readFromPicker = readFromPicker;
	this.encodeColors = encodeColors;
	this.sys = sys;
}

// get color array; element 0 is reserved for nodata
function getColors(n) {
	switch (parseInt(n)) {
		case 0: return new Array(this.colors[0]); break;	
		case 1: return new Array(this.colors[0], this.colors[1]); break;	
		case 2: return new Array(this.colors[0], this.colors[1], this.colors[10]); break;
		case 3: return new Array(this.colors[0], this.colors[1], this.colors[5], this.colors[10]); break;
		case 4: return new Array(this.colors[0], this.colors[1], this.colors[4], this.colors[7], this.colors[10]); break;
		case 5: return new Array(this.colors[0], this.colors[1], this.colors[4], this.colors[6], this.colors[8], this.colors[10]); break;
		case 6: return new Array(this.colors[0], this.colors[1], this.colors[3], this.colors[4], this.colors[6], this.colors[8], this.colors[10]); break;
		case 7: return new Array(this.colors[0], this.colors[1], this.colors[3], this.colors[4], this.colors[6], this.colors[7], this.colors[9], this.colors[10]); break;
		case 8: return new Array(this.colors[0], this.colors[1], this.colors[2], this.colors[4],this.colors[5],  this.colors[6], this.colors[7], this.colors[9], this.colors[10]); break;
		case 9: return new Array(this.colors[0], this.colors[1], this.colors[2], this.colors[3], this.colors[4], this.colors[6], this.colors[7], this.colors[8], this.colors[9], this.colors[10]); break;
		case 10: return new Array(this.colors[0], this.colors[1], this.colors[2], this.colors[3], this.colors[4], this.colors[5], this.colors[6], this.colors[7], this.colors[8], this.colors[9], this.colors[10]); break;
		default: return new Array(this.colors[0], this.colors[1], this.colors[2], this.colors[3], this.colors[4], this.colors[5],	this.colors[6], this.colors[7], this.colors[8], this.colors[9], this.colors[10]);
	}
}

function encodeColors() {
	var s = "shading-colors:";
	for (var i=0; i<this.colors.length; i++) {
		if (i>0) s += ',';
		s += this.colors[i];
	}
	s += ";label-color:" + this.label_color;
	s += ";road-color:" + this.road_color;
	return s;
}

function writeToPicker(cpicker) {
	for (var i=0; i<this.colors.length; i++) {
		cpicker.getElementById('val'+i).style.setProperty('fill', this.colors[i]);
		cpicker.getElementById('val'+i).getParentNode().style.setProperty('visibility', 'visible');
	}

	cpicker.getElementById('vallabel').style.setProperty('fill', this.label_color);
	cpicker.getElementById('vallabel').getParentNode().style.setProperty('visibility', 'visible');

	cpicker.getElementById('valroad').style.setProperty('fill', this.road_color);
	cpicker.getElementById('valroad').getParentNode().style.setProperty('visibility', 'visible');
	
}

function readFromPicker(cpicker) {
	for (var i=0; i<this.colors.length; i++) {
		this.colors[i] = cpicker.getElementById('val'+i).style.getPropertyValue('fill');
	}
	this.label_color = cpicker.getElementById('vallabel').style.getPropertyValue('fill');
	this.road_color = cpicker.getElementById('valroad').style.getPropertyValue('fill');

}


/*
function writeToPicker(cpicker) {
	for (var i=0; i<this.colors.length; i++) {
		cpicker.getElementById('val'+i).style.setProperty('fill', this.colors[i]);
		cpicker.getElementById('val'+i).getParentNode().style.setProperty('visibility', 'visible');
	}
}

function readFromPicker(cpicker) {
	for (var i=0; i<this.colors.length; i++) {
		this.colors[i] = cpicker.getElementById('val'+i).style.getPropertyValue('fill');
	}
}
*/

