(function ($) {

	// Options to pass in --------------------------------------------------------------------------------------
	// randomizer = '/webtools/randomimage/randomimage.cfm';	// Path to random image AJAX app
	// image_dir = '/images/refresh/rotating_new';				// Folder for random images
	// delay = 2;												// Delay (in seconds) between rotating images
	// fadetime = 1;											// Seconds for fading in replacement images
	
	// Do not change these variables
	var g_lastnumber = 1;
	var g_imagecount = 0;
	var g_xml;


	$.fn.cross = function(options) {
		
		return this.each(function(i) {
			// Cache start image
			var $$ = $(this);
			

			// Get random image set
			$.ajax({
				type: "GET",
				url: options.randomizer,
				data: {
					action: 'getrandomimageset',
					folder: options.image_dir
				},
				dataType: "xml",
				success: function(xml) {
					if ($(xml).find('url').size() > 0) {
						g_xml = $(xml);
						g_imagecount = g_xml.find('root').attr('count');
						g_lastnumber = 1 + Math.floor(g_imagecount * (Math.random() % 1));
						var src = g_xml.find('#' + g_lastnumber).text();
						setupFader(src);
					}
				}
			});	


			var setupFader = function(src) {

				// Get target
				//var target = $$.css('backgroundImage').replace(/^url|[\(\)'"]/g, '');
				//$$.css('backgroundImage', 'url(' + src + ')');

				// Wrap img element in span
				var child =	$$.wrap('<div style="position: relative; float: left;"></div>')
					// Change selector to newly created span
					.parent()
					// Add image inside span
					.prepend('<img>')
					// Change selector to newly created image
					.find(':first-child')
					// Set the image to the target
					.attr('src', src);

				// Position original image
				// the CSS styling of the start image needs to be handled
				// differently for different browsers
				if ($.browser.msie || $.browser.mozilla) {
					$$.css({
						'position' : 'absolute',
						'left' : 0,
						'top' : this.offsetTop
					});
				} else if ($.browser.opera && $.browser.version < 9.5) {
					// Browser sniffing is bad - however opera < 9.5 has a render bug 
					// so this is required to get around it we can't apply the 'top' : 0 
					// separately because Mozilla strips the style set originally somehow...                    
					$$.css({
						'position' : 'absolute', 
						'left' : 0,
						'background' : '',
						'top' : "0"
					});
				} else { // Safari
					$$.css({
						'position' : 'absolute', 
						'left' : 0,
						'background' : ''
					});
				}

				// Fade out initial image to show new image
				$$.stop().animate({
					opacity : 0
				}, options.fade_time * 1000, function() {
					
					// Remove loading indicator
					$$.removeClass('loading');

					// Change image every X seconds
					var nOpacity = 1;
					$(document).everyTime(options.delay * 1000, function(i) {
						
						// Get next image number
						g_lastnumber = (g_lastnumber < g_imagecount) ? g_lastnumber + 1 : 1;
							
						// Set source or background of correct image
						var src = g_xml.find('#' + g_lastnumber).text();
						(nOpacity) ? $$.css('backgroundImage', 'url(' + src + ')') : child.attr('src', src);
						
						// Animate
						$$.stop().animate({
							opacity: nOpacity
						}, options.fade_time * 1000);
						if (nOpacity) nOpacity = 0; else nOpacity = 1;
									
					});

					// END After initial image has faded in
				}); 

			};
		
		});

	};
	

})(jQuery);

