// ----- Functions ----- 

	function checkForHover(index) {
		var found = $('#glossary-nav li').hasClass("hover1");
		if (!found) {
			var $items = $('#glossary-nav li');
			if (index > 3) $items.eq(index - 4).addClass("hover4");
			if (index > 2) $items.eq(index - 3).addClass("hover3");
			if (index > 1) $items.eq(index - 2).addClass("hover2");
			$items.eq(index - 1).addClass("hover1");
			$items.eq(index).addClass("active");
			if (index < $items.length + 1) $items.eq(index + 1).addClass("hover1");
			if (index < $items.length + 2) $items.eq(index + 2).addClass("hover2");
			if (index < $items.length + 3) $items.eq(index + 3).addClass("hover3");
			if (index < $items.length + 4) $items.eq(index + 4).addClass("hover4");	
		}
	}

	function rotateSplash(delay) {
		var t = setTimeout("rotateSplash("+delay+")",delay);
		var hovering = $('#splash li').hasClass("hovering");
		if (!hovering) {
			var $items = $('#splash li');
			var index = $("#splash li").index($('.active')[0]);
			if (index < $items.length - 1) {
				var nextItem = index + 1;
			} else {
				var nextItem = 0; 
			}
			//console.log("index: " + index + " nextItem: " + nextItem);
			$items.eq(index).fadeOut("slow");
			$items.eq(index).toggleClass("hide");
			$items.eq(index).toggleClass("active");
			$items.eq(nextItem).fadeIn("slow");
			$items.eq(nextItem).toggleClass("hide");
			$items.eq(nextItem).toggleClass("active");
		}
	}
	
