/*!
 * jQuery cycleBackground plugin v.0.1.0
 * Date: 2010-02-04
 */
(function($){
$.fn.cycleBackground = function (imgCount, options) {
	// Properties
	var imgCount = imgCount;
	var swapTime = 6000;
	var animTime = 1300;
	var container = this;
	var content = this.find('div.content');
	var imgList = [ ];
	var imgPt = 0;
	
	if(imgCount > 0) {
		// Create an image container DIV for each image
		// $(arguments).each( function(index, array) {
		for(var i = 0; i < imgCount; i++) {
			// if(typeof(this[0]) !== 'string') return;
			// var div = $('<div class="cycleBackground" id="cycleBackground-img' + index + '" style="background: url(' + this + ');"></div>');
			var div = $('<div class="cycleBackground" id="cycleBackground-img' + i + '"></div>');
			imgList.push(div);
			container.prepend(div);
		}
	
		// Clear existing background and set first DIV
		this.css('background-image', 'none');
		swap(0);
		scheduleSwap();
		// Setup cycler
		// Using an anonymous function because the argument above was being stored.
	}
	
	// Return jQuery object
	return this;
	
	// Helper functions
	// 
	function swap(forceImg) {
		imgPt = (forceImg !== undefined) ? forceImg : (imgPt + 1 < imgList.length) ? imgPt + 1 : 0;
		var img = imgList[imgPt];
		// alert(imgList);
		if(forceImg !== undefined) {
			// Don't animate for the force.
			content.before( img );
		} else if(img !== undefined) {
			img.hide();
			content.before( img );
			img.fadeIn(animTime, scheduleSwap );
		}
	}
	function scheduleSwap() {
		setTimeout( function(){swap();}, swapTime);
	}
}
})(jQuery);