if ( typeof(console) == 'undefined' ) {
  console = { log : function(){}, error : function(){} };
}


// util!

function binding(method, scope) {
  for (var i = 2, bound_args = []; i < arguments.length; i++)
    bound_args.push(arguments[i]);

  return function() {
    for (var i = 0, args = []; i < arguments.length; i++)
      args.push(arguments[i]);
    return method.apply(scope, bound_args.concat(args));
  };
}

// Like jQuery.fn.serializeArray, but returns a hash.
jQuery.fn.serializeObject = function() {
  var data = {};
  jQuery.each(this.serializeArray(), function(){
    data[this.name] = this.value;
  });

  return data;
};


// lights

function lights_down() {
  $('body').addClass('lights-down');
  $('#sidebar, #shadow').fadeIn();
  $.cookie('lights_are_down', true, { path : '/' });
};

function lights_up() {
  $('body').removeClass('lights-down');
  $('#sidebar, #shadow').fadeOut();
  $.cookie('lights_are_down', false, { expires : -1, path : '/' });
}

$(function() {
  $('#wrapper').before(
    '<div id="shadow"><div id="screen"></div><div id="foreground"></div><img src="/images/big_shadow.png"></div>'
  );

  if ( $.cookie('lights_are_down') ) {
    $('#sidebar, #shadow').show();
    $('body').addClass('lights-down');

    $('#site-title').toggle(lights_up, lights_down);
  } else {
    $('#site-title').toggle(lights_down, lights_up);
  }
});

// comments

var Commenter = {
  setup : function() {
    var f = $('#comment_form');
    f.children('a').toggle(
      binding(this.open_popup, this),
      binding(this.close_popup, this)
    );
    f.submit(binding(this.submit, this));
  },
  open_popup : function(e) {
    $('#comment_form .popup').fadeIn();
    $('body').bind('click:comment_form', binding(this.close_popup, this));
    return false;
  },
  close_popup : function(e) {
    $('#comment_form .popup').fadeOut();
    $('body').unbind('click:comment_form');
    return false;
  },

  // ajax submit/callbacks
  submit : function(e) {
    var form = $(e.target);
    var action = form.attr('action');
    $.ajax({
      url : action,
      type : 'POST',
      data : form.serialize(),
      dataType : 'json',
      error : binding(this.submit_error, this),
      success : binding(this.submit_success, this)
    });
    return false;
  },
  submit_error : function(xhr, status, error) {},
  submit_success : function(data, status) {
    $('#comment_form .error').fadeOut();

    if ( data.errors ) {
      $.each(data.errors, function(attr, val){
	$('#comment_form .' + attr + '_field .error').fadeIn();
      });
    } else {
      this.close_popup();
      var c = $(data.html).appendTo('#comments').hide();
      setTimeout(binding(c.fadeIn, c), 500);
    }
  }
};

$(function(){ Commenter.setup() });


// code syntax highlighting with shjs
$(function(){
  sh_highlightDocument('/js/sh_lang/', '.js');
});

// email de-obfuscation
(function(){
  var key = "ffff28cf71bc1259dc6436af2f3776af63d1ad635f47d6d93a40ee6133d5c696807ffa3324a0535d715b3867a86538ecf307848c6135c156c2bdce581ee0408e";

  //function obfuscator(anchor){
  //  var href = $(anchor).attr('href');
  //  var codes = [], val;

  //  for (var i = 0; i < href.length; i++) {
  //    val = (href.charCodeAt(i) + key.charCodeAt(i % key.length)) % 128;
  //    codes.push(val < 0 ? val + 128 : val);
  //  }

  //  $(anchor).attr('href', 'obf:' + codes.join('-'));
  //}

  function deObfuscator(anchor){
    var encrypted = $(anchor).attr('href').replace(/obf:/, '').split('-');

    var codes = [], val;
    for (var i=0; i < encrypted.length; i++) {
      val = (parseInt(encrypted[i]) - key.charCodeAt(i % key.length)) % 128;
      codes.push(val < 0 ? val + 128 : val);
    }

    var decrypted = String.fromCharCode.apply(null, codes);
    $(anchor).attr('href', decrypted);
    if ( $(anchor).text() == 'email' ) {
      $(anchor).text(decrypted.replace(/mailto:/, ''));
    }
  }

  $(function(){
    $('a[href^=obf:]').each(function(){ deObfuscator(this) });
  });
})();