$(document).ready(function() {
// ----- Home page splash image -----
	rotateSplash(5000); // rotate the home page image every 5 seconds
	var hovering = false;
	$("#splash li").hover(
		function() {
			$(this).find(".intro").slideDown('slow');
			$(this).addClass("hovering");
		},
		function() {
			$(this).find(".intro").slideUp('slow');
			$(this).removeClass("hovering");
		}
	);
	
// ----- Drop shadows ----
	$(".dropshadow").wrap("<div class='wrap1'><div class='wrap2'>" + "<div class='wrap3'></div></div></div>");

// ----- Nav drop down for IE6 ----- 
	$('#nav li').hover(function() {
		$(this).children("div.subnav").addClass('dropdown');
	}, function() {
		$(this).children("div.subnav").removeClass('dropdown');
	});

	$('#alpha > .tabs > li.active-tab').prev().find('a').addClass("previous");
	
// ----- Search field text ----- 
	var searchtext = $('#searchform #keywords').val();
	
	$('#searchform #keywords').focus(function() {
		$('#searchform #keywords').val("");
	});
	$('#searchform #keywords').blur(function() {
		if ($('#searchform #keywords').val() == "") {
			$('#searchform #keywords').val(searchtext);
		}
	});

	var glossarysearchtext = $('#glossarysearchform #keywords').val();
	
	$('#glossarysearchform #keywords').focus(function() {
		$('#glossarysearchform #keywords').val("");
	});
	$('#glossarysearchform #keywords').blur(function() {
		$('#glossarysearchform #keywords').val(glossarysearchtext);
	});
	
// ----- Production Notes ----- 
	$("#notes").hide();
	$("#notes-link").click(function() {
		$("#notes").toggle();
	});
	
// ----- Image Switcher Examples ----- 
	// Add some HTML that would otherwise be unnecessary
	$("#main-content .interactive ul:first").wrap('<div class="image-switcher"></div>');
	$("#main-content .interactive ul:first").before('<a class="show-hide">Hide</a>');
	$("#main-content .interactive .thumb img").after('<span></span>');

	$(".image-switcher .show-hide").click(function() {
		$(".image-switcher ul").toggle();
		$(".show-hide").toggleClass('hidden');
	});

	$("#main-content .interactive ul li a:first").addClass('active');
	//$("#main-content img:not(:first)").addClass('hide');

	// Hide all images that aren't thumbnails
	// A .image div is wrapped around Flash components
	$("#main-content .interactive img, #main-content .interactive .image").addClass('hide');
	
	// Show the thumbs and first one
	$("#main-content .interactive #image1").removeClass("hide");
	$("#main-content .interactive .thumb img").removeClass("hide");	
	$("#main-content .interactive img:first, #main-content .interactive .image:first").removeClass("hide");
	
	$("#main-content .interactive ul li a").click(function() {
		$("#main-content .interactive ul li a").removeClass("active");
		$(this).addClass("active");
		
		// var target = $(this).attr('href');
		var target = this.hash;
		$("#main-content .interactive img, #main-content .image").addClass("hide");
		$('#main-content .interactive '+target).removeClass("hide");
		$("#main-content .interactive .thumb img").removeClass("hide");	
		$("#main-content .interactive img.show-hide").removeClass("hide");
		return false;
	});
		
	/*
	$("#main-content ul li a").click(function() {
		$("#main-content ul li a").removeClass("active");
		$(this).addClass("active");
		
		var target = $(this).attr('href');
		$("#main-content img").addClass("hide");
		$('#main-content '+target).removeClass("hide");		
		return false;
	});
	*/
	
// ----- Rollover Examples ---- //
	// Roll over any image that doesn't have the .noroll class
	$("#main-content .rollover img:not(.noroll)").hover(
		function () {
			imgsrc = $(this).attr("src");
			matches = imgsrc.match(/-over/);
			if (!matches) {
				imgsrcON = imgsrc.replace(/.gif$/ig,"-over.gif");
				$(this).attr("src", imgsrcON);
			}
		},
		function () {
			$(this).attr("src", imgsrc);
		}
	);

	// Preload rollover images
	$("#main-content .rollover img:not(.noroll)").each(function() {
		rollsrc = $(this).attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"-over.gif");
		$("<img>").attr("src", rollON);
	});

// ----- Glossary -----  
	$('#glossary-nav ul').localScroll({
	   target:'#glossary-list'
	});
	
	$('#glossary-nav a').hover(
		function() {
			var index = $('#glossary-nav a').index(this);
			var $items = $('#glossary-nav li');
			if (index > 3) $items.eq(index - 4).addClass("hover4");
			if (index > 2) $items.eq(index - 3).addClass("hover3");
			if (index > 1) $items.eq(index - 2).addClass("hover2");
			$items.eq(index - 1).addClass("hover1");
			if (index < $items.length + 1) $items.eq(index + 1).addClass("hover1");
			if (index < $items.length + 2) $items.eq(index + 2).addClass("hover2");
			if (index < $items.length + 3) $items.eq(index + 3).addClass("hover3");
			if (index < $items.length + 4) $items.eq(index + 4).addClass("hover4");	
		},
		function() {
			$('#glossary-nav li').removeClass();
			var lastIndex = $('#glossary-nav a').index(this);
			setTimeout("checkForHover("+lastIndex+")",10);
		}
	);

	$("#glossary-list li h3").click(function () {
		$(this).siblings(".definition").slideToggle("fast");
		$(this).parent("li").toggleClass("expanded");
		$(this).find(".more, .desc").toggle();
	});
	
	$("#glossary-list li .definition .tools .more").click(function () {
		$(this).parents(".definition").slideToggle("fast");
		$(this).parents(".definition").siblings("h3").find(".more, .desc").toggle();
	});

// ----- Glossary Term Bubbles----- 
	$('a.autoterm').cluetip({
		attribute:	'name',
		ajaxCache:	true,
		positionBy: 'bottomTop',
		clickThrough: 'true',
		dropShadow: false,
		arrows: 'true'
	});	

// ----- Library -----
	$('a.library-add, a.library-remove').livequery('click', function() {
		var myclass = $(this).attr("class");
		var mybutton = $(this);
		$(this).removeClass();
		$(this).addClass("spinner");
		var id = $(this).attr("alt");
		$.get(this.href, function(data) {	
			$.get('/include/tools-library-ajax/' + id, function(data) {	
				$(mybutton).parents('.tools-library').html(data);
				$(mybutton).addClass(myclass);
			});	
		});	
		return false;
	});

	$('#librarypage a.library-remove').click(function() {
		//$(this).parents(".trashcan").html('<span class="spinner">Loading...</span>');
    	$(this).parents('li').fadeOut("slow");
    	$.get(this.href);
		return false;
	});
	
// ----- Share This -----
	$(".sharethis").hide();
	$('.share').livequery('click', function() {
		$(this).parents('.tools').find('.sharethis').toggle();
	});	
});
