$(document).ready(function() {


	
	// Detects sidebar height and changes main-content height apprpriately to avoid awkward emptiness

function setContentMinHeight(){

				//alert('trying...');
				var sidebarHeight = $('aside.sidebar').height();
				//alert(sidebarHeight);
				var contentMinHeight = sidebarHeight + 40;
				$('section.main-content').css('min-height',contentMinHeight+'px');
				//alert('done');
	}
	
setContentMinHeight();
	


	// Add objects that should automatically receive a "first" or "last" class
	var first_last = new Array(
		'table tr',
		'table td',
		'dl dt',
		'ul li',
		'.table_container .table_default',
		'.split .column',
		'.split-2 .column',
		'.split-3 .column',
		'.split-4 .column'
		);


	for (var i=0; i<first_last.length; i++){
		var f = first_last[i];
		$(f+":first-child").addClass("first");
		$(f+":last-child").addClass("last");
		}

	$("table tr:odd").addClass("odd");
	
	// odd-numbered focus-article siblings should get the "odd" class
	$(".main-content-item > .focus-article:even").addClass("odd");
	$(".focus > .focus-article:even").addClass("odd");
	
	// Bind tabbing action from all uls with a class of 'tabs' to all divs
	// with a class of '.pane' that share the same container div
	$("ul.tabs").tabs("> .pane", {
		tabs: 'li',
		current: 'selected'
	});
	
	// create a dot corresponding to each item in the slideshow
	$('.scrollable-items > .highlight-box-slideshow').each(function(i) {
		var circle;
		if (i==0) {
			circle = '<li class="circle selected" id="'+i+'"></li>';
		} else {
			circle = '<li class="circle" id="'+i+'"></li>';
		}
		$(this).parent().parent().prevAll('.slideshow-nav').eq(0).append(circle);
	});
	
	// make scrollable
	var scroll = $(".scrollable").scrollable({
		circular: true,
		speed: 900,
		api: true
	});
	
	$('.slideshow-nav li').each(function(i) {
		var li = $(this);
		li.data('index', i);
		li.click(function(e){
			var index = $(this).data('index');
			scroll.seekTo(index);
			$(this).parent().children('.selected').removeClass('selected');
			$(this).addClass('selected');
			clearInterval(t);
		});
	});
	
	var t=setInterval(function(){cycleNext(scroll);}, 21000);

	// Set dropdowns on click
	$(".dropdown-trigger").toggle(
		function() {
			$(this).parent(".dropdown-container").addClass("active");
		},
		function() {
			$(this).parent(".dropdown-container").removeClass("active");
		}
	);
	/*
	var imageSlideshowLength = $('.image-slideshow li').size();
	var imageSlideshowCurrent = 0;
	$('.image-slideshow-pages').text('1 of ' + imageSlideshowLength);
	$('.image-slideshow li').each(function(i) {
		if (i != 0) {
			$(this).hide();
		}
	});
	$('.back').click(function() {
		var newCurr = imageSlideshowCycle(imageSlideshowCurrent,(-1),imageSlideshowLength);
		imageSlideshowCurrent = newCurr;
		$('.image-slideshow-pages').text((imageSlideshowCurrent+1) + ' of ' + imageSlideshowLength);
	});
	$('.next').click(function() {
		var newCurr = imageSlideshowCycle(imageSlideshowCurrent,1,imageSlideshowLength);
		imageSlideshowCurrent = newCurr;
		$('.image-slideshow-pages').text((imageSlideshowCurrent+1) + ' of ' + imageSlideshowLength);
	});
	*/
	
	// sidebar select
	
	$('.sidebar-select-container > select').sSelect();
	//$('.sidebar-select').sSelect({ddMaxHeight: '300px'});
	
	//$('.ire-history-slideshow').imageSlideshow({
	//	'background': '#232323',
	//	'fade': true
	//});
	
});
	
	var j=1;
	/*
	function imageSlideshowCycle(curr, increment, max) {
		newCurrent = curr + increment;
		
		if ((newCurrent + 1) >= max) {
			$('.button-row > .next').addClass('inactive');
		} else if (newCurrent <= 0) {
			$('.button-row > .back').addClass('inactive');
		} else {
			$('.button-row > .next').removeClass('inactive');
			$('.button-row > .back').removeClass('inactive');
		}
		
		if ((newCurrent) == max) {
			newCurrent = curr;
		}
		if ((newCurrent) < 0) {
			newCurrent = 0;
		}
		
		$('.image-slideshow li').each(function(i) {
			if (i == newCurrent) {
				$(this).show();
			} else {
				$(this).hide();
			}
		});
		return newCurrent;
	}
*/
	function cycleNext(p) {
		var max = $('.slideshow-nav').size() + 1;
		if (j==max) {j = 0;}
		p.next();
		// change selected slideshow navigation circle
		$('.slideshow-nav li').each(function() {
			if (j == $(this).attr('id')) {
				$(this).addClass('selected');
			} else {
				$(this).removeClass('selected');
			}
		});
		
		j++;
	}
	
