free = { flow:'', leadvilleastronaut:'', sirfiggothick:'', disorganizedcockroach:'', psychoticelephant:'', serpentknotform:''};
morefree = { checkerhat:'', kozmicjaggedhands:'', letterslaughing:'', locus:'', mrphone:'', mattts:'',pixel:'', underlapped:'', everydayfont:'', jrandomc:'', ovial:'', rubbingfont:'', tobecontinued:'', bananapeels:'', ripple:'', swisscheesed:'', bitmaps:''};
$.extend(free, morefree);
bitmaps = {gothic:'', worldssmallestbitmap:'', cowboy:'', fixedsys:'', shaston:''};

curPage = '';
baseTitle = '';
downloaded = false;
fadeDuration = 250;

function postShow(newPage) {
  if (newPage in morefree) {
    show_more_free();
  }

  setTimeout( function() { $('#'+newPage + ' a.buy').animate({'opacity':'1'}, fadeDuration*2); }, fadeDuration*2);

  var t = $('#'+newPage).attr('title');
  if (t) {
    t += ' • ';
  }
  document.title = t + baseTitle;
  pageTracker._trackEvent('transition', newPage);
}

function postHide(oldPage) {
  $('#'+oldPage + ' a.buy').css('opacity', '0');
}

function setMinHeight(height) {
  $('#sidebar').css('min-height', height);
  $('#body').css('min-height', height);
}


function transition(oldPage, newPage) {
  if (newPage in free) {
    $('#'+newPage).append($('#donate').remove());
  }
  if (! oldPage) {
    oldPage = 'home';
  } else if (! newPage) {
    newPage = 'home';
  }

  fadeBetween(oldPage, newPage);
}


function fadeInFadeOut(oldPage, newPage) {
  // fade the old page out and then fade the new page in.
  $('#'+oldPage).fadeOut(fadeDuration, function () { 
    $('#'+newPage).fadeIn(fadeDuration);
    postShow(newPage);
    setMinHeight($('#'+newPage).css('height'));
  });
}


function fadeBetween(oldPage, newPage) {
  // fade the old page out and the new page in at the same time.
  $('#'+oldPage).css('z-index', 0).fadeOut(fadeDuration*2, function () {
    postHide(oldPage);
  });
  $('#'+newPage).css('z-index', 1).fadeIn (fadeDuration*2, function () {
    postShow(newPage);
  });
  setTimeout( function () {
    setMinHeight($('#'+newPage).css('height'));
  }, fadeDuration/2);
}


function dumbLoopStart() {
    dumbLoop = setInterval(function () {
      var oldPage = curPage;
      if (location.hash.length > 1){
        var newPage = location.hash.slice(1);
        if (newPage in bitmaps) {
          newPage = 'bitmaps';
        }
        if (newPage != curPage) {
          // it's different
          if ($('#'+newPage).length) {
            // it's found
            transition(oldPage, newPage);
            curPage = newPage;
          } else {
            // it's not found
            transition(oldPage, '');
            // TODO: location.replace() this page from browsa history
            location.hash = '';
            console.error("Couldn't find " + newPage + ", removing.")
          }
        } else {
          ; // it's not different, do nothing
        }
      } else {
        curPage = '';
        if (oldPage) {
          transition(oldPage, '')
        }
      }
    }, 100);
}


function hide_more_free() {
  $('#more_free ~ *').fadeOut(fadeDuration, function () { $('#more_free').show(); });
}


function show_more_free() {
  $('#more_free').hide();
  $('#more_free ~ *').fadeIn(fadeDuration);
}


function click_download(a) {
  downloaded = true;
  $('#donate').fadeIn(fadeDuration);
  setMinHeight($('#'+curPage).css('height'));
  pageTracker._trackEvent('download', location.hash.slice(1));
}


function keyUp(e) {
  if (e.which == 39) { // right arrow
    var next = $('#'+ (curPage?curPage:'home') +' ~ div').attr('name');
    if (next) {
      location.hash = next;
    } else {
      location.hash = '';
    }
    e.preventDefault();
  } else if (e.which == 37) { // left arrow
    history.go(-1);
  }
}

function submit_paypal(){
  // I don't know why but I can't seem to bind this using jQuery
  pageTracker._trackEvent('donate', 'paypal');
}

function track_feed(){
  pageTracker._trackEvent('subscribe', 'atom')
}

$(document).ready(function (){
  $('a.contact').attr('href', 'mailto:'+'matt'+unescape('%2d')+'typography'+unescape('%40')+'theory'+unescape('%2e')+'org');
  $('a.download').click(click_download);
  baseTitle = document.title;
  $('#more_free ~ *').hide();
  $('#splash').fadeOut(fadeDuration * 4);
  $('#content').fadeIn(fadeDuration * 8);
  dumbLoopStart();

  if ($.browser.msie) {
    $('#msie').show();
    show_more_free();
   } else {
    $('#logo').replaceWith('<iframe id="logo" src="images/logo.svg"></iframe>');
    $('#rightarrow').show();
    $(document).keyup(keyUp);
  }
});
