$(document).ready(function () {
	
	/*
	*  Table Striping
	*/
	/*$("table thead tr").addClass("even");*/
	$("table tbody tr:nth-child(even)").addClass("even");
	/*$("table tr:even").addClass("even");*/
	$("table tr td:last-child, table tr th:last-child").addClass("rightmost");
	
	
						
	/*
	*  Nav Onstates
	*/
	$('#nav ul li a').each(function(i){
		var currentPage = location.href;
		//alert('this.href ' + this.href);
		if (currentPage.match(this.href)){  
		  this.setAttribute("class", "on");
		};
		return true;
	  });
	
	
	$('#subnav ul li a').each(function(i){
		var currentPage = location.href;
		var noSlash = '';
		var currentPage = currentPage.replace('index.aspx', '');
		if (currentPage.charAt(currentPage.length-1) == '/'){
		   noSlash = currentPage.substring(0, currentPage.length-1);
		}
		//alert('this.href ' + this.href);
		if (currentPage == this.href || noSlash == this.href){  
		  this.setAttribute("class", "on");
		};
		return true;
	  });
	
	
	
	/*
	*  Nav Classes for IE6 to do the suckerfishes.
	*/
	$("#nav > ul > li").mouseenter(function(){
										
	  if ($(this).hasClass("rightlast")){
	    $(this).addClass("rightlastover");
	  } 
	  $(this).addClass("listover");
	  
	 }).mouseleave(function(){
		 
	  if ($(this).hasClass("rightlast")){
	    $(this).removeClass("rightlastover");
	  } 
	  
	  $(this).removeClass("listover");
	  });
	
	/*
	*  subnav accordion
	*/
	
	$('#subnav ul li ul li a').prepend("&raquo; ").css({"text-indent" : "-1.2em", "padding-left" : "1em"});
	
	
	/*
	* Click open/closed.
	*/
	$('#subnav ul li ul').hide().parent().addClass("closed").children("a").bind("click", 
	  function(e){
		  if ($(this).parent().hasClass("closed")){
			$(this).parent().removeClass("closed").addClass("open").children("ul").slideToggle("fast");
		  } else {
			  $(this).parent().children("ul").slideToggle("fast", function(e){
			  if ($(this).parent().hasClass("open")){
				$(this).parent().removeClass("open").addClass("closed");
			  }
																		   });
		  }
		  return false;
	  }).bind("dblclick", 
	  function(e){
		  window.location = $(this).attr("href");
	  });
	// Make the current menu stay open.
	$('#subnav > ul > li > a').each(function(){
		var myHref = $(this).attr("href");
		var here = location.href;
	  //alert(here.substr(0, here.lastIndexOf("/")));
	  if (myHref == here.substr(0, here.lastIndexOf("/"))){
		  $(this).parent().removeClass("closed").addClass("open").children("ul").show();
	  }
	});
	
	
	/*
	*  Rollover/out expansion.
	
	
	var config = {    
     sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
     interval: 100, // number = milliseconds for onMouseOver polling interval    
     over: menuExpand, // function = onMouseOver callback (REQUIRED)    
     timeout: 1500, // number = milliseconds delay before onMouseOut    
     out: menuContract // function = onMouseOut callback (REQUIRED)    
    };


	$('#subnav > ul > li').children("ul").hide().parent().addClass("closed").hoverIntent(config);
	
	function menuExpand(){
	  if ($(this).hasClass("closed")){
	    $('#subnav > ul > li').removeClass("open").addClass("closed").children("ul").hide();
		$(this).removeClass("closed").addClass("open").children("ul").slideToggle("fast");
	  } 
	  return false;
	}
	function menuContract(){
	  if ($(this).hasClass("open")){
	    $(this).removeClass("open").addClass("closed").children("ul").slideToggle("fast");
	  } 
	}
	
	// Make the current menu stay open.
	$('#subnav > ul > li > a').each(function(){
		var myHref = $(this).attr("href");
		var here = location.href;
	  //alert(here.substr(0, here.lastIndexOf("/")));
	  if (myHref == here.substr(0, here.lastIndexOf("/"))){
		  $(this).parent().removeClass("closed").addClass("open").unbind().children("ul").show();
	  }
	});
	*/
	

	/*
	*  Create tabs for 'tabs' divs.
	*/	
	$('div.tabs > div > h2').hide();
	$('div.tabs > div').hide();

	$('div.tabs ul.tabnav').children('li').children('a').bind('click', function () {							 
	  var tabContainer = $(this).parents('div.tabs');
	  tabContainer.children('div').hide().filter(this.hash).fadeIn('slow');
	  $(this).parents('ul').children('li').children('a').removeClass('selected').end().end().end().addClass('selected');
	  return false;
	  }).filter(':first').click();
	
	/*
	*  CONTACT EXPAND/CONTRACT for Contact page
	*/
	
	// hide tables and make the open/close link.
	$("#content div.contactdiv").each(function(){
	  $(this).children("table").hide();
	  $(this).children("div.three-col").children("div.column:last-child").html('<a href="#" class="stafflink closed">Show Staff Listing</a>').children("a").bind("click", function(e){
	$(this).parent().parent().parent().children("table").slideToggle("fast");
	if ($(this).text() == 'Show Staff Listing'){
		$(this).removeClass("closed").addClass("open").text('Hide Staff Listing');
	} else {
		$(this).removeClass("open").addClass("closed").text('Show Staff Listing');
	}
	   return false;
	});
	  });
	// if you shrink the tables and there's an anchor link, we hit the wrong spot.
	// so revisit the anchor after manipulating the DOM.
	var here = location.href;
	if (here.match("#")){window.location = location.href;}
	
});

/*
*
*  Homepage image preloader & slideshow script
*
*/

function homepageRotator(imgArray){
	
	$('#homepage-rotator-img').empty();
	var img1 = new Image(587,390); 
    img1.src = 'images/homepage-rotator/' + imgArray[0]; 
	$(img1).bind('load', function() { 
      $('#homepage-rotator-img').append(this);
	  var img2 = new Image(587,390); 
      img2.src = 'images/homepage-rotator/' + imgArray[1];
	  $(img2).bind('load', function() { 
	    $('#homepage-rotator-img').append(this).cycle({
		timeout:5000,
		speed:3000,
		before: onBefore
		});
	  });
    });
	
	
	
	// preload images into an array;
	if (imgArray.length > 2){
		var stack = [];
		for (var i = 2; i < imgArray.length; i++) { 
		  var img = new Image(587,390); 
		  img.src = 'images/homepage-rotator/' + imgArray[i]; 
		  $(img).bind('load', function() { 
			stack.push(this); 
		  }); 
		}  
	}
		
	// add images to slideshow 
	function onBefore(curr, next, opts) { 
		if (opts.addSlide) // <-- important! 
			while(stack.length) 
				opts.addSlide(stack.pop());  
	}; 
}
