Cufon.replace('#title>h1,#title>p,#gallery h2,#gallery p.subtext');

$(function(){
	$('#gallery').gallerify();
	$('#sections li').linkEntireBox();
	$('#questions').openCloseFaq();
});

$.fn.gallerify = function(){
	return this.each(function(){
		var container = $(this).css('overflow','hidden');
		var slides = $('>div',this);
		var scrollAmount = container.width();
		var ul = $('>ul',slides);
		var items = $('>li',ul);
		var timer;
		
		items.eq(0).clone().appendTo(ul);
		
		var slideGallery = function(e){
			var currentScroll = slides.scrollLeft();
			var nextPos;
			if (e.data.direction == 'prev') {
				if (currentScroll) {
					nextPos = currentScroll-scrollAmount;
				} else {
					slides.scrollLeft(scrollAmount*(items.length))
					nextPos = scrollAmount*(items.length-1);
				}
			} else {
				nextPos = currentScroll + scrollAmount;
			}
			slides.animate({ scrollLeft: nextPos },500,function(){
				if (nextPos >= scrollAmount*(items.length)) {
					slides.scrollLeft(0);
				}
			});
		}
		
		slides.css({
			height: items.outerHeight(),
			overflow: 'hidden'
		});
		ul.css({width: items.outerWidth(true) * items.length * 2 + 'px'});
		
		var prevButton = $('<button id="button_prev"><img src="/images/button_prev.png" alt="Previous" /></button>')
			.bind('click',{direction:'prev'},function(e){
				window.clearInterval(timer);
				if (!slides.is(':animated')) {
					slideGallery(e);
				}
			}).appendTo(container);
		var nextButton = $('<button id="button_next"><img src="/images/button_next.png" alt="Next" /></button>')
			.bind('click',{direction:'next'},function(e){
				window.clearInterval(timer);
				if (!slides.is(':animated')) {
					slideGallery(e);
				}
			}).appendTo(container);
			
		timer = window.setInterval(function(){
			slideGallery({data:{direction:'next'}});
		},7000);
		
	});
}

$.fn.linkEntireBox = function(){
	return this.each(function(){
		$(this).hover(function(){
			$(this).addClass('over');
		},function(){
			$(this).removeClass('over');
		});
	});
}

$.fn.openCloseFaq = function(){
	return this.each(function(){
		$('h3',this).addClass('closed').click(function(){
			if ($(this).is('.closed')) {
				$(this).removeClass('closed').addClass('open').next().slideDown();
			} else {
				$(this).removeClass('open').addClass('closed').next().slideUp();
			}
		}).each(function(){
			$(this).next().hide();
		});
	});
}
