var Stream = function () {
	// For future JavaScript features
	return {
		fadeNav: function (delay) {
		  if(typeof delay != 'number') {
		    delay = 2500;
		  }
		
			$('#nav li').each(function () {			
    	  if(!$(this).hasClass('selected')) {
          $(this).delay(delay).animate({opacity:.4}, 1500);
        }
    	});
		},
		
		// Deselect current nav item(s)
		deselectNav: function () {
			$('#nav li').each(function () {	
        $(this).removeClass('selected');
      });
		},
		
		// Initialize stream list
		// This is where we set the container width after filtering streams
		initStreams: function (numStreams) {
		  if (typeof numStreams == 'undefined') {
		    numStreams = $('.streamItem').length;
        $('.streamItem').fadeIn();
		  }
		  $('#stream').width(536 * numStreams + 190);
		}
	}
}();

$('#nav_about').ready(function () {
	// Scroll to the right
	$.scrollTo({top:0, left:$("#stream").width()});
});

$('document').ready(function() {
	// Scrolling animation for navigation
	$('#nav li a').click(function(e){
    if($(this).attr("id") == "logout") return true;
    if($(this).attr("target") == "_blank") return true;
    e.preventDefault();

    var link = $(this).attr('href');
    if (typeof link == 'undefined') {
      // We have clicked on a stream tag

      var tag = $(this).text();

      // Find streams with the specified tag
      streams = [];
      $('.stream').each(function () {
        var stream = this;
        $(this).find('.tags li').each(function () {
          if ($(this).text().toLowerCase() == tag) {
            // A match was found, add the stream to the list
            streams.push(stream);
            return;
          }
        });
      });

      // Trigger the image to load if it hasn't already due to lazyLoad
      // For now we load all images
      // TODO: Design a way to load just the visible image
      $('.streamItem img').trigger('appear');

      // Hide all the streams including about and projects
      $('.streamItem').hide();

      // Initialize the stream container based on the new number of visible streams
      Stream.initStreams(streams.length);

      // Scroll to the beginning (right side)
      $.scrollTo({top:0, left:'100%'});

      $(streams).fadeIn();

      return;
    }

    // First reset streams in case we are in a tag view now
    Stream.initStreams();
    $.scrollTo('#stream-' + link.replace('#stream-',''), 800, {offset: {top:0, left:-15}});
	});

	$('#nav-about').unbind().click(function(e){
		e.preventDefault();

    // First reset streams in case we are in a tag view now
    Stream.initStreams();
		$.scrollTo( {top:0, left:0}, 800 );
	});
	$('.nav-login').unbind().click(function(e){
		e.preventDefault();

    // First reset streams in case we are in a tag view now
    Stream.initStreams();
		$.scrollTo( {top:0, left:0}, 800 );
	});
	$('#nav-begin, #logo').unbind().click(function(e){
	  if ($('#project').length) {
	    window.location = CI.base_url;
	  }
		e.preventDefault();
    Stream.initStreams();
		$.scrollTo( {top:0, left:'100%'}, 800 );
	});
	/*$('#nav-projects').unbind().click(function(e){
		e.preventDefault();
    Stream.initStreams();
		$.scrollTo('.first-project', 800, {offset: {top:0, left:-15}});
	});*/
	
	// Hide navigation on page load except "streams"
  Stream.fadeNav();
	
	// Show navigation items on hover
	$('#nav li').hover(
    function () {
      $(this).stop(true, true).animate({opacity:1});
    },
    function () {
      // If this isn't the currently selected menu item, fade out
      if (!$(this).hasClass('selected')) {
        $(this).delay(1000).stop(true, true).animate({opacity:.4}, 1000);
      }
    }
	);
	
	
	// Select item on click
	$('#nav li').click(function () {
    // Remove current selection
    Stream.deselectNav();
    
    $(this).addClass('selected');
    
    Stream.fadeNav(0);
	});
	
	// Show stream details on hover	
  $('.streamItem').hover(
    function() {
      $(this).find('.streamDetails').stop(true, true).fadeIn();
    }, 
    function () {
      $(this).find('.streamDetails').stop(true, true).fadeOut();
    }
  );
  
  
  // Load images when they scroll into view
  $('.streamItem img').lazyload({
    placeholder   : 'static/images/empty.png',
    effect        : 'fadeIn',
    failurelimit  : 999
  });
  
  
  // About pane contact tooltip thing
  $('.contact a').hover(function() {
    $('#contact-text span').text($(this).text()).stop(true,true).fadeIn();
  }, function () {
    $('#contact-text span').stop(true,true).fadeOut();
  });
  
  // Arrows hover 
  $('#arrows').hover(function () {
    $(this).find('span').stop(true,true).fadeIn();
  },
  function () {
    $(this).find('span').stop(true,true).fadeOut();
  }).find('span').hide();


  // Center the navigation menu
   var width = 0;
   $("#nav ul:not(#login_logout)").each(function(){
       width += $(this).width();
   })
   $("#nav").css("left",$(window).width()/2-width/2);
});

