// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function request_login_or_redirect(url) {
  var field = document.getElementById("ajax_login[url_to_redirect]");
  field.value = url;
  
  jQuery.ajax({
    type: 'GET',
    url: '/sessions/user_is_logged_in', 
    success: function(status){
      if (status == "logged") {
        window.location = url;
      } else if (status == "not_logged") {
        jQuery("#container").attr('opacity', '0.3');
        jQuery("#ajax_login_box").show();
      }
    }
  });
}

function cancel_ajax_login() {
  jQuery("#ajax_login_box").hide();
}

function submit_ajax_login() {
  var form = jQuery("#ajax_login_form");

  var login = form.attr('ajax_login[login]').value;
  var password = form.attr('ajax_login[password]').value;
  
  this.login = login;
  this.password = password;
  _this = this;
  
  jQuery.ajax({
    type: 'POST',
    url: '/sessions/ajax_login?login=' + _this.login + '&password=' + _this.password, 
    async: false,
    success: function(status){
      if (status == "true") {
        cancel_ajax_login();
        jQuery("#ajax_login_warn").hide();
        jQuery("#container").attr('opacity', '1');
        var field = document.getElementById("ajax_login[url_to_redirect]");
        window.location = field.value;
      } else if (status == "false") {
        jQuery("#ajax_login_warn").show();
      }
    }
  });
  
  return false;
}


function hide_notices() {
  var element = jQuery("#notice_messages_container");
  setTimeout(function(){element.fadeOut("slow");}, 5000);
}

jQuery(document).ready(function() {
  jumpSelect();
  selectFilter();
  getStreetsByCity();
  streetChange();
  hide_notices();
});

function getStreetsByCity(){
  jQuery('select#citySelect').change(function() {
    var city_slug = $(this).value;
    jQuery.ajax({
      type: 'GET',
      dataType: 'script',
      url: '/streets/streets_by_city/?city_slug=' + city_slug,
      success: function(data){
        jQuery('select#streetSelect').html(data);
      }
    });
  });
}
function streetChange() {
  jQuery('select#streetSelect').change(function() {
    window.location = this.value;
  });
}

function jumpSelect() {
  jQuery("#regionsFilter option").each(function() {
    uri = new RegExp('^.*'+jQuery(this).attr('value')+'$');
    
    if(uri.exec(window.location))
      jQuery(this).attr('selected','selected');
  });
  
  jQuery("#regionsFilter").change(function() {
    if(this.value.length > 0)
      window.location = this.value;
  });
}


function add_credits(select_obj, agent_id) {
  var select = select_obj;
  
  if (select.value != "0") {
    jQuery.ajax({
      type: 'GET',
      url: '/agents/add_credits?agent_id=' + agent_id + '&' + 'value=' + select.value,
      success: function(new_value){
        jQuery('#credits_value').html(new_value);
        jQuery('#credits_select').attr('selectedIndex', 0);
      }
    });
    
  }
}

function featured_listing_update_status(select_obj) {
  var listing_id;
  var status;
  var agent_id;
  var splitted = select_obj.value.split("_");
  var jselect_obj = jQuery(select_obj);
  status = splitted[0];
  listing_id = splitted[1];
  agent_id = splitted[2];
  
  var span_msg = jQuery("#featured_listing_status_message_" + listing_id);
  
  jselect_obj.hide();
  span_msg.html('Updating...');
  span_msg.show();
  
  jQuery.ajax({
    type: 'GET',
    url: '/agents/' + agent_id + '/listing_claims/' + listing_id + '/swap_featured_status', 
    success: function(new_status){
      if (new_status != "error") {
        span_msg.html("<span style='color:green;'>Updated!</span>");
      } else {
        span_msg.html("<span style='color:red;'>Error</span>");
        jselect_obj.attr("selectedIndex", !jselect_obj.attr("selectedIndex") );
      }
      setTimeout(function() { span_msg.hide(); jselect_obj.show(); }, 3000);
    }
  });
}

function selectFilter() {
  jQuery('select.filterSelect').change(function() {
    window.location = this.value;
  });
  
  jQuery("select.filterSelect option").each(function() {
    uri = new RegExp('^.*'+jQuery(this).attr('value')+'$');
    
    if(uri.exec(window.location))
      jQuery(this).attr('selected','selected');
  });
}

function textCounter(source, display) {
  var result = 3000 - source.value.length;
  if (result >= 0) {
    display.value = result;
  } else {
    display.value = 0;
  }
}

function enable_month_container(year, index) {
  var class_name_base = "." + "month_container_";
  var link_class_name_base = "." + "link_";
  var container_size = 3;

  for (var i=0; i < container_size; i++) {
    jQuery(class_name_base + (year - i)).hide();
  }

  jQuery(class_name_base + (year - index)).show();

  for (var i=0; i < container_size; i++) {
    jQuery(link_class_name_base + (year - i)).html("<a href='#' onclick='return enable_month_container(" + year + ", "+ i +");'>" + (year-i) + "</a>");
  }

  jQuery(link_class_name_base + (year - index)).html(year - index);

  return false;
}

function redirect_to_selected_option() {
  var select = jQuery("#iam_looking_for_select");
  window.location = select.attr('value');
}

function facebook_button() {
  jQuery(document).ready(function() {
    jQuery.getScript('http://static.ak.fbcdn.net/connect.php/js/FB.Share')
  });
}