﻿// Functions supporting the listing search pages.

$(document).ready(function() {
    var fmlsCount = $("img[src='Site/Images/FMLS.jpg']").length;
    if (fmlsCount = 0) {
        $('#fmlsdisc').hide();
    }
});

var lastStandardMapCenter;
var currentMarker;

function collapseAllOtherListingDetails(divToExclude){
    var listings = document.getElementById('Listings');
    var divInListings = listings.getElementsByTagName('DIV');
    
    for( var x = 0; x < divInListings.length; x++ ) {
        if ((divInListings[x].id.search(/ListingDetails/) > -1) && (divInListings[x].id != divToExclude.id)){ 
            divInListings[x].style.display = 'none';
            divInListings[x].parentNode.className = 'listing';
        }
    }
}

function toggleDetails(rowID, index, mapIndex, moveMap, scrollList) {

    //attempt to load streetview
    currentMarker = mapIndex-1;
    returnMapToStandardView();
    myPano.setLocationAndPOV(markers_gmap[mapIndex - 1].getPoint());

    //gather all div within active listing
    var thisListing = document.getElementById(rowID);
    var resultCount = $('.listing').length;
    resultCount += $('.listingSelected').length;
    var divsInListing = thisListing.parentNode.getElementsByTagName('Div');
    for( var x = 0; x <divsInListing.length; x++ ) {
        //find details listing div tag
        if (divsInListing[x].id.search(/ListingDetails/) > -1){
            //toggle display attribute
            if (divsInListing[x].style.display == 'block'){
                divsInListing[x].style.display = 'none';
            }
            else {
                if (resultCount == 8) {
                    $('#gmap').height(649);    //Added by Eddie 9-20-11
                }
                divsInListing[x].style.display = 'block';
                thisListing.parentNode.className = 'listingSelected';
                collapseAllOtherListingDetails(divsInListing[x]);

                if (moveMap == true) {                
                    // Center small map and add InfoWindow Toggle effect.
                    mOver(mapIndex - 1, 'gmap');
                    //eval('map_smallMap' + index).checkResize();
                    //setCenterAndZoom(eval('bounds_smallMap' + index), eval('map_smallMap' + index), 15);
                }
                
                if (scrollList == true) {
                    // Scroll results list to item
                    var list = document.getElementById('Listings');
                    list.scrollTop = ((index - 1) * 65);
                }
            }
        }        
    }
    //map_mapSmall2.checkResize();
} 



//var myPano = new GStreetviewPanorama(document.getElementById("pano"));
//GEvent.addListener(myPano, "error", handleNoFlash);

function initialize() {
    if (GBrowserIsCompatible()) {
        //var myPano = new GStreetviewPanorama(document.getElementById("pano"));
        //GEvent.addListener(myPano, "error", handleNoFlash);
        ////var map = new GMap2(document.getElementById("map_gmap"));
        ////map_gmap.setCenter(new GLatLng(42.345573, -71.098326), 14);
        
        svOverlay = new GStreetviewOverlay();
        map_gmap.addOverlay(svOverlay);
        GEvent.addListener(map_gmap, "click", function(overlay, latlng) {
            myPano.setLocationAndPOV(latlng);
        });
    }
}

function panoView(lat, lng) {
    
    
    
    map_gmap.getInfoWindow().hide();
    $('#pano').show();
    $('#gmap').height(275);
    $('#Listings').css('visibility', 'hidden');
    //$('#Listings').css('overflow-y', 'hidden');
    
    lastFullMapCenter = map_gmap.getCenter();
    map_gmap.checkResize();
    map_gmap.setCenter(new GLatLng(lat, lng));
    //map_gmap.setZoom(14);
    //map_gmap.getInfoWindow().show();  //below don't work in IE
    markers_gmap[currentMarker].openInfoWindowHtml(markerWindows_gmap[currentMarker]);

    //build new listing box
    if (!$('#tempListing').length > 0) {
        $('<div class="listings">')
        .attr('id', 'tempListing')
        .css({
            'position': 'absolute',
            'z-index': '2500',
            'width': '475px',
            'height': '246px',
            'top': '0px',
            'left': '0px',
            'opacity': '1',
            'background-color': 'white',
            'overflow':'hidden',//ie7 error fix
            'margin':'0px',
            'background':'url(/site/images/bg-listingClone.gif) repeat-x 0 0'
        })
        .html(
            $('#Listings > div:eq(' + currentMarker + ')')
            .clone()
            .css({ 'background': 'none', 'border': 'none', 'margin':'0px' })
        )
        .appendTo('.listingPageContainer');
        $('#streetView').clone(true).show().appendTo('#tempListing');
    }

    if ($('#gmap').width() == 940) {
        $('#tempListing').hide();
    }
    
}

function recenterMap() {
    lastStandardMapCenter = map_gmap.getCenter();
    map_gmap.checkResize();
    map_gmap.setCenter(lastStandardMapCenter);
}


function handleNoFlash(errorCode) {
    if (errorCode) { // == FLASH_UNAVAILABLE) {        
        if (errorCode == 600) {
            //alert('No street view available');
        }
        setTimeout("removeStreetViewLink()", 200);
        $('#pano').hide();
        //alert("Error: Flash doesn't appear to be supported by your browser");
        return;
    }    
}


function removeStreetViewLink() {
    //alert($('#listingMapInfoWindowStreetView').length);
    $('#listingMapInfoWindowStreetView').hide();
}

function returnMapToStandardView() {

    //remove temp listing
    $('#tempListing').empty().remove();
    
    $('#pano').hide();
    $('#gmap').height(528);
    $('#tempListing').empty().remove();
    $('#Listings').css('visibility', 'visible');
    $('#pano').hide();
    //map_gmap.setZoom(11);
    //$('#Listings').height(528);
    //$('#Listings').css('overflow-y', 'scroll');
    recenterMap();
}
