// Markup-based unobtrusive comprehensive DOM-ready execution by Paul Irish and Jason Garber
// http://www.viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution/

CONTROLLERS = { 
	common: { // controllers correspond to body data-controller attribute
		init: function() { // functions correspond to body data-action attributes
			
		}
	},
  magazines: {
    init: function() {
      $('#thumbs a:first').addClass('current');
      $('#thumbs a').click(function () {
        var $this = $(this),
            post_id = $this.attr('href').substr(1);

        $('#thumbs a').removeClass('current');
        $this.addClass('current');

        $('#legende span:first').html(posts[post_id]['title']);
        $('#legende span:eq(2)').html(posts[post_id]['poste']);
        $('#legende span:last').html(posts[post_id]['client']);
        $('#ISSUU').html(posts[post_id]['issuu']);

        return false;
      });

      $('#arrows .prev').click(function () {
        var prev = $('#thumbs a.current').parent().prev().find('a');
        if (prev.length)
          prev.click();

        return false;
      });

      $('#arrows .next').click(function () {
        var next = $('#thumbs a.current').parent().next().find('a');
        if (next.length)
          next.click();

        return false;
      });      
    }
  },
  identites_visuelles: {
    init: function() {
      $('#thumbs').scrollbarPaper();
      $('#picture img:first').show().addClass('notHidden');

      $('#thumbs a:first').addClass('current');
      $('#thumbs a').click(function () {
        var $this = $(this),
            post_id = $this.attr('href').substr(1);

        $('#thumbs a').removeClass('current');
        $this.addClass('current');

        $('#proj').fadeOut(400, function () {
          $('#picture img.notHidden').hide().removeClass('notHidden');
          $('#picture img[data-post-id='+post_id+']').show().addClass('notHidden');
          $('#legende span:last').html(posts[post_id]['title']);
          $(this).fadeIn(400);
        });

        return false;
      });

      $('#arrows .prev').click(function () {
        var prev = $('#thumbs a.current').parent().prev().find('a');
        if (prev.length)
          prev.click();

        return false;
      });

      $('#arrows .next').click(function () {
        var next = $('#thumbs a.current').parent().next().find('a');
        if (next.length)
          next.click();

        return false;
      });
    }
  },
  editions: {
    init: function() {
      $('#thumbs').scrollbarPaper();
      $('#picture img:first').show().addClass('notHidden');

      $('#thumbs a:first').addClass('current');
      $('#thumbs a').click(function () {
        var $this = $(this),
            post_id = $this.attr('href').substr(1);

        $('#thumbs a').removeClass('current');
        $this.addClass('current');

        $('#proj').fadeOut(400, function () {
          $('#picture img.notHidden').hide().removeClass('notHidden');
          $('#picture img[data-post-id='+post_id+']').show().addClass('notHidden');
          $('#legende span:first').html(posts[post_id]['intitule']);
          $('#legende span:eq(2)').html(posts[post_id]['poste']);
          $('#legende span:last').html(posts[post_id]['title']);
          $(this).fadeIn(400);
        });

        return false;
      });

      $('#arrows .prev').click(function () {
        var prev = $('#thumbs a.current').parent().prev().find('a');
        if (prev.length)
          prev.click();

        return false;
      });

      $('#arrows .next').click(function () {
        var next = $('#thumbs a.current').parent().next().find('a');
        if (next.length)
          next.click();

        return false;
      });
    }
  }
};

UTIL = {
  exec: function(controller, action) {
    var ns = CONTROLLERS,
        action = (action === undefined) ? "init" : action;

    if (controller !== "" && ns[controller] && typeof ns[controller][action] == "function") {
      ns[controller][action]();
    }
  },

  init: function() {
    var body = document.body,
        controller = body.getAttribute("data-controller"),
        action = body.getAttribute("data-action");

    UTIL.exec("common");
    UTIL.exec(controller);
    UTIL.exec(controller, action);
  }
};

$(document).ready(UTIL.init);
