/**
* 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 = document.getElementById("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";
		$j('#cacherfeuille').css("top",(height/2 - 12) + "px");
		$j('#fdr_contenu').css("height",(height-150) + "px");
		if(ZONE_MODE_NOTATION || ZONE_MODE_OSB)
		{
			$j('#notation_contenu').css("height",height + "px");
			$j('#osb_contenu').css("height", height + "px");
			$j('#notation').css("height",height + "px");
			$j('#notation_accordion').accordion("resize");
			$j('#notation_selection').accordion("resize");
			$j('#notation_table_div').css("height",(height-90) + "px");
			$j('#balade_infos').css("height",height+"px");
			$j('#balade_infos2').css("height",height+"px");
			$j('#liste_balades_contenu').css("height",height+"px");
			$j('#circuits_accordion #circuits_contenu').css("height",(height-0)+"px");
		}
	}
	map.updateSize();
}

/**
 * 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];
}

/**
 * Gestion de l'affichage des boutons d'échelle (.boutonZoom)
 */
var gestionBoutonZoom = {
	timeoutID : null,// identifieur d'action planifiée pour le masquage des éléments
	sortie : function(){
		this.timeoutID = setTimeout(function(){$j('.boutonZoom').fadeOut()},3000);
	},
	entree : function(){
		$j('.boutonZoom').fadeIn();
		clearTimeout(this.timeoutID);
	},
	attacherEvenements : function(elt){
		$j(elt).bind("mouseover",function(){gestionBoutonZoom.entree()});
		$j(elt).bind("mouseout",function(){gestionBoutonZoom.sortie()});
	}
};

$j(function(){
		gestionBoutonZoom.attacherEvenements("#zoneDetectionBoutonZoom");
		gestionBoutonZoom.attacherEvenements(".boutonZoom");
		gestionBoutonZoom.attacherEvenements(".olControlPanZoomBar");
		});
