
// This bit of code is to help trigger the responds_to block so that
// we can be sent back javascript and not html.
jQuery({
  'beforeSend': function(xhr){xhr.setRequestHeader("Accept", "text/javascript")}    
})

$(document).ready(function() {


  // All non-GET requests will add the authenticity token
  // if not already present in the data packet
  $("body").bind("ajaxSend", function(elm, xhr, s) {
    if (s.type == "GET") return;
    if (s.data && s.data.match(new RegExp("\\b" + window._auth_token_name + "="))) return;
    if (s.data) {
      s.data = s.data + "&";
    } else {
      s.data = "";
      // if there was no data, jQuery didn't set the content-type
      xhr.setRequestHeader("Content-Type", s.contentType);
    }

    s.data = s.data + encodeURIComponent(window._auth_token_name) + "=" + encodeURIComponent(window._auth_token);
    });


    // More behaviours
    // Featured Listings Star
    $('a.featured_star').click(function(link){
      var link = $(this);
      var id = link.attr("id");
      var link_image = link.children("img").attr("src")

      $.post('/clients/featured', "id=" + id, function(data){
        if(data == "1")
          link.children("img").attr("src", "/images/icons/star.png");
        else
          link.children("img").attr("src", "/images/icons/star-off.png");
        
      });
     });
    
     // Featured Client
    $('a.featured').click(function(event){
      var link = $(this);
      var id = link.attr("id")
      
      var url = '/clients/' + id + '/request_client'
      $.get(url, function(data){
        
        $('#featured').html(data)
        
      })
      
      event.preventDefault();
      return
      
    });
    
    
           
    
});

















