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

jQuery.ajaxSetup({'beforeSend':function(xhr){
  xhr.setRequestHeader("Accept", "text/javascript");
}});

function localStorageExists() {
  return ('localStorage' in window) && window['localStorage'] !== null;
}

if(typeof(String.prototype.trim) === "undefined")
{
    String.prototype.trim = function() 
    {
        return String(this).replace(/^\s+|\s+$/g, '');
    };
}

jQuery(document).ready(function(){
  $(".twitter_query").click(function(e){
    var query = this.getAttribute("data-query");
    var rate = this.getAttribute("data-rate");
    var details = this.getAttribute("data-details");
    var title = $(this).text();
    
    try {
      $("#twitterResults").each(function(){this.twitter.clear()});
    } catch (err) {
      //just here for first run
    }
    
    
    $("#twitterDetails").text(details);
    
    $("#twitterResults").liveTwitter(query, {limit: 50, rate: rate * 1000});
    
    $(".twitter_li, .twitter_query").removeClass("active");
    
    $(this).addClass("active");
    $(this).parents("li").addClass("active");
    
    return false;
  });
  
  $("img.image_submit").click(function(e){
    var name = this.getAttribute("data-name");
    var value = this.getAttribute("data-value");
    var form = $(this).parents("form");
    form.append("<input type='hidden' name='" + name+ "' value='" + value + "'/>");
    form.submit();
    
    return false;
  });
  
  $("#filters .expandy").click(function(){
    var filterDiv = $(this).parents("#filters");
    if (filterDiv.hasClass("compact")) {
      filterDiv.removeClass("compact", "slow");
      $(this).text("Hide Filters");
      if (localStorageExists()) {
        window.localStorage["filters_expanded"] = true;
      }
    } else {
      filterDiv.addClass("compact", "slow");
      $(this).text("Show Filters");
      if (localStorageExists()) {
        window.localStorage["filters_expanded"] = false;
      }
    }
  });
  
  if (localStorageExists()) {
    if (window.localStorage["filters_expanded"] == "true") {
      $("#filters").removeClass("compact");
      $("#filters .expandy").text("Hide Filters");
    }
  }
  
  $(".flash").click(function(){
    $(this).slideUp("fast");
  });
  
  $("a.add_to_schedule").removeAttr("onclick").live("click", function(e){
    e.preventDefault();
    var a = $(this);
    
    var event_id = a.parents(".event").attr("id").replace("event_", "");
    var user = $(document.body).data("user");
    
    $.post("/appointments.json", {"appointment[user_id]":user.id, "appointment[event_id]":event_id}, function(data){
      console.log(data);
      a.attr("id", "appointment_" + data.id);
      a.fadeOut("fast", function(){
        a.attr("class", "remove_from_schedule").fadeIn();
      });
    });
  });
  
  $(".event a.remove_from_schedule").removeAttr("onclick").live("click", function(e){
    e.preventDefault();
    var a = $(this);
    
    var appt_id = a.attr("id").replace("appointment_", "");
    
    $.post("/appointments/" + appt_id, {"_method": "DELETE"}, function(data){
      a.fadeOut("fast", function(){
        a.attr("class", "add_to_schedule").fadeIn();
      });
    });
  });
  
  $(".schedule_item a.remove_from_schedule").removeAttr("onclick").live("click", function(e){
    e.preventDefault();
    var a = $(this);
    
    var appt_id = a.attr("id").replace("appointment_", "");
    
    $.post("/appointments/" + appt_id, {"_method": "DELETE"}, function(data){
      a.parents(".schedule_item").slideUp("fast");
    });
  });
  
  
  $(".filtergroup.locations li").click(function(e){
    window.location = $(this).find("a").attr("href");
  });
});
