$(function(){
	

	// $("div.post img").each(function() {
	//   	var width = $(this).width();
	//   	if (width > 610) {
	//   		$(this).css("width", "610px");
	//       var src = $(this).attr('src').replace('img_T','img_L');
	//       var a = $('<a/>').attr('href', src).attr('rel', 'facebox');
	//       $(this).wrap(a);
	//   	}
	//   });

  $(".forum-navigation ul").superfish({ 
	    delay:       100,
	    animation:   {opacity:'show', height:'show'},
	    speed:       'fast'
	});
  
  $(".admin .sticky-control a").click(function(){
    var vars = {}
    if($(this).hasClass("sticky")) {
      $.post(this.href, {is_sticky:1, _method:"PUT"})
      $(this).removeClass("sticky")
      $(this).removeClass("green")
      $(this).addClass("unsticky")
      $(this).addClass("grey")
      $(this).text("Make this topic unsticky")
    } else {
      $.post(this.href, {is_sticky:0, _method:"PUT"})
      $(this).removeClass("unsticky")
      $(this).removeClass("grey")
      $(this).addClass("sticky")
      $(this).addClass("green")
      $(this).text("Make this topic sticky")
    }
    return false
  })
  
  $("#notify_of_posts").change(function(){
    $(this).attr("disabled", true)
    var that = this;
    var text = $(this).parent().find("span")
    text.text("Updating your preferences...")
    text.addClass("disabled")
    
    if(this.checked){
      $.post("/monitored_topics", {topic_id: $(this).attr("name")}, function(){
        text.text("Preferences updated. Uncheck this checkbox to stop receiving email updates for this topic.")
        text.removeClass("disabled")
        $(that).attr("disabled", false)
      })
    } else {
      $.post("/monitored_topics/1", {topic_id: $(this).attr("name"), _method:"DELETE"}, function(){
        text.text("Preferences updated. Check this checkbox to receive email updates for this topic again.")
        text.removeClass("disabled")
        $(that).attr("disabled", false)
      })
    }
  })
  
})

$(".flag a").livequery(function(){
  var href = this.href
  var that = this
  
  $(this).unbind("click")
  $(this).click(function(){
    $(this).text("Flagging...")
    $.post(this.href, {}, function(){
      $(that).hide()
      $('.unflag a', $(that).parent().parent()[0]).show()
      $(that).text("Flag")
    })
    return false;
  })
})

$(".unflag a").livequery(function(){
  var href = this.href
  var that = this
  $(this).unbind("click")
  $(this).click(function(){
    $(this).text("Unflagging...")
    $.post(this.href, {destroy:"true"}, function(){
      $(that).hide()
      $('.flag a', $(that).parent().parent()[0]).show()
      $(that).text("Unflag")
    })
    return false;
  })
})
