function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 3000 );
});


// Tell Vimeo what function to call
var oEmbedCallback = 'embedVideo';

// Set up the URL
var oEmbedUrl = 'http://vimeo.com/api/oembed.json';

// Load the first one in automatically?
var loadFirst = true;

// This function puts the video on the page
function embedVideo(video) {
	var videoEmbedCode = video.html;
	document.getElementById('embed').innerHTML = unescape(videoEmbedCode);
}

// This function runs when the page loads and adds click events to the links
function init() {
	var links = document.getElementById('thumbs').getElementsByTagName('a');

	for (var i = 0; i < links.length; i++) {
		// Load a video using oEmbed when you click on a thumb
		if (document.addEventListener) {
			links[i].addEventListener('click', function(e) {
				var link = this;
				loadScript(oEmbedUrl + '?url=' + link.href +' &width=700&height=400&title=0&byline=0&portrait=0&color=66CC00&callback=' + oEmbedCallback);
				e.preventDefault();
			}, false);
		}
		// IE (sucks)
		else {
			links[i].attachEvent('onclick', function(e) {
				var link = e.srcElement.parentNode;
				loadScript(oEmbedUrl + '?url=' + link.href + ' &width=700&height=400&title=0&byline=0&portrait=0&color=66CC00&callback=' + oEmbedCallback);
				return false;
			});
		}
	}

	// Load in the first video
	if (loadFirst) {
		loadScript(oEmbedUrl + '?url=' + links[0].href + '&width=700&height=400&title=0&byline=0&portrait=0&color=66CC00&callback=' + oEmbedCallback);
	}
}

// This function loads the data from Vimeo
function loadScript(url) {
	var js = document.createElement('script');
	js.setAttribute('src', url);
	document.getElementsByTagName('head').item(0).appendChild(js);
}

