var googlemapclass = Class.create();
googlemapclass.prototype = {
	initialize: function(div, loadData, mypos)
	{
		this.styles             = [[{
				url: '/process_cc/atol_gmap_flag_big.png',
				height: 29,
				width: 28,
				opt_anchor: [1, 29],
				opt_textColor: 'novisible'
			},
			{
				url: '/process_cc/atol_gmap_flag_big.png',
				height: 29,
				width: 28,
				opt_anchor: [1, 29],
				opt_textColor: 'novisible'
			},
			{
				url: '/process_cc/atol_gmap_flag_big.png',
				height: 29,
				width: 28,
				opt_anchor: [1, 29],
				opt_textColor:'novisible'
			}]],
		this.div                = $(div);
		this.loadData           = loadData;
		this.map                = new GMap2(this.div);
		this.map.addControl(new GMapTypeControl());
		this.centerdefault      = new GLatLng(46.9502622421856, 2.6806640625);
		this.zoomdefault        = 5;
		this.map.setCenter(this.centerdefault, this.zoomdefault);
		this.zoommaxi           = 16;
		this.geocoder           = null;
		this.geocoder           = new GClientGeocoder();
		this.bounds             = this.map.getBounds();
		this.zoommini           = 5;
		this.markers            = [];
		this.markersClusterer  = null;
		this.mypos             = mypos;
		this.map.addControl(new GSmallMapControl());
		this.map.enableScrollWheelZoom();
		GEvent.addListener(this.map, 'zoomend', this.zoomend.bind(this));
		GEvent.addListener(this.map, 'moveend', this.moveend.bind(this));
		Event.observe(window, 'unload', function(){ GUnload();});
		this.getSpots(loadData);
		this.gdir               = new GDirections(this.map, document.getElementById("ResultsList"));
		GEvent.addListener(this.gdir, "load", this.onGDirectionsLoad);
		GEvent.addListener(this.gdir, "error", this.handleErrors);
	},
	foundIti:function(fromAddress, toAddress)
	{
		this.setDirections(fromAddress, toAddress, "FR");
	},
	setDirections:function (fromAddress, toAddress, locale)
	{
		this.gdir.loadFromWaypoints([fromAddress,toAddress],{travelMode:G_TRAVEL_MODE_DRIVING,locale:locale});
	},
	setCenter:function(point, zoom){this.map.setCenter(point, zoom);},
	zoomend:function()
	{
		if(this.map.getZoom()<this.zoommini)
		{
			this.map.setZoom(this.zoommini);
		}
		else
		{
			if(this.map.getZoom()>this.zoommaxi)
			{
				this.map.setZoom(this.zoommaxi);
			}
		}
	},
	moveend:function(){},
	getSpots:function(loadData)
	{

		if(loadData != null && loadData.length > 0)
		{

			if(loadData != 'all')
			{
				var params  = {};
				params.cp   = loadData;
			}
			else
			{
				var params  = {};
			}

			new Ajax.Request('/process_cc/get-opticiens.php',
			{
				method: 'post',
				parameters:params,
				onSuccess:this.updateContent.bind(this)
			});
		}
	},
	updateContent:function(e)
	{

		var Icon               = new GIcon();
		Icon.iconSize          = new GSize(18,19);
		Icon.iconAnchor        = new GPoint(1, 19);
		Icon.infoWindowAnchor  = new GPoint(1, 1);
		Icon.image             = "/process_cc/atol_gmap_flag_small.png";
    this.map.clearOverlays();
  //  var data1               = eval(e.responseText);

    var data               = eval(e.responseText);

    //var bRegroup           = true;
    var bRegroup           = false;

    var distance           = 9999999;
    var oOpticien          = null;
    this.bounds            = new GLatLngBounds();
    for (var iData=0; iData < data.length; iData++)
    {

            var point            = new GLatLng(data[iData].o_lat, data[iData].o_lng);
            var marker           = new GMarker(point ,{icon:Icon});
            GEvent.addListener(marker, "click", this.gotoFiche.bindAsEventListener(this, data[iData]));

            if(this.mypos != null)
            {
                    this.map.addOverlay(marker);
                    var temp = this.distanceFrom(point,this.mypos);
                    if (temp < distance)
                    {

                            distance         = temp;
                            oOpticien        = data[iData];
                    }
                    this.map.removeOverlay(marker);
            }
            this.bounds.extend(marker.getLatLng());
            this.markers.push(marker);
            if(bRegroup == false)this.map.addOverlay(marker);
	}

		if(oOpticien != null)
		{

			this.getProcheOpticien(oOpticien.o_id);
		}


		if(this.loadData != 'all' && data.length > 0 )
              	{

			var iZoom = this.map.getBoundsZoomLevel(this.bounds);

			this.map.setCenter(this.bounds.getCenter(), (iZoom-1));

		}

    if(bRegroup == true)this.refreshMap();

	},
	getProcheOpticien:function(id)
	{
		new Ajax.Request('/process_cc/get-opticiens.php?id='+id,
		{
			method: 'post',
			onSuccess:this.writeProcheOpticien.bind(this)
		});
		this.mypos = null;
	},
	writeProcheOpticien:function(e)
        {

		if(e.responseText != "")
		{
			$('magpluprc').setStyle({display: 'block'});
			$('magpluprcct').update(e.responseText);
		}
	},
	distanceFrom:function(a2,a)
	{
		var b     = a2.x-a.x;
		var c     = a2.y-a.y;
		return Math.sqrt(b*b+c*c);
	},
	refreshMap:function()
	{
		if (this.markerClusterer != null)
		{
			this.markerClusterer.clearMarkers();
		}
		this.markerClusterer = new MarkerClusterer(this.map, this.markers, {maxZoom: null, gridSize: null, styles: this.styles[0]});

	},
	gotoFiche:function(o, o2){window.location='/pages/marque-atol/detail-magasin.php?id='+o2.o_id;},
	gotoFrontiere:function(){this.setCenter( new GLatLng(49.993615462541136, 3.988037109375) ,7);},
	//Suisse
	gotoSuisse:function(){this.setCenter( new GLatLng(46.656977, 6.525879) ,7);},
	
	handleErrors:function()
	{
		var msg = "";
		switch(_googlemap.gdir.getStatus().code)
		{
		//case 400:msg = "";break;
		//case 500:msg = "";break;
		//case 601:msg = "";break;
		case 602:msg = "Google Map ne connait pas cette adresse.";break;
		case 603:msg = "Cette adresse semble indisponible";break;
		case 604:msg = "Google Map ne parvient pas a determiner l'itineraire";break;
		case 610:msg = "";break;
		//case 620:msg = "";break;
		default:msg = "";break;

		}
		// alert(_googlemap.gdir.getStatus().code+" : "+msg);
		alert(msg);
	},
	onGDirectionsLoad:function (){}

};




