/*** JS utilities ***/
// TODO: Switch to utilities.js

function bodyID(id, fn){
  $(function(){ if($('body#'+id)[0]){ fn(); } });
}

$.ajaxSetup({
  // For compatibility with Rails' `respond_to`
  beforeSend: function(xhr){
    xhr.setRequestHeader('Accept', 'text/javascript');
  }
});

/*** jQuery extensions ***/
// TODO: Switch to utilities.js

// $.browser.chrome=/chrome/.test(navigator.userAgent.toLowerCase());

(function($){
  jQuery.fn.newWindowOnClick=function(){
    return $(this).filter('a[href]').click(function(ev){
      window.open(this.href); ev.preventDefault();
    });
  }
})(jQuery);



/*** Behaviors ***/

$(function(){
  $('a[rel=external]').newWindowOnClick();
});

bodyID('posts-index', function(){
  // Infinite scrolling
  var $posts = $('div.posts'),
      $pagination = $posts.find('div.pagination'),
      $nextLink = $pagination.find('a.next_page');
  if(!$nextLink[0]){ return; }
  var nextURL = $nextLink.attr('href'),
      isLoading = false,
      $window = $(window),
      pxFromBottom = 2000;
  $window.scroll(function(ev){
    if(!isLoading && $(document).height() - $window.height() - $window.scrollTop() <= pxFromBottom){
      isLoading = true;
      $posts.find('div.loading').show();
      $.get(nextURL, {}, function(data, status){
        $pagination.remove();
        $posts.append(data);
        $pagination = $posts.find('div.pagination');
        $nextLink = $pagination.find('a.next_page');
        $posts.find('div.loading').hide();
        if($nextLink[0]){
          nextURL = $nextLink.attr('href');
          isLoading = false;
            // If there's another page, reset flag to allow update
        }
      }, 'script');
    }
  });
});

/*
$(function(){
  // Fading navigation
  var liHSpace=parseInt($('#site-header ul li').width(),10)+
    parseInt($('#site-header ul li').css('margin-right'),10);
  $('#site-header ul li:not(.current) a').each(function(){
    var pos=$(this).position();
    if(pos.left%liHSpace==0){
      // Correct pos.left if browser miscalculates as 0
      pos.left+=($(this).parent('li').width()-$(this).width())/2;
    }
    $(this).css({
      position:'absolute',
      left:pos.left+'px', top:pos.top+'px',
      'z-index':9
    }).after($(this).parent('li').html());
    $(this).next('a').addClass('hover').css({
      position:'absolute',
      left:pos.left+'px', top:pos.top+'px',
      display:'none',
      'background-position':'0 -44px',
      'z-index':5
    });
  });
  $('#site-header ul li:not(.current) a:not(.hover)').hover(function(){
    var $h=$(this).next('a.hover');
    if($h.css('opacity')==1){ $(this).css('opacity',0.01); }
    $h.show();
  },function(){
    $(this).fadeTo('medium',1).next('a.hover').fadeOut('slow');
  });
});
*/

bodyID('projects-show', function(){
  setupZoom(); // FancyZoom
  
  // Convert to rounded corners. The markup contains <img /> tags to remain
  // semantic, since these images are content.
  $('div.screenshots ul li a').each(function(){
    var $this = $(this),
        $img = $this.children('img');
    $this.css({
      width:  $img.attr('width'),
      height: $img.attr('height'),
      'background-image':       'url(' + $img.attr('src') + ')',
      'border-radius':          '8px',
      '-webkit-border-radius':  '8px',
      '-khtml-border-radius':   '8px'
    });
    $img.hide();
  });
});
