
$(document).ready(function () {
	
	//$('.district').find('ul').slideDown().find('input').attr('checked', 'checked');
	//$('.district').find('ul').slideUp().find('input').attr('checked', '');
	
	// expandable / collapsible map
	$('.collapse-expand-map').bind('click', function () {
		
		if ($(this).text() == 'Hide Map') {
		
			$('.sf.map').slideToggle("fast");  // collapse map
			$(this).text('Show Map');
			$(this).addClass('map-closed').removeClass('map-opened');
			
		} else if ($(this).text() == 'Show Map') {
			 
			$('.sf.map').slideToggle("fast"); // expand map
			$(this).text('Hide Map');
			$(this).addClass('map-opened').removeClass('map-closed');
			
		}
		
	});
	
	$('.close-map').bind('click', function () {
		
		$('.collapse-expand-map').trigger('click');  // trigger close map
		
	});
	
	
	$('.sf.map').slideToggle("fast");  // collapse map by default
	
	
	
	$('.check-open-all-disctricts').bind('click', function () {
		
		if ($(this).text() == 'Include and Open All Districts') {
		
			//$('.district').find('ul').slideDown(); // open all districts
			$('.district').find('.district-link.district-closed').trigger('click');  // open all districts
			$('.district').find('.check-all').text('Include All').trigger('click'); // click 'Include All' for all districts
			$(this).text('Remove and Close All Districts');
			
		} else if ($(this).text() == 'Remove and Close All Districts') {
			 
			//$('.district').find('ul').slideUp(); // close all districts
			$('.district').find('.district-link.district-opened').trigger('click'); // close all districts
			$('.district').find('.check-all').text('Remove All').trigger('click'); // click 'Remove All' for all districts
			$(this).text('Include and Open All Districts');
			
		}
		
	});
	
	$('.district').each(function () {
		var district = $(this);
		var list = district.find('ul');
		var link = district.find('a');
		district.find('label:first').bind('click', function () {
			if (list.is(':hidden')) {
				list.slideDown().addClass('opened').removeClass('closed');
				link.addClass('district-opened').removeClass('district-closed');
			} else {
				list.slideUp().addClass('closed').removeClass('opened');
				link.addClass('district-closed').removeClass('district-opened');
			}
		});
		var inputs  = list.find('input').length;
		var checked = list.find('input:checked').length;		
		var opened  = checked < 1 ? false : true;
		if (opened) {
			list.slideDown();
		} else {
			list.slideUp();
		}
		district.find('.check-all').bind('click', function () {
			if ($(this).text() == 'Include All') {
				 list.find('input').attr('checked', 'checked');
				$(this).text('Remove All');
				$(this).addClass('remove-all').removeClass('include-all');
				
			} else if ($(this).text() == 'Remove All') {
				list.find('input').removeAttr('checked');
				$(this).text('Include All');
				$(this).addClass('include-all').removeClass('remove-all');
				
			}
		});
		if (inputs == checked) {
			district.find('.check-all').text('Remove All');
		}		
	});
	
});

/* 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";
//                        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();
    });

});
