// bof closure
;(function($) {	

	// plugin declaration
	$.fn.bannerFade = function(data) {
		
		// some variables
		var 
			$wrap = $(this).css({
				position:'relative'
			}).parents().eq(0) /*.css('backgroundImage','url()') */,
			d = {
				width:'980px',
				height:'310px'
			},
			img = data.images,
			delay = 8000,
			speed = 1000,
			count = 0,
			cur = 0,
			t;
			
		// loop through the data
		$.each(img, function(i) {			

			// build row
			var $row = $('<div>')
				.addClass('nxBannerRow')
					.attr('id','nxBannerRow'+i)					
						.css({
							background:'url('+img[i].src+') no-repeat top left',
							position:'absolute',
							opacity:(i == 0) ? 1 : 0,
							height:d.height,
							width:d.width,
							padding:0,
							margin:0,
							left:0,
							top:0
						});
			
			// add
			$wrap.prepend($row);
			
			// increase counter
			count++;
		});
		
		// check if more than one image
		if (count > 2) t = setInterval(swith, delay);
		
		// switch banner
		function swith() {
			
			var rand = Math.floor(Math.random()*count);
			if (rand > count) rand = 0;
			if (rand == cur) rand++;
			$('#nxBannerRow'+rand).fadeTo(speed, 1);			
			$('#nxBannerRow'+cur).fadeTo(speed, 0);			
			cur = rand;
		};		
	};

// eof closure
})(jQuery);
