		jQuery.fn.limitMaxlength = function(options) {
 
  var settings = jQuery.extend({
    attribute: "maxlength",
    onLimit: function(){},
    onEdit: function(){}
  }, options);
  
  // Event handler to limit the textarea
  var onEdit = function(){
    var textarea = jQuery(this);
    var maxlength = parseInt(textarea.attr(settings.attribute));
    if(textarea.val().length > maxlength){
      textarea.val(textarea.val().substr(0, maxlength));
      jQuery.proxy(settings.onLimit, this)();
    }
    jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
  }
 
  this.each(onEdit);
  return this.keyup(onEdit).keydown(onEdit).focus(onEdit);
}

$(document).ready(function() {
  
  $('.vote').click(function() {
    var id = this.id;
    $(".votes-" + id).load('/controls/vote/' + id);
    $('.vote-' + id).html('<div class="btn-voted">&nbsp;</div>');
  });
	
	
  $('#select-category').click (
    function () {
      $('.categories').show();
    }
  );
	
  $('.categories').hover (
    function () {
      $('.categories').show();
    },
    function () {
      $('.categories').hide();
    }
  );
  
  $('li.category').click(function () {
    $('input.category_id').val(this.id);
    $('#select-category').removeClass('default');
    $('#select-category').addClass('option');
    $('.option-text').text($(this).html());
    $('.categories').hide();
  });
  
  var onEditCallback = function(remaining){
    
    $('.remaining').html(remaining);
    
    if(remaining > 0){
      $(this).css('background-color', 'white');
    	$(this).css('color', '#333');
    	$('#add-comment').attr('action', '/comments/add');
    }
    else {
    	$('#add-comment').attr('action', '#');
    }
  }
  
  var onLimitCallback = function(){
    $(this).css('background-color', '#961D08');
    $(this).css('color', '#FFF');
  }
  
  $('textarea[maxlength]').limitMaxlength({
    onEdit: onEditCallback,
    onLimit: onLimitCallback,
  });
  
  
		$('#submitform').ajaxForm({
			target: '#error',
			success: function() {
				$('#error').fadeIn('slow');
				$('#error').html('Successfully updated');
			}
		});
		
  $('a[rel*=facebox]').facebox({
    loadingImage : '/img/loading.gif',
    closeImage   : '/img/closelabel.png'
  })
  
	$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("ul.nav li span").click(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});  
  
});

twttr.anywhere(function (T) {
    T.hovercards();
    T("#follow-placeholder").followButton('studdrs');
  });
