// JavaScript Document

;(function($){
	/*  Variables  */
	var _bgStretcherTm = null;
	var containerStr = '';
	var allImgs = '';
	var current = 0;
	
	$.fn.bgStretcher = function(settings){
		settings = $.extend({}, $.fn.bgStretcher.defaults, settings);
		$.fn.bgStretcher.settings = settings;
		
		function _build(){
			if(!settings.images.length){ return; }
			
			containerStr = '#' + settings.imageContainer;
			container = $(containerStr);
			if(!container.length){ return; }
			
			allImgs = settings.images;
			
			$(containerStr).css({ background: "transparent url('"+allImgs[current]+"') no-repeat right top" });
			
			if(settings.slideShow && $(allImgs).length > 1){
				_bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', settings.nextSlideDelay);
			}
		};
		
		/*  Start bgStretcher  */
		_build();
	};
	
	$.fn.bgStretcher._clearTimeout = function(){
       if(_bgStretcherTm != null){
           clearTimeout(_bgStretcherTm);
           _bgStretcherTm = null;
       }
	};
	
	$.fn.bgStretcher.slideShow = function(){
		current++;
		if(current>allImgs.length-1) current = 0;
		$(containerStr).css({ background: "transparent url('"+allImgs[current]+"') no-repeat right top" });
		_bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', $.fn.bgStretcher.settings.nextSlideDelay);
	};
	
	/*  Default Settings  */
	$.fn.bgStretcher.defaults = {
		imageContainer:             'bgstretcher',
		images:                     [],
		nextSlideDelay:             3000,
		slideShow:                  true
	};
	$.fn.bgStretcher.settings = {};
})(jQuery);