// Extension des fonctions de l'api google map
if (!google.maps.Polygon.prototype.getBounds) {
	google.maps.Polygon.prototype.getBounds = function(latLng) {
		var bounds = new google.maps.LatLngBounds();
		var paths = this.getPaths();
		var path;
		for (var p = 0; p < paths.getLength(); p++) {
			path = paths.getAt(p);
			for (var i = 0; i < path.getLength(); i++) {
				bounds.extend(path.getAt(i));
			}
		}
		return bounds;
	}
}

if (!google.maps.Polygon.prototype.contains) {
	google.maps.Polygon.prototype.contains = function(latLng) {
		// Outside the bounds means outside the polygon
		if (this.getBounds && !this.getBounds().contains(latLng)) {
			return false;
		}
		var lat = latLng.lat();
		var lng = latLng.lng();
		var paths = this.getPaths();
		var path, pathLength, inPath, i, j, vertex1, vertex2;
		// Walk all the paths
		for (var p = 0; p < paths.getLength(); p++) {
			path = paths.getAt(p);
			pathLength = path.getLength();
			j = pathLength - 1;
			inPath = false;
			for (i = 0; i < pathLength; i++) {
				vertex1 = path.getAt(i);
				vertex2 = path.getAt(j);
				if (vertex1.lng() < lng && vertex2.lng() >= lng || vertex2.lng() < lng && vertex1.lng() >= lng) {
					if (vertex1.lat() + (lng - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < lat) {
						inPath = !inPath;
					}
				}
				j = i;
			}
			if (inPath) {
				return true;
			}
		}
		return false;
	}
}


// Fonction qui va dezoomer si des marqueurs sont en dehors de la partie visible de la map
if (!google.maps.Map.prototype.initZoom) {
	google.maps.Map.prototype.initZoom = function(marker) {
		var zoom = this.getZoom();
		if(!this.getBounds().contains(marker.getPosition())){
			zoom--;
			this.setZoom(zoom);
			this.setCenter(this.getBounds().getCenter());
			this.initZoom(marker);
		}
	}
}

// Extension de la fonction Array de javascript
if (!Array.prototype.in_array) {
	Array.prototype.in_array = function(p_val) {
	    for(var i = 0, l = this.length; i < l; i++) {
	        if(this[i] == p_val) {
	            rowid = i;
	            return new Array(true,rowid);
	        }
	    }
	    return false;
	}
}

		//Conversion d'un tableau en JSON
if (!Array.prototype.to_json) {
	Array.prototype.to_json = function() {
	    var parts = [];
	    var is_list = (Object.prototype.toString.apply(this) === '[object Array]');
	    for(var key in this) {
	    	var value = this[key];
	        if(typeof value == "object") { //Custom handling for arrays
	            if(is_list) parts.push(value.to_json()); /* :RECURSION: */
	            else parts[key] = value.to_json(); /* :RECURSION: */
	        }else if(typeof value != "function") {
	            var str = "";
	            if(!is_list) str = '"' + key + '":';

	            //Custom handling for multiple data types
	            if(typeof value == "number") str += value; //Numbers
	            else if(value === false) str += 'false'; //The booleans
	            else if(value === true) str += 'true';
	            else str += '"' + value + '"'; //All other things
	            // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)
	            parts.push(str);
	        }
	    }
	    var json = parts.join(",");
	    if(is_list) return '[' + json + ']';//Return numerical JSON
	    return '{' + json + '}';//Return associative JSON
	}
}

function number_format (number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands
    //
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/number_format    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    number = (number + '').replace(',', '').replace(' ', '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }    return s.join(dec);
}
