// 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 IMAGES
	this.images = this.container.find('li img');
	if(this.images.length < 2) return false;

	// Get the Collection, Loader, Content
	this.collection = ($('#' + container + '-images').length) ? $('#' + container + '-images') : $('.' + container + '-images');
	this.loader = ($('#' + container + '-loader').length) ? $('#' + container + '-loader') : $('.' + container + '-loader');
	this.content = ($('#' + container + '-content').length) ? $('#' + container + '-content') : $('.' + container + '-content');

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

		// Complex CAPTIONS
		if(me.content.length){
			me.content.find('[slide=' + jC.attr('slide') + ']').hide();
			me.content.find('[slide=' + jN.attr('slide') + ']').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.content.length){
		this.content.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-header',true);
});
