$( document ).ready( function() {
  

   /* ### VISUAL FX ### */

   /* logo animation */

   $("#logo a").hover( function() {
     $(this).children(".paperplane").animate({"marginTop":"-12px","marginLeft":"-12px","paddingRight":"12px","paddingBottom":"12px"},100);
   }, function() {
     $(this).children(".paperplane").animate({"marginTop":"0px","marginLeft":"0px","paddingRight":"0px","paddingBottom":"0px"},400);
   });

  /* navigation animation */

  $("ul#menu-navigation li").hover( function(e) {
     e.preventDefault();
     $(this).children("ul").fadeIn(400);
  }, function(e) {
     e.preventDefault();
     $(this).children("ul").fadeOut(400);
  } );

  /* ### SCROLLING NAVIGATION ### */

  classes = $("#menu-navigation").closest("div").attr("class");
  nav_offset = $("#menu-navigation").offset();

  $("#nav-placeholder").css({ "height": $("ul#menu-navigation").height() })

  $(document).bind( "scroll" , function() {


    place_plane()


    if( $(window).scrollTop() > nav_offset.top ) {
        $("#nav-placeholder").show();
        $("#menu-navigation").closest("div").attr("class", classes + " scrolling" );
        $("#badge-newsletter").addClass("scrolling");
        
    } else {
        $("#menu-navigation").closest("div").attr("class", classes );
        $("#nav-placeholder").hide();
        $("#badge-newsletter").removeClass("scrolling");
    }


  } );

  /* ### TYPOGRAPHY ### */

  /* detect browsers that use letter-spacing < 1px but > 0px as 0px */

  if( $("body").css("letterSpacing") != "0.6px" ) $("body").css({"letterSpacing":"1px"});
  
  
  
  /* ### CROSS-BROWSER ### */
  
  
  if( $.browser.webkit ) {
  	/* alert( $.browser.version ); */
  	browser_v = $.browser.version.split('.');
  	browser_v = browser_v[0] + '.' + browser_v[1];
  	/* alert( browser_v ); */
  	if( browser_v < 533.16 ) { 
  	  /* alert("safari 4"); */
  	  $("html").removeClass("rgba").addClass("no-rgba");
  	  }
  }
  
  



  /* ### FANCYBOX ### */




    $(".imagelist a, a.fbox").fancybox({
      'overlayColor' : '#000',
      'titlePosition' : 'inside'
    });


  /* ### ANACHRONISM ### */

  $("#anachronism a").click( function(e) {
    e.preventDefault();

    $('html, body').animate({scrollTop:0}, 3000, 'easeInOutCirc');   /* let it fly! */

  });


  /* ### SPLASH SCREEN ### */


 $(" #tagline a").click( function(e) {
 
   e.preventDefault();
   show_splash();

   window.setTimeout("hide_splash('slow')", 2000);

 } );


 $("#flickr").bind("click", function() {
   hide_splash();
 });



  if( !$("body").hasClass("nosplash") ) {
    window.setTimeout("hide_splash('slow')", 3000);
  }

  /* ### PAPER PLANE ### */

  $plane = $("#plane");

  w_height = $( window ).height();
  d_height = $( document ).height();
  init_left = parseInt( $("#plane").css("marginLeft") );
  init_top = parseInt( $("#plane").css("top") );

  yway = w_height - init_top - $plane.height() - 300 ;

  xway = 816;

  // alert( "$(document).height() : " + $(document).height() + "\n" +
  // "$( window ).height() : " + $( window ).height() );

  /* ### POPUP ### */

  $("a[rel=popup]").fancybox({
		'width'				: '35%',
		'height'			: '75%',
        'autoScale'     	: false,
		'type'				: 'iframe',
        'showNavArrows'     : false
	});



} );

var percentage;

function place_plane() {
  percentage = $( window ).scrollTop() / ( d_height - w_height );

  p_left = init_left + ( xway  * percentage );
  p_top = init_top + ( yway * percentage );
  
  $plane.css({"marginLeft" : p_left + "px", "top" : p_top + "px" })

}



function hide_splash(speed) {
  if( speed == 'slow') { sp = 1000; } else { sp = 300; }
  $("#flickr").fadeOut(sp, function() { $( this ).css({"marginTop":"-200%"}) });
}
function show_splash() {
  $("#flickr").fadeIn(300).css({"marginTop":"0%"});
}
