﻿var RED_PIN_ICON = new GIcon();
RED_PIN_ICON.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
RED_PIN_ICON.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
RED_PIN_ICON.iconSize = new GSize(12, 20);
RED_PIN_ICON.shadowSize = new GSize(22, 20);
RED_PIN_ICON.iconAnchor = new GPoint(6, 20);
RED_PIN_ICON.infoWindowAnchor = new GPoint(5, 1);

var BLUE_DOT_ICON = new GIcon(G_DEFAULT_ICON);
BLUE_DOT_ICON.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
BLUE_DOT_ICON.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
BLUE_DOT_ICON.iconSize = new GSize(12, 20);
BLUE_DOT_ICON.shadowSize = new GSize(22, 20);
BLUE_DOT_ICON.iconAnchor = new GPoint(6, 20);
BLUE_DOT_ICON.infoWindowAnchor = new GPoint(5, 1);

var AIRPLANE_ICON = new GIcon();
AIRPLANE_ICON.image = "/images/icons/32/airplane.png";
AIRPLANE_ICON.iconSize = new GSize(32, 32);
AIRPLANE_ICON.iconAnchor = new GPoint(16, 16);
AIRPLANE_ICON.infoWindowAnchor = new GPoint(16, 16);

var PALM_ICON = new GIcon();
PALM_ICON.image = "/images/icons/32/palm_tree.png";
PALM_ICON.iconSize = new GSize(32, 32);
PALM_ICON.iconAnchor = new GPoint(16, 16);
PALM_ICON.infoWindowAnchor = new GPoint(16, 16);


var g_icons = new Array();
g_icons['RED_PIN'] = RED_PIN_ICON;
g_icons['BLUE_DOT'] = BLUE_DOT_ICON;
g_icons['AIRPLANE'] = AIRPLANE_ICON;
g_icons['PALM_TREE'] = PALM_ICON;

function addMapPoints(gMap) {
    addMapPoints(gMap, 12);
}
function addMapPoints(gMap,defaultZoom){
    
    var count = 0;
    var bounds = new GLatLngBounds();
    $("[id^=map-point-]").each(function() {
        var values = '';
        var inp = $(this).find('input');
        if (inp.length > 0)
            values = inp.val().split(';');

        //alert(values[0]);
        if (values != '') {
            var marker = addMapMarker(bounds, gMap, values[0], values[1], values[2], values[4]);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(getInfoWindowHtml(values[0], '', values[3]));
            });
            $(this).mouseover(function() {
                marker.openInfoWindow(getInfoWindowHtml(values[0], '', values[3]));
            });
            ++count;
        }
    });
    if (count > 1) {
        gMap.setCenter(bounds.getCenter(), gMap.getBoundsZoomLevel(bounds));
    } else {
    gMap.setCenter(bounds.getCenter(), defaultZoom);
    }
    
    return count;
}

function addMapMarker(bounds,gMap,title,lng,lat,markerIconAlias){
    
    var markerOptions;
    if(markerIconAlias != ''){
        var markerIcon = g_icons[markerIconAlias];        
        markerOptions = { icon:markerIcon };
    }
    
    var marker;
    if(markerOptions){
         marker = new GMarker(new GLatLng(lat, lng),markerOptions);
    } else{
        marker = new GMarker(new GLatLng(lat, lng));
    }
    
    gMap.addOverlay(marker);    
    bounds.extend(marker.getPoint());
    return marker;    
}

function getInfoWindowHtml(title,bread,infoWindowIcon){
    if(infoWindowIcon && infoWindowIcon != ''){
        return '<div class="map-info-win"><h4 style="background-image:url(\'/images/icons/'+infoWindowIcon+'\');">' + title + '</h4></div>';
    } else {
        return '<div class="map-info-win"><h4>' + title + '</h4></div>';
    }
}


