﻿var Pages = {};
var Controls = {};

Pages.Default = {
    Init: function() {
        $(".properties-by-area-by-neighborhood-right ul.neighborhoods li h2 a").click(function() {
            $(".properties-by-area-by-neighborhood-right ul.properties").hide();
            $(".properties-by-area-by-neighborhood-right h2.opened").removeClass('opened').addClass('closed');
            $(this).parent().removeClass('closed').addClass('opened');
            $(this).parent().next().show();

            var home_anchor = $(".communities-by-area-by-neighborhood-right h2.opened a").attr('title');
            document.location.hash = '#condo-' + $(this).attr('title') + '&home-' + home_anchor;
        });

        $(".communities-by-area-by-neighborhood-right ul.neighborhoods li h2 a").click(function() {
            $(".communities-by-area-by-neighborhood-right ul.communities").hide();
            $(".communities-by-area-by-neighborhood-right h2.opened").removeClass('opened').addClass('closed');
            $(this).parent().removeClass('closed').addClass('opened');
            $(this).parent().next().show();

            var condo_anchor = $(".properties-by-area-by-neighborhood-right h2.opened a").attr('title');
            document.location.hash = '#condo-' + condo_anchor + '&home-' + $(this).attr('title');
        });

        /* Search By Area Panel */
        $(".search-by-area select").change(function() {
            $(".search-by-area option:selected").each(function() {
                var val = $(this).val();
                if (val.length > 0) {
                    window.location = "/forsale/florida/" + val + ".aspx";
                }
            });
        });

        //show selected submenu for "Miami Condos" and "Homes for Sale" blocks using anchors
        var location = document.location.hash;
        var current_condo = location.match(/condo-([^&]*)&/);
        if (current_condo) {
            $(".properties-by-area-by-neighborhood-right ul.properties").hide();
            $(".properties-by-area-by-neighborhood-right h2.opened").removeClass('opened').addClass('closed');
            var link = $('.properties-by-area-by-neighborhood-right a[title=' + current_condo[1] + ']');
            $(link).parent().removeClass('closed').addClass('opened');
            $(link).parent().next().show();
        }

        var current_home = location.match(/&home-(.*)$/)
        if (current_home) {
            $(".communities-by-area-by-neighborhood-right ul.communities").hide();
            $(".communities-by-area-by-neighborhood-right h2.opened").removeClass('opened').addClass('closed');
            var link = $('.communities-by-area-by-neighborhood-right a[title=' + current_home[1] + ']');
            $(link).parent().removeClass('closed').addClass('opened');
            $(link).parent().next().show();
        }
        
    },
    PropertyNameSelected: function(source, eventArgs) {
        var val = eventArgs.get_value();
        if (val.length > 0) {
            window.location = "/forsale/florida/" + val + ".aspx";
        }
    }
}

Controls.PhotoView = {
    Init: function() {
        $('#photos').galleryView({
            panel_width: 512,
            panel_height: 400,
            frame_width: 100,
            frame_height: 100,
            overlay_height: 20,
            background_color: '#666666',
            nav_theme: 'light',
            transition_speed: 200,
            transition_interval: 40000
        });

        $("a.fancybox").fancybox();        
    }
}

Controls.SpecialsSwitcher = {
    Init: function(checkbox1, checkbox2) {
        $('#' + checkbox1).click(function() { $('#' + checkbox2).attr('checked', false); });
        $('#' + checkbox2).click(function() { $('#' + checkbox1).attr('checked', false); });
    }
}

Controls.PropertyInformation = {
    NameControlID: '',
    SeoUrlControlID: '',
    Init: function(propertyName, seoUrl) {
        Controls.PropertyInformation.NameControlID = propertyName;
        Controls.PropertyInformation.SeoUrlControlID = seoUrl;

        $('#' + Controls.PropertyInformation.NameControlID).change(Controls.PropertyInformation.NameChanged);
    },
    NameChanged: function() {
        $('#' + Controls.PropertyInformation.SeoUrlControlID).val($('#' + Controls.PropertyInformation.NameControlID).val().replace(/\s/g,'-'));
    }
}

Controls.CoordinatesEditor = {
    Lng: '',
    Lat: '',
    Init: function(lat, lng) {
        Controls.CoordinatesEditor.Lat = lat;
        Controls.CoordinatesEditor.Lng = lng;

        var latValue = document.getElementById(lat).value;
        var lngValue = document.getElementById(lng).value;

        var latLng = new google.maps.LatLng(25.759869903526265, -80.13993697738647);
        
        if (latValue != '' && lngValue != '') {
            latLng = new google.maps.LatLng(latValue, lngValue);
        }
        
        var map = new google.maps.Map(document.getElementById('mapCanvas'), {
            zoom: 11,
            center: latLng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var marker = new google.maps.Marker({
            position: latLng,
            title: 'Property',
            map: map,
            draggable: true
        });

        google.maps.event.addListener(marker, 'dragend', function() {
            var latLng = marker.getPosition();
            document.getElementById(Controls.CoordinatesEditor.Lat).value = latLng.lat();
            document.getElementById(Controls.CoordinatesEditor.Lng).value = latLng.lng();
        });
    }
}
