/*
 * jQuery custom background slider plugin
 * v0.1.1
 * last change: 2011-04-23 19:00
 * options:
 * img_array: array of images used in slideshow
 * timeBetweenImages: number determining time between transitions; in miliseconds
 * fadeType: A string or number determining how long the animation will run. Strings = 'fast', 'slow'; number in miliseconds
 * adjustHeight: boolean determining if image is vertically visible with right scrollbar on browser window smaller than image
 * min-height: string with px units determining css min-height property of image container
 * min-width: string with px units determining css min-width property of image container
 * backgroundPosition: fixed or absolute background position
 */

(function($) {

	$.fn.extend({
		bgSlider: function(options) {
			// default settings
			var defaults = {
				fadeType: 'slow',
				timeBetweenImages: 3000,
				adjustHeight: false,
				minHeight: false,
				minWidth: false,
                                backgroundPosition: 'fixed'
			};

			var options = $.extend(defaults, options);

			return this.each(function() {
				var o = options;
				//Assign current element to variable
				var obj = $(this);
				
				var counter = 0;
				var currentDiv = 1;
				var currentDiv = 2;
				var imgContainerPrefix = 'imgContainer';
				var imgContainerPrefixWithId = "#" + imgContainerPrefix;
				
				obj.prepend('<div id="' + imgContainerPrefix + '-1"></div><div id="' + imgContainerPrefix + '-2"></div>');
				//obj.css({"z-index": "-30"});
				obj.css({"background-image": "none"});
				
				if(o.adjustHeight) {
					$("html").css({"height": "100%"});
				}
				
				$(imgContainerPrefixWithId + "-1").css({
					"background-repeat": "no-repeat",
					"background-position": "top center",
					"position": o.backgroundPosition,
					"top": "0px",
					"left": "0px",
					"z-index": "-10",
					"width": "100%",
					"height": "100%"
				});
				
				$(imgContainerPrefixWithId + "-2").css({
					"background-repeat": "no-repeat",
					"background-position": "top center",
					"position": o.backgroundPosition,
					"top": "0px",
					"left": "0px",
					"z-index": "-20",
					"width": "100%",
					"height": "100%"
				});
				
				if(o.minHeight) {
					$(imgContainerPrefixWithId + "-1").css({
						"min-height": o.minHeight
					});
					$(imgContainerPrefixWithId + "-2").css({
						"min-height": o.minHeight
					});
				}
				
				if(o.minWidth) {
					$(imgContainerPrefixWithId + "-1").css({
						"min-width": o.minWidth
					});
					$(imgContainerPrefixWithId + "-2").css({
						"min-width": o.minWidth
					});
				}
			
				(function initSlideShow(img_array) {
					//set current div container
					currentDiv = (currentDiv == 1) ? 2 : 1;
					previousDiv = (currentDiv == 1) ? 2 : 1;
					
					var curImg = img_array[counter] + "?" + (new Date()).getTime();
					
					$('<img />')
						.attr('src', curImg)
						.load(function(){
							$(imgContainerPrefixWithId + "-" + currentDiv).css({
								'background-image' : 'url(' + curImg + ')',
								'display' : 'none'
							});
							
							if(o.adjustHeight) {
								$(imgContainerPrefixWithId + "-" + currentDiv).css({
									'height' : this.height + 'px'
								});
							}
							
							$(imgContainerPrefixWithId + "-" + previousDiv).fadeOut(o.fadeType);
							
							$(imgContainerPrefixWithId + "-" + currentDiv).fadeIn(o.fadeType, function() {
								setTimeout(function() {
										counter ++;
										if(counter == o.img_array.length) {
											counter = 0;
										}
										
										initSlideShow(img_array);
								}, o.timeBetweenImages);
							});
						});
				})(o.img_array);
				
			});  
		}

	});

})(jQuery);
