// Slideshow Functions
function jSlideshow(container,delay,flair) {

	// Create reference for THIS instance
	var me = this;

	// Find the CONTAINER
	this.container = ($('.' + container).length) ? $('.' + container) : $('#' + container);
	if(!this.container.length) return false;

	// Get the IMAGES
	this.images = this.container.find('li img');
	if(this.images.length < 2) return false;

	// Get the Collection & Loader
	this.collection = this.container.find('.' + container + '-images');
	this.loader = this.container.find('.' + container + '-loader');

	// Set the Delay
	this.delay = (!delay || isNaN(delay)) ? 3000 : delay;

	// Load jQuery UI
	if(flair && !jQuery.ui){$('<script/>',{type:'text/javascript',src:'/js/jquery/jquery-ui.js'}).appendTo('head');}

	// Slide In & Out
	this.action = function(){

		// Get CURRENT Frame
		var jC = me.images.parents('li:visible').filter(':first');

		// Get NEXT or FIRST Frame
		var jN = (!jC.next().length) ? me.images.filter(':first').parents('li:first') : jC.next(); 

		// Toggle Animation
		jC.add(jN).fadeToggle(1000,function(){
			if($(this).is(':hidden')){
				$(this).children('div').hide();
			}else{
				$(this).children('div').show('slide',{direction:'down'});
			}
		});

	}

	// Initialize 
	this.collection.add(this.loader).fadeToggle();
	this.timer = setInterval(this.action,this.delay);

}

// Go!
$(window).load(function(){
	sS = new jSlideshow('slideshow',5000,true);
	hS = new jSlideshow('home-slideshow',5000,true);
});
