$(document).ready(function(){
   var swapTestimonials = function() {
      $('#praise').animate({opacity: 0.0}, 'default', function() {
         $('#praise .left .quote').html(testimonials[testimonials_index].text);
         $('#praise .left .author').html(testimonials[testimonials_index].author);
         testimonials_index = (testimonials_index+1) % testimonials.length;
         $('#praise .right .quote').html(testimonials[testimonials_index].text);
         $('#praise .right .author').html(testimonials[testimonials_index].author);
         testimonials_index = (testimonials_index+1) % testimonials.length;
         $('#praise').animate({opacity: 1.0});
         setTimeout(swapTestimonials, 7000);
      });
   };
   
   swapTestimonials();
   
   if ( window.history && history.pushState ) {
      historyedited = false;
      $(window).bind('popstate', function(e) {
         if ( typeof(popStateEventHandler) == 'undefined' || popStateEventHandler() == false ) {
            if ( historyedited ) {
               loadPage(location.pathname);
            }
         }
      });
      
      $('#next').click(function(e) {
         e.preventDefault();
         history.pushState(null, null, $(this).attr('href'));
         historyedited = true;
         loadPage($(this).attr('href'), 'next');
      });
      
      $('#prev').click(function(e) {
         e.preventDefault();
         history.pushState(null, null, $(this).attr('href'));
         historyedited = true;
         loadPage($(this).attr('href'), 'prev');
      });
   }
});

function setPopStateEventHandler(func) {
   popStateEventHandler = func;
}

function setNavigateAwayEventHandler(func) {
   navigateAwayEventHandler = func;
}

function loadPage(page, direction) {
   if ( typeof navigateAwayEventHandler != 'undefined' ) {
      navigateAwayEventHandler();
      navigateAwayEventHandler = undefined;
   }
   
   var transition = function(next) {
      if ( typeof(direction) != 'undefined' ) {
         $('#panel-container').animate({'top': '-100px'}, 100, function() { 
            $('#panel-container').animate({'top': '1000px'}, function() { 
             }); 
         });
         $('#main').animate({ 'margin-left': (direction == 'prev' ? '2000px' : '-2000px') }, 400, next );
      } else {
         next();
      }
   }
   
   transition(function() {
      $('#main-container').addClass('loading');
      
      $.getJSON('/ajax.php?page='+page, function(data) {
         $('#main').html(data.main);
         $('#panel').html(data.panel);
         if ( data.js != null ) {
            eval(data.js);
         }
         
         $('#main-container').removeClass('loading');
         
         $('body').attr('class', data.name);
         
         $('#prev').attr('href', '/' + data.navigation_prev[0]);
         $('#prev').html(data.navigation_prev[1]);
         $('#next').attr('href', '/' + data.navigation_next[0]);
         $('#next').html(data.navigation_next[1]);
         
         if ( typeof(direction) != 'undefined' ) {
            $('#panel-container').fadeIn();
            $('#main').css('margin-left', (direction == 'prev' ? '-2000px' : '2000px'));
            $('#main').animate({ 'margin-left': '0' }, 400);
            $('#panel-container').animate({'top': '-100px'}, function() { 
               $('#panel-container').animate({'top': '0px'}, 100); 
            });
         }
      });
   });
}
