window.addEvent('domready',function(){
	//-- initialize navigation (meetings,groups,annonce,idees)
	['meetings','groups','annonce','idees'].each(function(item, index){
		if($chk($(item+'_content'))){
			if($(item+'_content').getChildren().length > 4){
				var fx = new Fx.Scroll(item+'_scroller', {
					onComplete: function(){
						if((($(item+'_content').getChildren().length-3)*288) >= Math.abs($(item+'_scroller').getPosition($(item)).x)){ $(item+'_next').setStyle('visibility','visible');
						}else{ $(item+'_next').setStyle('visibility','hidden');
						}
						if(Math.abs($(item+'_scroller').getPosition($(item)).x) >  1){ $(item+'_previous').setStyle('visibility','visible');
						}else{ $(item+'_previous').setStyle('visibility','hidden');
						}	
					}
				});
				$(item+'_next').addEvent('click',function(){ fx.start(Math.abs($(item+'_scroller').getPosition($(item)).x)+288,0);
				});
				$(item+'_previous').addEvent('click',function(){ fx.start(Math.abs($(item+'_scroller').getPosition($(item)).x)-288,0);
				});
				fx.start(0,0);
				$(item+'_next').setStyle('visibility','visible');
			}
		}
	});
	
	//-- gestion de la carte
	$$('div#claim div.left a').each(function(item, index){
		$(item).addEvent('click',function(){
			$('map_canvas').setStyles({'margin-right':'18px','width':'434px'});
			map = new google.maps.Map2($('map_canvas'));
			map.disableDragging();
			map.addControl(new PZoomControl());
			var loading = new PControlLoading
			map.addControl(loading);
			GEvent.addListener(map,'dragend',function(){
				getMapContent(map);
			});
			geocoder = new GClientGeocoder();
				geocoder.getLatLng($(item).innerHTML,function(point) { map.setCenter(point, 13);
				getMapContent(map,loading);
			});
			document.body.addEvent('unload',GUnload);
		});
	});
	
	//-- gestion du formulaire
	$('zipCodeBtn').addEvent('click',function(){
		if($('zipCode').value.length > 1){
			$('map_canvas').setStyle('margin-right','18px');
			map = new google.maps.Map2($('map_canvas'));
			map.disableDragging();
			map.addControl(new PZoomControl());
			var loading = new PControlLoading
			map.addControl(loading);
			GEvent.addListener(map,'dragend',function(){
				getMapContent(map);
			});
			geocoder = new GClientGeocoder();
				geocoder.getLatLng($('zipCode').value+', FRANCE',function(point) { map.setCenter(point, 13);
				getMapContent(map,loading);
			});
			document.body.addEvent('unload',GUnload);
		}
	});
	
	$('zipCode').addEvent('keydown',function(event){
		if (event.key == "enter") {
			$('zipCodeBtn').fireEvent('click');
		}
	});

});


function getMapContent(map,loading){
	var jsonRequest = new Request.JSON({url: '../xmlPGoogleMap.php', onComplete: function(content){
		if(content.length > 0){
			content.each(function(item, index){
				map.addOverlay(getMarker(item));
				if(index == content.length-1){ loading.hide(); }
			});
		}else{
			loading.hide();
		}
	}}).get({'limit':100, 'type':'peuple', 'output':'json', 'coord': map.Enveloppe().toMysql(), 'zoom':map.getZoom()});
}

function getMarker(item){
	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	baseIcon.iconSize = new GSize(item.pictoOut.w, item.pictoOut.h);
	
	var icon = new GIcon(baseIcon);
	icon.image = item.pictoOut.href;
	
	var marker = new GMarker(new GLatLng(parseFloat(item.x),parseFloat(item.y)),icon);
	var label = new PLabel(marker.getLatLng(), item.login);
	
	GEvent.addListener(marker, "mouseover", function() {
		map.addOverlay(label);
	});
	
	GEvent.addListener(marker, "click", function() {
		document.location.href = 'http://www.peuplade.fr/home/subscribe.php';
	});

	GEvent.addListener(marker, "mouseout", function() {
		map.removeOverlay(label);
	});
	
	return marker;
}



function PControlLoading() {}
PControlLoading.prototype = new GControl();
PControlLoading.prototype.initialize = function(map) {
	var div = document.createElement("div");
	var img = document.createElement("img");
	img.src = "http://www.peuplade.fr/pics/homeLoader.gif";
	
	div.style.width = map.getSize().width + "px";
	div.style.height = ((map.getSize().height) - Math.max(map.getSize().height/3) + 2) + "px";
	div.style.color = '#993300'; 
	div.style.fontSize =  '20px';
	div.style.fontWeight = 'bold';
	div.style.textAlign = 'center';
	div.style.backgroundColor = '#FFFFFF';
	div.style.paddingTop = Math.max(map.getSize().height/3) + "px";
	
	div.innerHTML = 'Chargement en cours..';
	div.appendChild(img);
	
	this.div_ = div;
	this.map_ = map;
	
	map.getContainer().appendChild(div);
	return div;
}

PControlLoading.prototype.show = function (){
	this.div_.style.display = 'block';
}

PControlLoading.prototype.hide = function (){
	this.div_.style.display = 'none';
}

PControlLoading.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}
