﻿function ListingMarker(lat, lng) {
    var me = this;
    this.lat = lat;
    this.lng = lng;
    this.listings = new Hash();
    this.gmarker = null;
    this.openHouse = false;
    this.gStreetViewData = null;  
    
    // get marker street view
    this.getStreetViewData = function() {
        var sv = new GStreetviewClient();
        sv.getNearestPanorama(new GLatLng(this.lat, this.lng), function(panoData) {
            if (panoData.code != 200) {
                me.gStreetViewData = null;
                return;
            }
            me.gStreetViewData = panoData;
        });
    };
    
    // display marker street view
    this.showStreetView = function() {
        var svpano = new GStreetviewPanorama(document.getElementById('streetView'));
        svpano.setLocationAndPOV(this.gStreetViewData.location.latlng, this.gStreetViewData.location.pov);
        $.fn.colorbox({ inline: true, href: "#streetView" });
        $().bind('cbox_closed', function() {
            $("#streetView").html("");
			MoveToMapOnPage();
        });     
    }

    // build target mouse over html
    this.getTargetMouseOverHtml = function() {
        var mouseHtml = "<div class=\"tooltip\">";
        if (this.listings.length == 0) {
            // no similar listings - use target address
            mouseHtml += this.targetListing.streetAddress + "<br />";
        }
        else if (this.listings.length == 1) {
            // only one similar listing - use address of listing
            for (var i in this.listings.items) {
                var listing = this.listings.getItem(i);
                mouseHtml += listing.streetAddress + "<br />";
            }
        }
        else {
            // multiple similar listings
            mouseHtml += this.listings.length + " properties...";
        }
        mouseHtml += "</div>";
        return mouseHtml;
    }

    // build target marker info window html
    this.getTargetInfoWindowHtml = function() {
        var infoHtml = "";
        if (this.listings.length == 0) {
            // featured listing is only listing at this geolocation
            var listingUrl = "http://www.sibcycline.com/viewlisting.asp?mls=" + this.targetListing.mlsNumber +
                "&b=" + this.targetListing.mlsBoardId +
                "&p=" + this.targetListing.propertyType +
                "&s=" + this.targetListing.propertySubType +
                "&m=" + this.targetListing.mlsBoard +
                "&sender=MapTab&a=" + this.targetListing.streetAddress.replace(" ", "-") + "-" + this.targetListing.city.replace(" ", "-") + "-" + this.targetListing.state.replace(" ", "-") + "-" + this.targetListing.zip.replace(" ", "-");
            var imgUrl = "http://www.sibcycline.com/imgStamp.ashx?mlsnumber=" + this.targetListing.mlsNumber +
                "&board=" + this.targetListing.mlsBoard +
                "&p=1&s=" + this.targetListing.sibcyYN;
            infoHtml += "<div class=\"mapInfoWindow\">";
            infoHtml += "<div class=\"mapInfoWindowSingle\">";
            infoHtml += this.targetListing.streetAddress + "<br />";
            infoHtml += this.targetListing.city + ", " + this.targetListing.state + " " + this.targetListing.zip + "<br />";
            infoHtml += this.targetListing.displayPropertyType + "<br />";
            infoHtml += "Price: $" + MappingUtils.commaFormat(this.targetListing.price) + "<br />";
            if (this.targetListing.bedrooms != "") {
                infoHtml += "Beds: " + this.targetListing.bedrooms + "<br />";
            }
            if (this.targetListing.bathrooms != "") {
                infoHtml += "Baths: " + this.targetListing.fullBaths + "." + this.targetListing.partBaths + "<br />";
            }
            if (this.gStreetViewData != null) {
                infoHtml += "<a href='javascript:void(0);' class='streetViewBox' onclick='lmap.targetMarker.showStreetView(lsv.sv);'>Street View</a><br />";
            }
            infoHtml += "<a href='" + listingUrl + "'><img src='" + imgUrl + "' alt='Property Listing' /></a>";
            infoHtml += "</div>";
            infoHtml += "</div>";
        }
        else {
            // multiple listings at featured listing geolocation
            //infoHtml = generateListingInfoWindowHtml(targetMarker);
            infoHtml = this.getInfoWindowHtml();
        }
        return infoHtml;
    }      

    // build marker mouse over html
    this.getMouseOverHtml = function() {
        var mouseHtml = "<div class=\"tooltip\">"
        if (this.listings.length == 1) {
            // only one listing
            for (var i in this.listings.items) {
                var listing = this.listings.getItem(i);
                mouseHtml += listing.streetAddress + "<br />";
            }
        }
        else if (this.listings.length > 1) {
            // multiple listings
            mouseHtml += this.listings.length + " properties..."
        }
        mouseHtml += "</div>";
        return mouseHtml;
    };    
    
    // build marker info window html
    this.getInfoWindowHtml = function() {
        var infoHtml = "";
        if (this.listings.length == 0) {
            // no listings on this marker - return empty string
            return infoHtml;
        }
        if (this.listings.length == 1) {
            // only one listing - generate full listing html
            for (var i in this.listings.items) {
                var listing = this.listings.getItem(i);
                var listingUrl = "http://www.sibcycline.com/viewlisting.asp?mls=" + listing.mlsNumber +
                    "&b=" + listing.mlsBoardId +
                    "&p=" + listing.propertyType +
                    "&s=" + listing.propertySubType +
                    "&m=" + listing.mlsBoard +
                    "&sender=MapTab&a=" + listing.streetAddress.replace(" ", "-") + "-" + listing.city.replace(" ", "-") + "-" + listing.state.replace(" ", "-") + "-" + listing.zip.replace(" ", "-");
                var imgUrl = "http://www.sibcycline.com/imgStamp.ashx?mlsnumber=" + listing.mlsNumber +
                    "&board=" + listing.mlsBoard +
                    "&p=1&s=" + listing.sibcyYN;
                infoHtml += "<div class=\"mapInfoWindow\">";
                infoHtml += "<div class=\"mapInfoWindowSingle\">";
                infoHtml += listing.streetAddress + "<br />"; 
                infoHtml += listing.city + ", " + listing.state + " " + listing.zip + "<br />";
                infoHtml += listing.displayPropertyType + "<br />";
                infoHtml += "Price: $" + MappingUtils.commaFormat(listing.price) + "<br />";
                if (listing.bedrooms != "") {
                    infoHtml += "Beds: " + listing.bedrooms + "<br />";
                }
                if (listing.bathrooms != "") {
                    infoHtml += "Baths: " + listing.fullBaths + "." + listing.partBaths + "<br />";
                }
                if (this.gStreetViewData != null) {
                    infoHtml += "<a href='" + listingUrl + "'>More Details </a>-<a href='javascript:void(0);' class='streetViewBox' onclick='lmap.listingMarkersHash.getItem(\"" + (this.lat.toString() + "|" + this.lng.toString()) + "\").showStreetView(lsv.sv);'> Street View</a><br />";
                }
                else {
                    infoHtml += "<a href=\"" + listingUrl + "\">More Details </a><br />";
                }
                if (listing.openHouse) {
                    infoHtml += "<span style=\"color:#b22222; font-weight:bold;\">Upcoming open house!</span>";
                }
                if (listing.displayPhoto) {
                    infoHtml += "<a href=\"" + listingUrl + "\"><img src=\"" + imgUrl + "\" alt=\"Property Listing\" /></a>";
                }
                infoHtml += "</div>";
                infoHtml += "</div>";
            }
        }
        else if (this.listings.length > 1 && this.listings.length < 4) {
            // 2-3 listings - generate abbreviated html
            infoHtml += "<div class=\"mapInfoWindow\">";
            for (var i in this.listings.items) {
                var listing = this.listings.getItem(i);
                var listingUrl = "http://www.sibcycline.com/viewlisting.asp?mls=" + listing.mlsNumber +
                        "&b=" + listing.mlsBoardId +
                        "&p=" + listing.propertyType +
                        "&s=" + listing.propertySubType +
                        "&m=" + listing.mlsBoard +
                        "&sender=MapTab&a=" + listing.streetAddress.replace(" ", "-") + "-" + listing.city.replace(" ", "-") + "-" + listing.state.replace(" ", "-") + "-" + listing.zip.replace(" ", "-");
                var imgUrl = "http://www.sibcycline.com/imgStamp.ashx?mlsnumber=" + listing.mlsNumber +
                        "&board=" + listing.mlsBoard +
                        "&p=1&s=" + listing.sibcyYN;
                infoHtml += "<div class=\"mapInfoWindowMultiple\">";
                infoHtml += "<div class=\"mapInfoWindowLeft\">";
                infoHtml += listing.streetAddress + "<br />"; 
                infoHtml += listing.city + ", " + listing.state + " " + listing.zip + "<br />";
                infoHtml += listing.displayPropertyType + "<br />";
                infoHtml += "Price: $" + MappingUtils.commaFormat(listing.price) + "<br />";
                if (this.gStreetViewData != null) {
                    infoHtml += "<a href='" + listingUrl + "'>More Details </a>-<a href='javascript:void(0);' class='streetViewBox' onclick='lmap.listingMarkersHash.getItem(\"" + (this.lat.toString() + "|" + this.lng.toString()) + "\").showStreetView(lsv.sv);'> Street View</a><br />";
                }
                else {
                    infoHtml += "<a href=\"" + listingUrl + "\">More Details </a><br />";
                }
                if (listing.openHouse) {
                    infoHtml += "<span style=\"color:#b22222; font-weight:bold;\">Upcoming open house!</span>";
                }
                infoHtml += "</div><div class=\"mapInfoWindowRight\">";
                if (listing.displayPhoto) {
                    infoHtml += "<a href=\"" + listingUrl + "\"><img src=\"" + imgUrl + "\" alt=\"Property Listing\" /></a>";
                }
                infoHtml += "</div>";
                infoHtml += "</div>";
                infoHtml += "<div class=\"clear\"></div>";
            }
            infoHtml += "</div>";
        }
        else {
            // 4 or more listings - generate summary html
            infoHtml += "<div class=\"mapInfoWindow\" style=\"height:200px; width:200px; overflow:auto;\">";
            for (var i in this.listings.items) {
                var listing = this.listings.getItem(i);
                var listingUrl = "http://www.sibcycline.com/viewlisting.asp?mls=" + listing.mlsNumber +
                        "&b=" + listing.mlsBoardId +
                        "&p=" + listing.propertyType +
                        "&s=" + listing.propertySubType +
                        "&m=" + listing.mlsBoard +
                        "&sender=MapTab&a=" + listing.streetAddress.replace(" ", "-") + "-" + listing.city.replace(" ", "-") + "-" + listing.state.replace(" ", "-") + "-" + listing.zip.replace(" ", "-");
                infoHtml += "<div class=\"mapInfoWindowMultiple\">";
                infoHtml += listing.streetAddress + "<br />";        
                infoHtml += listing.city + ", " + listing.state + " " + listing.zip + "<br />";
                infoHtml += listing.displayPropertyType + "<br />";
                infoHtml += "Price: $" + MappingUtils.commaFormat(listing.price) + "<br />";
                if (this.gStreetViewData != null) {
                    infoHtml += "<a href='" + listingUrl + "'>More Details </a>-<a href='javascript:void(0);' class='streetViewBox' onclick='lmap.listingMarkersHash.getItem(\"" + (this.lat.toString() + "|" + this.lng.toString()) + "\").showStreetView(lsv.sv);'> Street View</a><br />";
                }
                else {
                    infoHtml += "<a href=\"" + listingUrl + "\">More Details </a><br />";
                }
                if (listing.openHouse) {
                    infoHtml += "<span style=\"color:#b22222; font-weight:bold;\">Upcoming open house!</span>";
                }
                infoHtml += "</div>";

            }
            infoHtml += "</div>";
        }
        return infoHtml;
    }
}
