var slide_timer = undefined;
var current_slide = undefined;
var current_banner = undefined;

jQuery(function($) {

  classify_datepickers();

  $('.no-script-featured-listings').hide();
  var listings = $('.featured-listing').get().sort(function(){
    return Math.round(Math.random())-0.5;
  });
  var counter = 1;
  $(listings.slice(0, 3)).each(function () {
    var me = $(this);
    $('.featured-listing-1-' + counter).html(me.html());
    counter += 1;
  });
  counter = 1;
  $(listings.slice(3, 6)).each(function () {
    var me = $(this);
    $('.featured-listing-2-' + counter).html(me.html());
    counter += 1;
  });

  $('.numeric').numeric();


  jQuery('.thumbnail-slide').click(function(){
    me = jQuery(this);
    var thumb = me.find('img');

    jQuery('.photo_slide').hide();
    jQuery(thumb.attr('title')).show();
    current_slide = me;
    return false;
  });

  jQuery('.play-slide').click(function(){
    if (slide_timer == undefined) {
      slide_timer = setTimeout("next_slide();", 2500);
    }
    return false;
  });

  jQuery('.stop-slide').click(function(){
    if (slide_timer) {
      clearTimeout(slide_timer);
      slide_timer = undefined;
    }
    return false;
  });


  jQuery('.thumbnail-slide').first().click();
  jQuery('.slideshow-controls').show();
  jQuery('.play-slide').click();

  jQuery('.banner_slide').hide();
  current_banner = jQuery('.banner_slide').first();
  jQuery(current_banner).show();
  setTimeout('next_banner();', 3000);


//  $('*[data-notes]').tipsy();
//  $('select[multiple]').multiselect({sortable: false, searchable: false});
});

function next_banner() {
  var next_banner = jQuery(current_banner).nextAll('.banner_slide');
  if (next_banner.size() >= 1) {
    current_banner = next_banner.first();
    }
  else {
    current_banner = jQuery('.banner_slide').first();
  }
  jQuery('.banner_slide').hide();
  jQuery(current_banner).show();
  setTimeout('next_banner();', 3000);
}

function next_slide() {
  var next = jQuery(current_slide).nextAll('.thumbnail-slide');
  if (next.size() >= 1) {
    current_slide = next.first();
  } else {
    current_slide = jQuery('.thumbnail-slide').first();
  }
  current_slide.click();
  slide_timer = setTimeout("next_slide();", 2500);
}


function hide_element(element, hide) {
  if(hide) {
    $(element).hide();
  } else {
    $(element).show();
  }
}

function classify_autocomplete_fields() {
  $(".user-selector").tokenInput("/users/search", {
    classes: {
      tokenList: "token-input-list-facebook",
          token: "token-input-token-facebook",
          tokenDelete: "token-input-delete-token-facebook",
          selectedToken: "token-input-selected-token-facebook",
          highlightedToken: "token-input-highlighted-token-facebook",
          dropdown: "token-input-dropdown-facebook",
          dropdownItem: "token-input-dropdown-item-facebook",
          dropdownItem2: "token-input-dropdown-item2-facebook",
          selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
          inputToken: "token-input-input-token-facebook"
          },
        hintText: "Type in a user name",
        searchDelay: 600
    });
}

function classify_datepickers() {
  $(".rails-ui-datepicker").datepicker({
    yearRange: "-115:+4",
    showOn: "button",
    buttonImage: "/images/calendar.png",
    duration: '',
    buttonImageOnly: true
  });
}

function update_avatar_previews(image, options) {
  var dim_ratio = {
  x: image.width / options.width,
  y: image.height / options.height
  };

  $('.preview-image').each(function() {
      parent_elem = {
      width: $(this).parent().width(),
      height: $(this).parent().height()
      };

      pos_ratio = {
      x: options.width / parent_elem.width,
      y: options.height / parent_elem.height
      };

      calc_pos = {
      w: Math.ceil(parent_elem.width * dim_ratio.x) + 'px',
      h: Math.ceil(parent_elem.height * dim_ratio.y) + 'px',
      x: '-' + Math.ceil((options.x1) / pos_ratio.x) + 'px',
      y: '-' + Math.ceil((options.y1) / pos_ratio.y) + 'px'
      };

      // resize image and move the image in the preview area
      this.style.width = calc_pos.w;
      this.style.height = calc_pos.h;
      this.style.left = calc_pos.x;
      this.style.top = calc_pos.y;
    });

  $('#resizer_x').val(options.x1);
  $('#resizer_y').val(options.y1);
  $('#resizer_width').val(options.width);
  $('#resizer_height').val(options.height);
}

