/* accordion.js for TIE */
jQuery.fn.nextItem = function() {
	var tempElem = this.next();
	if(tempElem.length == 0)
		tempElem = this.siblings().first();
	return tempElem;
}
jQuery.fn.prevItem = function() {
	var tempElem = this.prev();
	if(tempElem.length == 0)
		tempElem = this.siblings().last();
	return tempElem;
}
$(document).ready(function() {
	var currentlySliding = false;
	var carouselSpeed = 400;
	var theCarousel = $(".carousel");
		var currentPane = $(".pane:first-child",theCarousel).addClass("on");
	if(theCarousel.children('.slide-container').children('li').length>1) {
		var prevPane = $(".pane:last-child",theCarousel);
		var nextPane = currentPane.next();
		theCarousel.delegate("a.next","click",function(e) {
			e.preventDefault();
			carouselSlide(1);
		});
		theCarousel.delegate("a.prev","click",function(e) {
			e.preventDefault();
			carouselSlide(-1);
		});
		theCarousel.delegate(".pane .minimizeCaption","click",function(e) {
			e.preventDefault();
			$(this).parent().addClass("minimized");
		});
		theCarousel.delegate(".pane .expandCaption","click",function(e) {
			e.preventDefault();
			$(this).parent().removeClass("minimized");
		});
		$(document).keyup(function (e) {
			if(39 == e.which)
				carouselSlide(1);
			if(37 == e.which)
				carouselSlide(-1);
		});
		function carouselSlide(inDir) {
			if(!currentlySliding) {
				if(1==inDir){
					currentlySliding = true;
					nextPane.css('left','100%');
					currentPane.stop(true).animate({'left':'-100%'},carouselSpeed);
					nextPane.stop(true).animate({'left':'0%'},carouselSpeed,carouselDone());
					prevPane = currentPane;
					currentPane = nextPane;
					nextPane = nextPane.nextItem();
				}
				else if(-1==inDir){
					currentlySliding = true;
					prevPane.css('left','-100%');
					currentPane.stop(true).animate({'left':'100%'},carouselSpeed);
					prevPane.stop(true).animate({'left':'0%'},carouselSpeed,carouselDone());
					nextPane = currentPane;
					currentPane = prevPane;
					prevPane = prevPane.prevItem();
				}
			}
		}
		function carouselDone() {
			currentlySliding = false;
		}
	}
});
