(function($) {
		  
	$.fn.c1slideshow = function(settings){
		var config = {
			'vRoot'			: 'none',
			'baseFolder'	: 'homepage_flash',
			'folderName'	: 'set1',
			'random'		: 'yes',
			'delay'			: 5,
			'fadeSpeed'		: 1
		};
		if (settings){$.extend(config, settings);}
		
		// loop thru each matched element
		return this.each(function(index, list) {
			var element = this;
			var myContainer = "#pictures-" + config.folderName;
			
			//AJAX AND BUILD ARRAY
			var imagesArray = new Array();
			
			var xmlURL = "/" + config.vRoot + "/assets/templates/utilities/getrandomimagesxml.asp?base_folder=" + config.baseFolder + "&folderName=" + config.folderName;
			
			$.ajax({
				type: "GET",
				url: xmlURL,
				success: doWork
			});
			
			function doWork(xml){
				if (window.DOMParser){
					parser=new DOMParser();
					xmlDoc=parser.parseFromString(xml,"text/xml");
				} else {
					xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
					xmlDoc.async="false";
					xmlDoc.loadXML(xml); 
				}
				var folderPath = $(xmlDoc).find("album").attr("lgPath");
				$("img",xmlDoc).each(function(){
					imagesArray.push(folderPath + $(this).attr("src"));
				});
				//SORT IMAGES RANDOMLY (IF CALLED)
				if(config.random !== "no"){
					imagesArray = imagesArray.sort(function(){return 0.5 - Math.random()});
				}
				
				buildStructure();
				
				function buildStructure(){
					//BUILD CONTAINER FOR ROTATOR
					var structure = "<ul id='pictures-" + config.folderName + "' style='position:relative; list-style:none; margin:0px; padding:0px'></ul>";
					$(element).prepend(structure);
					//INITIAL FUNCTION RUN
					loadImage(0);
				}
				
				//BUILDS DATA
				function loadImage(currentFile){
					var totalFiles = imagesArray.length;
					if(currentFile<totalFiles){
						//ADD LI FOR EACH ITEM
						$("<li class='imgHolder loading' />").css({
							"margin" 	: "0px",
							"padding"	: "0px",
							"display"	: "none",
							"position"	: "absolute",
							"top"		: "0px",
							"left"		: "0px" 
						}).prependTo(myContainer);
						
						//CURRENT LI TO LOAD TO
						var currentLI = $("li.imgHolder:eq(0)", myContainer);
						
						//PRELOAD
						$("<img />").load(function(){
							$(this).appendTo(currentLI);
							$(currentLI).removeClass("loading").show();
							loadImage(currentFile+1);
							//RUN TIMER ON SECOND IMAGE COMPLETE
							if(currentFile == 1){
								rotateImages();
								rotateTimer();
							}
						}).attr('src',imagesArray[currentFile]);
					}
				}
				
				
				//ROTATE FUNCTION
				function rotateTimer(){
					rotateTimer = setInterval(function(){
						rotateImages();
					}, config.delay*1000);	
				}
				
				function rotateImages(){
					if(!$("li:last", myContainer).prev().hasClass("loading")){
						if(!$("li:last", myContainer).prev().hasClass("loading")){
							$("li:last", myContainer).prev().show();
							$("li:last", myContainer).fadeOut(config.fadeSpeed*1000, function(){
								$(this).parent().prepend(this);
								$(this).show();
							});
						}
					}
				}
			
			
			}
			
			
			
		});
	};

})(jQuery); 
