var tobinMicrosite = tobinMicrosite || {};


tobinMicrosite.setPaths = function(rowNumber) {
  $('.row-' + rowNumber + ' .download-track').each(function() {
    // apply to all download links (in this row)
    pageTracker._trackPageview('/downloads/' + fileName);    
    
    var fileName = $(this).attr('href').replace('pdf/', '').replace('.pdf', '');
    
    $(this).clone().insertAfter(this).addClass('original').hide();
    $(this).addClass('thickbox').attr('href', 'download_popup.php?KeepThis=true&book=' + fileName +  '&TB_iframe=true&height=405&width=275');
    $(this).click(function() {
      tobinMicrosite.resetPaths(rowNumber);
      $.cookie('tobin_download_' + rowNumber, 1, {expires: 365243, domain: 'tobinproject.org', path: '/'});
    });
  }); 
}

tobinMicrosite.resetPaths = function(rowNumber) {
    $('.row-' + rowNumber + ' .original').show();
    $('.row-' + rowNumber + ' .thickbox').hide();
}

$(document).ready(function() {
  $('[rel=external]').attr('target', '_BLANK');
  
  $('#content .row').each(function() {
    var row = $(this).attr('class').replace('row row-', '');
    
    if (!$.cookie('tobin_download_' + row)) {
     tobinMicrosite.setPaths(row);
    }
  });
  
// the end
});
