(function($) { 
	function slideShow(el, showFor, switchSpeed, captionOpacity) {
		var gallery = $(el);
		
 		var slides = $('img', gallery).css({opacity: 0.0});
 		gallery.html(slides);
 		
 		var caption = $('<div />').addClass('caption').prependTo(gallery);
 		var content = $('<div />').addClass('content').prependTo(caption);
 		
 		var captionHeight = parseInt(caption.css('height'))+'px'; 
 		
 		var next =  function() {
			var current = $(slides).filter('.show');
			var next = current.next();
			if (current.length <= 0 || next.length <= 0) {
				next = $(slides).filter(':first');
			}
		
			next
				.css({opacity: 0.0})
				.addClass('show')
				.animate({opacity: 1.0}, switchSpeed);
		 
			current
				.animate({opacity: 0.0}, switchSpeed)
				.removeClass('show');
				
			caption
				.animate({opacity: 0.0}, { queue: true, duration: switchSpeed*.25, complete: function(){
					
					content
						.html('<h3>'+ next.attr('title').replace('|', '</h3>'));
			
					caption
						.animate({opacity: captionOpacity}, { queue: true, duration: switchSpeed*.5 });
				
				} });
			
		}
		
		next();			
		setInterval(next, showFor);
				
	}
	
	$(function() {		
		
		slideShow('#slideshow', 6000, 1000, .7);
		
		$('#gallery a').each(function(){ $(this).attr('rel', 'thumbs[thumb]');}).prettyPhoto();
		
		var palettes = $('link[@rel*=style][id]');
		var paletteIndex = Math.floor(Math.random() * palettes.length) + 1;
				
		var qs = location.search;
		if (qs && qs.substring) {
			qs = qs.substring(1).split("&");
			if (qs.length > 0) {
				var pal = parseInt(qs[0].split("=")[1])
	      paletteIndex = palettes.length - pal;
	    }
	  }
		
		palettes.each(function() {
			if (this.id != 'color_00' + paletteIndex) {
				$(this).remove(); 
			}
		});
	});
})(jQuery);

