var map;
var geocoder;
var agencies=new Array();
var baseIcon = new GIcon();
baseIcon.iconSize=new GSize(24,26);
baseIcon.iconAnchor=new GPoint(12,12);

function initMap(container) {
	map=new GMap2(container);
	map.enableScrollWheelZoom();
	map.addControl(new GMenuMapTypeControl());
	map.addControl(new GSmallMapControl());
	geocoder=new GClientGeocoder();
}

function initTable() {
	$('#list tr').each(function() {
		$(this).mouseover(function() {
			$(this).addClass("over");
		});
		$(this).mouseout(function() {
			$(this).removeClass("over");
		});
		$(this).click(function() {
			showAgency($(this).attr('index'));
			location.href="#map";
		});
	});
	var h=695+$('#list').height();
	$('#body').css('height',h+'px')
}

function centerMap(address,zoom) {
	geocoder.getLatLng(address+' France',function(point) {
		map.setCenter(point,zoom);
	});
}

function addAgency(address,infos) {
	var agency=new Object();
	agency.index=agencies.length;
	agency.address=address;
	agency.infos=infos;
	agencies[agency.index]=agency;
	geocoder.getLatLng(address,function(point) {
		agencies[agency.index].marker=new GMarker(point,{icon:new GIcon(baseIcon,'http://www.siaparisidf.com/common/puce1.gif',null,'')});
		agencies[agency.index].point=point;
		GEvent.addListener(agency.marker,'click',function() {
			showAgency(agency.index);
		});
		map.addOverlay(agencies[agency.index].marker);
	});
}

function showAgency(index) {
	map.closeInfoWindow();
	map.openInfoWindowHtml(agencies[index].point,agencies[index].infos);
	map.setZoom(16);
}

function verifForm() {
	if($('#lieu').val()=='') {
		error('Vous devez entrer une ville ou un code postal.',true);
		return false;
	} else if(isNumber($('#lieu').val().charAt(0)) && $('#lieu').val().length<5) {
		error('Le code postal entré n\'est pas valide.',true);
		return false;
	} else {
		return true;
	}
}

function isNumber(chr) {
	return (chr.charCodeAt(0)>47 && chr.charCodeAt(0)<58);
}

function verifFormKey() {
	var val=$('#lieu').val();
	var firstChar=val.substr(0,1);
	var result='';
	if(isNumber(firstChar)) {
		$('#lieu').attr('maxlength','5');
		for(i=0;i<val.length;i++) {
			if(isNumber(val.charAt(i))) {
				result+=val.charAt(i);
			}
		}
	} else {
		$('#lieu').attr('maxlength','50');
		for(i=0;i<val.length;i++) {
			if(!isNumber(val.charAt(i))) {
				result+=val.charAt(i);
			}
		}
	}
	$('#lieu').attr('value',result);
}

function error(msg,timeout) {
	$('#error').html(msg);
	$('#error').fadeIn(300);
	if(timeout) setTimeout(errorEnd,3000);
}

function errorEnd() {
	$('#error').fadeOut(1000);
}
