$(function(){

//****************************************************************************************
//reorganize subnavs
//****************************************************************************************
$('#nav').find('ul.subnav, .sub-subnav ul').each(function(){
	var $ul = $(this)
	var $li = $ul.children().get();
	if($li.length >6){
		var $listOne = $("<ul class='sub-ul sub-one' />");
		var $listTwo = $("<ul class='sub-ul sub-two' />");
		var length = $li.length;
		while ($li.length > 0){
			if($li.length > Math.floor(length/2)) $($li.shift()).appendTo($listOne);
			else $($li.shift()).appendTo($listTwo);
		}
		var $container = $('<div class="subnav" />').append($listOne).append($listTwo);
		$ul.parent().append($container);
		$ul.remove();
	}
	
});
$('.li_container').addClass('subnav');

//*********************************************************************************************
//nav hover effect        *opacity change breaks third level dropdown in IE for some reason...
//*********************************************************************************************
var timeout;

$("#main-nav > li:has(ul)").hover(function(){
	clearTimeout(timeout);
	$(this).addClass('hover')
	.find('.subnav')
		.stop()
		.animate({opacity: 1}, 150)
	.end()
	.siblings()
		.removeClass('hover')
		.find('.subnav')
			.css({opacity: 0})
		;
}, function(){
	$this = $(this)
	timeout = setTimeout(function(){
		$this.find('.subnav')
		.stop()
		.animate({opacity: 0}, 150, function(){ $(this).parent().removeClass('hover').find('.subnav').css({opacity: 0}); });
	}, 200);
}).find('.subnav')
.css({opacity: 0});


//for the sub-subnav
	$("#main-nav > li > ul > li:has(div)")
		.find("> a")
			.append(' &raquo;') //show that this has a sub-subnav
			.end()
		.hover(function(){
		clearTimeout(timeout);
		$(this).addClass('hover')
		.find('.sub-subnav')
			.stop()
			.animate({opacity: 1}, 150)
		.end()
		.siblings()
			.removeClass('hover')
			.find('.sub-subnav')
				.css({opacity: 0})
			;
	}, function(){
		$this = $(this)
		timeout = setTimeout(function(){
			$this.find('.sub-subnav')
			.stop()
			.animate({opacity: 0}, 150, function(){ $(this).parent().removeClass('hover').find('.sub-subnav').css({opacity: 0}); });
		}, 200);
	}).find('.sub-subnav')
	.css({opacity: 0});

//****************************************************************************************
//Sidebar Nav reorganization
//****************************************************************************************
if($("#sidebar .nav").length){
	$("#sidebar .nav > ul > li.active, #sidebar .nav > ul > li.current-cat").removeClass('current-cat').clone().prependTo("#sidebar .nav > ul").addClass('current-cat');
}

//****************************************************************************************
//Front Page Slider
//****************************************************************************************
if($('#fp-rotator').length && ( $('#fp-rotator').children().length > 1) ){
	$('<div id="fp-rot-pager" />').appendTo($('#fp-rotator-wrap'));
	$('#fp-rotator').cycle({
		timeout: 15000,
		speed: 3000,
		pager: '#fp-rot-pager'
	})
	.find('img')
		.attr('title', '');
}

//****************************************************************************************
//slideshows
//****************************************************************************************
if( $('.versabox').find('.slideshow > .thumbs > .thumb').length > 1 ){
	$('.versabox .slideshow > .thumbs').cycle({
		timeout: 5000,
		speed: 3000
	});
}

//****************************************************************************************
//Communities Page
//****************************************************************************************
if($('.community-links').length){
	var $ul = $('.community-links > ul')
	var $li = $ul.children().get();
	var $listOne = $("<ul class='sub-ul sub-one' />");
	var $listTwo = $("<ul class='sub-ul sub-two' />");
	var length = $li.length;
	while ($li.length > 0){
		if($li.length > Math.floor(length/2)) $($li.shift()).appendTo($listOne);
		else $($li.shift()).appendTo($listTwo);
	}
	$ul.parent().append($listOne).append($listTwo);;
	$ul.remove();
}

//****************************************************************************************
//Archives Sidebar
//****************************************************************************************
if($('#sb_archives').length){
	$sb_archives = $('#sb_archives');
	$sb_archives.find('ul > li > a').each(function(){
		$this = $(this);
		var text = $this.parent().text();
		$this.text(text).parent().empty().append($this);
		
	});
	
}

//****************************************************************************************
//Hide Bios on Staff Page
//****************************************************************************************
if($('#content-wrap.staff').length){
	$('div.bio').each(function(){
		var $this, $bioWrap, $bioLink;
		$this = $(this);
		$bioWrap = $('<div class="bio-wrap" />');
		$this.find('p').appendTo($bioWrap);
		$bioWrap.appendTo($this).hide();
		$bioLink = $this.find(' > strong').wrap("<a href='#' />");
		$bioLink.html('Biography').click(function(){
			if($(this).text() === 'Biography') $(this).html('<em>Hide</em> Biography')
			else $(this).html('Biography');
			$bioWrap.toggle('normal');
			return false;
		})
	});
	
	
}


//****************************************************************************************
//pop up iframe for list sign up
//****************************************************************************************
$("a[href='http://listserver.co.la.ca.us/4th-dist-registration.htm']")
	.colorbox({
		width: '600px',
		height: '350px',
		iframe: true
	});

//****************************************************************************************
//galleries
//****************************************************************************************
$('div.thumbs .thumb a').colorbox({
	
});


//****************************************************************************************
//add bullets to nav
//****************************************************************************************

$bull = $('<strong class="bullet">&bull;</strong>');
$bull.clone().prependTo($('#nav ul > li:not(.first)'));
$bull.clone().prependTo($('#sidebar li'));	
});
