// Functions to call when the DOM is ready
$(document).ready(function() {
	px_slideshow();
	buckets();
});

// Adds cross-fade transitions to slideshows and rotates through slides
// Internal links to slides become slideshow navigation
function px_slideshow() {
	// initialize slideshows (any element with class "px_slideshow")
	$('.px_slideshow').each(function(i) {
		// create ID for slideshow if it doesn't have one
		if (!this.id) this.id = 'slideshow_' + i;
		// add class to allow us to style slidshow that has been initialized
		$(this).addClass('px_slideshow_initialized');
		// create array to hold slides belonging to this slideshow
		$(this).get(0).slides = [];
		// initialize slides (any child of slideshow with class "px_slide")
		var slideshow = this;
		$(this).find('.px_slide').each(function(i) {
			// create ID for slide if it doesn't have one
			if (!this.id) this.id = slideshow.id + '_slide_' + i;
			// add this slide to the slides array
			slideshow.slides[i] = this;
			// tell this slide what its parent slideshow is
			this.slideshow = slideshow;
			// find any internal links to this slide, and make them show this slide when clicked
			$('a[href$="#' + this.id + '"]').each(function() {
				$(this).click(function(event) {
					event.preventDefault();
					var id = this.href.substring(this.href.indexOf('#') + 1);
					var slide = document.getElementById(id);
					px_slideshow_switch(slide.slideshow,slide);
					// end the automatic slideshow rotation
					clearTimeout(slide.slideshow.playSlideshow);
				});
			});
			
		});
		// set the first slide as the current slide
		px_slideshow_switch(slideshow,slideshow.slides[0],true);
		// start the automatic slideshow rotation
		slideshow.playSlideshow = setInterval(function() {px_slideshow_switch(slideshow)},5000);
	});
}

// px_slideshow() uses this function to switch slides
// "slideshow" is required, "next" is not
function px_slideshow_switch(slideshow,next,init) {
	// if no next slide was specified
	if (!next) {
		// find the slide that follows the currently visible slide
		var next = $(slideshow).children('.px_slide:visible').next();
		// if there are no more slides, go back to the first slide
		if (next.length == 0) {
			next = $(slideshow).children('.px_slide:first');
		}
		next = next[0]; // we want the DOM element, not a jQuery object
	}
	// show the next slide
	$(next).fadeIn();
	// give a class to the next slide's links' parent elements
	$('a[href$="#' + next.id + '"]').each(function() {
		$(this).parent().addClass('px_ad_nav_current');
	});
	// hide all other slides
	for (var i in slideshow.slides) {
		var slide = slideshow.slides[i];
		if (slide != next) {
			// if this is the first call, don't animate hiding of other slides
			if (init == true) {
				$(slide).hide();
			}
			else {
				$(slide).fadeOut();
			}
			// give a class to the next slide's links' parent elements
			$('a[href$="#' + slide.id + '"]').each(function() {
				$(this).parent().removeClass('px_ad_nav_current');
			});
		}
	}
}


// for IE6, adds an over state to the three buckets
// (newer browsers use :hover pseudo class)
function buckets() {
	if ($.browser.version.substring(1) <= 6) {
		$('.px_bucket').mouseover(function() {
			$(this).addClass('px_over');
		});
		$('.px_bucket').mouseout(function() {
			$(this).removeClass('px_over');
		});
	}
}
