/**
 * E-NOISE theme script
 *
 * @author Lupo Montero
 * @author Sven Lito
 */

(function ($) {

var enoise = function () {
  this.initLeftColScrollFix();
  this.initTopDrawer();
  this.initFooter();

  $('#content_wrapper a').filter(function () {
    return (this.hostname && this.hostname !== location.hostname);
  }).addClass('external-link');

  $('#content .posts li:last').css('borderBottom', 'none');
};

enoise.prototype.initLeftColScrollFix = function () {
  var
    leftCol = $('#main-menu'),
    marginTop = parseFloat(leftCol.css('marginTop').replace(/auto/, 0)),
    top = leftCol.offset().top - marginTop;

  this.leftColFix = function () {
      var y = $(window).scrollTop();

      // whether that's below the nav
      if (y >= top) {
        leftCol.addClass('fixed');
      } else {
        leftCol.removeClass('fixed');
      }
    };

  $(window).bind('scroll', this.leftColFix);
  $('#main-menu ul li a').filter('.active, .parent').next('ul').show();
};

enoise.prototype.initTopDrawer = function () {
  var
    self = this,
    topDrawerContent = [],
    topDrawerHref = false,
    topDrawer = $('<div id="top-drawer">'),
    topDrawerStage = $('<div id="top-drawer-stage">'),
    topDrawerCloseBtn = $('<a href="#" id="top-drawer-close">close <span id="top-drawer-close-x">&times;</span></a>'),
    topDrawerLoad = function (url) {
      $(window).unbind('scroll', self.leftColFix);

      if (typeof topDrawerContent[url] === 'undefined') {
        $.ajax({
          url: url,
          async: false,
          success: function (data) {
            topDrawerContent[url] = data;
            topDrawerStage.html(data);
            Mashine.initToolTips('.tooltip');
            topDrawer.slideDown('slow');
          }
        });
      } else {
        topDrawerStage.html(topDrawerContent[url]);
        Mashine.initToolTips();
        topDrawer.slideDown('slow');
      }
    },
    topDrawerClose = function () {
      topDrawer.slideUp('slow');
      $(window).bind('scroll', self.leftColFix);
    };

  topDrawer.append(topDrawerStage);
  topDrawer.append(topDrawerCloseBtn);
  topDrawer.hide();
  $('#header').before(topDrawer);

  // Handle top drawer close button
  topDrawerCloseBtn.click(function (e) {
    e.preventDefault();
    topDrawerClose();
  });

  $('#top-tabs li a.toptab').bind('click', function (e) {
    var href = $(this).attr('href') + '?ajax=1';

    e.preventDefault();

    if (topDrawer.css('display') === 'none') {
      topDrawerLoad(href);
    } else if (href != topDrawerHref) {
      topDrawer.slideUp('slow', function () {
        topDrawerLoad(href);
      });
    } else {
      topDrawerClose();
    }

    topDrawerHref = href;
  });
};

enoise.prototype.initFooter = function () {
  var container = $('#latest-from-blog');

  $.ajax({
    url: 'api/content?parent_id=120&limit=1',
    success: function (data) {
      var
        post = data[0],
        html = '<h6><a href="' + post.slug + '">' + post.title + '</a></h6>' +
               '<p>' + post.excerpt.limitChars(120) + '</p>' +
               '<p>Posted by: ' + post.author + ' on ' + post.pub_date + '</p>';

      container.append(html);
    }
  });
};

$(document).ready(function () {
  var e = new enoise();
});

})(jQuery);

