// Slideshow Functions
function ncSlideshow(container,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 Collection, Loader, Content
	this.slides = ($('#' + container + '-slide').length) ? $('#' + container + '-slide') : $('.' + container + '-slide');
	this.captions = ($('#' + container + '-caption').length) ? $('#' + container + '-caption') : $('.' + container + '-caption');

	if(this.slides.length < 2) return false;

	// Set the Delay
	this.delay = (!this.container.attr('delay') || isNaN(this.container.attr('delay'))) ? 3000 : this.container.attr('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.slides.filter(':visible');

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

		// Toggle Animation
		jC.hide('slide',{direction:'left'},1000);
		jN.show('slide',{direction:'right'},1000);

		// Complex CAPTIONS
		if(me.captions.length){
			me.captions.filter('[slide="' + jC.attr('slide') + '"]').fadeOut();
			me.captions.filter('[slide="' + jN.attr('slide') + '"]').fadeIn(); //show('slide',{direction:'left',easing:'easeOutCirc'});
		}

	}

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

	// Cancel Animation on HOVER
	if(this.captions.length){
		this.captions.hover(function(i){
			if(i.type == 'mouseenter'){clearInterval(me.timer);}
			else{me.timer = setInterval(me.action,me.delay);}
		});
	}

}

// Go!
$(window).load(function(){
	hS = new ncSlideshow('global-showcase',true);
});
