var map = null;

var garage_colors = {
	"public" : "#1162DB",
	dash : "#DF2F23",
	validated : "#1C4996",
	free : "#8204E8",
	freeto10 : "#FF7930",
	moto : "#DF2F23"
};

var line_colors = {
	"1" : "#000000",
	"2" : "#FF3333"
};

var line_icon = new GIcon();
line_icon.iconSize = new GSize(9, 9);
line_icon.iconAnchor = new GPoint(4, 4);
line_icon.infoWindowAnchor = new GPoint(4, 4);
line_icon.infoShadowAnchor = new GPoint(4, 4);

function entity_escape ( str ) {
	return str.replace(/&/g, "&amp;")
		.replace(/"/g, "&quot;")
		.replace(/'/g, "&#039;")
		.replace(/</g, "&lt;")
		.replace(/>/g, "&gt;");
}

function in_poly ( a, points ) {
	var last_i, i, a_x, a_y, x, y, last_x, last_y, inside = false;
	
	a_x = a.lng();
	a_y = a.lat();
	
	last_i = points.length - 1;
	last_x = points[last_i].lng();
	last_y = points[last_i].lat();
	
	for ( i = 0 ; i < points.length ; i++ ) {
		x = points[i].lng();
		y = points[i].lat();
		
		if ( ( ( y <= a_y && a_y < last_y ) || ( last_y <= a_y && a_y < y ) )
				&& ( a_x < (a_y - y)*(last_x - x)/(last_y - y) + x) ) {
			inside = ! inside;
		}
		
		last_i = i;
		last_x = x;
		last_y = y;
	}
	
	return inside;
}

function find_point ( origin, v_x, v_y, d ) {
	// v_x, v_y, d define the vector to the new point.
	var o_x, o_y, n_x, n_y, s;
	
	o_x = origin.lng();
	o_y = origin.lat();
	
	if ( v_x == 0 ) {
		n_x = o_x;
		
		if ( v_y > 0 ) {
			n_y = o_y + d;
		} else {
			n_y = o_y - d;
		}
	} else {
		s = v_y/v_x;
		n_x = d*Math.sqrt(1/(1+s*s)) + o_x;
		n_y = s*(n_x - o_x) + o_y;
	}
	
	return new GLatLng(n_y, n_x);
}

function point_to_array ( point ) {
	if ( point ) {
		return [ point.lat(), point.lng() ];
	} else {
		return [ null, null ];
	}
}

function array_to_point ( a ) {
	return new GLatLng(a[0], a[1]);
}

function getGarageColor(garage) {
	if (garage.programsIdx['dash'] == 1) {
		return garage_colors['dash'];
	} else if (garage.programsIdx['freeto10'] == 1) {
		return garage_colors['freeto10'];
	} else if (garage.programsIdx['free'] == 1) {
		return garage_colors['free'];
	} else if (garage.programsIdx['validated'] == 1) {
		return garage_colors['validated'];
	} else {
		return garage_colors['public'];
	}
}


function ZoomControl () {
}

ZoomControl.prototype = new GControl();

ZoomControl.prototype.initialize = function ( map ) {
	var container = document.createElement("div"), link;
	
	if ( map.getZoom() <= 15 ) {
		container.className = "zoom_control zoom_control_small";
	} else {
		container.className = "zoom_control zoom_control_big";
	}
	
	GEvent.addListener(map, "zoomend",
		function ( old_zoom, new_zoom ) {
			if ( new_zoom <= 15 ) {
				container.className = "zoom_control zoom_control_small";
			} else {
				container.className = "zoom_control zoom_control_big";
			}
		});
	
	link = document.createElement("a");
	link.className = "zoom_big_link";
	link.innerHTML = "&nbsp;";
	link.onclick = function() { map.setZoom(16); };
	container.appendChild(link);
	
	link = document.createElement("a");
	link.className = "zoom_small_link";
	link.innerHTML = "&nbsp;";
	link.onclick = function() { map.setZoom(15); };
	container.appendChild(link);
	
	map.getContainer().appendChild(container);
	return container;
}

ZoomControl.prototype.getDefaultPosition = function () {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}
