jQuery(document).ready(function(){

    jQuery("a#inline").fancybox();


    // If the user submits the form via the submit button, set the page back to one
    jQuery("#filterbutton").click(function(e){
        e.preventDefault();
        jQuery('#filterpage').val(1);
        jQuery('#filterForm').submit();
    });

    jQuery(".numbersOnly").keyup(function () {
        this.value = this.value.replace(/[^0-9\$,]/g,'');
    });

    jQuery(".listingPager").click(function(e){
       e.preventDefault();
       jQuery('#filterpage').val(jQuery(this).attr("rel"));
       jQuery('#filterForm').submit();
    });

    jQuery('.maplistimage').parent().click(function(e){
        jQuery(this).css('cursor','pointer');
        window.location = jQuery(this).attr("rel");
    });

    jQuery('#subscribevipemail').keyup(function(){
        
        var url = $('#subscribevip').attr('rel') + "?email=" + jQuery("#subscribevipemail").val();
        jQuery("#subscribevip").attr('href',url);

    });

    jQuery("#viewFavesBtn").click(function(e){
        e.preventDefault();
        showFavoritesList();
    });

    jQuery(".addToFavorites").live('click',function(e){
            e.preventDefault();

            var postData = {
                action: 're_add_to_faves',
                post_id: jQuery(this).attr('rel')
            }
            
            jQuery.post(ajax_url, postData, function(response){
                if (response){
                    display_response = response.substring(0, response.length - 1);
                    jQuery('#favescontainer').html(display_response);
                    showFavoritesList();
                    setTimeout(showFavoritesList,4000);
                }

            });

       jQuery(this).html('<span>Added to Favorites!</span>');

    });

    jQuery(".favoriteListing").live('mouseover mouseout', function(e){
        if (e.type == 'mouseover') {
            jQuery(this).children('.deleteFavorite').show();
        } else {
            jQuery(this).children('.deleteFavorite').hide();
        }
    });


    jQuery(".deleteFavorite").live('click', function(e){

       e.preventDefault();
       jQuery(this).parents('.favoriteListing').fadeOut();
       var postData = {
           action: 're_remove_from_faves',
           post_id: jQuery(this).attr('rel')
       }

        jQuery.post(ajax_url, postData, function(response){
            if( response.indexOf( "empty" ) !== -1 ){
                jQuery('#favescontainer').html('<h2>Click on "Add To Favorites" on any property</h2>');
            }
        });

    });

    

});

function showFavoritesList()
{

        var currentMargin = parseInt(jQuery("#favoritesbox").css('marginRight'),10)

        if (currentMargin == -154){
            jQuery("#favesArrow").attr('src','/graphics/favesarrow-close.gif');
        } else {
            jQuery("#favesArrow").attr('src','/graphics/favesarrow.gif');
        }

        jQuery("#favoritesbox").animate({
            marginRight: currentMargin == -154 ?
                0 :
                -154
        })

}

var markers = [];
var overlays = [];
var markerBounds = new google.maps.LatLngBounds();
if (jQuery('#mapgoog').length > 0){
            var latlng = new google.maps.LatLng(26.561814,-81.953545);
            var reOptions = {
                zoom: 13,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }

            var map = new google.maps.Map(document.getElementById('mapgoog'), reOptions);

            var i = 0;

            inactiveIcon = '/graphics/mapicon.png';

            activeIcon = '/graphics/mapicon-over.png';

            jQuery('.maplistimage').each(function(){
                lat = jQuery(this).children('.latitude').val();
                lng = jQuery(this).children('.longitude').val();
                jQuery(this).children('.mapPointer').val(i);

                //alert('lat= ' + lat +' lng= ' +lng);
                if (lat.length>0 && lng.length>0){
                    var point = new google.maps.LatLng(lat, lng);
                    marker = new google.maps.Marker({
                        position: point,
                        map: map,
                        icon: inactiveIcon
                    });

                    var innerHTML = jQuery(this).parent().children('.mapbubble-wrap').html();

                    var myOptions = {
                             content: innerHTML
                            ,disableAutoPan: false
                            ,maxWidth: 0
                            ,pixelOffset: new google.maps.Size(16, -100)
                            ,zIndex: null
                            ,boxStyle: {width: "280px"}
                            ,closeBoxMargin: "10px 10px 2px 2px"
                            ,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
                            ,infoBoxClearance: new google.maps.Size(1, 1)
                            ,isHidden: false
                            ,pane: "floatPane"
                            ,enableEventPropagation: false
                    };
                    var ib = new InfoBox(myOptions);

                    attachListeners(marker, ib);
                    markerBounds.extend(point);
                    markers[i] = marker;
                    overlays[i] = ib;
                    i++;

                    jQuery(this).parent().bind({
                      mouseover: function() {
                        map.panTo(markers[jQuery(this).children('.maplistimage').children('.mapPointer').val()].getPosition());
                        markers[jQuery(this).children('.maplistimage').children('.mapPointer').val()].setIcon(null);
                        markers[jQuery(this).children('.maplistimage').children('.mapPointer').val()].setIcon(activeIcon);
                        //overlays[jQuery(this).children('.maplistimage').children('.mapPointer').val()].open(map, markers[jQuery(this).children('.maplistimage').children('.mapPointer').val()]);
                      },
                      mouseout: function() {
                        markers[jQuery(this).children('.maplistimage').children('.mapPointer').val()].setIcon(null);
                        markers[jQuery(this).children('.maplistimage').children('.mapPointer').val()].setIcon(inactiveIcon);
                        //overlays[jQuery(this).children('.maplistimage').children('.mapPointer').val()].close(map, markers[jQuery(this).children('.maplistimage').children('.mapPointer').val()]);
                      }
                    });

                }



            });



            map.fitBounds(markerBounds);



    }

    var UserActiveIcon = false;
    function attachListeners(marker, overlay)
    {
          
          google.maps.event.addListener(marker, 'mouseover', function() {
            for (i in markers)
            {
                markers[i].setIcon(inactiveIcon);
            }
            marker.setIcon(null);
            marker.setIcon(activeIcon);
            //overlay.open(map, marker);

          });

            var listenerHandle = google.maps.event.addListener(marker, 'mouseout', function() {
                marker.setIcon(null);
                //overlay.close(map, marker);
                marker.setIcon(inactiveIcon);
            });

          google.maps.event.addListener(marker, 'click', function() {
            for (i in overlays)
            {
                overlays[i].close(map, markers[i]);
            }
            UserActiveIcon = marker;
            marker.setIcon(null);
            marker.setIcon(activeIcon);
            overlay.open(map, marker);
            map.panTo(marker.getPosition());

            google.maps.event.removeListener(listenerHandle);
          });

    }
