/**
 * Path to the marker image
 */
var _marker_image = 'http://www.praguefoodfestival.cz/google/img/ico2.png';

/**
 * Path to the source XML file
 */
var _source_xml = 'http://www.praguefoodfestival.cz/google/source.onsite.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) {
    var html   = '<h1 style="font-size: 150%; color: #A2094C; padding: 0; text-align: left; font-weight: normal; margin-bottom: 5px; font-family: Times New Roman, serif; background: none; margin-bottom: 10px;">' + title + '</h1>' + desc;
    var point  = new GLatLng(lat, lng);
    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.083252,14.426945), 12); // <- 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 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);
	}
}


