﻿function GoogleMap() {
}

var googleMap = null;

window.onload = function() {

    //  INITIALISE THE GoogleMap OBJECT
    googleMap = new GoogleMap();

    //  INITIALISE THE MAP
    googleMap.Load('map_canvas', -27.461263677578762, 153.028821900, 9);
    googleMap.GetVICs();
}

GoogleMap.prototype = {
    map: null,
    homeCenter: null,
    homeZoom: 9,
    bounds: null,
    zoom: 9,
    zoomChanged: false,
    positionChanged: false,

    Load: function(divID, lat, lng, zoom) {
        var thisMap = this;

        thisMap.homeCenter = new google.maps.LatLng(lat, lng);
        thisMap.homeZoom = zoom;
        var myOptions = {
            zoom: thisMap.homeZoom,
            center: thisMap.homeCenter,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        thisMap.map = new google.maps.Map(document.getElementById(divID), myOptions);
        thisMap.map.scrollwheel = false;

        //  INITIALISE THE MapMarker PROTOTYPE
        MapMarker.prototype.map = thisMap.map;

        //  CREATE THE Home BUTTON CONTROL
        var homeControlDiv = document.createElement('DIV');
        homeControlDiv.className = 'home-control';
        var homeControl = new HomeControl(thisMap.map, homeControlDiv, thisMap.homeCenter, thisMap.homeZoom);

        //  ADD THE Home BUTTON CONTROL TO THE MAP COLLECTION
        homeControlDiv.index = 1;
        thisMap.map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);

        thisMap.GetVICs();

        thisMap.classificationIDList = '';
    },

    ClearMarkers: function() {
        //  CLEAR THE ORIGINAL MAP MARKERS
        if (openInfoWindow) {
            openInfoWindow.CloseInfoWindow();
        }
        while (arrItems.length > 0) {
            arrItems[0].RemoveMarker();
            arrItems.shift();
        }
    },

    SetPosition: function(lat, lng, zoom, precinctID) {
        var thisMap = this;

        thisMap.map.setZoom(zoom);
        thisMap.map.setCenter(new google.maps.LatLng(lat, lng));
    },

    GetVICs: function() {
        var thisMap = this;

        var proxy = new Web.Service.MFS();
        proxy.GetVICs(thisMap.Search_Success, thisMap.Search_Fail);
    },

    Search: function(classIDList) {
    },


    Search_Success: function(result) {
        var tmpArray = new Array();

        //  CLEAR THE ORIGINAL MAP MARKERS
        googleMap.ClearMarkers();

        if (result && result.length > 0) {
            for (var i = 0; i < result.length; i++) {
                var test = new MapMarker(googleMap.map);

                test.Initialise(result[i], i);
                test.SetMarker();
                test.ShowMarker();

                arrItems[i] = test;
            }
        }

    },

    Search_Fail: function() {

    }

}

function HomeControl(map, controlDiv, home, zoom) {

    // We set up a variable for the 'this' keyword
    // since we're adding event listeners later
    // and 'this' will be out of scope.
    var control = this;

    // Set the home property upon construction
    control.SetHome(home);
    control.SetZoom(zoom);
    
    // Set CSS for the control interior
    var goHomeText = document.createElement('DIV');
    goHomeText.innerHTML = 'Home';
    controlDiv.appendChild(goHomeText);

    // Setup the click event listener for Home:
    // simply set the map to the control's current home property.
    google.maps.event.addDomListener(goHomeText, 'click', function() {
        googleMap.zoomChanged = true;
        map.setCenter(control.GetHome());
        map.setZoom(control.GetZoom());
    });
}

HomeControl.prototype = {
    home_: null,
    zoom_: null,

    GetHome: function() {
        return this.home_;
    },
    SetHome: function(home) {
        this.home_ = home;
    },
    GetZoom: function() {
        return this.zoom_;
    },
    SetZoom: function(zoom) {
        this.zoom_ = zoom;
    }
}



//  ****************************************  //
//  **                                    **  //
//  **  MFS Item                          **  //
//  **                                    **  //
//  ****************************************  //

var openInfoWindow = null;     //  STORES THE CURRENTLY OPEN Info Window
var arrItems = new Array();    //  ARRAY FOR MFS Item SEARCH RESULTS

function MapMarker(map) {
    var me = this;
    me.title = null;
    me.lat = null;
    me.lng = null;
    me.infoWindowHTML = '';

    me.Initialise = function(item) {
        var windowClass = 'marker-w-prod';
        
        me.title = item.Title;
        me.lat = item.Lat;
        me.lng = item.Lng;
        
        me.infoWindowHTML = me.infoWindowHTML +
                            '<div class="t" >' +
                                '<h3 style="margin-bottom:2px; color:#004664;">' + item.Title + '</h3>' +
                                '<b>P:</b> ' + item.Phone + '<br/>' +
                                '<b>W:</b> <a target="_blank" href="http://' + item.Website + '">' + item.Website +
                            '</div>';
    }

    me.SetMarker = function() {
        if (me.lat && me.lng && me.title) {
            me.googleMarker = new google.maps.Marker({
                position: new google.maps.LatLng(me.lat, me.lng),
                title: me.title,
                draggable: false,
                icon: '/Travel/Resources/IMG/MFS/vic-map-icon-small.png'
            });

            me.googleInfoWindow = new google.maps.InfoWindow();
            me.googleInfoWindow.setContent(me.infoWindowHTML);

            //  WIRE UP THE click EVENT
            google.maps.event.addListener(me.googleMarker, 'click', function() {
                if (openInfoWindow) {
                    openInfoWindow.CloseInfoWindow();
                }
                me.ShowInfoWindow();
                openInfoWindow = me;
            });
        }
    }

    me.ShowMarker = function() {
        if (me.googleMarker) {
            me.googleMarker.setMap(me.map);
        }
    }

    me.RemoveMarker = function() {
        if (me.googleMarker) {
            me.googleMarker.setMap(null);
        }
    }

    me.ShowInfoWindow = function() {
        if (me.googleMarker) {
            me.googleInfoWindow.open(me.map, me.googleMarker);
        }
    }

    me.CloseInfoWindow = function() {
        if (me.googleMarker) {
            me.googleInfoWindow.close();
        }
    }
}

MapMarker.prototype.map = null;


/*  EVENTS  */
function PrecinctMenu_Selected(lat, lng, zoom) {
    
}

