var moma = {
  selected: 'index'
};

moma.items = [
  'index',
  'numeri-in-corso',
  'press',
  'dove-trovare-moviement',
  'links',
  'contatti'
];

moma.checkImage = function( image ) {
  if ( image != undefined )
    return magazine.image_path;
  else
    return '/magazines/cover-placeholder.png';
}

moma.select = function( element ) {
  moma.selected = element.id;

  $('.menu > li > a').each( function(i, e) {
    $(e).removeClass('selected');
  });
  
  $( '#' + element.id ).addClass( 'selected' );

  if ( moma.selected != 'index' ) {
    $('.header .menu').hide();
    $('.footer .links').hide();
  } else {
    $('.header .menu').show();
    $('.footer .links').show();
  }
};

moma.update = function( e ) {
  // select current item
  moma.select( e );

  // FIXME: save it for the first time in LocalStorage
  //        then get it from there
  // XXX: only for static items
  $.ajax({
    url: e.href + '?data',
    success: function( data ) {
      $('.content').html( data );
      $('.content').removeAttr('class').addClass('content ' + e.id );
  
      moma.onEveryLoad();
    }
  });
};

moma.onEveryLoad = function() {
  // click on new magazine
  $('#new-magazine').click( function(ev) {
    moma.update( { id: 'numeri-in-corso', href: '/numeri-in-corso' } );
  });

  // hide menu if on home
  if ( moma.selected == 'index' )
    $('.header .menu').hide();
  else
    $('.header .menu').show();
}

$(document).ready( function() {
  moma.selected = 'index';

  $('#logo').click( function() { window.location.replace('/'); } );

  moma.onEveryLoad();

  // navigation menu
  $('.header .menu > li > a').each( function(i, e) {
    $(e).click( function(ev) {
      ev.preventDefault();
      moma.update( e );
    });
  });

  // home menu
  $('.content .indexmenu > li > a').each( function(i, e) {
    $(e).click( function(ev) {
      ev.preventDefault();
      moma.update( e );
    });
  });



  $('.footer .links > li > a').each( function(i, e) {
    $(e).css('opacity', '0.1');
    
    $(e).mouseover( function() {
      $(this).stop().animate({ opacity: 1.0 }, 500);
    }).mouseout( function() {
      $(this).stop().animate({ opacity: 0.1 }, 500);
    });
  });

  // google
  var _gaq = [['_setAccount', 'UA-8814083-1'], ['_trackPageview']];
  (function(d, t) {
    var g = d.createElement(t), s = d.getElementsByTagName(t)[0];
    g.async = true;
    g.src = '//www.google-analytics.com/ga.js';
    s.parentNode.insertBefore(g, s);
  })(document, 'script');
});

