jQuery.noConflict();
jQuery(document).ready(function($){
	// Scope body as JS capable
	$('body').addClass('js');
	
	// The showcase items
	// Initialize showcase
	var showcase = $('#showcase');
	var showcaseList = $('#showcase_items');
	var showcaseListItems = $('#showcase_items li');
	showcase.append('<ul id="showcase_pips"></ul>');
	
	// Initial setup to undo CSS rules that apply by default to make the showcase somewhat usable by non-js browsers
	showcaseListItems.each(function(){
		$(this).css({
			position: "absolute",
			top: "0px",
			left: showcaseListItems.index($(this))*showcase.width() + "px",
		});
		// Add a pip for each item
		$('#showcase_pips').append('<li></li>');
		$('#showcase_pips li:first').addClass('current');
	});
	
	// Show the navigation buttons
	showcase.append('<a id="showcase_previous" href="#">Previous</a><a id="showcase_next" href="#">Next</a>');
	
	
	// Previous item button
	$('a#showcase_previous').click(function(event){
		event.preventDefault();
		if($(':animated').length) {
			return false;
		}
		if($('#showcase_items li:first').position().left >= 0) {
			showcaseListItems.animate({left: "-=" + showcase.width()*(showcaseListItems.size() - 1)}, "slow");
			changePip('last');
		} else {
			showcaseListItems.animate({left: "+=" + showcase.width()}, "slow");
			changePip('prev');
		}
	});
	
	// Next item button
	$('a#showcase_next').click(function(event){
		event.preventDefault();
		if($(':animated').length) {
			return false;
		}
		if($('#showcase_items li:last').position().left <= 0) {
			showcaseListItems.animate({"left": "+=" + showcase.width()*(showcaseListItems.size() - 1)}, "slow");
			changePip('first');
		} else {
			showcaseListItems.animate({"left": "-=" + showcase.width()}, "slow");
			changePip('next');
		}
	});
	
	// Start automatic clicks to act like an auto slideshow for showcase
	var showcaseIntervalId = setInterval(function(){$('a#showcase_next').click()}, 7000 );
	
	// Stop automatic clicks when the user mousedowns on a showcase-nav link container (we can't use the same click event as the actual link)
	$('#showcase').mousedown(function(event){
		clearInterval(showcaseIntervalId);
	});
	
	
	function changePip(direction) {
		var dir = direction;
		var showcasePips = $('#showcase_pips li');
		var lastPip = $('#showcase_pips li.current');
		var nextPip;
		
		switch(dir) {
			case 'first':
				nextPip = showcasePips.first();
			break;
			case 'last':
				nextPip = showcasePips.last();
			break;
			case 'next':
				nextPip = lastPip.next();
			break;
			case 'prev':
				nextPip = lastPip.prev();
			break;
		}
		
		lastPip.removeClass('current');
		nextPip.addClass('current');
	}
	
	// Expandables
	var toggles = $('.toggles li');
	toggles.each(function() {
		$(this).append('<span class="arrow"></span><br class="clearboth" />');
	});
	toggles.click(function(event){
		$(this).children().not('h2, .arrow').slideToggle('slow', function(){
			//Animation complete
		});

		if($(this).hasClass('open')) {
			$(this).removeClass('open');
		} else {
			$(this).addClass('open');
		}
	});
	
	var togglenext = $('.togglenext');
	togglenext.click(function(event){
		event.preventDefault();
		$(this).next('ul').slideToggle('slow');
		$(this).toggleClass('open');
	});
	
	// Table list items
	$('li').each(function(){
		var anchors = $(this).find('a');
		if(anchors.length==1) {
			$(this).click(function(){window.location.href=this.getElementsByTagName('a')[0].href; return false;});
			$(this).addClass('clickable');
		}
	});
	
	// Photo stack indicator
	var pStack = $('.billboard a img');
	if (pStack.size > 1) {
		$('.billboard').append('<span id="photo-stack-icon">' + pStack.size + ' photos</span>');
	}
	
	// Cache-defeating on specific hyperlinks
	var noCacheLinks = $('a.no-cache, a[href*="welserver"]');
	noCacheLinks.attr('href', function(i, val) {
		return val + '?' + new Date().getTime();
	});
});

