
/* Refine Bar, Property Type, */
$(document).ready(function () {
    $('input.idx-search-type, select.idx-search-type').bind('click', function () {
        
        /* Selected Type */
        type = $(this).val();

        /* Random Process ID */
        pid = Math.random() * 5;
        
        /* Price Ranges */
        if (type in {'Rental' : true, 'Lease' : true, 'Residential Lease' : true, 'Commercial Lease' : true}) {
            
            /* Show Rental Prices */
            $('#search_rent').show().find('select').removeAttr('disabled');
            
            /* Hide Price Ranges */
            $('#search_price').hide().find('select').attr('disabled', 'disabled');
            
        } else {
         
            /* Show Price Ranges */
            $('#search_price').show().find('select').removeAttr('disabled');
            
            /* Hide Rental Prices */
            $('#search_rent').hide().find('select').attr('disabled', 'disabled');
            
        }
        
        /* Sub-Type Selection */
        if ($('select[name="search_subtype"]').length > 0) {
            
            $.ajax({
                'url'      : '/idx/inc/php/ajax/json.php?searchTypes',
                'type'     : 'POST',
                'dataType' : 'json',
                'data'     : {
                    'pid' : pid,
                    'search_type' : type
                },
                'error'    : function (XMLHttpRequest, textStatus, errorThrown) {
                    $('input[name="search_subtype"]').hide();
                },
                'success'  : function (json, textStatus) {
                    if (typeof(json) == 'undefined') return;
                    if (typeof(json.pid) != 'undefined') {
                        if (json.pid != pid) {
                            return;
                        }
                    }
                    if (json.returnCode == 200) {
                        var html = '<select name="search_subtype">' + "\n";
                        
                        if (type == '') {
                            html += '<option value="">All Properties</option>' + "\n";
                        } else {
                            html += '<option value="">All ' + type + '</option>' + "\n";
                        }
    
                        if (json.options.length > 0) {
                            var i = 0, len = json.options.length;
                            var subtype = $('select[name="search_subtype"]').val();
                            while (i < len) {
                                var checked = (subtype == json.options[i].value) ? ' selected="selected"' : '';
                                html += '<option value= "' + json.options[i].value + '"' + checked + '>' + json.options[i].title + '</option>' + "\n";
                                i++;
                            }
                        }
    
    
                        html += '</select>';
                        var parent = $('select[name="search_subtype"]').parent();
                        $('select[name="search_subtype"]').remove();
                        parent.append(html);
    
                    }
                }
            });
            
        }
        
    });
    $('input.idx-search-type:checked, select.idx-search-type').trigger('click');
});

/* Enable Location Fields */
function enableLocations () {

    /* Return if Viewport Checked */
    if ($('input[name="map[viewport]"]').attr('checked')) return disableLocations();

    /* Return if PolygonSearch Exists */
    if (polygonControl != null || (polygonControl && polygonControl.polygonSearch)) return disableLocations();

    /* Return if RadiusSearch Exists */
    if (radiusControl && radiusControl._radiusSearches && radiusControl._radiusSearches.length != 0) return disableLocations();

    var $form = $('#idx-map-form');
    $form.find('input.location').removeAttr('disabled');
    $form.find('select.location').removeAttr('disabled');
    
    var $form = $('#searchForm');
    $form.find('input.location').removeAttr('disabled');
    $form.find('select.location').removeAttr('disabled');

};

/* Disable Location Fields */
function disableLocations () {

    var $form = $('#idx-map-form');
    $form.find('input.location').attr('disabled', 'disabled');
    $form.find('select.location').attr('disabled', 'disabled');
    
    var $form = $('#searchForm');
    $form.find('input.location').attr('disabled', 'disabled');
    $form.find('select.location').attr('disabled', 'disabled');

};/* hide while loading */
$('#feature .panes').css({'position':'absolute', 'left':'-999em'});

/* Onload */
$(document).ready(function () {

    var feature = 'test';
    $("ul.tabs").tabs("div.panes > div",{
        'tabs'    : 'li',
        'effect'  : 'fade',
        'onClick' : function() {
            $('#feature .photo img:visible').not('.fixed').each(function(){
                $(this).css({width:'auto', height:'auto'}).rewImgSizer({method:'crop'}).addClass('fixed');
            });
        }
    });
    $("#pane-1").load('/idx/inc/php/ajax/featured-listings.php?limit=5', function(){
        feature = $("#pane-1").scrollable({
            'clickable' : false,
            'size'      : 1,
            'speed'     : 900,
            'loop'      : true,
            'easing'    : 'swing',
            'onBeforeSeek' : function() {
                if ($('#feature #pane-1').is(':visible')) {
                    $('#feature #pane-1').fadeOut(450).fadeIn(450);
                }
            }
        }).autoscroll({'interval' : 15000, 'api' : true });
        $('#feature .panes').css({'position':'relative', 'left':'0'});
        Cufon.refresh();
    });

});
