
/*
 * REWMOD ant[2010-10-19 11:22:54]
 */
/* 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,
                    'idx' : $('input[name="idx"]').val(),
                    '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');
    
    
    /**
     * REWMOD 27-05-2011 - Dynamic City List Based On Counties
     */
    $('select[name="search_county"]').bind('change', function () {
    	
    	/* Selected Type */
        type = $(this).val();

        /* Random Process ID */
        pid = Math.random() * 5;
    	
    	$.ajax({
            'url'      : '/idx/inc/php/ajax/json.php?searchCounty',
            'type'     : 'POST',
            'dataType' : 'json',
            'data'     : {
                'pid' : pid,
                'idx' : $('input[name="idx"]').val(),
                'search_county' : type
            },
	        'success'  : function (json, textStatus) {
	            if (typeof(json) == 'undefined') return;
	            if (typeof(json.pid) != 'undefined') {
	                if (json.pid != pid) {
	                    return;
	                }
	            }
	            if (json.returnCode == 200) {
	            	
	            	//Cities
	                var html = '<select size="4" multiple="multiple" name="search_city[]" class="location">' + "\n";
	                
	                html += '<option value="">Select a City</option>' + "\n";
	
	                if (json.city_options.length > 0) {
	                    var i = 0, len = json.city_options.length;
	                    var city_vals = $('select[name="search_city[]"]').value;
	                    
	                    if(city_vals == null){
	                    	city_vals = "";
	                    }

	                    while (i < len) {
	                    	var city_opts = json.city_options[i].value;
	                        var checked = (city_vals.indexOf(city_opts) > -1) ? ' selected="selected"' : '';
	                        html += '<option value= "' + json.city_options[i].value + '"' + checked + '>' + json.city_options[i].title + '</option>' + "\n";
	                        i++;
	                    }
	                }
	
	
	                html += '</select>';
	                var parent = $('select[name="search_city[]"]').parent();
	                $('select[name="search_city[]"]').remove();
	                parent.append(html);
	                
	                //Elementary School
	                html = '<select name="school_elementary">' + "\n";
	                
	                html += '<option value="">Any Elementary Schools</option>' + "\n";
	
	                if (json.elementary_school_options.length > 0) {
	                    i = 0, len = json.elementary_school_options.length;
	                    var elem_vals = $('select[name="school_elementary"]').val();
	                    
	                    if(elem_vals == null){
	                    	elem_vals = "";
	                    }

	                    while (i < len) {
	                        checked = (elem_vals == json.elementary_school_options[i].value) ? ' selected="selected"' : '';
	                        html += '<option value= "' + json.elementary_school_options[i].value + '"' + checked + '>' + json.elementary_school_options[i].title + '</option>' + "\n";
	                        i++;
	                    }
	                }
	
	
	                html += '</select>';
	                parent = $('select[name="school_elementary"]').parent();
	                $('select[name="school_elementary"]').remove();
	                parent.append(html);
	                
	                //Middle School
	                html = '<select name="school_middle">' + "\n";
	                
	                html += '<option value="">Any Middle Schools</option>' + "\n";
	
	                if (json.middle_school_options.length > 0) {
	                    i = 0, len = json.middle_school_options.length;
	                    var mid_vals = $('select[name="school_middle"]').val();
	                    
	                    if(mid_vals == null){
	                    	mid_vals = "";
	                    }

	                    while (i < len) {
	                        checked = (mid_vals == json.middle_school_options[i].value) ? ' selected="selected"' : '';
	                        html += '<option value= "' + json.middle_school_options[i].value + '"' + checked + '>' + json.middle_school_options[i].title + '</option>' + "\n";
	                        i++;
	                    }
	                }
	
	
	                html += '</select>';
	                parent = $('select[name="school_middle"]').parent();
	                $('select[name="school_middle"]').remove();
	                parent.append(html);
	                
	                //High School
	                html = '<select name="school_high">' + "\n";
	                
	                html += '<option value="">Any High Schools</option>' + "\n";
	
	                if (json.high_school_options.length > 0) {
	                    i = 0, len = json.high_school_options.length;
	                    var high_vals = $('select[name="school_high"]').val();
	                    
	                    if(high_vals == null){
	                    	high_vals = "";
	                    }

	                    while (i < len) {
	                        checked = (high_vals == json.high_school_options[i].value) ? ' selected="selected"' : '';
	                        html += '<option value= "' + json.high_school_options[i].value + '"' + checked + '>' + json.high_school_options[i].title + '</option>' + "\n";
	                        i++;
	                    }
	                }
	
	
	                html += '</select>';
	                parent = $('select[name="school_high"]').parent();
	                $('select[name="school_high"]').remove();
	                parent.append(html);
	
	            }
	        }
    	});
    });
    $('select[name="search_county"]').trigger('change');
    
    
});

/* 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();
    });

});
