/**
 * Path to the marker image
 */
var _marker_image = 'http://projects.wdf.cz/google-mapplets/united-islands2/img/ico.png';

/**
 * Path to the source XML file
 */
// DEV addr:
//var _source_xml = 'http://google.mapplet.dev5/united-islands2/source.xml';

// STAGE addr:
var _source_xml = '/assets/xml/gmap/source.xml';

/**
 * Powered by Inmite (http://www.inmite.eu)
 */
var map;

/**
 * Do post-init routines
 */
var marker_icon    = null;
var marker_options = null;
function after_init() {
	// Setup marker icon
	marker_icon = new GIcon();
	marker_icon.image = _marker_image;
	marker_icon.iconSize = new GSize(32, 31);
	marker_icon.iconAnchor = new GPoint(13,13);
	marker_icon.infoWindowAnchor = new GPoint(13, 0);
	
	// Setup marker options -- basicly, add there the icon
	marker_options = { icon: marker_icon };
}

/**
 * Put marker on the map // global function so we have variables in correct scope
 */
function putMarker(lng, lat, title, desc, scene) {
    var html   = '<h1 style="font-size: 24px; color: #D93A1F; padding: 0; text-align: left; font-family: Arial, Tahoma, sans-serif; background: none; margin: 5px 0 15px 0;">' + title + '</h1>' + desc;
    var point  = new GLatLng(lat, lng);
	marker_options['title'] = scene;
    var marker = new GMarker(point, marker_options);
    GEvent.addListener(marker, "click", function() {
        map.openInfoWindowHtml(point, html);
    });
    map.addOverlay(marker);      
}

/**
 * Initialize map and populate it with data
 */
function initialize() {
    if (GBrowserIsCompatible()) {              
        // Prepare map
        map = new GMap2(document.getElementById("map"));
        map.enableScrollWheelZoom();
        map.enableContinuousZoom();

        // Set up controls
        map.addControl(new GMapTypeControl());
        map.addControl(new GLargeMapControl());
        map.addControl(new GOverviewMapControl());

        // Restrict zoom levels
        var mt = map.getMapTypes();
        for (var i=0; i < mt.length; i++) {
            mt[i].getMinimumResolution = function() {return 1;};
            mt[i].getMaximumResolution = function() {return 16;};
        }

        // Set the scroll & zoom
    	map.setCenter(new GLatLng(50.078797,14.410815), 15); // <- this is prague
        
        // after init
        after_init();

        // Now, get the source XML data and parse them. Therefore,
        // we *will* show it on out map
        var xml = LoadXML(_source_xml, populate);
    }
}

/**
 * Populate the map with specified xml parser
 * 
 */
function populate(xml) {
    // And parse all of the nodes
    var points = xml.getElementsByTagName("point");
    for(var i = 0; i < points.length; i++) {
        var title = points[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
        var scene  = points[i].getElementsByTagName("scene")[0].childNodes[0].nodeValue;
        var desc  = points[i].getElementsByTagName("desc")[0].childNodes[0].nodeValue;
        var lng   = points.item(i).getAttribute("lng");
        var lat   = points.item(i).getAttribute("lat");
        
    	// And append the node into map
        putMarker(lng, lat, title, desc, scene);
	}
}



