
//smooth scrolling

$(document).ready(function() {
  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }

  var locationPath = filterPath(location.pathname);
  $('body').click(function(event) {
    var $tgt = $(event.target);
    var $link = ($tgt.is('a') && $tgt) || ($tgt.parents('a').length && $tgt.parents('a:first')) || null  ;
    //stop if it's not a link
    if (!$link || $link.parent().is('.reply')) { return; }
  
    var link = $link[0];
    var thisPath = filterPath(link.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == link.hostname || !link.hostname)
    && link.hash.replace(/#/,'') ) {
      event.preventDefault();
      var $target = $(link.hash), target = link.hash;
      if ($target.length) {
        var targetOffset = $target.offset().top;

        $('html, body').animate({scrollTop: targetOffset}, 1600, function() {
          location.hash = target;
        });
      }
    }

  });
  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
  });
});


// search form label thing 
$(document).ready(function() {

  var $s = $('#s');
  var searchLabel = $('#searchform label').hide().text();

  function restoreSearch() {
    if ($s.val() == '') {
      $s.val(searchLabel);
    } 
    if ($s.val() == searchLabel) {
      $s.addClass('faded');
    }
  }
  
  restoreSearch();
  
  $s.focus(function() {
    $s.removeClass('faded');
    if ($s.val() == searchLabel) {
      $s.val('');
    }
  })
  .blur(restoreSearch);
  
});


//google analytics
$(window).bind('load', function(event) {
  $.getScript('http://www.google-analytics.com/ga.js', function() {
    try {
    var pageTracker = _gat._getTracker("UA-1941862-1");
    pageTracker._initData();
    pageTracker._trackPageview();
    } catch(err) {
      if (window.console && console.log) { console.log(err);}
    }
  });  
});

// auto page contents
// $(document).ready(function() {
//   var $h2links = $('#content h2:has(a)');
//   if ( $h2links.length > 1) {
//     $('<div id="page-contents"></div>')
//       .prepend('<h3>Page Contents</h3>')
//       .append('<div></div>')
//       .prependTo('body');
//       
//     var thisId = '';
//     $h2links.each(function(index) {
//       $this = $(this);
//       thisId = this.id;
//       $this
//         .clone()
//         .find('a')
//           .attr({
//             'title': 'jump to ' + $this.text(), 
//             'href': '#' + thisId
//           })
//         .end()
//         .attr('id', 'pc-' + index)
//         .appendTo('#page-contents div');
//     });
//       
//     $('#page-contents h3').click(function() {
//       $(this).toggleClass('arrow-down')
//         .next().slideToggle('fast');
//     });
//   }
// });


/** ===reply to comment
************************************************************
************************************************************/

$(document).ready(function() {
  var $respond = $('#respond'),
      $closeReply = $('<a href="#" id="close-reply">close and cancel</a>'),
      $respondPlaceholder = $('<div id="respond-placeholder"></div>');
      
  $(document).click(function(event) {
    var $eventTarget = $(event.target);
    if ($eventTarget.parent().is('.reply')) {
    
      var replytocom = event.target.search.split('=');
      if ((/replytocom/).test(replytocom[0])) {
        
        var $parentDiv = $eventTarget.parent().parent(),
            replytoname = $parentDiv.find('cite.fn').text(),
            rHeight = $respond.height(),
            rWidth = $respond.width();
        if (!$eventTarget.data('position')) {
          $eventTarget.data('position', {
            left: event.pageX,
            top: event.pageY + 10,
            width: rWidth + 'px'
          });
        }

        $respondPlaceholder.height(rHeight);
        $respond
        .hide()
        .addClass('replying').css($eventTarget.data('position'))
        .fadeIn(200);
        $closeReply
        .appendTo($respond)
        .attr('title', 'close and cancel reply to ' + replytoname)
        .click(function() {
          $(this).remove();
          $respondPlaceholder.remove();
          $respond.removeClass('replying').children('h3:first').text('Leave a Reply');
          $('#comment_parent').val('');
          return false;
        });
        $respondPlaceholder.insertAfter('#respond');
        $respond.children('h3:first').text('Leave a Reply to ' + replytoname);
        $('#comment_parent').val(replytocom[1]);
        if (document.getElementById('author')) {
          $('#author')[0].focus();
        } else {
          $('#comment')[0].focus();
        }
        return false;
      }
    }
  });

});

$(document).ready(function() {
  if (typeof $.fn.socialize != 'undefined') {
    $('.post:first').socialize();
  }
});


// get IE to recognize <abbr> tag 
$(document).ready(function() {
  document.createElement('abbr');
});

