// JavaScript Document

var Init; if (!Init) Init = {};

Init.Gmap = function(selector,_opts) {
		
	// Options
	this.opts = jQuery.extend({
		'center' : [45.5,-73.583333],
		'zoom' : 7,
		'dragstart' : function(map,marker) {},
		'drag' : function(map,marker) {},
		'dragend' : function(map,marker) {},
		'click' : function(map,marker) {},
		'init' : function(map,marker) {}
	}, _opts);
	
	var $el = $(selector);
	
	if(!$el.length) { return; }
	
	var self = this;
	
	//////////////////////////////////////////
	//////Fonction Publiques/////////////////
	//////////////////////////////////////////
	self.addDragMarker = function(point,opts) {
		opts = jQuery.extend(self.opts, opts);
		
		if(!point.lat) {
			point = new google.maps.LatLng(point[0], point[1]);
		}
		
		var marker = new GMarker(point, {draggable: true});
		
		if(!opts.dragstart) {}
		else { GEvent.addListener(marker, "dragstart",function(){ opts.dragstart.call(self,self.map,marker); }); }
		if(!opts.dragend) {}
		else { GEvent.addListener(marker, "dragend",function(){ opts.dragend.call(self,self.map,marker); }); }
		if(!opts.drag) {}
		else { GEvent.addListener(marker, "drag",function(){ opts.drag.call(self,self.map,marker); }); }
		if(!opts.click) {}
		else { GEvent.addListener(marker, "click",function(){ opts.click.call(self,self.map,marker); }); }
		
		self.map.addOverlay(marker);
		
		if(!opts.init) {}
		else { opts.init(self.map,marker); }
		
		return marker;
	};
	
	self.addMarker = function(point,opts) {
		opts = jQuery.extend(self.opts, opts);
		
		if(!point.lat) {
			point = new google.maps.LatLng(point[0], point[1]);
		}
		
		var marker = new GMarker(point);

		if(!opts.click) {}
		else { GEvent.addListener(marker, "click",function(){ opts.click(self.map,marker); }); }
		
		self.map.addOverlay(marker);
		
		if(!opts.infoWindow) {}
		else { marker.openInfoWindowHtml(opts.infoWindow); }
		
		if(!opts.init) {}
		else { opts.init(self.map,marker); }
		
		return marker;
	};
	
	self.findAddress = function(address,callback) {
	  self.geocoder.getLatLng(address, function(point) { callback.call(self,point,address); });
	};
	
	self.setCenter = function(lat,lng,zoom) {
		self.center = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
		self.opts.zoom = (zoom || 14);
		self.map.setCenter(self.center, self.opts.zoom);
	};
	
	self.clearMarkers = function() {
		self.map.clearOverlays();
	};
	
	self.degToDec = function(deg,min,sec) {
		deg = parseFloat(deg);
		min = parseFloat(min);
		sec = parseFloat(sec);
		
		if(!(deg+0)){ deg = 0 };
		if(!(min+0)){ min = 0 };
		if(!(sec+0)){ sec = 0 };
		
		// Convert to Decimal Degrees Representation
		var dec = deg + (min/60) + (sec / 60 / 60);
		return Math.round(parseFloat(dec)*1000000000)/1000000000;
	};
	
	self.decToDeg = function(dec) {
		dec = dec + "";
		vars = dec.split(".");
		deg = vars[0];
		tempma = "0."+vars[1];
		tempma = tempma * 3600;
		min = Math.floor(tempma / 60);
		sec = tempma - (min*60);
		return {"deg":deg,"min":min,"sec":sec.toPrecision(8)};
	};
	//////////////////////////////////////////
	
	
	var container = $el.get(0);
	$el = $("<div>"+$el.html()+"</div>");
	
	self.geocoder = new GClientGeocoder();
	self.map = new GMap2(container);
	
	self.map.setUIToDefault();
	
	self.center = new google.maps.LatLng(self.opts.center[0], self.opts.center[1]);
	
	if($(selector).attr("rel") && $(selector).attr("rel").length > 2) {
		
		var latlong = $(selector).attr("rel").split("|");
		self.center = new google.maps.LatLng(parseFloat(latlong[0]), parseFloat(latlong[1]));
		self.opts.zoom = 15;
	}
	
	self.map.setCenter(self.center, self.opts.zoom);
	
	/////Ajoute des marqueurs
	$el.find(".marker").each(function() {
									  
		var point = self.center;
		if($(this).attr("rel") && $(this).attr("rel").length > 2) {
			var latlong = $(this).attr("rel").split("|");
			point = new google.maps.LatLng(parseFloat(latlong[0]), parseFloat(latlong[1]));
		}
		
		if($(this).is(".draggable")) {
			self.marker = self.addDragMarker(point,{
				'dragstart' : self.opts.dragstart,
				'dragend' : self.opts.dragend,
				'click' : self.opts.click,
				'init' : self.opts.init,
				'infoWindow' : ( ($(this).find(".infoWindow").length) ? $(this).find(".infoWindow").html():"" )
			});
		} else {
			self.marker = self.addMarker(point,{
				'click' : self.opts.click,
				'init' : self.opts.init,
				'infoWindow' : ( ($(this).find(".infoWindow").length) ? $(this).find(".infoWindow").html():"" )
			});
		}
	});
	///////////////////////////////

	
};
