(function($)  {
    var current_rig_slide_image = 0;		// Index of the current slide
    var rig_slide_images = new Array();		// Array to hold the information for each image slide
    
    // Image control -- go to next image
    function next_slide()  {
        current_rig_slide_image++;
        if ( current_rig_slide_image > rig_slide_images.length-1 )  {
            current_rig_slide_image = 0;
        }
    }
    
    // Image control -- back to previous image
    function prev_slide()  {
        current_rig_slide_image--;
        if ( current_rig_slide_image < 0 )  {
            current_rig_slide_image = rig_slide_images.length-1;
        }
    }
    
	// Populates the newly created html with the data that was scraped from the original HTML:
	// 1.) Linking URL, 2.) Source of the image, 3.) Image caption, 4.) Index of the image in the set.
	function display_slide()  {
		if(!rig_slide_images[current_rig_slide_image]) {
			return;
		} else {
			$('div#newArtPics a').attr('href', rig_slide_images[current_rig_slide_image].href);
			$('div#picRun>a>img').attr('src', rig_slide_images[current_rig_slide_image].src);
			$('div#newArtPics div#picText').html(rig_slide_images[current_rig_slide_image].text);
			$('#buttonNav').text(rig_slide_images[current_rig_slide_image].currentImage);
		}
	}
     
	$(document).ready(function()  {
		// Scans the HTML contained in each <div> within <div id="artPics"> for the 
		// 1.) Linking URL, 2.) Source of the image, 3.) Image caption, 4.) Index of the image in the set
		// and writes it to the array "slide".
		var art_images = $('div#artPics div');
		for ( var i=0; i<art_images.length; i++ )  {
			var self = art_images[i];
			
			var new_html = '';
			var img_url = $('img', self).attr('src');
			var img_link = $('a', self).attr('href');
			var text_description = '';
			// IE and Firefox handle <br> different -- IE capitalizes it automagically, and counts the
			// array differently.  This if/else handles that to make it work appropriately.
			var caption_split = $(self).html().split(/(<br>|<BR>)/);
			if ( caption_split.length == 5 )  {
				text_description = caption_split[caption_split.length-3];
			} else {
				text_description = caption_split[caption_split.length-2];
			}
			var indexItem = i + 1;
			// Now let's push the necessary image information to the "Slide" array.
			var slide = {src: img_url, href: img_link, text: text_description, currentImage: indexItem};
			// And now let's push that slide to the caroussel.
			rig_slide_images.push(slide);
			// And since we are finished with that particular div of information, let's remove it and go to the next one.
			jQuery(art_images[i]).remove();
		}
		
	
		// This builds the new HTML with the tags that will be populated by the display_slide function.
		$('div#artPics').after('<div id="newArtPics" style="display: none;"></div>');
		$('div#newArtPics').html('<div id="picRun"><a><img /></a><div id="picText" style="font-size:11px; text-align:center;"></div></div>');
		// If there is more than 1 image, we need to show the next/previous buttons -- otherwise, they get left off.
		if (rig_slide_images.length >= 2) {
			$('div#newArtPics').after('<div id="nextPrev"><a class="prev"><img src="/images/button_arrowLeft.gif" class="leftArrow" /></a>' + '<div id="navIndex"><span id="buttonNav"></span>' + '/' + rig_slide_images.length +  '</span></div><a class="next"><img src="/images/button_arrowRight.gif" class="rightArrow" /></a></div>');
		}
	   
	   // Loads the previous slide when you click the Previous button.
        $('div#nextPrev a.prev').click(function()  {
            prev_slide();
            display_slide();
		  return false;
        });
        
	   // Loads the next slide when you click the Next button.
        $('div#nextPrev a.next').click(function()  {
            next_slide();
            display_slide();
		  return false;
        });
        
	   // Now populate the new HTML with the stuff in the array so we get the new layout.
        display_slide();
        
	   
	   // Hide the old HTML, and show the new stuff.
        $('div#artPics').hide();
        $('div#newArtPics').show();
    });
})(jQuery);
// Amen.
