$.cycleTabs = function(o){
	
	$.cycleTabs.defaults = {
		cycleTabsID : 'cycleTabs',
		switchSpeed : 1000,
		timeout : 4000,
		tabControls : false
	};
	o = $.extend({}, $.cycleTabs.defaults, o);
	
	var tcount = $('#'+o['cycleTabsID']).find('img').length;
	var scount = 0;
	
	// Fisrt wrap elements so they can be targeted
	$('#'+o['cycleTabsID']+' .images').children().each(function(index){
		scount++
		$(this).wrap('<div id="swap-'+scount+'" class="tab-content">')	
	})
	// Wrap the spash cover with an a tag for use later on, 
	// we can't use normal links because the cover used for the rounded borders on the images blocks links from being clicked
	$('#splash-mask').wrap('<a id="slideshow-link" href="">');
	
	function loadTab(tabID,clear){
	// Set initial vars
		var targetab = $('#swap-'+tabID);
		var runCycle = 0;
		
	// Visual actions
		if(!$('.tab-content').hasClass('active')) $('.tab-content').stop().fadeOut(o['switchSpeed']);
		if(!targetab.hasClass('active')){
			$('.tab-content.active').stop().removeClass('active').fadeOut(o['switchSpeed']);
			$('#swap-'+tabID).fadeIn(o['switchSpeed']).addClass('active');
		};
		$('#tabs li.active').removeClass('active');
		$('#tabs li#tab-'+tabID).addClass('active');
		// here we set the links for each image
		var href = $('.tab-content.active').children('a').attr('href');
		if(href!=undefined){
			$('#slideshow-link').attr('href',href);	
		}
		else $('#slideshow-link').removeAttr('href');
		
	// Iterate 'tabID' and load tab after a timeout 
		
		nextTab = tabID+1
		runCycle = setTimeout(function(){
			if(nextTab>tcount) nextTab = 1;
			loadTab(nextTab);
		}, o['timeout']);
	
		if(clear) clearTimeout(runCycle);
	};
	
	
	
	if(o['tabControls'] == true){
		$('#tabs a').click(function(){
			loadTab(parseInt(this.id.replace('tab_','')), true);
			return false;
		})
	}
	
	// Initialy call loadTab once to start things off
	loadTab(1);
	
}
