var veil;
var posterDisplay = 'back';
var currentYear = 2011;

$(document).ready(function(){
  veil = $("#tijdlijn_veil");
  
  $("#toggleFrontBack").click(function(){
    if(posterDisplay == 'back') {
      posterDisplay = 'front';
      $("#toggleFrontBack").html("Toon achterkant");
    }
    else {
      posterDisplay = 'back';
      $("#toggleFrontBack").html("Toon voorkant");
    }
    
    getPoster();
  });
  
  bindBackEvents();
  
  bindYearClickHandlers();
  
  $("#tijdlijn_popup .order").click(function(){
    $("#contact_link").click();
    $("textarea[name=reactie]").html("Ik wil graag een poster bestellen.\n\nJaartal: "+currentYear+"\n\nAantal: 1");
    return false;
  });
});

function getPoster() {
  $.getJSON('/ajax/tijdlijn.php', {"type": 'poster', "display": posterDisplay, "year": currentYear},
  function(data) {
    $("#poster").html(data.html);
    
    bindBackEvents();
  });
}

function bindYearClickHandlers() {
  $('.navigation span:not(.active)').bind('click', function(){
    $('.navigation span').removeClass('active')
      .unbind('click');
    
    $(this).addClass('active');
    
    // Switch year
    currentYear = $(this).attr('value');
    
    $('#downloadPDF').attr('href', '/pdf/Cultuur op de kaart - Poster ' +currentYear+ '.pdf');
    
    bindYearClickHandlers();
    
    getPoster();
  });
}

function bindBackEvents() {
  // Attach click event listener to "read more"
  $("#tijdlijn_popup .tijdlijn_popup_overview").click(function(evt){
    var size = '';
    // Set page id
    if (evt.target.className.indexOf('large') != -1) { size='large'; }
    var id = evt.target.href.substr(evt.target.href.lastIndexOf('#') +1, evt.target.href.length);
    openPopupOverview(id,size);
    return false;
  });
}

function openPopupOverview(id,size) {
  $.getJSON('/ajax/tijdlijn.php', {"type": 'overview', "id": id, "year": currentYear},
  function(data) {
    veil.show();
    var readmore = data.readmore ? '<a href="#" class="more" title="Lees meer"><span>&nbsp;</span></a>' : '';
    var className = data.readmore ? '' : 'noflap';
    if (size!='') { className = className == '' ? size : className + ' ' + size; }
    $("body").append('<div id="tijdlijn_popup_overview" class="'+className+'"><div class="ie6fix"><div class="close_btn"></div><div class="actions"> <a href="#" class="download">Download als PDF</a></div>'+data.html+readmore+'</div></div>');
    
    // Attach click event listener to "read more"
    $("#tijdlijn_popup_overview a.more").click(function(){
      openPopupDetail(id);
      return false;
    });
    
    // Attach click event listener to "PDF download"
    $("#tijdlijn_popup_overview .actions a.download").click(function(){
      window.open('/pdf/tijdlijn/'+currentYear+'/'+id+'.pdf','Download'); 
      return false;
    });
    
    // Attach click event listener to "close"
    $("#tijdlijn_popup_overview .close_btn").click(function(){
      veil.hide();
      $(this).parent().parent().remove();
    });
  });
}

function openPopupDetail(id) {
  $.getJSON('/ajax/tijdlijn.php', {"type": 'detail', "id": id, "year": currentYear},
  function(data) {
    $("body").append('<div id="tijdlijn_popup_large"><div class="ie6fix"><div class="close_btn"></div>'+data.html+'</div></div>');
    
    $("#tijdlijn_popup_large .close_btn").click(function(){
      $(this).parent().parent().remove();
    });
  
  // RF: scrollable aanroepen voor de poster bijlages
  $(".scrollable").scrollable({size: 1, clickable: false});

  });
  return false;
}
