/**
 * File: sdbc.js
 * Purpose: JavaScript for UW-Whitewater SDBC website.
 * Author: Brad Westness
 * Date: February 10, 2011
 */

var rotator;

$(document).ready(function() {
  // fix footer ul items in ie
  $("#footer li:first-child").css({
    borderLeft:"none",
    marginLeft:0,
    paddingLeft:0
  });
  
  $("#footer li:last-child").css({
    borderRight:"none",
    marginRight:0,
    paddingRight:0
  });
  
  // set up the splash item rotator
  if($("#splash #splash-nav").length){
		setUpIndexRotator();
	}
  
  // show overlay expansion hint
  $("#splash-items .splash-overlay").append("<p class='expand-hint'>Click to expand</p>");
  
  // expand overlay on click
  $("#splash-items .splash-overlay").click(function() {
		// cancel the automatic rotation, since
		// the user clicked on a thumbnail
		clearTimeout(rotator);
    
    if($(this).hasClass("active")) {
      $(this).animate({top:250}).removeClass("active");
      $(this).children(".expand-hint").text("Click to expand");
    } else {
      var newTop = 325 - $(this).outerHeight(true);
      $(this).animate({top:newTop}).addClass("active");
      $(this).children(".expand-hint").text("Click to hide");
    }
  });
  
  // set up the qm navigation
	qm_create(0,false,0,500,false,false,false,false,false);
  
  // pull blog feed on the homepage
  $("ul#blogFeed").jFeed(
    "http://blogs.uww.edu/sbdc/feed/",
    1,
    "M jS g:i a",
    "image"
  );
});

function setUpIndexRotator(){
	// set the rotator to automatically go after the delay
	var delay = 10000,
	rotator = setTimeout(function(){
		autoRotateFeaturedItems(delay)
	},delay);
	
	// bind the click() event to the thumbnails
	$("#splash #splash-nav li").click(function(){
		// cancel the automatic rotation, since
		// the user clicked on a thumbnail
		clearTimeout(rotator);
		
		// activate the selected item
		var index = $("#splash #splash-nav li").index(this);
		activateFeaturedItem(index);
	});
	
	// activate the first item
	activateFeaturedItem(0);
}

function activateFeaturedItem(index){	
	// set the thumb as 'current'
	$("#splash #splash-nav li").removeClass("current");
	$("#splash #splash-nav li:eq(" + index + ")").addClass("current");

	// move the selection indicator arrow
	index = parseInt(index);
	var arrowTop = ($("#splash #splash-nav li:first").outerHeight(true) * index) + 10;
	$("#splash #splash-arrow").animate({top:arrowTop}, "slow");
	
	// move the actual featured item
	var itemTop = ($("#splash #splash-items li:first").outerHeight(true) * index * -1);
	if($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8){
		// hack for internet explorer older than version 8
		// itemTop -= index * 3;
	}
	$("#splash #splash-items").animate({top:itemTop}, "slow");
}

function autoRotateFeaturedItems(delay){
	var current = $("#splash #splash-nav li").index($("#splash #splash-nav .current")),
	next = current + 1;
  
	if(next >= $("#splash #splash-nav li").length){
		// we're on the last thumbnail, so start over
		next = 0;
	}
	
	// rotate to the target slide
	activateFeaturedItem(next);
	
	// set the rotator to go again after the delay
	rotator = setTimeout(function(){
		autoRotateFeaturedItems(delay)
	},delay);
}
