/*** required: lib/lre/auto_slideshow ***/

function auto_slideshow(visible_rows) {

  var $front = $('#slideshow .stages .front.stage');
  var $back = $('#slideshow .stages .back.stage');

  var $swap;
  var animating = false;
  var speed = 1500;
  var num_super_featured = $('#slideshow .offstage .actor').length;

  var $previews = $('#slideshow .previews');
  var num_images = $previews.find('ul li img').length;
  var images_per_row = Math.floor($('.stage').width() / $('.previews ul li').width());
  var num_rows = Math.ceil(num_images / (images_per_row + 0.0));

  if(num_super_featured > 1) {
    var rotation_index = 1;
    var rotator;

    setTimeout(function() {
      show_next_super_featured();
      rotator = setInterval(function() {
        show_next_super_featured();
      }, 5000);
    }, 3500);

    function show_next_super_featured() {
      display_super_featured(rotation_index % num_super_featured);  
      rotation_index += 1;
    }

    function display_super_featured(index) {

      // move the border around the current listings previews
      $('#slideshow .previews li.current').removeClass('current');
      $('#' + index + '_preview').addClass('current');

      var $details = $($('#slideshow .offstage .actor')[index]);
      $back.html($details.html());

      if(!animating) {
        animating = true;
        $front.fadeOut(speed);
        $back.fadeIn(speed, function() {
          $swap = $front;
          $front = $back;
          $back = $swap;
          animating = false;
        });
      }
    }

    $('#slideshow .previews li a').click(function() {
      speed = 500;
      clearInterval(rotator);
      var index = parseInt($(this).closest('li').attr('id'));
      display_super_featured(index);
      return false;
    });

    // preload images in the offstage
    $('#slideshow .offstage img').each(function() {
      var img = new Image();
      img.src = this.src;
    });

    if(visible_rows && visible_rows < num_rows) {

      var min_height = (59 * visible_rows) + 'px'; 
      var max_height = (59 * num_rows) + 'px'; 
      var $ul = $previews.find('ul');

      var $expand = $('<a href="#" class="previews_expand_link">More &darr;</a>');
      $expand.toggle(function() {
        $ul.css('height', min_height).css('max-height', max_height).animate({ 'height' : max_height });
        $(this).html('Less &uarr;');
        return false;
      }, function() {
        $ul.css('height', max_height).css('max-height', max_height).animate({ 'height' : min_height });
        $(this).html('More &darr;');
        return false;
      });

      $previews.append($expand);

    }
  } else {
    $('#slideshow .previews').hide();
  }

}

/*** page javascript ***/

auto_slideshow();

// IE7 hand-holding
$("dl.week_in_review dd:last-child").addClass("last");
$("dl.agent_blog_posts dd:last-child").addClass("last");

// frame-busting-js
if (top.location != location) 
  top.location.href = document.location.href;

// Rental Form Selection
var swapForm = function(){
  if ($('#select_long_term:checked').length > 0) {
    $('#long_term_form').show();
    $('#vacation_rental_form').hide();
  }
  if ($('#select_vacation_rental:checked').length > 0) {
    $('#long_term_form').hide();
    $('#vacation_rental_form').show();
  }
};
swapForm();
$('#form_type input[type=radio]').change(function(){
  swapForm();
  Analytics.trackPage({ads: ['acc09d3b']});
});

// Community Jump Select
$('#communities_jump').change(function() {
  document.location.href = $(this).val();
});

// Keep Media box as high as Noteworthy Professionals
$('#media').each(function(){
  var noteworthyHeight = $('.noteworthy_professionals_box').height();
  var thisPadding = $(this).css('padding-top');
  var newHeight = (noteworthyHeight - (parseInt(thisPadding)*2)) + 'px';
  $(this).css({minHeight: newHeight});
});

var $country_states = $('.country_states');

$country_states.find('.country select').change(function() {
  var country = $(this).val();
  var $container = $(this).parent().parent();
  var $states = $container.find('.states');
  if(country) {
    $states.find('select').hide();
    var $state_select = $states.find('.' + country + '_states');
    $state_select.show();
    if($state_select[0])
      $states.show();
    else
      $states.hide();
  } else {
    $states.hide();
  }
  $container.find('input[name=area]').val(country);
});

$country_states.find('.states select').change(function() {
  var state = $(this).val();
  var $container = $(this).parent().parent();
  if(state) {
    $container.find('input[name=area]').val(state);
  } else {
    var country = $container.find('.country select').val();
    $container.find('input[name=area]').val(country);
  }
});



