var map = null;
var div = null;

// initialize map
function k1_initialize_map() {
	var mapElement = document.getElementById("map");
	map = new GMap2(mapElement);

    // xm default_list map
    if( $('#map').hasClass('map') ) {
    	map.enableScrollWheelZoom();
        map.setCenter(new GLatLng(65.109148,26.323242),5);
        map.addControl(new GLargeMapControl3D());
        var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(100,10));
        map.addControl(new GScaleControl(), pos);        

        // marking points
		var bounds = new GLatLngBounds();
		
	    $('.katsastusasemat .station_coordinates, .katsastusasemat .search_coordinates').each(function(i){
	    	var coords = $(this).text().split(',');
            var latlng = new GLatLng(coords[0],coords[1]);
            if( $(this).prop('id').match(/station_coordinates_/) ) {
            	var station_id = $(this).prop('id').replace('station_coordinates_','');
            	var marker = k1_marker(latlng,$('#station_name_'+station_id).text());
            	var station_info = document.getElementById('station_info_'+station_id).cloneNode(true);
            	$(station_info).css('display','block');
	            marker.bindInfoWindow(station_info);
            } else {
    	        var marker = k1_marker(latlng,$('.katsastusasemat_search input[name=address]').val(),'orange');
	            //marker.bindInfoWindowHtml( $('.katsastusasemat_search input[name=address]').val() );
            }
            
            map.addOverlay(marker);
            bounds.extend(latlng);
        });

        if( $('.search_results').length > 0 ) {
			map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
        }
    }

 	// xm default_detail map
    if( $('#map').hasClass('smallmap') ) {
        var latlng = new GLatLng($('#contentlat').text(),$('#contentlng').text());
        var marker = k1_marker(latlng,false);
        
    	map.addControl(new GSmallMapControl());
        map.setCenter(latlng,15);
        map.addOverlay(marker);
        // listeners for opening & closing the info box
        GEvent.addListener(map, "click", function() {
            div.remove();
        });
        GEvent.addListener(marker, "click", function() {
            k1_showInfoBox(div, marker);
        });
        k1_showInfoBox(div, marker);
    }
	$(window).unload(function() { GUnload(); });

}

// set info box on the map
function k1_showInfoBox(div, marker) {
    div.css({
        'position': 'absolute'
    }).appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));
    var markerOffset = map.fromLatLngToDivPixel(marker.getPoint());
    div.show().css({ top:markerOffset.y - 120, left:markerOffset.x + 110 });
}

// set marker on the map
function k1_marker(latlng,title,type) {
	var k1Icon = new GIcon(G_DEFAULT_ICON);
	k1Icon.shadow = false;
	k1Icon.shadowSize = false;
	k1Icon.iconSize = new GSize(13,13);
    // this ancor the image to the point on map
	k1Icon.iconAnchor = new GPoint(7,7);
	k1Icon.infoWindowAnchor = new GPoint(7,7);
	k1Icon.image = $('base').attr('href') + "images/google_map_dot"+(type?"_"+type:"")+".gif";
    var markerOptions = { 'icon':k1Icon, 'draggable': false, 'title': title};
	var marker = new GMarker(latlng, markerOptions);
	
	return marker;
}

// On ready
$(function() {
	if( $('#map').length > 0  ) {
        // load div for the info box -- has to be done before map initializing
        div = $('#infopane');

		// this actually loads the map
		google.load("maps", "2", {"callback" : k1_initialize_map, "language": "fi", "other_params": "sensor=false"});
	}  
});

