addpoints = function () {
	for (var i=0;i<defaultPoint.length;i++) {
		marker({
			description:defaultPoint[i][3],
			icon:defaultPoint[i][2],
			latlng:new GLatLng(defaultPoint[i][1], defaultPoint[i][0]),
			remove:function (lng,lat,icon) {
				new Ajax.Request('ajax/delpoint.ajax.php',{
					parameters:{
						'lng':lng,
						'lat':lat,
						'icon':icon
					}
				});
			}
		});
	}
}
hideshow = function(e) {
	if (e) {
		for($i=0;$i<iconslist.length;$i++) {
			iconslist[$i].show();
		}
	} else {
		for($i=0;$i<iconslist.length;$i++) {
			iconslist[$i].hide();
		}
	}
}
initialize = function() {
	if (GBrowserIsCompatible()) {
		map = new GMap2($('map_canvas'));
		map.setCenter(new GLatLng(45.5276294282777, 5.503045320510864), 12);
		map.addControl(new GScaleControl());
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GMenuMapTypeControl());
		iconslist = new Array();
		GEvent.addListener(map, 'click', function(ov,la) {
			marker({
				icon:document.getElementById('addicon').value,
				latlng:la,
				callback:function (lng,lat) {
					if (lat != 0 || lng != 0) { 
						new Ajax.Request('ajax/addpoint.ajax.php',{
							parameters:{
								'lat':lat,
								'lng':lng,
								'desc':$('adddesc').value,
								'icon':$('addicon').value
							}
						});
					}
				},
				remove:function (lng,lat,icon) {
					new Ajax.Request('ajax/delpoint.ajax.php',{
						parameters:{
							'lat':lng,
							'lng':lat,
							'icon':icon
						}
					});
				}
			});
		});
	}
}
//Création du bouton
marker = function(json) {
	var mode = 0;
	var _latlng = null;
	var _marker = null;
	var _option = {}
	// Gestion du mode
	if (json.mode) {
		mode = json.mode;
	} else {
		mode = 0;
	}
	//x : 5.503045320510864
	//y : 45.5276294282777
	// Gestion des icones personnalisées
	if (json.icon) {
		var _icon = new GIcon(G_DEFAULT_ICON);
		_icon.shadowSize = new GSize(0,0);
		_icon.iconSize = new GSize(17,17);
		_icon.iconAnchor = new GPoint(9,10);

		_icon.image = json.icon;
		_icon.shadow = null;
		_option.icon = _icon;
	}
	// Gestion latitude et longitude
	if (json.lat && json.lng) { 
		_latlng = new GLatLng(json.lat, json.lng);
	} else if (json.latlng) {
		_latlng = json.latlng;
	} else {
		_latlng = new GLatLng(0, 0);
	}
	// Marqueur
	_marker = new GMarker(_latlng, _option)
	 
	 GEvent.addListener(_marker, "click", function() {
        _marker.openInfoWindowHtml(json.description);
     });
	 
	GEvent.addListener(_marker, 'click', function() {
		if (document.getElementById('mode').checked) {
			map.removeOverlay(this);
			if (json.remove) {
				json.remove(_latlng.x, _latlng.y,this.getIcon().image);
			}
		} else if (document.getElementById('edit').checked) {
			//$('darkback').show();
			var myta = Builder.node('textarea',{
				id:'edittext',
				style:'border:0;height:100%;width:100%;'
			});
			var mynode = Builder.node('div',{
				style:'background:#fff;border:1px solid #000;position:absolute;height:300px;left:50%;margin:-150px 0 0 -300px;width:600px;top:50%;'
			},myta);
			var icon = this.getIcon().image;
			new Ajax.Request('ajax/getpoint.ajax.php',{
				parameters:{
					'lng':_latlng.x,
					'lat':_latlng.y,
					'icon':icon
				},
				onSuccess:function (x) {
					myta.value = x.responseText;
					document.body.appendChild(mynode);
					myta.focus();
					myta.observe('blur',function () {
						new Ajax.Request('ajax/modpoint.ajax.php',{
							parameters:{
								'lng':_latlng.x,
								'lat':_latlng.y,
								'icon':icon,
								'desc':myta.value
							},
							onSuccess:function () {
								//$('darkback').hide();
								myta.remove();
								mynode.setStyle({
									backgroundColor:'#007f00',
									color:'#ffffff',
									fontSize:'2em',
									height:'auto',
									padding:'30px',
									textAlign:'center'
								});
								mynode.innerHTML='Modification r&eacute;ussie !';
								window.setTimeout(function () {
									mynode.remove();
								},2000);
							}
						});
					});
				}
			});
			// EDIT
			//if (json.remove) {
			//	json.remove(_latlng.x, _latlng.y,this.getIcon().image);
			//}
		}
	});
	iconslist[iconslist.length] = _marker;
	map.addOverlay(_marker);
	// Fonction de callback
	if (json.callback) {
		json.callback(_latlng.x, _latlng.y);
	}
}
resize = function () {
	var size = Size();
	//$('darkback').style.width = size[0] +'px';
	//$('darkback').style.height = size[1] +'px';
	$('map_canvas').style.width = size[0] + 'px';
	$('map_canvas').style.height = size[1] + 'px';
}
Size = function () {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	return new Array(pageWidth,pageHeight);
}
$(document).observe('dom:loaded',function () {
	//$('darkback').setOpacity(0.5);
	resize();
	initialize();
	addpoints();

	$$('.boxhead').each(function (e) {
		e.observe('click',function () {
			if (e.getStyle('backgroundImage').indexOf('less.png') >= 0) {
				e.setStyle({
					backgroundImage:'url(imgs/plus.png)'
				});
				e.next().hide();
				if (e.parentNode.id == 'actions') {
					//$('darkback').hide();
				}
			} else {
				e.setStyle({
					backgroundImage:'url(imgs/less.png)'
				});
				e.next().show();
				if (e.parentNode.id == 'actions') {
					//$('darkback').show();
				}
			}
		});
	});
	$('addbtn').observe('click',function () {
		//$('darkback').hide();
		$$('.boxhead').each(function (e) {
			e.setStyle({
				backgroundImage:'url(imgs/plus.png)'
			});
			e.next().hide();
		});
	});
	$('edit').observe('click',function () {
		if (this.checked) {
			$('mode').checked = false;
		}
	});
	$('mode').observe('click',function () {
		if (this.checked) {
			$('edit').checked = false;
		}
	});
	new Draggable('actions');
	new Draggable('legendbox');
	new Draggable('options');
});
Event.observe(window,'resize',function () {
	resize();
});
