var $j = jQuery.noConflict();
/*
	Background Stretcher jQuery Plugin
	© 2009 ajaxBlender.com
	For any questions please visit www.ajaxblender.com 
	or email us at support@ajaxblender.com
*/

;(function($j){
	/*  Variables  */
	var container = null;
	var allImgs = '', allLIs = '', containerStr = '';
	
	var element = this;
	
	$j.fn.bgStretcher = function(settings){
		settings = $j.extend({}, $j.fn.bgStretcher.defaults, settings);
		$j.fn.bgStretcher.settings = settings;
		
		function _build(){
			if(!settings.images.length){ return; }
			
			_genHtml();
			
			containerStr = '#' + settings.imageContainer;
			container = $j(containerStr);
			allImgs = '#' + settings.imageContainer + ' IMG';
			allLIs = '#' + settings.imageContainer + ' LI';
			
			$j(allLIs).hide();
			$j(allLIs + ':first').show().addClass('bgs-current');
			
			if(!container.length){ return; }
			$j(window).resize(_resize);
			
			if(settings.slideShow && $j(allImgs).length > 1){
				setTimeout('$j.fn.bgStretcher.slideShow()', settings.nextSlideDelay);
			}
			_resize();
		};
		
		function _resize(){
			var winW = $j(window).width();
			var winH = $j(window).height();
			var imgW = 0, imgH = 0;

			//	Update container's height
			container.width(winW);
			container.height(winH);
			
			//	Non-proportional resize
			if(!settings.resizeProportionally){
				imgW = winW;
				imgH = winH;
			} else {
				var initW = settings.imageWidth, initH = settings.imageHeight;
				var ratio = initH / initW;
				
				imgW = winW;
				imgH = winW * ratio;
				
				if(imgH < winH){
					imgH = winH;
					imgW = imgH / ratio;
				}
			}
			
			//	Apply new size for images
			if(!settings.resizeAnimate){
				$j(allImgs).width(imgW).height(imgH);
			} else {
				$j(allImgs).animate({width: imgW, height: imgH}, 'normal');
			}
		};
		
		function _genHtml(){
			var code = '<div id="' + settings.imageContainer + '" class="bgstretcher"><ul>';
			for(i = 0; i < settings.images.length; i++){
				code += '<li><img src="' + settings.images[i] + '" alt="" /></li>';
			}
			code += '</ul></div>';
			$j(code).prependTo('body');
		};
		
		/*  Start bgStretcher  */
		_build();
	};
	
	$j.fn.bgStretcher.slideShow = function(){
		var current = $j(containerStr + ' LI.bgs-current');
		var next = current.next();
		if(!next.length){
			next = $j(containerStr + ' LI:first');
		}
		
		$j(containerStr + ' LI').removeClass('bgs-current');
		next.addClass('bgs-current');
		
		next.fadeIn( $j.fn.bgStretcher.settings.slideShowSpeed );
		current.fadeOut( $j.fn.bgStretcher.settings.slideShowSpeed );
		
		setTimeout('$j.fn.bgStretcher.slideShow()', $j.fn.bgStretcher.settings.nextSlideDelay);
	};
	
	/*  Default Settings  */
	$j.fn.bgStretcher.defaults = {
		imageContainer:			'bgstretcher',
		resizeProportionally:	true,
		resizeAnimate:			false,
		images:					[],
		imageWidth:				1024,
		imageHeight:			768,
		nextSlideDelay:			8000,
		slideShowSpeed:			2000,
		slideShow:				true
	};
	$j.fn.bgStretcher.settings = {};
})(jQuery);
