/**
* Fichier	: utilOL.js
* Contenu	: Code javascript contenant des fonctions utiles pour la gestion de la carte
* Date		: 23/12/09
* @author	Gael Sauvanet
* @version	1.0
*/

/**
 * Permet d'adapter la taille de la carte à la taille de l'écran
 */
function resizeApp()
{
	var offsetTop = 0;
	var mapElem = e('map');

	for (var elem = mapElem; elem != null; elem = elem.offsetParent)
		offsetTop += elem.offsetTop;

	var height = getWindowHeight() - offsetTop - 25 - 40;
	if(height < 370) height = 370;
	if (height >= 0) 
	{
		mapElem.style.height = height + "px";
		e('cacherfeuille').style.top = (height/2 - 12) + "px";
		e('feuillederoute2b').style.height = (height-150) + "px";
	}
	map.updateSize();
}

/**
 * Retourne un élement par rapport à son identifiant
 */
function e(id) {return document.getElementById(id);}

/**
 * Retourne la taille de la fenêtre
 */
function getWindowHeight()
{
	if (window.self && self.innerHeight)
		return self.innerHeight;

	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;

	return 0;
}

/**
 * Construit l'url pour récupérer une dalle de carte
 */
function getTileURL(bounds)
{
	var res = this.map.getResolution();
	var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
	var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
	var z = this.map.getZoom();
	var limit = Math.pow(2, z);

	if (y < 0 || y >= limit)
	{
		return null;
	}
	else
	{
		x = ((x % limit) + limit) % limit;

		var url = this.url;
		var path = z + "/" + x + "/" + y + ".png";

		if (url instanceof Array) {
			url = this.selectUrl(path, url);
		}
		return url + path;
	}
}

/**
 * LonLat vers Point
 */
function LonLatToPoint(ll)
{
	return new OpenLayers.Geometry.Point(ll.lon,ll.lat);
}

/**
 * Mercator vers longitude/latitude
 */
function MToLonLat(ll)
{
	return ll.transform(new OpenLayers.Projection("EPSG:900913"),new OpenLayers.Projection("EPSG:4326"));  
}

/**
 * longitude/latitude vers Mercator
 */
function LonLatToM(ll)
{
	return ll.transform(new OpenLayers.Projection("EPSG:4326"),new OpenLayers.Projection("EPSG:900913"));  
}

/**
 * Retourne un objet layer par rapport à son nom
 */
function getLayer(nom)
{
	var layers = map.getLayersByName(nom);
	return layers[0];
}



