﻿namespace('nnet.sd');

nnet.sd.MapView = $j.klass({
    initialize: function() {
        this.Panel = $j('#MapContainer');
        this.Map = null;
        this.MapCreated = false; // so map can be created after first display, for Safari/Chrome map must be displayed
        this.ListingTable = document.getElementById("SMLTable");
        this.ListingTableBody = document.getElementById("SMLTbody");
        //if (this.Map == null) this.Map = new GMap2(document.getElementById('MapContainer'));
    },

    startup: function() {
        try {
            this.Panel = $j('#MapContainer');
            this.ListingTable = document.getElementById("SMLTable");
            this.ListingTableBody = document.getElementById("SMLTbody");
        } catch (e) { logE("sd.MapView.startup", e); }
    },

    setCenter: function() {
        try {
            this.Map.setCenter(new GLatLng(NNet.SiteLatitude, NNet.SiteLongitude), NNet.SiteMapZoom);
        } catch (e) { logE("sd.MapView.setCenter", e); }
    },

    show: function() {
        try {
            this.Panel.show();

            // Create map first time it is shown, required for Safari/Chrome
            if (!this.MapCreated) {
                this.createMap();
                this.createMapOverlay();
                this.MapCreated = true;
            } else {
                this.Map.checkResize();
                this.Map.setCenter();
            }
        } catch (e) { logE("sd.MapView.show", e); }
    },

    hide: function() {
        try {
            this.Panel.hide();
        } catch (e) { logE("sd.MapView.hide", e); }
    },

    // Should be moved to service-listing
    callback: function(mapData) {
        try {
            SD.ServiceListing.Data = mapData;
            if (SD.MapView.MapCreated) SD.MapView.createMapOverlay()
            //CreateServiceMap(SD.MapView, ServiceMap);
            if (SD.ContentViewState == sVars.LIST_CONTENT_VIEW_STATE) {
                SD.ContentToolBar.showMapButton();
            } else if (SD.ContentViewState == sVars.MAP_CONTENT_VIEW_STATE) {
                SD.NavigationTabs.enableMapTab();
                SD.ContentToolBar.ListButton.show();
                if (SD.SearchSource == sVars.CATEGORY_SEARCH_SOURCE) SDCallback.GetServiceList(SD.CurrentCategoryId, SD.CurrentCategoryType, SD.CurrentSearchSite, NNet.ProcessID, GetServiceList_Callback, OnListCallbackError);
                else if (SD.SearchSource == sVars.TEXT_SEARCH_SOURCE) SDCallback.GetServiceListSearch(ParseSeachString(currentTextSearch), SD.CurrentSearchSite, NNet.ProcessID, GetServiceList_Callback, OnListCallbackError);
                else if (SD.SearchSource == sVars.SERVICE_SEARCH_SOURCE) SDCallback.GetOneServiceList(SD.CurrentServiceId, NNet.ProcessID, GetServiceList_Callback, OnListCallbackError);
                NNet.PageHistory.add();
            } else if (SD.ContentViewState == sVars.DETAIL_CONTENT_VIEW_STATE) {
            }
        } catch (e) { logE("sd.MapView.callback", e); }
    },

    createMap: function() {
        try {
            if (GBrowserIsCompatible()) {
                // Google map stuff
                if (this.Map == null) this.Map = new GMap2(document.getElementById('MapContainer'));
                this.Map.addControl(new GSmallMapControl());
                this.Map.addControl(new GMapTypeControl());
                this.Map.addControl(new GOverviewMapControl());
                this.setCenter();
            }
        } catch (e) { logE("sd.MapView.createMap", e); }
    },

    createMapOverlay: function() {
        try {
            if (GBrowserIsCompatible()) {
                this.Map.clearOverlays();

                var myNewtbody = document.createElement("tbody");
                myNewtbody.id = "SMLTbody";
                var docFragment = document.createDocumentFragment();
                var listings = SD.ServiceListing.Data.Listings;

                for (x = 0; x < listings.length; x++) {
                    var Listing = listings[x];
                    var point = new GLatLng(Listing.Lat, Listing.Long);
                    this.Map.addOverlay(createMarker(point, Listing.sid, docFragment, this.Map,
                        Listing.lid, Listing.Name, Listing.Description, Listing.URL,
                        Listing.Address, Listing.MapImageURL, Listing.EnhancedListing,
                        SD.ServiceListing.Data.UserLogedIn, Listing.EMail, Listing.IsEditable, Listing.FeedbackCount, Listing.RatingImage));

                }
                // Map Side bar stuff
                myNewtbody.appendChild(docFragment);
                this.ListingTable.replaceChild(myNewtbody, this.ListingTableBody);
                this.ListingTableBody = myNewtbody;
            }
        } catch (e) { logE("sd.MapView.createMapOverlay", e); }
    }


});
