/* Author: Aaron Whitman */
jQuery.noConflict();
jQuery(document).ready(function($){
	// The showcase items
	// Initialize showcase
	if(window.location.hash.substring(1) != "show_interesting_work") {
		$('div#showcase_container').hide();
	} else {
		$('a#showcase_nav_open, body.home a#interesting_work').addClass('open_link');
	}
	
	// Initial setup to undo CSS rules that apply by default to make the showcase somewhat usable by non-js browsers
	$('ul#showcase_items li').each(function(){
		$(this).css({
			position: "absolute",
			top: "0px",
			left: $('ul#showcase_items li').index($(this))*$(this).width() + "px"
		});
	});
	
	// Show the navigation buttons
	$('a#showcase_nav_back, a#showcase_nav_forward, a#showcase_nav_close').show();
	
	// Previous item button
	$('a#showcase_nav_back').click(function(event){
		event.preventDefault();
		if($(':animated').length) {
			return false;
		}
		if($('ul#showcase_items li:first').position().left >= 0) {
			$('ul#showcase_items li').animate({left: "-=" + $('ul#showcase_items li:first').width()*($('ul#showcase_items li').size() - 1)}, "slow");
			$('div#showcase').height('ul#showcase_items li:last').height();
		} else {
			$('ul#showcase_items li').each(function(){
				$(this).animate({left: "+=" + $(this).width()}, "slow");
				if($(this).position().left == 0) {
					$('div#showcase').animate({
						height: $(this).prev().height()
					});
				}
			});
		}
		changePip(-1);
	});
	
	// Next item button
	$('a#showcase_nav_forward').click(function(event){
		event.preventDefault();
		if($(':animated').length) {
			return false;
		}
		if($('ul#showcase_items li:last').position().left <= 0) {
			$('ul#showcase_items li').animate({"left": "+=" + $('ul#showcase_items li:first').width()*($('ul#showcase_items li').size() - 1)}, "slow");
			$('div#showcase').height('ul#showcase_items li:first').height();
		} else {
			$('ul#showcase_items li').each(function(){
				$(this).animate({"left": "-=" + $(this).width()}, "slow");
				if($(this).position().left == 0) {
					$('div#showcase').animate({
						height: $(this).next().height()
					});
				}
			});
		}
		changePip(1);
	});
	
	// Showcase close button
	$('a#showcase_nav_close').click(function(event){
		event.preventDefault();
		if($(':animated').length) {
			return false;
		}
		$('div#showcase_container').slideUp();
		$('a#showcase_nav_open, body.home a#interesting_work').removeClass('open_link');
	});
	
	// Showcase open buttons
	$('a#showcase_nav_open, body.home a#interesting_work').click(function(event){
		event.preventDefault();
		if($(':animated').length) {
			return false;
		}
		
		$('div#showcase_container').css('display') == 'none' ? $('a#showcase_nav_open, body.home a#interesting_work').addClass('open_link') : $('a#showcase_nav_open, body.home a#interesting_work').removeClass('open_link');
		
		$('div#showcase_container').slideToggle();
		
		// Initial setup to undo CSS rules that apply by default to make the showcase somewhat usable by non-js browsers
		$('ul#showcase_items li').each(function(){
			$(this).css({
				position: "absolute",
				top: "0",
				left: $('ul#showcase_items li').index($(this))*$(this).width() + "px"
			});
		});
	});
	
	function changePip(increment) { // increment is 1 or -1
		var inc = increment;
		var pips = $('#showcase_pips li');
		var pip = $('#showcase_pips li.current');
		pip.removeClass('current');

		if(inc == 1) {
			if((pips.index(pip) + 1) == pips.size()) {
				pips.first().addClass('current');
			} else {
				pip.next().addClass('current');
			}
		} else if(inc == -1) {
			if(pips.index(pip) == 0) {
				pips.last().addClass('current');
			} else {
				pip.prev().addClass('current');
			}
		}
	}
	
	// Areas of work
	if($('dl#areas_of_work')) {
		$('dl#areas_of_work dt').addClass('expandable');
		$('dl#areas_of_work dd').hide();
		$('dl#areas_of_work dt').click(function(){
			$(this).next('dd').slideToggle();
			$(this).hasClass('expanded') ? $(this).removeClass('expanded') : $(this).addClass('expanded');
		});
	}
	
});
