// application.js

(function($) {

    /*
     * Auto-growing textareas; technique ripped from Facebook
     */
    $.fn.autogrow = function(options) {

        this.filter('textarea').each(function() {

            var $this       = $(this),
                minHeight   = $this.height(),
                lineHeight  = $this.css('lineHeight');

            var shadow = $('<div></div>').css({
                position:   'absolute',
                top:        -10000,
                left:       -10000,
                width:      $(this).width() - parseInt($this.css('paddingLeft')) - parseInt($this.css('paddingRight')),
                fontSize:   $this.css('fontSize'),
                fontFamily: $this.css('fontFamily'),
                lineHeight: $this.css('lineHeight'),
                resize:     'none'
            }).appendTo(document.body);

            var update = function() {

                var times = function(string, number) {
                    for (var i = 0, r = ''; i < number; i ++) r += string;
                    return r;
                };

                var val = this.value.replace(/</g, '&lt;')
                                    .replace(/>/g, '&gt;')
                                    .replace(/&/g, '&amp;')
                                    .replace(/\n$/, '<br/>&nbsp;')
                                    .replace(/\n/g, '<br/>')
                                    .replace(/ {2,}/g, function(space) { return times('&nbsp;', space.length -1) + ' ' });

                shadow.html(val);
                $(this).css('height', Math.max(shadow.height() + 20, minHeight));

            }

            $(this).change(update).keyup(update).keydown(update);

            update.apply(this);

        });

        return this;

    }

})(jQuery);

// Debugging courtesy of http://happygiraffe.net/blog/2007/09/26/jquery-logging/
jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};

$(document).ready(function() {
  $('#wp-submit').click(function() {
    $(this).addClass('onclick');
  });
  $('ul.sidebarlogin_otherlinks li a').click(function() {
    $(this).addClass('onclick');
  });
  $('ul.pagenav li a').click(function() {
    $(this).addClass('onclick');
  });
  $('#comments-meta h3#new-comment a').click(function() {
    $(this).addClass('onclick');
  });
  $('#respond form #submit').click(function() {
    $(this).addClass('onclick');
  });
  //$('.new-post a').click(function() {
  //  $(this).addClass('onclick');
  //});

  $('#sort input').attr("checked", false);
  $('#sort #sort-activity').attr("checked", true);

  $('#sort-entry').click(function(event) {
    $.get("index.php?t=posts", {},
	  function(data){
	    $('#open-discussion').html(data);
    $('#sort input').attr("checked", false);
    $('#sort-entry').attr("checked", true);
	  }
	 );
    return false;
  });

  $('#sort-activity').click(function(event) {
    $.get("index.php?t=empty", {t: ""},
	  function(data){
	    $('#open-discussion').html(data);
    $('#sort input').attr("checked", false);
    $('#sort-activity').attr("checked", true);
	  }
	 );
    return false;
  });

  if( $('form#commentform').length < 1 ) {
    $('#comments-meta h3#new-comment').click(function(event) {
      $('#comments-meta span#not-logged-in-message').fadeIn(800).text('Du må logge inn for å kommentere').animate({borderWidth: '0'}, 1600).fadeOut(400);
      return false;
    });
  }

  if ( $('#loginform form').length > 0 ) {
    $('#information-header .new-post .new-post-button a').click(function(event) {
      $('#information-header .new-post-message div').fadeIn(800).text('Du må logge inn for å poste').animate({borderWidth: '0'}, 1600).fadeOut(400);
      return false;
    })
    $('#discussion-header .new-post-button').click(function(event) {
      $('#sort').hide();
      $('#discussion-header .new-post-message').show();
      $('#discussion-header .new-post-message div').fadeIn(800).text('Du må logge inn for å poste').animate({borderWidth: '0'}, 1600).fadeOut(400, function() {
	$('#sort').show();
	$('#discussion-header .new-post-message').hide();
      });
      return false;
    })
    $('#footer .new-post-button a').click(function(event) {
      $('#footer .new-post-message div').fadeIn(800).text('Du må logge inn for å poste').animate({borderWidth: '0'}, 1600).fadeOut(400);
      return false;
    })
  }
  else {
    $('.new-post a').click(function() {
      $(this).addClass('onclick');
    });
  }

  $('textarea#comment').autogrow();
  
  $("#commentform").validate({

       rules: {
	 comment: "required"
       },
       messages: {
	 comment: "Kommentarfeltet er tomt."
       },
       errorLabelContainer: '#messagebox',
       wrapper: 'li',
       invalidHandler: function(form, validator) {
	 $('#respond form #submit').removeClass('onclick');
       }
     });

    $("#user_login").focus(function() {
        if(this.value=='Brukernavn') {
            this.value='';
            $(this).removeClass('blur');
        }
    });
    $("#user_login").blur(function() {
        if(this.value=='') {
            this.value='Brukernavn';
            $(this).addClass('blur');
        }
    });
    $("#user_pass").focus(function() {
        if(this.value=='Passord') {
            this.value='';
            $(this).removeClass('blur');
        }
    });
    $("#user_pass").blur(function() {
        if(this.value=='') {
            this.value='Passord';
            $(this).addClass('blur');
        }
    });

});

