/*global google*/

/* Check for, and if necessary, create the namespace - TRUKZ */
var TRUKZ = (typeof(TRUKZ) === "undefined") ? {} : TRUKZ;

/* Create a new child object to TRUKZ, access via TRUKZ.maps */
TRUKZ.maps = function () {
  
    /* Define our private variable object */
    var icons = {}, continents = [], mapCenter = [], regionalZoom = [], routeColors = [], routeWeights = [], routeOpacity = [];
    
    /* Private function */
    function createIcons() {
      
        /* Set up our base level marker */
        var trukzIcon = new google.maps.Icon();
        trukzIcon.shadow = "/images/mm_20_shadow.png";
        trukzIcon.iconSize = new google.maps.Size(12, 20);
        trukzIcon.shadowSize = new google.maps.Size(22, 20);
        trukzIcon.iconAnchor = new google.maps.Point(6, 20);
        trukzIcon.infoWindowAnchor = new google.maps.Point(5, 1);
        trukzIcon.infoShadowAnchor = new google.maps.Point(18, 25);
      
      /* Private function to help with the creation */
        function add(color) {
            icons[color] = new google.maps.Icon(trukzIcon, '/images/m_20_' + color + '.png');
            if (color === "truck") { // There's always one that has to be different...
                icons[color].shadow = "";
                icons[color].iconSize = new google.maps.Size(16, 16);
                icons[color].iconAnchor = new google.maps.Point(8, 8);
            }
        }
      
        /* Now create the markers we'll actually be using */
        /* Ensure that there's an image in /_img/markers/xxx.png when calling add('xxx'); */
        add('red');
        add('blue');
        add('blueHQ');
        add('green');
        add('yellow');
        add('truck');
        
    }
    
    /* All set up, now lets create them */
    createIcons();
    
    /* Create some other commonly used variables */
    continents   = ["North%20America", "Europe", "Australia"];
    mapCenter   = [new google.maps.LatLng(50, -110), new google.maps.LatLng(52, 15), new google.maps.LatLng(-26, 133)];
    regionalZoom = [3, 3, 4];
    routeColors  = ["#FF0000", "#00FF00", "#0000FF"];
    routeWeights = [2, 4, 6];
    routeOpacity = [0, 0.2, 0.4, 0.6, 0.8, 1];
   
   /* Now the publicly accessible stuff */
    return {
        continents: function (regionID) {
            return continents[regionID];
        },
        mapCenter: function (regionID) {
            return mapCenter[regionID];
        },
        icons: function (color) { // example usage: var marker = new GMarker(points, {icon: TRUKZ.maps.icons("red")});
            return icons[color];
        },
        regionalZoom: function (regionID) {
            return regionalZoom[regionID];
        },
        routeColors: function (colorID) {
            return routeColors[colorID];
        },
        routeWeights: function (weightID) {
            return routeWeights[weightID];
        },
        routeOpacity: function (opacityID) {
            return routeOpacity[opacityID];
        },
        city: function (lat, lng, city, region, country, supply1, supply2, demand1, demand2, temperature, precipitation, markerColour) {
            var html = "", marker;
            html = '<strong>' + city + '</strong> - ' + region + ' - ' + country + '<br /><strong>Supplies</strong> ' + supply1 + ', ' + supply2 + '<br /><strong>Demands</strong> ' + demand1 + ', ' + demand2 + '<br /><strong>Temperature</strong> ' + temperature + '&#730; F / ' + parseInt((temperature - 32) / 1.8, 10) + '&#730; C<br /><strong>Chance of Precipitation</strong> ' + precipitation + '%';
            marker = new google.maps.Marker(new google.maps.LatLng(lat, lng), {icon: TRUKZ.maps.icons(markerColour), title: city});
            google.maps.Event.addListener(marker, "click", function () {
                marker.openInfoWindowHtml(html);
            });
            return marker;
        }
    };
}();
