// Feed items
function reloadFeedItems(pulsateCount, name) {
  pulsateCount = typeof(pulsateCount) != 'undefined' ? pulsateCount : 1;
  name = typeof(name) != 'undefined' ? name : "latest";
  
  if(name == "archived") {
    var urlAddition = "/archived"
  } else {
    var urlAddition = ""
  }
  
  $("#" + name).load("/feed_items" + urlAddition, null, function(){
    $("#" + name + "-tab").effect("pulsate", {times:pulsateCount})
    resetTabCount(name)
  })
}

function resetTabCount(tabName) {
  var size = $("#" + tabName + " .list tr").size() - 1
  if(size < 0) {
    size = 0
  }
  $("#" + tabName + "-tab .size").text(size)
}

// archive
$("#latest .archive").livequery(function(){
  var that = this
  $(this).click(function(){
    $.post(this.href, {_method:"delete"},function(){
      $(that).parents("tr").fadeOut()
      reloadFeedItems(1, "archived")
      resetTabCount("latest")
    })
    return false;
  })
})


// Feeds themselves
NewFeedForm = $.klass(Remote.Form, {
	initialize: function($super, options) {		
		this.submit_button = $("input[type=submit]", this.element);
		this.submit_text = this.submit_button.text();
		
    // this.spinner = $(".spinner", this.element);
		$super();
	},
	
	beforeSend: function() {
	  if(!$("#error-message").hasClass("hide")){
	    $("#error-message").addClass("hide")
	  }
		this.disable();
	},
	
	complete: function() {
    // this.spinner.hide();
		this.enable();
	},
	
	success: function(response, result) {
	  $("#feed-list").append(response)
	  $("#feed-list .feed:last").effect("highlight")
	  $("#new_feed input[type=text]").val("")
	  reloadFeedItems();
	},
	
	error: function(request, textStatus, erorThrown) {
	  $("#error-message").removeClass("hide")
	  $("#error-message").text(request.responseText)
	},
	
	disable: function() {
		this.submit_button.attr('disabled', 'disabled').val('Saving');
	},
	
	enable: function() {
    this.submit_button.attr('disabled', false).val('Add Feed');
	}
});

$(function(){
  $("#new_feed").attach(NewFeedForm)
  $(".tabs").tabs()
})

NewOpportunityFromFeedForm = $.klass(Remote.Form, {
	initialize: function($super, options) {		
		this.submit_button = $("input[type=submit]", this.element);
		this.submit_text = this.submit_button.text();
		
    this.spinner = $("#new_opportunity_spinner", this.element);
		$super();
	},
	
	beforeSend: function() {
	  this.spinner.show();
		this.disable();
	},
	
	complete: function() {
    this.spinner.hide();
		this.enable();
	},
	
	success: function(response, result) {
	  $.facebox.close();
	  reloadFeedItems(1, "latest");
	  reloadFeedItems(1, "archived");
	},
	
	error: function(request, textStatus, erorThrown) {
	  $("#facebox #new-form").replaceWith(request.responseText)
	},
	
	disable: function() {
		this.submit_button.attr('disabled', 'disabled').val('Saving');
	},
	
	enable: function() {
    this.submit_button.attr('disabled', false).val('Add Opportunity');
	}
});

$(".feeds #new_opportunity").livequery(function(){
  $(this).attach(NewOpportunityFromFeedForm)
})

// Delete feeds
$(".feed .delete").livequery(function(){
  $(this).click(function(){
    $.post("/feeds/" + this.feed.record_id, {_method:"delete"})
    $(this.feed).remove()
    reloadFeedItems(1)
    reloadFeedItems(1, "archived")
  })
})

$('a[rel*=facebox]').livequery(function(){
  if ( typeof( $.data($(this)[0], 'events') ) != 'object' ) {
    $(this).facebox();
  }
})