// ________________________________________________________
// Add marker
function __addMarker(position, label, html, type){
	var image = new google.maps.MarkerImage($.path + "images/ico_" + type + ".png",
		// This marker is 32 pixels wide by 32 pixels tall.
		new google.maps.Size(32, 37),
		// The origin for this image is 0,0.
		new google.maps.Point(0,0),
		// The anchor for this image is the base of the flagpole at 0,32.
		new google.maps.Point(0, 32));
	
	var shadow = new google.maps.MarkerImage($.path + "images/ico_" + type + "_shadow.png",
		// The shadow image is larger in the horizontal dimension
		// while the position and offset are the same as for the main image.
		new google.maps.Size(37, 37),
		new google.maps.Point(0,0),
		new google.maps.Point(0, 32));
	
	// Shapes define the clickable region of the icon.
	// The type defines an HTML &lt;area&gt; element 'poly' which
	// traces out a polygon as a series of X,Y points. The final
	// coordinate closes the poly by connecting to the first
	// coordinate.
	var shape = {
		coord: [1, 1, 1, 30, 28, 30, 28 , 1],
		type: 'poly'
	};
	
	var marker = new google.maps.Marker({
        position: position,
        map: $.gmap,
        title: label,
		shadow: shadow,
        icon: image,
        shape: shape,
		zIndex: $.gmarkers.length + 1
    });
	google.maps.event.addListener(marker, "click", function(){
		$.gwin.setContent('<div class="map_popup" style="width:320px;height:160px;">' + html + '</div>');
		$.gwin.open($.gmap, marker);
		$("#left .item a").removeClass("current");
		$("#left .item a." + (marker.getZIndex() - 1)).addClass("current");
		
		// Lightbox
		if($(".map_popup .lightbox").length > 0){
			$(".map_popup .lightbox").colorbox({
				width: "810px",
				//height: "700px",
				onOpen: function(){ $("object").css("visibility", "hidden"); },
				onClosed: function(){ $("object").css("visibility", "visible"); },
				opacity: 0.6
			});
		}
	});
	var page = window.location.href;
	page = page.substring(page.lastIndexOf("/") + 1, page.lastIndexOf("."));
	var obj = $("#left .item ul").append('<li><a href="' + page + '.php?id=' + $.gmarkers.length + '" class="' + $.gmarkers.length + '">' + label + '</a></li>');
	$("#left .item a." + $.gmarkers.length).click(function(e){
		var i = $(this).attr("class");
		google.maps.event.trigger(marker, "click");
		return false;
	});
	$.gmarkers.push(marker);
	
	return marker;
}

// ________________________________________________________
// Resize map
function __resizeMap(){
	var h = $(window).height() - $("#header").innerHeight() - $("#footer").innerHeight() - 71;
	$("#map_canvas").css({
		width: "100%",
		height: h + "px"
	});
	if($.browser.msie6){
		$("#map_canvas").css("width", "99%");
	}
	$("#left").css("height", h + 50);
}

// ________________________________________________________
// Init map
function __initMap(){
	var bounds = new google.maps.LatLngBounds;
	var position = new google.maps.LatLng(46.80335, -71.242905);
	var options = {
		zoom: 13,
		center: position,
		//mapTypeId: google.maps.MapTypeId.HYBRID,
		//mapTypeId: google.maps.MapTypeId.TERRAIN,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true,
		streetViewControl: false
	}
	$.gmarkers = [];
	$.gwin = new google.maps.InfoWindow();
	$.gmap = new google.maps.Map(document.getElementById("map_canvas"), options);
	
	// Resize map
	__resizeMap();
	$(window).resize(function(){
		__resizeMap();
	});
	
	// Sidebar
	if($("#left .item").length == 0){
		$("#left").append('<div class="item"><ul></ul></div>');
	}
	
	// Load markers
	var marker = null;
	var arr = {};
	var href = window.location.href;
	if(href.indexOf("?") >= 0){
		var elm = href.substring(href.indexOf("?") + 1).split("&");
		for(var i = 0; i < elm.length; i++){
			var p = elm[i].split("=");
			arr[p[0]] = p[1];
		}
	}
	arr["lang"] = $.lang;
	$.get($.path + "mod/gmarkers.php", arr, function(data){
		$(data).find("marker").each(function(i){
			position = new google.maps.LatLng($(this).find("lat").text(), $(this).find("lng").text());
			marker = __addMarker(position, $(this).find("label").text(), $(this).find("html").text(), $(this).find("type").text());
			bounds.extend(marker.getPosition());
			if(i == $(data).find("marker").length - 1){
				$.gmap.fitBounds(bounds);
				if($.gmap.getZoom() > 17){
					$.gmap.setZoom(17);
				}
			}
		});
	});
	
	// Disable markers
	$("input[type='checkbox']").click(function(e){
		for(var n in $.gmarkers){
			if($(this).val() == $.gmarkers[n].type){
				if($(this).is(":checked")){
					$.gmarkers[n].marker.setVisible(true);
					$("a." + n).parent().show();
				}else{
					$.gmarkers[n].marker.setVisible(false);
					$("a." + n).parent().hide();
				}
			}
		}
	});
}

$(document).ready(function(){
	if($("#map_canvas").length > 0){
		__initMap();
		$("body").addClass("gmap");
		$("body").removeAttr("style");
	}
});
