

(function($) {
$(document).ready(function () {
	
	
	//define jLoader object with some default config settings
	$.jLoader = {
		defaults: {
			imgDir: "img/",
			imgContainer: "",
			imgTotal: 6,
			imgFormat: ".jpg",
			simpleFileNames: true
		}
	};
	
	//extend jquery with the plugin
	$.fn.extend({
		jLoader:function(config, fileNames) {
			
			//use defaults or properties supplied by user
			var config = $.extend({}, $.jLoader.defaults, config);
			
			//get the id of the selected element
			config.imgContainer = this.attr("id");
			
			//use simpleLoad to load numerically named images or complexLoad for user-specified file names
			(config.simpleFileNames == true) ? simpleLoad(config) : complexLoad(config, fileNames) ;
			
			//return the jquery object for chaining
			return this;
		}
	});
	
  //simple image load
	function simpleLoad(config) {
		
		//loop through images and load sequentially
		for (var x = 1; x < config.imgTotal + 1; x++) {
			
			//create image
			$("<img />").attr({ 
				id: "image" + x, 
				src: config.imgDir + x + config.imgFormat,
				title: "Image" + x
			}).appendTo("#" + config.imgContainer).css({ display: "none" });			
		}
	};
	
	//complex image load
	function complexLoad(config, fileNames) {
		
		//loop through the array in the second argument
		for (var x = 0; x < fileNames.length; x++) {
				
				//create image
				$("<img />").attr({
					id: fileNames[x],
					src: config.imgDir + fileNames[x] + config.imgFormat,
					title: fileNames[x]
				}).appendTo("#" + config.imgContainer).css({ display: "none" });
			if(x == fileNames.length-1){
				//Call code to swap background
				
			}
		}
	};
	
	//Swapout
	/*
	function swapim(config, fileNames, counter, timeoutLength){
		var zImg1 = $("backgroundImage1").css("z-index");
		var zImg2 = $("backgroundImage2").css("z-index");
		if(zImg1 == 2){
			$("backgroundImage1").fadeOut("slow", function(){
				$("backgroundImage1").src = config.imgDir + fileNames[counter] + config.imgFormat;
				$("backgroundImage1").css("z-index", 1);
				$("backgroundImage2").css("z-index", 2);
			});
			$("backgroundImage2").fadeIn("slow");
			
		}else{
			$("backgroundImage2").fadeOut("slow", function(){
				$("backgroundImage2").src = config.imgDir + fileNames[counter] + config.imgFormat;
				$("backgroundImage2").css("z-index", 1);
				$("backgroundImage1").css("z-index", 2);
			});
			$("backgroundImage1").fadeIn("slow");
		}
		
		counter++;
		if(counter > 6){
			counter = 1;
		}
		//setTimeout(swapim,timeoutLength,[config, fileNames, counter, timeoutLength]);
	}*/
});
})(jQuery);
