/*!
 * BloomBox Social Networking and UGC Toolkit
 * Copyright 2006-2010, Mint Digital Limited, All rights reserved.
 */

/*global  window, alert, confirm,
          BB, $ */



/*** Global ***/

$(function(){
  // if we just posted a comment to a post and we have errors, go straight to the errors
  if($("#blog-posts-show")[0] && $("#blog-post-comment-form .error")[0]){
    window.location = window.location.href + "#blog-post-comment-form";
  }

  // Convert static timestamps to relative timestamps
  $('.datetime[title]').each(function(){
    var $this     = $(this),
        date      = new Date($this.attr('title')),
        humanTime = $.trim($this.html());
    $this.relativeTimestamp(date);
    $this.attr('title', humanTime);
  });

  // Disable submit buttons on click to prevent double-submit
  $('form').submit(function(ev){
    if(ev.isDefaultPrevented()){ return; }

    var $submitWrapper = $(this).find('div.submit');
    if($submitWrapper[0]){
      $submitWrapper.find('input[type=submit], button[type=submit]').
        attr('disabled', 'disabled');
      $submitWrapper.find('span.alt').fadeOut('fast');
    }
  });

  // Auto-focus first visible input in all forms
  $('div#site-body form input:visible:first').focus();

  // Open <a href="..." rel="external">External link</a> in a new window
  $('a[rel=external]').live('click', function(ev){
    window.open(this.href);
    ev.preventDefault();
  });

  $(window).bind('popstate', function(){
    // Reload if path and title don't match. This can happen through the use
    // of `history.pushState` and the back button, e.g., on people pages.
    var ppc = window.personPageContent;
    if( ppc && (location.pathname === ppc.path) !== // xor
        (document.title === ppc.title)){
      // Reload without preserving last scroll offset, which may have been
      // incorrect due to `div.person-page` scrolling.
      location.reload();
    }
  });

  function updateHistory(title, path){
    if(history && history.pushState){
      // Push the state of the actual page (root or /people) that will be
      // scrolled to
      history.pushState(null, title, path);
    }
    document.title = title;
  }

  /*** People ***/
  $('.person-page .back-to-site a').live('click',function(ev){
    var ppc = window.personPageContent,
        scrollTo = 0;
    updateHistory(ppc.title, ppc.path);

    if(ppc.scrollOffset && ppc.scrollOffset > 0){
      scrollTo = ppc.scrollOffset;
    } else {
      scrollTo = $('#container').offset().top;
    }
    $('html, body').animate({
      scrollTop: scrollTo
    }, 500);
    ev.preventDefault();
  });
  function showPersonPage(e){
    var $personPage = $('div.person-page'),
        $person     = $(this);
    if(!$personPage.length){
      window.personPageContent.scrollOffset = $(window).scrollTop() + $(window).height();
      $personPage = $('<div class="person-page" style="height:0;"></div>');
      $personPage.prependTo('body');
      $personPage.animate({ height: "100%" }, 500);
    } else {
      window.personPageContent.scrollOffset = $(window).scrollTop();
      $personPage.html('');
    }
    $('html, body').animate({ scrollTop: 0 }, 500);
    $.ajax({
      url: $person.attr('href') + '.js',
      success: function(data){
        var $cardHtml = $(data.card);
        $personPage.append($cardHtml);
        updateHistory(data.title, data.path);
      },
      dataType: 'json'
    });
    e.preventDefault();
  }
  BB.bodyID('people-index', function(){
    $('a.person-profile-link').bind('click', showPersonPage);
  });
});

