﻿
/// <reference path="GMAPJSHelper_Release.js" />
/// <reference path="markermanager.js" />

//declare global variables
var gmarkersA = [];
var map = new GMap2(document.getElementById("map"));
var mm;
var area;
var cPoint = new GLatLng();
var zoomLevel;

    function loadMap() {
        if (GBrowserIsCompatible()) {

            var i = 0;
            area = Request.QueryString("subdiv");
            //build map and get markers
            readMap("0");

        }
    }

    function createMarker(point, html, icon, propid) {
        /// <summary>
        /// This creates the markers
        /// </summary>
        /// <param name="point">
        /// point as GLatLng
        /// </param>
        /// <param name="Marker HTML">
        /// HTML
        /// </param>
        /// <param name="icon">
        /// Marker Icon as Icon
        /// </param>
        /// <param name="propid">
        /// propid as String
        /// </param>
        /// <returns>
        /// Marker
        /// </returns>
        
        var marker = new GMarker(point, { icon: icon, title: propid });
        GEvent.addListener(marker, "click", function() {
            test(marker.getLatLng());
            marker.openInfoWindowHtml(html);
        });
         
        return marker;
    }

    function test(pnt) {
        var panoClient = new GStreetviewClient();
        panoClient.getNearestPanorama(pnt, showPanoData);
    }
   
   function showPanoData(panoData) {
       var panoDiv = document.getElementById("street");
       if (panoData.code != 200) {
           panoDiv.style.display = "none";
          return;
       }
       else {
           //testing street view
           var myPanoView = new GStreetviewPanorama(panoDiv);
           myPanoView.setLocationAndPOV(panoData.location.latlng);
           panoDiv.style.display = "block";
       }
   }
    
    function TextualCenterControl() {
    }

    // To "subclass" the GControl, we set the prototype object to
    // an instance of the GControl object
    TextualCenterControl.prototype = new GControl();

    // Creates a one DIV for each of the buttons and places them in a container
    // DIV which is returned as our control element. We add the control to
    // to the map container and return the element for the map class to
    // position properly.
    TextualCenterControl.prototype.initialize = function(map) {
        var container = document.createElement("div");

        var centerDiv = document.createElement("div");
        this.setButtonStyle_(centerDiv);
        container.appendChild(centerDiv);
        centerDiv.appendChild(document.createTextNode("Center Map"));
        GEvent.addDomListener(centerDiv, "click", function() {
            var panoDiv = document.getElementById("street");
            panoDiv.style.display = "none";
            mapCenter();
        });

        map.getContainer().appendChild(container);
        return container;
    }

    // By default, the control will appear in the top left corner of the
    // map with 7 pixels of padding.
    TextualCenterControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(3, 103));
    }

    // Sets the proper CSS for the given button element.
    TextualCenterControl.prototype.setButtonStyle_ = function(button) {
        button.style.textDecoration = "underline";
        button.style.color = "#0000cc";
        button.style.backgroundColor = "white";
        button.style.font = "small Arial";
        button.style.border = "1px solid black";
        button.style.padding = "2px";
        button.style.marginBottom = "3px";
        button.style.textAlign = "center";
        button.style.width = "6em";
        button.style.cursor = "pointer";
    }

    function readMap(sdate) {
                
        var saticon = new GIcon();
        saticon.image = "../../images/green.png";
        saticon.shadow = "../../images/shadow.png";
        saticon.iconSize = new GSize(12, 20);
        saticon.shadowSize = new GSize(22, 20);
        saticon.iconAnchor = new GPoint(6, 20);
        saticon.infoWindowAnchor = new GPoint(5, 1);

        var sunicon = new GIcon();
        sunicon.image = "../../images/yellow.png";
        sunicon.shadow = "../../images/shadow.png";
        sunicon.iconSize = new GSize(12, 20);
        sunicon.shadowSize = new GSize(22, 20);
        sunicon.iconAnchor = new GPoint(6, 20);
        sunicon.infoWindowAnchor = new GPoint(5, 1);

        var notrentedicon = new GIcon();
        notrentedicon.image = "../../images/gray.png";
        notrentedicon.shadow = "../../images/shadow.png";
        notrentedicon.iconSize = new GSize(12, 20);
        notrentedicon.shadowSize = new GSize(22, 20);
        notrentedicon.iconAnchor = new GPoint(6, 20);
        notrentedicon.infoWindowAnchor = new GPoint(5, 1);


        var cdricon = new GIcon();
        cdricon.image = "../../images/cdr.png";
        cdricon.shadow = "../../images/cdrs.png";
        cdricon.iconSize = new GSize(32, 32);
        cdricon.shadowSize = new GSize(56, 32);
        cdricon.iconAnchor = new GPoint(16, 32);
        cdricon.infoWindowAnchor = new GPoint(16, 0);
        
        var bounds = new GLatLngBounds();

        GDownloadUrl("../../coords.xml", function (data) {
            //var gmarkersA = [];
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("marker");

            for (var i = 0; i < markers.length; i++) {
                // obtain the attribues of each marker
                var tester = 0;
                var html = "trial";
                var lat = parseFloat(markers[i].getAttribute("lat"));
                var lng = parseFloat(markers[i].getAttribute("lng"));
                var propid = markers[i].getAttribute("propid");
                var point = new GLatLng(lat, lng);

                if (i == (markers.length - 1)) {
                    tester = 1;
                    html = '<p><center><span style="font-size:small"><strong>Our South Office<\/strong><\/span><\/center><\/p><img src="../../images/kittyhawkoffice.png" \/><br \/>';
                    html = html + '<p><center><span style="font-size: small">3903 Croatan Highway,<br \/>(158 Bypass MP 4)<br \/>Kitty Hawk, NC 27949<\/span><\/center><\/span><\/p>';
                }
                else if (i == (markers.length - 2)) {
                    tester = 2;
                    html = '<p><center><span style="font-size:small"><strong>Our Duck Office<\/strong></span><\/center><\/p><img src="../../images/duckoffice.png" \/>';
                    html = html + '<p><center><span style="font-size: small">1197 Duck Road,<br \/>Duck, NC 27949<\/span><\/center><\/span><\/p>';
                }
                else {
                    for (var c = 0; c < propidArr.length; c++) {
                        if (propidArr[c].substring(0, propidArr[c].indexOf('>')) == propid) {
                            var propname = propidArr[c].substring(propidArr[c].indexOf('>') + 1, propidArr[c].indexOf(' '));
                            html = '<a href="javascript:winClose(' + '\'' + propname + '\'' + ')"><center><img src="../../housepix/featured/' + propid + '_google.jpg" alt = "Click to view more info!" width="260px" height="184px"\/><\/a><br \/><center><a href="javascript:winClose(' + '\'' + propname + '\'' + ')"><span style="font-size:small">Book Now!</span><\/a><\/center>';
                        }
                    }

                }

                for (var c = 0; c < propidArr.length; ++c) {
                    if (propidArr[c].substring(0, propidArr[c].indexOf('>')) == propid) {
                        bounds.extend(point);
                        var checkday = propidArr[c].substring(propidArr[c].indexOf(' ') + 1);
                        var markertitle = (propidArr[c].substring(0, propidArr[c].indexOf('>')) + ' ' + (propidArr[c].substring(propidArr[c].indexOf(' ') + 1)));
                        if (checkday.substring(0, 3) == 'Sat') {
                            var marker = createMarker(point, html, saticon, markertitle);
                        }
                        else if (checkday.substring(0, 3) == 'Sun') {
                            var marker = createMarker(point, html, sunicon, markertitle);
                        }
                        else {
                            var marker = createMarker(point, html, notrentedicon, markertitle);
                        }
                        gmarkersA.push(marker);
                    }
                }

                if (tester == 1 && area == 'kitty hawk') {
                    bounds.extend(point);
                    var marker = createMarker(point, html, cdricon, propid);
                    gmarkersA.push(marker);
                }
                else if (tester == 2 && area == 'duck') {
                    bounds.extend(point);
                    var marker = createMarker(point, html, cdricon, propid);
                    gmarkersA.push(marker);

                }
            }
            map.setCenter(bounds.getCenter());
            map.setZoom(map.getBoundsZoomLevel(bounds));
            map.addControl(new GSmallMapControl());
            map.addControl(new TextualCenterControl());
            map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(5, 15)));
            cPoint = bounds.getCenter();
            zoomLevel = map.getBoundsZoomLevel(bounds);
            //            svOverlay = new GStreetviewOverlay();
            //            map.addOverlay(svOverlay);
            //instantiate MarkerManager
            mm = new MarkerManager(map);
            mm.addMarkers(gmarkersA, 0);
            mm.refresh();

        });
    }
    
    
    function newMarkers(passtext, firstLoad, startdt) {

        /// <summary>
        /// Clears marker array and refills with new markers based on checkin date.
        /// </summary>
        /// <param name="passtext" type="String">
        /// The string used to build array. Split on "|"
        /// </param>

        if (firstLoad == 0) {
            mm.clearMarkers();
        }
        propidArr.length = 0;
        gmarkersA.length = 0;
        propidArr = passtext.split("|");
        readMap(startdt);
        
    }

    function singmap(singlat, singlng, singpropid) {
        
        var point = new GLatLng(singlat, singlng);
        var marker = new GMarker(point, { title: singpropid });
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(singlat, singlng), 15);
        map.setUIToDefault();
        map.addOverlay(marker);

    }

    function mapCenter() {
        map.closeInfoWindow();
        map.setCenter(cPoint, zoomLevel);
    }

    function handleNoFlash(errorCode) {
        if (errorCode == FLASH_UNAVAILABLE) {
            alert("Error: Flash doesn't appear to be supported by your browser or is not installed.");
            return;
        }
    }  
