/**
 * Site navigation
 * Allows the nav to move with the users scrolling of the site so the nav is always in easy reach for the user
 * and sponsor carousel rotation
 * Auth: Chris Reeves POKE, 2010-02-17
 */

/**
 * POKE.nav namespace
 */
POKE.nav = {}

/**
 * Required namespace attributes
 */
POKE.nav.lastScroll = new Date();
POKE.nav.animationDuration = 500;

/**
 * Init method
 * Sets up the navigation and binds events
 */
POKE.nav.init = function() {
  POKE.nav.setHeight();
  POKE.nav.bindEvents();
  POKE.nav.carousel.start();
}

/**
 * jQuery Events
 * Binds the required events
 */
POKE.nav.bindEvents = function() {
  $(window).bind('resize', POKE.nav.setHeight);
  if($.browser.msie && $.browser.version == '6.0') {
    $(window).bind('scroll', POKE.nav.slide);
  }
}

/**
 * Set Navigation Height
 * Stes the navigations height acording to the viewable window height
 */
POKE.nav.setHeight = function() {
  // get client height
  if($.browser.msie && $.browser.version == '6.0') {
    var myHeight = document.documentElement.clientHeight;
  } else {
    var myHeight = document.body.clientHeight;
  }
  $('#nav').height(myHeight+'px');
}

/** 
 * Slide navigation
 * Slides the navigation after a delay to the users current scroll top position
 */
POKE.nav.slide = function() {
  var now = new Date();
  setTimeout(function() {
		var diff = now.getTime() - POKE.nav.lastScroll.getTime();
		var top;
		if(diff == 0 || diff == -1) {
		  if($(window).scrollTop() == $(document).height() - $(window).height()) {
		    top = $(document).height() - $(window).height();
	    } else {
	      top = $(window).scrollTop();
	    }
	    POKE.nav.animate(top, 500);
		}
  }, 250);
  POKE.nav.lastScroll = new Date();
}

/**
 * Navigation slide animation
 * Performs the animation used to slide the navigation from position to position
 * @param int top position
 * @param int animation duration
 */
POKE.nav.animate = function (top, duration) {
  $('#nav').animate({
    top: top
  }, duration);
}

/**
 * Sponsor Carousel Namespace
 * Fades sponsors in and out overtime
 */
POKE.nav.carousel = {}

/**
 * POKE.nav.carousel required attributes
 */
POKE.nav.carousel.intID = null;
POKE.nav.carousel.rotationDelay = 4000;

/**
 * Start Carousel
 * Starts the sponsor carousel rotation
 */
POKE.nav.carousel.start = function() {
  var random = POKE.nav.carousel.selectRandomSponsor();
  $('#logo-carousel li:eq('+random+')').show();
  POKE.nav.carousel.intID = setInterval(function() {
    POKE.nav.carousel.rotate();
  }, POKE.nav.carousel.rotationDelay);
}

/**
 * Rotate Carousel
 * Rotates the navigation sponsor carousel
 */
POKE.nav.carousel.rotate = function() {
  var current = $('#logo-carousel li:visible');
  var next;
  if(current.next().length > 0) {
    next = current.next();
  } else {
    next = $('#logo-carousel li:first');
  }
  current.fadeOut('slow', function() {
    next.fadeIn('slow');
  });
}

/**
 * Random sponsor selection
 * Selections a random sponsor from the list to be the starting point
 */
POKE.nav.carousel.selectRandomSponsor = function() {
  var max = ($('#logo-carousel li').length)-1;
  var randVal = 0+(Math.random()*(max-0));
  return typeof floatVal=='undefined' ? Math.round(randVal):randVal.toFixed(1);
}