(function($) {
  var FrontendRouter = Backbone.Router.extend({
    routes: {
      "/cimke/:slug": "tag",
      "/projekt/:slug": "project",
      "/ugyfel/:slug": "client",
      "/*slug": "page"
    },

    initialize: function(options) {
      this._referenceView = options.referenceView;
      this._pageView = options.pageView;
    },

    tag: function(slug) {
      var view = this;
      $.get('/cimke/' + slug)
        .success(function(data){
          view._referenceView.render(data);
        });
    },

    project: function(slug) {
      var view = this;
      $.get('/projekt/' + slug)
        .success(function(data){
          view._referenceView.render(data);
        });
    },

    client: function(slug) {
      var view = this;
      $.get('/ugyfel/' + slug)
        .success(function(data){
          view._referenceView.render(data);
        });
    },

    page: function(slug) {
      var view = this;
      $.get('/' + slug)
        .success(function(data){
          view._pageView.render(data);
        });
    }
  });

  var BlockView = Backbone.View.extend({
    render : function(content) {
      content = $(content);
      content.find('a').replaceLinks();
      $(this.el).html(content.html());
      return this;
    }
  });

  $.fn.replaceLinks = function() {
    var host = window.location.protocol + '//' + window.location.host;
    var blacklisted = ['thumbnails'];

    this.each(function () {
      var link = $(this);
      var isNotBlacklisted = _.isEmpty(_.intersection(blacklisted, link.attr('href').split('/')));
      var isLocalAbsolute = 0 === link.attr('href').indexOf(host);

      if (isNotBlacklisted && -1 === link.attr('href').indexOf('#') && (!link.attr('href').match(/^(:?http|www)/) || isLocalAbsolute))
      {
        if (isLocalAbsolute)
        {
          var base = host + window.location.pathname.replace(/\/+$/, '');
          link.attr('href', link.attr('href').replace(new RegExp('^'+base), base + '#'));
        }
        else
        {
          link.attr('href', '#' + link.attr('href').replace(/^\/?.+\.php/, ''));
        }
      }
    });

    return this;
  };

  $(document).ready(function(){
    $('.block > h1').live('click', function () {
      var clicked = $(this);
      clicked.next('.content').slideToggle('slow', function() {
        clicked.find('span').toggleClass('closed');
      });
    });

    $('ul.gallery li > a').live('click', function (event) {
      event.preventDefault();

      var clicked = $(this);
      var target = $('div#img_container > img');

      target.attr('src', clicked.attr('href'));
      clicked.closest('ul').find('li.active').removeClass('active');
      clicked.closest('li').addClass('active');
    });

    $('#footer ul.menu a').live('click', function () {
      $.scrollTo('div.block.text', {duration: 1000});
    });

    $('ul.tagcloud a').replaceLinks();
    $('div.block.references a').replaceLinks();
    $('#footer ul.menu a').replaceLinks();

    var referenceView = new BlockView({el: "div.block.references"});
    var pageView = new BlockView({el: "div.block.text"});
    new FrontendRouter({referenceView: referenceView, pageView: pageView});
    Backbone.history.start({root: window.location.pathname});
  });
})(jQuery);
