$(document).ready(function(){

  if( !($.browser.msie && $.browser.version.substr(0,1)<=7)) {
  
    // Set the height each element should be when they're not the one moused-over
    $('#home a').data('collapsedHeight', "75px");
    $('#about a').data('collapsedHeight', "75px");
    $('#services a').data('collapsedHeight', "68px");
    $('#work a').data('collapsedHeight', "76px"); 
    $('#contact a').data('collapsedHeight', "72px"); 
    $('#lab a').data('collapsedHeight', "80px"); 
    
    // Set the height each element should return to on mouse-out
    $('#nav a').each(function(){
      $(this).data('expandedHeight', $(this).height());
      $(this).data('hoverHeight', $(this).height()+10);
      $(this).data('zindex', $(this).css('z-index'));
    });
    
    $('#nav li a').hover(
  
      // mouseover
      function(){   
        $(this).stop().animate({ height: $(this).data('hoverHeight') }, { easing:'swing', duration:'fast'});

        if( $.browser.msie ) {
          $(this).siblings("img").show();
        } else {
          $(this).siblings("img").css({ display: 'block', opacity: 0 })
          $(this).siblings("img").stop().fadeTo('fast', 1);
        }

        $(this).parent().siblings("li").find('a').each(function(){
          $(this).stop().animate(
            { height:$(this).data('collapsedHeight') },{ queue:false, easing:'easeOutBack'});
        });
      },
      
      // mouseout
      function(){
        $(this).stop().animate({ height: $(this).data('expandedHeight') }, { easing:'easeOutBack'} );
        $(this).siblings("img").stop().hide(); //fadeTo('fast', 0);
        $(this).parent().siblings("li").find('a').each(function(){
          $(this).animate(
            { height:$(this).data('expandedHeight') },{ easing:'easeOutBack' });
        });    
      }
    );
  
    $cloud_count = Math.floor($(document).width() / 250);
    
    $("#header").append("<div class='clouds' />");
    
    for( $i = 0; $i < $cloud_count; $i++) {
  
      $image = $('<img src="http://www.sitestrux.com/_display/images/cloud.png" class="cloud" />').css({
          left: ((100/$cloud_count) * $i) + "%",
          opacity: "." + (Math.floor(Math.random()*30) + 20),
          top: (Math.floor(Math.random()*100) + 10) - 40
        });
    
      $("#header .clouds").append($image);
    }
  } else {
    $("#header .container").addClass("cloud_fallback");
  } 
  
  cleanSubNav();
});

function cleanSubNav(){
  $("#entries_categories").append("<p>");
  $("#entries_categories li").each(function(){
    $("#entries_categories p").append($(this).html());
    $("#entries_categories ul").remove();
  });
  
  $("#entries_categories a:not(#entries_categories a:last)").each(function(){
    $(this).html( $(this).html() + ", ");
  }); 
  

  $("#entries_authors").append("<p>");
  $("#entries_authors li").each(function(){
    $("#entries_authors p").append($(this).html());
    $("#entries_authors ul").remove();
  });
  
  $("#entries_authors a:not(#entries_authors a:last)").each(function(){
    $(this).html( $(this).html() + ", ");
  }); 
}

var $animationID = 0; 

function showLoader($container) {

  var top = 0;
  var left = 0;

  if( $container ) {
    
    if( $container.length == 0 ) {
      return;
    }
  
    var offset = $container.offset();
    top = (offset.top + (($container.height()/2) - 20)) + "px";
    left = (offset.left + (($container.width()/2) - 20)) + "px";
  } else {
    top = "50%";
    left = "50%";
  }

  $("body").append('<div id="loader" style="width: 40px; height: 40px; overflow: hidden; position: absolute; top: '+top+'; left: '+left+';"><div style="position: relative;"><img src="/_display/images/spinner.png" style="position: absolute; top: 0;" /></div></div>');
  $animationID = setInterval("animateLoader()", 100);
}

function hideLoader() {
  clearInterval($animationID);
  $("body #loader").remove();
}

loadingFrame = 0;
function animateLoader() {

  $("#loader img").css('top', (loadingFrame * -40) + 'px');
  loadingFrame = (loadingFrame + 1) % 12;
}