jQuery(function($) { // Execute when DOM is ready

// $("a[href*='eclipsebank.com']").attr("title","coming soon").attr("style","cursor:default;border-color:#ddd").attr("rel","").attr("href","").click(function() {
	// return false;
// });

// First text element
$('h2 + h3', '#resume').addClass('first');

// New window
$("a[rel='external']").click(function() {
	window.open($(this).attr('href'));
	return false;
});

/* Gallery tooltip
-------------------------------------------------------------- */

// Preload icons
// function preload(images) {
//     $(images).each(function(){
//         $('<img/>')[0].src = this;
//     });
// }
// preload([
//     '/img/ico-view.png',
//     '/img/ico-launch.png'
// ]);

// Toggle tooltip on hover
$('.project-gallery a').hover(
   function () {
      // Animate tooltip if one does not already exist
      if (!$(this).is(':has(span)')) {
         var message = $(this).attr('title');
         // Shorten message to first word
         message = message.split(" ",1);
         $('<span class="' + message + '">' + message + '</span>')
            .css('opacity', 0)
            .appendTo($(this))
            .animate({
               opacity: 0.94,
               marginTop: '-=15'
               },250
            );
      }
   }, 
   // Remove tooltip
   function () {
      $(this).find("span:last")
         .fadeOut('fast', 
            function() {
               $(this).remove();
            }
         );
   }
);

/* ScrollTo (plugin)
-------------------------------------------------------------- */

if ($('#menu').length) {
   // Global variables
   var $projects = $('div.project'),
       $menu_items = $('#menu li'),
       click_scroll;
  
   // Page load
   // Scroll to project included in URL if it exists
   var selected_project = location.hash;
   // If hash has something in it besides blank space
   if (selected_project.length > 2) {
      $.scrollTo(
         selected_project,
         0,
         {
            axis: 'y',
            offset: {top: 7}
         }
      );
      // Clear the URL hash with a blank space since it can't be stripped
      location.hash = '0';
   }
   else {
      $.scrollTo(0);
   }

   // Menu click
   // Scroll to clicked project
   $('#menu a').click(function(){
      $menu_items.removeClass('selected');
      $(this).parent().addClass('selected');
      click_scroll = true;
      $.scrollTo(
         this.hash,
         1000,
         {
            axis: 'y',
            offset: {top: 7},
            onAfter: function() {
               click_scroll = false;
            }
         }
      );
      return false;
   });

   // Menu highlighting
   // During scroll, determine which project is in the viewport and highlight it in menu
   $(window).scroll(function() {
      // Ignore if scrolling is triggered from menu click
      if (!click_scroll) {
         var scroll_position = $(window).scrollTop();
         // Determine position of each project during scroll
         $projects.each(function() {
            var project_position = $(this).offset(),
                project_anchor = $(this).attr('id');
            // If project is near top of page, highlight it
            if (project_position.top - scroll_position < 250) {
               var $menu_selected = $('a[href="#' + project_anchor + '"]').parent();
               $menu_items.removeClass('selected');
               $menu_selected.addClass('selected');
            }
         });
      }
   });
}

/* Sticky menu
-------------------------------------------------------------- */

if ($('#menu').length) {
   // Initial menu position
   var menu_top_init = $('#menu').offset().top;
   $(window).scroll(function () {
      // Vertical scroll position
      var scroll_top = $(this).scrollTop();
      // Stick the menu before it is scrolled out of view
      if (scroll_top >= menu_top_init) {
         // Add fixed class
         $('#menu').addClass('fixed');
      } else {
         // Remove fixed class
         $('#menu').removeClass('fixed');
      }
   });
}

/* Slimbox (plugin)
-------------------------------------------------------------- */

// Close Slimbox when you click anywhere on it
$('#lbCenter').click(function(){
   $('#lbCloseLink').click();
});

/* Van Damme
-------------------------------------------------------------- */

var kkeys = [],
    // konami = "38,38,40,40,37,39,37,39,66,65,66,65";
    clay = "67,76,65,89";
$(document).keydown(function(e) {
	kkeys.push( e.keyCode );
	if ( kkeys.toString().indexOf( clay ) >= 0 ){
		$(document).unbind('keydown',arguments.callee);
		$('<img src="/stuff/vandance.gif" style="margin-left: -85px; position: absolute; top: -300px; left: 50%; z-index: 10; border: 5px solid #eee; -webkit-box-shadow: 0px 0px 9px #999; -moz-box-shadow: 0px 0px 9px #999; box-shadow: 0px 0px 9px #999;" />')
			.prependTo("#page")
			.animate({top: "150px"}, 2500);
	}
});

}); // End DOM ready execution
