/*
	Background Stretcher jQuery Plugin
	© 2009 ajaxBlender.com
	For any questions please visit www.ajaxblender.com 
	or email us at support@ajaxblender.com
	
	Version: 1.2
*/

(function($){
	/*  Variables  */
	var container = null;
	var allImgs = '', allLIs = '', containerStr = '';
	
	var element = this;
	var _bgStretcherPause = false;
	var _bgStretcherTm = null;
	
	$.fn.bgStretcher = function(settings){
		settings = $.extend({}, $.fn.bgStretcher.defaults, settings);
		$.fn.bgStretcher.settings = settings;
		
		function _build(){
			if(!settings.images.length){ return; }
			
			_genHtml();
			
			containerStr = '#' + settings.imageContainer;
			container = $(containerStr);
			allImgs = '#' + settings.imageContainer + ' .slides IMG';
			allLIs = '#' + settings.imageContainer + ' .slides LI';
			
			$(allLIs).hide();
			$(allLIs + ':first').show().addClass('bgs-current');
			$('.bgstretcher_pagination_block .pagination LI:first').addClass('active');
			
			if(!container.length){ return; }
			$(window).resize(_resize);
			
			if(settings.slideShow && $(allImgs).length > 1){
				_bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', settings.nextSlideDelay);
			}
			_resize();
		};
		
		function _resize(){
			var winW = $(window).width();
			var winH = $(window).height();
			var imgW = 0, imgH = 0;
			if(settings.inAdmin) { if(winH < $('#ca_id').height()) { winW = winW - 20; } else { winW = winW - 3; } winH = winH - 3; }
			//	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){
				$(allImgs).width(imgW).height(imgH);
			} else {
				$(allImgs).animate({width: imgW, height: imgH}, 'normal');
			}
		};
		
		function _genHtml(){
			var code = '<div id="' + settings.imageContainer + '" class="bgstretcher"><ul class="slides">';
			for(i = 0; i < settings.images.length; i++){
				code += '<li rel="'+ i +'"><img src="' + settings.images[i] + '" alt="" /></li>';
			}
			code += '</ul></div><div class="bgstretcher_pagination_block"><div class="indent_1"><div class="indent_2"><a href="javascript:void(0)" class="prev_button">'+ settings.prev +'</a><ul class="pagination">';
			for(i = 0; i < settings.images.length; i++){
				code += '<li><a href="javascript:void(0)" rel="'+ i +'">' + (i+1) + '</a></li>';
			}
			code += '</ul><a href="javascript:void(0)" class="next_button">'+ settings.next +'</a></div></div></div>';
			$(code).appendTo(settings.append);
		};
		
		/*  Start bgStretcher  */
		_build();
		
		$('.bgstretcher_pagination_block .pagination LI a').click(function(el) {
			var number = el.currentTarget.rel;
			
			current = $(containerStr + ' .slides LI.bgs-current');
			next = $(containerStr + ' .slides LI:eq('+number+')');
			
			$(containerStr + ' .slides LI').removeClass('bgs-current');
			$('.bgstretcher_pagination_block .pagination LI').removeClass('active');
			next.addClass('bgs-current');
			$('.bgstretcher_pagination_block .pagination LI:eq('+number+')').addClass('active');

			next.fadeIn($.fn.bgStretcher.settings.slideShowSpeed );
			current.fadeOut($.fn.bgStretcher.settings.slideShowSpeed);
			
			$.fn.bgStretcher._clearTimeout();
			if(!_bgStretcherPause){
			  _bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', $.fn.bgStretcher.settings.nextSlideDelay);
			}
		});
		$('.bgstretcher_pagination_block .prev_button').click(function() {
			current = $(containerStr + ' .slides LI.bgs-current');
			var nextRel = current.attr('rel') - 1;
			if(nextRel < 0){ nextRel= settings.images.length - 1 };
			next = $(containerStr + ' .slides LI:eq('+nextRel+')');
			
			$(containerStr + ' .slides LI').removeClass('bgs-current');
			$('.bgstretcher_pagination_block .pagination LI').removeClass('active');
			next.addClass('bgs-current');
			$('.bgstretcher_pagination_block .pagination LI:eq('+nextRel+')').addClass('active');

			next.fadeIn($.fn.bgStretcher.settings.slideShowSpeed );
			current.fadeOut($.fn.bgStretcher.settings.slideShowSpeed);
			
			$.fn.bgStretcher._clearTimeout();
			if(!_bgStretcherPause){
			  _bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', $.fn.bgStretcher.settings.nextSlideDelay);
			}
			
		});
		$('.bgstretcher_pagination_block .next_button').click(function() {
			$.fn.bgStretcher._clearTimeout();
			$.fn.bgStretcher.slideShow();
		});
		
	};
	
	$.fn.bgStretcher.play = function(){
       _bgStretcherPause = false;
       $.fn.bgStretcher._clearTimeout();
       $.fn.bgStretcher.slideShow();
       
	};
	
	$.fn.bgStretcher._clearTimeout = function(){
       if(_bgStretcherTm != null){
           clearTimeout(_bgStretcherTm);
           _bgStretcherTm = null;
       }
	}
	
	$.fn.bgStretcher.pause = function(){
	   _bgStretcherPause = true;
	   $.fn.bgStretcher._clearTimeout();
	};
	
	$.fn.bgStretcher.slideShow = function(){
		current = $(containerStr + ' .slides LI.bgs-current');
		next = current.next();
		if(!next.length){
			next = $(containerStr + ' .slides LI:first');
		}
		
		$(containerStr + ' .slides LI').removeClass('bgs-current');
		$('.bgstretcher_pagination_block .pagination LI').removeClass('active');
		next.addClass('bgs-current');
		$('.bgstretcher_pagination_block .pagination LI:eq('+next.attr('rel')+')').addClass('active');
		
		next.fadeIn( $.fn.bgStretcher.settings.slideShowSpeed );
		current.fadeOut( $.fn.bgStretcher.settings.slideShowSpeed );
		
		if(!_bgStretcherPause){
		  _bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', $.fn.bgStretcher.settings.nextSlideDelay);
		}
	};

	
	
	/*  Default Settings  */
	$.fn.bgStretcher.defaults = {
		imageContainer:             'bgstretcher',
		resizeProportionally:       true,
		resizeAnimate:              false,
		images:                     [],
		imageWidth:                 1200,
		imageHeight:                800,
		nextSlideDelay:             5400,
		slideShowSpeed:             900,
		slideShow:                  true,
		append:						'body',
		prev:						'prev',
		next:						'next',
		inAdmin:					false
	};
	$.fn.bgStretcher.settings = {};
})(jQuery);
