$(function(){
  $('.sliders').slidify({
    max:2500000,
    valueSplit:''
  });
	$(".sqft-sliders").slidify({
		max:6000,
		stepping:100,
		unit: 'sq ft',
		units: 'sq ft',
		valueSplit:''
	});
	$(".acreage-sliders").slidify({
		max:100,
		stepping:1,
		unit: 'acre',
		units: 'acres',
		valueSplit:''
	});
//	$(".bedroom-sliders").slidify({
//		max:6,
//		stepping:1,
//		unit: 'bedroom',
//		units: 'bedrooms',
//		valueSplit:''
//	});
//	$(".bathroom-sliders").slidify({
//		max:6,
//		stepping:1,
//		range: 'min',
//		unit: 'bath',
//		units: 'baths',
//		valueSplit:''
//	});
	//slider initialization needs to happen before hide
	$('.hide').hide();
	$('.show').show();
	$('#res-sqft').hide();
	$('#quick-toggle,#mls-toggle').click(function(){
		$('#quick-form').toggle();
		$('#mls-form').toggle();
		return false;
	});
	$('a.toggle').click(function(){
		var toToggle = '#' + this.id + '-toggle';
		var toHide = '#' + this.id + '-hide';
		$(toToggle).toggle();
		$(toHide).hide();
		return false;
	});
	$('a.slide').click(function(){
		var toSlide = '#' + this.id + '-toggle';
		var toHide = '#' + this.id + '-hide';
		$(toSlide).slideToggle();
		$(toHide).hide();
		return false;
	});
	$('#show-advanced').click(function(){
		$('#advanced').toggle();
		$(this).hide();
		$(this).siblings('span').hide();
		$('#res-sqft').show();
		return false;
	});

	$('.slider-toggle').toggle(function(){
	  // Show Inputs
	  $(this).html('Return to sliders');
	  $('~ .slider-help', this).empty().html('(Enter exact range values)');
	  // disable sliders from working even though they are hidden
	  $('~ .ui-slider', this).children('.ui-slider-handle:first').removeClass('low').hide();
	  $('~ .ui-slider', this).children('.ui-slider-handle:last').removeClass('hi').hide();
	  $('~ .ui-slider', this).children('.ui-slider-range').hide();
	  
	  // remove slider background
	  $('~ .ui-slider', this).css('background-image','none');
	  $('~ .slider-values', this).hide();
	  
	  $('~ .ui-slider', this).children('input:first').show().focus();
	  $('~ .ui-slider', this).children('input:last').show();
	  $('~ .ui-slider', this).children('span').show();
  }, function() {
    $(this).html('Enter exact range');
    $('~ .ui-slider', this).children('input:first').hide();
	  $('~ .ui-slider', this).children('input:last').hide();
	  $('~ .ui-slider', this).children('.slider span').hide();
	  
	  $('~ .slider-help', this).empty().html('(Drag sliders to select range)');
	  // enable sliders from working again after they are hidden
	  $('~ .ui-slider', this).children('.ui-slider-handle:first').addClass('low').show();
	  $('~ .ui-slider', this).children('.ui-slider-handle:last').addClass('hi').show();
	  $('~ .ui-slider', this).children('.ui-slider-range').show();
	  
	  // show slider background again
	  $('~ .ui-slider', this).css('background-image','url(http://dev.prudentialsteamboatrealty.gondolacms.com/s/static/img/slider.gif)');
	  $('~ .slider-values', this).show();
	});
	
	$('select.price-dropdown').change(function(){
	  var price_array = $('select.price-dropdown option:selected').val().split(',');
	  var high_price = price_array[0];
	  var low_price = price_array[1];
	  $('input#id_low_price').val(low_price);
	  $('input#id_high_price').val(high_price);
	});
	
	$('select.acre-dropdown').change(function(){
	  var acre_array = $('select.acre-dropdown option:selected').val().split(',');
	  var high_acre = acre_array[0];
	  var low_acre = acre_array[1];
	  $('input#id_low_acre').val(low_acre);
	  $('input#id_high_acre').val(high_acre);
	});
	
    $('.ir').jQIR('gif', mediaURL + 'img/');
    //if(typeof sIFR == "function"){
        sIFR.replaceElement(named({sSelector:".sifr", sFlashSrc:mediaURL + "swf/univers.swf", sColor:"#2f292b", sBgColor:"#ffffff", sWmode:'transparent'}));
        sIFR.replaceElement(named({sSelector:".sifr-black", sFlashSrc:mediaURL + "swf/univers.swf", sColor:"#000000", sBgColor:"#dbdcd5", sWmode:'transparent'}));
        sIFR.replaceElement(named({sSelector:".sifr-white", sFlashSrc:mediaURL + "swf/univers.swf", sColor:"#ffffff", sBgColor:"#2f292b", sWmode:'transparent'}));
    //}
	$('a.popup').attr( { target: "_blank" } );
	$('#subscribe').click(function(){
		$('#subscribe-form').slideToggle();
		return false;
	});
	
	$('a#createfeed').click(function(){
	    // Post to create an IDX RSS feed and open in a Thickbox.
	    var createFeedUrl = $('#createfeed-form').attr('action');
	    tb_show("Custom Listings Feed","#TB_inline?height=50&width=700&inlineId=DNE","");
	    $.post(createFeedUrl, {}, function(data){
	        var feed = '<p class="center"><strong>Your RSS feed is now available via the link below.</strong><br /><a href="'+data+'">'+data+'</a></p>';
	        $('#TB_ajaxContent').append(feed);
	    });
	    return false
	});
	
	$('.check-all').click(function(){
		if ($(this).text() == 'check all') {
			checkStatus = true;
			$(this).text('uncheck all');
		}
		else {
			checkStatus = false;
			$(this).text('check all');
		}
		fs = $(this).parents('fieldset').find('input').each(function(){
			this.checked = checkStatus;
		});
		return false;
	});
	
  $('#subscribe-form').submit(function() {
  	$('#subscribe-form .submit').hide();
  	$('#subscribe-form p').empty();
  	$('#subscribe-form p').append('<img id="loading" src="'+ mediaURL + 'img/loadingAnimation.gif" />');
  	input = {name : this.name.value, email : this.email.value, ajax : 'true'};
  	$.post(this.action, input, function(data){
  		if ( data == 'True') {
  			$('#loading').remove();
  			$('#subscribe-form').empty();
  			$('#subscribe-form').append('You have successfully subscribed. Please check your email for more details.');
  		}
  		else {
  			$('#subscribe-form .submit').show();
  			$('#loading').remove();
  			$('#subscribe-form p').empty();
  			$('#subscribe-form p').prepend('<div class="error">There was an error entering your subscription.<br />Please be sure you have entered both your <em>name</em> and a valid <em>email</em> address</div>');
  			$('#subscribe-form .submit').show();
  		}
  	});
  	return false;
  });
  
});
