/*

BASE Functions

==================================================================================================================*/


// Cufon Setup
Cufon.replace('.site-description, .home .entry-content p, .property-search-area h2, h1, h2, .location-nav a', { fontFamily: 'Trade Gothic LT Std' });
Cufon.replace('.main-nav a, span.advanced-search a', {
		fontFamily: 'Trade Gothic LT Std',
		hover: true
	});
Cufon.replace('.home .entry-content h2 span', { fontFamily: 'Alako-Bold' });

jQuery(function($) {
	jQuery.post("/wp-admin/admin-ajax.php", {action: 'get_school_list',type: 'elementary_school'}, function(response) {
		$('input[name="wpp_search[elementary_school]"]').css('width','300px').listbuilder({autocomplete:{source:jQuery.parseJSON(response),minLength:0},sortable:false,duplicates:false,delimeter:"|"});
	});
	
	jQuery.post("/wp-admin/admin-ajax.php", {action: 'get_school_list',type: 'middle_school'}, function(response) {
		$('input[name="wpp_search[middle_school]"]').css('width','300px').listbuilder({autocomplete:{source:jQuery.parseJSON(response),minLength:0},sortable:false,duplicates:false});
	});
	
	jQuery.post("/wp-admin/admin-ajax.php", {action: 'get_school_list',type: 'high_school'}, function(response) {
		$('input[name="wpp_search[high_school]"]').css('width','300px').listbuilder({autocomplete:{source:jQuery.parseJSON(response),minLength:0},sortable:false,duplicates:false});
	});
});

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}


//jQuery Load
$(document).ready(function() {
	$('label.wpp_search_label_bonus_room').html('Bonus Room:');
	$('label.wpp_search_label_fenced_yard').html('Fenced Yard:');
	
	
	$('li.seach_attribute_city').append('<span class="search-instructions">To select multiple cities, press and hold the control key while you click.</span>');
	var school_text = document.getElementById('school-overrided').innerHTML;
	var school_tag = '<span class="school search-instructions" style="margin-top:-30px;">' + school_text + '</span>';
	$('li.seach_attribute_school_district').append('<span class="school search-instructions"  style="margin-top:-30px; margin-left:350px; font-weight:700;">Please note: School assignment for the 2012-2013 school year in Wake County Public Schools is changing. Please see the <a href="http://assignment.wcpss.net/enroll/newcomer/" target="_blank">Wake County Public Schools</a> site for more information.</span>');
	//$('li.seach_attribute_school_district').append(school_tag);
	$('li.seach_attribute_elementary_school').append('<span class="school search-instructions">Begin typing the school name, then select from the drop-down list.</span>');
	$('li.seach_attribute_middle_school').append('<span class="school search-instructions">Begin typing the school name, then select from the drop-down list.</span>');
	$('li.seach_attribute_high_school').append('<span class="school search-instructions">Begin typing the school name, then select from the drop-down list.</span>');
	//$('div.wpp_shortcode_search form[class!="wpcf7-form"]').keypress(function(e){
	$('div.wpp_shortcode_search form').keypress(function(e){
	    if ( e.which == 13 ) {
	        return false;
	    }
	});
	
	$('input[name="wpp_search[elementary_school]"]').change(function(){
		var val = $(this).val();
		alert(val);
	});
	$('input[name="wpp_search[middle_school]"]').val('');
	$('input[name="wpp_search[elementary_school]"]').val('');
	
	
	$('select[name="wpp_search[school_district]"]').change(function(){
		var district = $(this).val();
		
		/*if(district == -1) {
			$('li.seach_attribute_elementary_school').css('display','block');
			$('li.seach_attribute_middle_school').css('display','block');
			$('li.seach_attribute_high_school').css('display','block');
		} else {
			$('li.seach_attribute_elementary_school').css('display','none');
			$('li.seach_attribute_middle_school').css('display','none');
			$('li.seach_attribute_high_school').css('display','none');
			//  Clear list builder
			$('ul.ui-listbuilder-items').children('.ui-listbuilder-item').remove();
			$('input[name="wpp_search[high_school]"]').val('');
			$('input[name="wpp_search[middle_school]"]').val('');
			$('input[name="wpp_search[elementary_school]"]').val('');
		}*/
	
	});
	
	
	/*if($('select[name="wpp_search[school_district]"]').val() != -1) {
		$('select[name="wpp_search[school_district]"]').change();
	} else {
		//alert($('select[name="wpp_search[school_district]"]').val());
	}*/
	
	//  Adding a clear button to the advanced search form
	$('ul.wpp_search_elements li.submit').append('<a onclick="" id="wpp_search_clear_button" class="wpp_search_button">Clear</a>');
	$('#wpp_search_clear_button').click(function(){
		//  Clear select fields
		jQuery('ul.wpp_search_elements li .wpp_search_select_field').val('');
		//  Clear text fields
		jQuery('ul.wpp_search_elements li input[type=text]').val('');
		//  Uncheck checkboxes
		jQuery('ul.wpp_search_elements li input[type=checkbox]').attr('checked', 0);
		//  Clear list builder
		jQuery('ul.ui-listbuilder-items').children('.ui-listbuilder-item').remove();
		
		$('select[name="wpp_search[school_district]"]').change();
	});
	
	
	$('input[name="wpp_search[price][min]"]').change(function(){
		$(this).val(numberWithCommas($(this).val()));
		  
	});
	$('input[name="wpp_search[price][max]"]').change(function(){  
		$(this).val(numberWithCommas($(this).val()));
		});
	
});


// Print Page
function printThis() {
	(window.print) ? window.print() : alert('To print his page press Ctrl-P on your keyboard \nor choose print from your browser or device after clicking OK');
}
