﻿function GoogleMap() {
}

var googleMap = null;

window.onload = function() {

    //  INITIALISE THE GoogleMap OBJECT
    googleMap = new GoogleMap();

    //  INITIALISE THE MAP
    googleMap.Load('map_canvas', -27.390163677578762, 153.028821900, 9);
    googleMap.GetRegions();
}

GoogleMap.prototype = {
    map: null,
    homeCenter: null,
    homeZoom: 9,
    classificationIDList: '',
    bounds: null,
    zoom: 9,
    zoomChanged: false,
    positionChanged: false,
    isRegions: true,
    isPrecincts: false,
    isProducts: false,
    isPrecinctSelected: 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);

        //  WIRE UP THE dragend EVENT
        google.maps.event.addListener(googleMap.map, 'idle', function() {
            thisMap.bounds = googleMap.map.getBounds();

            if (thisMap.zoomChanged) {
                thisMap.zoomChanged = false;
                if (thisMap.zoom <= 10) {
                    //$('prec').setStyle({ visibility: 'hidden' });
                    $('#wd').css('visibility', 'hidden');
                    $('#acc').css('visibility', 'hidden');
                    $('#sa').css('visibility', 'hidden');
                    if (!thisMap.isRegions /*&& !thisMap.isProducts*/) {
                        thisMap.GetRegions();
                        thisMap.isRegions = true;
                        thisMap.isPrecincts = false;
                        thisMap.isProducts = false;
                        thisMap.isPrecinctSelected = false;

                        thisMap.classificationIDList = '';
                    }
                } else if (thisMap.zoom > 10 && thisMap.zoom <= 13) {
                    //$('prec').setStyle({ visibility: 'hidden' });
                    $('#wd').css('visibility', 'hidden');
                    $('#acc').css('visibility', 'hidden');
                    $('#sa').css('visibility', 'hidden');
                    if (!thisMap.isPrecincts /*&& !thisMap.isProducts*/) {
                        thisMap.GetPrecinctsByRegion(0);
                        thisMap.isRegions = false;
                        thisMap.isPrecincts = true;
                        thisMap.isProducts = false;

                        thisMap.classificationIDList = '';
                    }
                } else {
                    //$('prec').setStyle({ visibility: 'visible' });
                    $('#wd').css('visibility', 'visible');
                    $('#acc').css('visibility', 'visible');
                    $('#sa').css('visibility', 'visible');
                    if (thisMap.classificationIDList && thisMap.classificationIDList != '') {
                        if (!thisMap.isProducts) {
                            thisMap.isRegions = false;
                            thisMap.isPrecincts = false;
                            thisMap.isProducts = true;
                            thisMap.isPrecinctSelected = false;
                        }
                    } else {
                        thisMap.ClearMarkers();
                    }
                }
            } else if (thisMap.positionChanged) {
                thisMap.positionChanged = false;
                if (thisMap.zoom > 13) {
                    if (thisMap.classificationIDList && thisMap.classificationIDList != '') {
                        thisMap.Search();
                    }
                }
            }
        });

        //  WIRE UP THE dragend EVENT
        google.maps.event.addListener(googleMap.map, 'zoom_changed', function() {
            thisMap.zoom = googleMap.map.getZoom();
            thisMap.zoomChanged = true;
        });

        //  WIRE UP THE dragend EVENT
        google.maps.event.addListener(googleMap.map, 'dragend', function() {
            thisMap.positionChanged = true;
        });

        //  WIRE UP THE dragend EVENT
        google.maps.event.addListener(googleMap.map, 'dragstart', function() {
            if (thisMap.zoom > 13) {
                thisMap.ClearMarkers();
            }
        });
    },

    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;

        if (precinctID) {
            thisMap.GetPrecinct(precinctID);
            thisMap.isRegions = false;
            thisMap.isPrecincts = true;
            thisMap.isProducts = false;
            thisMap.isPrecinctSelected = true;
        }

        thisMap.map.setZoom(zoom);
        thisMap.map.setCenter(new google.maps.LatLng(lat, lng));
    },

    GetRegions: function() {
        var thisMap = this;

        var proxy = new Web.Service.MFS();
        proxy.GetRegions(thisMap.Search_Success, thisMap.Search_Fail);
    },

    GetPrecinctsByLocation: function() {
        var thisMap = this;

        var proxy = new Web.Service.MFS();
        proxy.GetPrecinctByLocation(thisMap.bounds.getNorthEast().lat(), thisMap.bounds.getSouthWest().lat(),
                                    thisMap.bounds.getSouthWest().lng(), thisMap.bounds.getNorthEast().lng(),
                                    thisMap.Search_Success, thisMap.Search_Fail);
    },

    GetPrecinctsByRegion: function(regionID) {
        var thisMap = this;

        var proxy = new Web.Service.MFS();
        proxy.GetPrecinctByRegion(regionID, thisMap.Search_Success, thisMap.Search_Fail);
    },

    GetPrecinct: function(precinctID) {
        var thisMap = this;

        //  SET THE FLAGS
        isRegions = false;
        isPrecincts = true;
        isProducts = false;

        var proxy = new Web.Service.MFS();
        proxy.GetPrecinct(precinctID, thisMap.Search_Success, thisMap.Search_Fail);
    },

    Search: function(classIDList) {
        var thisMap = this;

        if (classIDList) {
            thisMap.classificationIDList = classIDList;
        }

        if (thisMap.zoom >= 13) {
            if (!thisMap.bounds) {
                thisMap.bounds = googleMap.map.getBounds();
            }

            //  SET THE FLAGS
            isRegions = false;
            isPrecincts = false;
            isProducts = true;

            var proxy = new Web.Service.MFS();
            proxy.SearchByClassification(thisMap.bounds.getNorthEast().lat(), thisMap.bounds.getSouthWest().lat(),
                                         thisMap.bounds.getSouthWest().lng(), thisMap.bounds.getNorthEast().lng(),
                                         thisMap.classificationIDList, thisMap.Search_Success, thisMap.Search_Fail);
        }
    },


    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.map = map;
    //me.googleMarker = null;
    //me.googleInfoWindow = null;
    //me.MFSItemID = null;
    //me.accountID = null;
    me.title = null;
    me.lat = null;
    me.lng = null;
    me.infoWindowHTML = null;

    me.Initialise = function(item) {
        var viewUrl = '/Travel/Products/ProductView.aspx?id=';
        var windowClass = 'marker-w-prod';
        if (item.MFSItemID == 344) {
            if (item.AccountID == 15 || item.AccountID == 27) {
                viewUrl = '/Travel/Destination-Maps/Region.aspx?rid=';
                windowClass = 'marker-w-prec';
            } else {
                viewUrl = '/Travel/Precinct/Default.aspx?pid=';
                windowClass = 'marker-w-prec';
            }
        }
        me.title = item.Title;
        me.lat = item.Lat;
        me.lng = item.Lng;
        
        if (item.ThumbUrl) {
            me.infoWindowHTML = '<div class="' + windowClass + '">' +
                                '<div style="float:left;">' +
                                    '<img id="img-preview" width="100px" height="75px" src="' + item.ThumbUrl.replace('~', '') + '" alt="preview" />' +
                                '</div>';
        } else {
            me.infoWindowHTML = '<div class="marker-window">' +
                                '<div style="float:left;">' +
                                    '<img id="img-preview" width="100px" height="75px" src="/Travel/Resources/IMG/MFS/default-thumb.gif" alt="preview" />' +
                                '</div>';
        }

        if (item.ContactDetails) {
            me.infoWindowHTML = me.infoWindowHTML +
                                '<div class="t" style="float:left;overflow:hidden">' +
                                    '<h1>' + item.Title + '</h1>' +
                                    '<div style="font-size:80%;margin-left:8px;font-weight:bold;">' +
                                        item.ContactDetails +
                                    '</div>' +
                                '</div>';
        } else {
            me.infoWindowHTML = me.infoWindowHTML +
                                '<div class="t" style="float:left;">' +
                                    '<h1 style="margin-bottom:2px;">' + item.Title + '</h1>' +
                                '</div>';
        }

            me.infoWindowHTML = me.infoWindowHTML +
                                '<div style="clear:both"></div>' +
                                '<div class="c" style="overflow:hidden; padding-top: 8px;">' +
                                    '<p id="p-body-preview">' + item.Comment + '</p>' +
                                '</div>'+
                                '<div class="f" style="overflow:hidden; padding-top: 8px;">' +
                                    '<div style="float:right;">' +
                                        '<a href="' + viewUrl + item.AccountID + '"><img src="/Travel/Resources/IMG/MFS/more-btn.png" border="0"></a>' +
                                    '</div>' +
                                    '<div style="clear:both"></div>' +
                                '</div>' +
                            '</div>';

        //me.infoWindowHTML = '<div class="' + windowClass + '">' +
        //                        '<div class="i">' +
        //                            '<img id="img-preview" width="100px" height="75px" src="' + item.ThumbUrl.replace('~', '') + '" alt="preview" />' +
        //                        '</div>' +
        //                        '<div class="t">' +
        //                            '<h1>' + item.Title + '</h1>' +
        //                        '</div>' +
        //                        '<div style="clear:both"></div>' +
        //                        '<div class="c">' +
        //                            '<p id="p-body-preview">' + item.Comment + '</p>' +
        //                        '</div>' +
        //                        '<div class="f">' +
        //                            '<div style="float:right;">' +
        //                                '<a href="' + viewUrl + item.AccountID + '"><img src="/Travel/Resources/IMG/MFS/more-btn.png" border="0"></a>' +
        //                            '</div>' +
        //                            '<div style="clear:both"></div>' +
        //                        '</div>' +
        //                    '</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/item-marker.png',
                shadow: '/Travel/Resources/IMG/MFS/item-marker-shadow.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) {
    
}

