/**
*	Constructor de l'objecte.
*	@param {NPoint} coord Coordenada del POI
*	@param {String} imagePath Imatge a mostrar
*	@param {Number} imageWidth Amplada de la imatge
*	@param {Number} imageHeight Alçada de la imatge
*	@param {String} identifier Identificador del POI. No te perquè ser únic. Paràmetre opcional.
*	@param {String} title Títol del POI. Paràmetre opcional.
*	@param {String} info Informació extesa del POI. Paràmetre opcional.
*	@param {String} type Tipus de poi. Serveix per si externament es necessita fer una classificació.
*/
function NPoi(coord,imagePath,imageWidth,imageHeight,identifier,title,info,type) {
	if (arguments.length ==3) {
		this.init2(arguments[0],arguments[1],arguments[2]);
	} else if ( arguments.length > 0 )
		this.init(coord,imagePath,imageWidth,imageHeight,identifier,title,info,type);
}

/**
*	Constructor de l'objecte.
*	@param {NPoint} coord Coordenada del POI
*	@param {String} imagePath Imatge a mostrar
*	@param {Number} imageWidth Amplada de la imatge
*	@param {Number} imageHeight Alçada de la imatge
*	@param {String} identifier Identificador del POI. No te perquè ser únic. Paràmetre opcional.
*	@param {String} title Títol del POI. Paràmetre opcional.
*	@param {String} info Informació extesa del POI. Paràmetre opcional.
*	@param {String} type Tipus de poi. Serveix per si externament es necessita fer una classificació.
*/
NPoi.prototype.init = function(coord,imagePath,imageWidth,imageHeight,identifier,title,info,type) {
	if (typeof(identifier)=='undefined') identifier="";
	if (typeof(title)=='undefined') title="";
	if (typeof(info)=='undefined') info="";
	if (typeof(type)=='undefined') type="";

	this.hasCoords=true;
	this.coord = coord;
	this.imagePath = imagePath;
	this.imageWidth = imageWidth;
	this.imageHeight = imageHeight;
	this.pixels = new NPoint(0,0);
	this.identifier = identifier;
	this.title = title;
	this.info = info;
	this.type = type;
};

/**
*	NPoi init2
*	@private
*/
NPoi.prototype.init2 = function(pixels,styleId,poiId) {
	try {
		
		this.hasCoords=false;
		this.coord = new NPoint(0,0);;
		this.imagePath = wfsstyles[styleId][1];
		this.imageWidth = 8;
		this.imageHeight = 8;
		this.pixels = pixels;
		this.identifier = poiId;
		this.title = "";
		this.info = "";
		this.type = "";

	}
	catch (e) {
		//alert("Error a NPoi.init2()\n"+e.message);
	}	
}

/**
*	Retorna la posició del POI
*	@return {NPoint}
*/
NPoi.prototype.getCoord = function() {
	return this.coord;
};

/**
*	Retorna el path a la imatge
*	@return {String}
*/
NPoi.prototype.getImagePath = function() {
	return this.imagePath;
};

/**
*	Retorna el height de la imatge
*	@return {String}
*/
NPoi.prototype.getImageHeight = function() {
	return this.imageHeight;
};

/**
*	Retorna el width de la imatge
*	@return {String}
*/
NPoi.prototype.getImageWidth = function() {
	return this.imageWidth;
};

/**
*	Returns the pixels coordinates corresponding to the real coords.
*	@return {NPoint}
*/
NPoi.prototype.getPixels = function() {
	return this.pixels;
};

/**
*	Retorna l'identificador del POI
*	@return {String}
*/
NPoi.prototype.getIdentifier = function() {
	return this.identifier;
};

/**
*	Retorna el títol del POI
*	@return {String}
*/
NPoi.prototype.getTitle = function() {
	return this.title;
};

/**
*	Retorna la informació del POI
*	@return {String}
*/
NPoi.prototype.getInfo = function() {
	return this.info;
};

/**
*	Actualitza la coordenada del POI
*	@param {NPoint} coord Coordenada
*/
NPoi.prototype.setCoord = function(coord) {
	this.coord = coord;
};

/**
*	Actualitza la coordenada del POI
*	@param {NPoint} coord Coordenada
*/
NPoi.prototype.setPixels = function(pixels) {
	this.pixels = pixels;
};

/**
*	NPoi toString
*	@private
*/
NPoi.prototype.toString = function() {
	return "[ Coordenada: " + this.coord + " , " + 
	"Coordenada: " + this.pixels + " , " + 
	"Identificador: " + this.identifier + " , " + 
	"Titol: " + this.title + " , " + 
	"Info: " + this.info + "]";
}

//----------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------

/**
*	Constructor de l'objecte.
*/
function NPois() {
		this.init();
}


/**
*	Constructor de l'objecte.
*/
NPois.prototype.init = function() {
	this.poisArray = new Array();
};


/**
*	Add a poi to the list
*	@param {NPoi} poi The POI
*/
NPois.prototype.add = function(poi) {
	var l=this.poisArray.length;
	this.poisArray[l]=poi;
}


/**
*	Returns true if this list contains no elements
*	@return {boolean}
*/
NPois.prototype.isEmpty = function() {
	return (this.poisArray.length==0);
}


/**
*	Get item from list
*	@param {Number} i Position
*/
NPois.prototype.item = function(i) {
	return this.poisArray[i];
}


/**
*	Returns number of pois in the list
*	@return {Number}
*/
NPois.prototype.count = function() {
	return this.poisArray.length;
}


/**
*	Clears the list
*/
NPois.prototype.clear = function() {
	this.poisArray=new Array();
}

/**
*	Get an array of items matching the poi id. TODO
*	@param {String} id identifier
*/
NPois.prototype.getById = function(id) {
	//TODO
	var retArray=new Array();
	alert("TODO");
	return retArray;
}


/**
*	Compute map pixels for the pois. 
*	@param {NExtent} id identifier
*	@param {width} width map image width in pixels
*	@param {height} height map image height in pixels
*/
NPois.prototype.computePixels = function(extent,width,height) {
	try {
		for (var i=0;i<this.count() ;i++ ) {
			var item=this.item(i);
			if (item.hasCoords) {
				var coord =item.getCoord();
				var diffX =item.getImageWidth()/2; // la imatge ha de quedar centrada al punt
				var diffY =item.getImageHeight()/2;

				// pixels2coord
				var centerX = (parseInt(coord.getX()) - extent.getMinX()) * width / (extent.getMaxX() - extent.getMinX());
				var centerY = (extent.getMaxY() - parseInt(coord.getY())) * height / (extent.getMaxY() - extent.getMinY());

				item.setPixels(new NPoint(Math.round(centerX-diffX),Math.round(centerY-diffY)));
			}
		}
	}
	catch (e) {
		alert("Error NPoi.computePixels() , "+e.message);
	}
}


/**
*	NPois toString
*	@private
*/
NPois.prototype.toString = function() {
	var str="";
	for (var i=0;i<this.count() ;i++ ) {
		str+=this.item(i).toString();
	}
	return str;
}