// JavaScript Document

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}


$(document).ready(function() {
						   
	// preload some images
	$.preloadImages(
		"/img/nav.highlight.tab.left.gif", 
		"/img/nav.highlight.tab.right.gif", 
		"/img/nav.highlight.left.gif", 
		"/img/nav.highlight.right.gif",
		"/img/nav.transparent.gif"); 
	
					   
	var timer;
						   		
	$.fn.featureRotator = function(starter) {
		timer = setTimeout( function() {
			$(".featured").removeClass("on");
			$(".tiny-box").removeClass("on");
			var next = starter + 1;
			if (next > $(".featured").length) {
				next = 1;
			}
			$("#featured-box-" + next).addClass("on");
			$("#tiny-box-" + next).addClass("on");
			$(document).featureRotator(next);
		}, 10000);
	};
	
	var starter = 1;
	$(document).featureRotator(starter);
	
	$(".tiny-box").hover(
      function () {
        $(this).css("cursor","pointer");
      }, 
      function () {
        $(this).css("cursor","auto");
      }
    ).click(function() {
		clearTimeout(timer);
		var target = $(this).attr("id").replace("tiny", "featured");
		$(".featured").hide();
		$("#"+target).show();
		$(".tiny-box").removeClass("on");
		$(this).addClass("on");
	});
	
	
	
	/*
	$(".featured .video embed").click(function() {
		clearTimeout(timer);
	});
	*/
	
	/* #### NAVIGATION MENUS #### */
	
	//add the transparent sides to the tabs
	$("#nav a").before("<img src='/img/nav.transparent.gif' style='float:left' width='4px' height='39' />");
	$("#nav a").after("<img src='/img/nav.transparent.gif' style='float:left' width='4px' height='39' />");
	
	// add the sides of the tab if its a menu
	$("#nav a.menu").hover( function() {
			$(this).prev("img").replaceWith("<img src='/img/nav.highlight.tab.left.gif' style='float:left' width='4px' height='39' />");
			$(this).next("img").replaceWith("<img src='/img/nav.highlight.tab.right.gif' style='float:left' width='4px' height='39' />");
			var currentId = $(this).attr("id").replace("nav-item-", "");
			// alert(currentId);
			$("#nav-menus ul#" + currentId).show();
		}, 
		function () {
			$(this).prev("img").replaceWith("<img src='/img/nav.transparent.gif' style='float:left' width='4px' height='39' />");
			$(this).next("img").replaceWith("<img src='/img/nav.transparent.gif' style='float:left' width='4px' height='39' />");
			var currentId = $(this).attr("id").replace("nav-item-", "");
			// alert(currentId);
			$("#nav-menus ul#" + currentId).hide();
		}
	);
	
	// add the sides of the tab if its just a link
	$("#nav a:not(.menu)").hover( function() {
			$(this).prev("img").replaceWith("<img src='/img/nav.highlight.left.gif' style='float:left' width='4px' height='39' />");
			$(this).next("img").replaceWith("<img src='/img/nav.highlight.right.gif' style='float:left' width='4px' height='39' />");
		}, 
		function () {
			$(this).prev("img").replaceWith("<img src='/img/nav.transparent.gif' style='float:left' width='4px' height='39' />");
			$(this).next("img").replaceWith("<img src='/img/nav.transparent.gif' style='float:left' width='4px' height='39' />");
		}
	);
	
	// when the cursor leaves the menu, hide it
	
	$(".nav-menu").hover( function(event) {
			event.preventDefault();
			$(this).show();
			$("#nav-item-" + $(this).attr("id")).addClass('menu-on');
			$("#nav-item-" + $(this).attr("id")).prev("img").replaceWith("<img src='/img/nav.highlight.tab.left.gif' style='float:left' width='4px' height='39' />");
			$("#nav-item-" + $(this).attr("id")).next("img").replaceWith("<img src='/img/nav.highlight.tab.right.gif' style='float:left' width='4px' height='39' />");
		},
		function(){
      		$(this).hide();
			$("#nav-item-" + $(this).attr("id")).removeClass('menu-on');
			$("#nav-item-" + $(this).attr("id")).prev("img").replaceWith("<img src='/img/nav.transparent.gif' style='float:left' width='4px' height='39' />");
			$("#nav-item-" + $(this).attr("id")).next("img").replaceWith("<img src='/img/nav.transparent.gif' style='float:left' width='4px' height='39' />");
    });
	
	
	// for each header in the content...
	/*
	$("#content :header").each( function(i) {
		
		//create an anchor link
		var anchor = $(this).text().replace(" ","") + i;
		var link = '<a class="anchor" href="#' + anchor + '">' + $(this).text() + '</a>';
		
		// and add it to the set of links at the top
		var newHtml = $("#anchors").html() + link;
		$("#anchors").html(newHtml);

	});
	*/
	

	
	$("#staff .col.right p").each(function(i,e){
		  var regex = /([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/,
		  		str = $(e).text();
		  if (regex.test(str)===true){
		     var str2 = $(e).text().replace(RegExp.lastMatch,'<a href="mailto:'+RegExp.lastMatch+'">'+RegExp.lastMatch+'</a>');
		    $(e).html(str2);
		  }
	});
	
	$(".announcement-link").click(function() {
		var id = $(this).attr("id").replace("_link","_body");
		$(".announcement-body#"+id).toggle();
	});
	
					   
});

