/*
	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 + ' IMG';
			allLIs = '#' + settings.imageContainer + ' LI';
			
			$(allLIs).hide();
			$(allLIs + ':first').show().addClass('bgs-current');
			
			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;

			//	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="wrap">';
			code += '<div id="header"></div><!-- end of header -->';
			code += '<div id="nav-wrap">';
			code += '	<div id="nav-left"></div>';
			code += '	<div id="nav-right" class="mynavbar">';
			code += '		<a href="Home.html" id="p1" class="navbartitle"';
			code += '					onmouseout="HideItem(\'home_submenu\',\'p1\');"';
			code += '					onmouseover="ShowItem(\'home_submenu\',\'p1\');">Home</a>';
			code += '		<a href="Locations.html" id="p2" class="navbartitle"';
			code += '					onmouseout="HideItem(\'locations_submenu\',\'p2\');"';
			code += '					onmouseover="ShowItem(\'locations_submenu\',\'p2\');">Locations</a>';
			code += '		<a href="#" id="p3" class="navbartitle"';
			code += '					onmouseout="HideItem(\'products_submenu\',\'p3\');"';
			code += '					onmouseover="ShowItem(\'products_submenu\',\'p3\');">Products</a>';
			code += '		<a href="#" id="p4" class="navbartitle"';
			code += '					onmouseout="HideItem(\'resourcelibrary_submenu\',\'p4\');"';
			code += '					onmouseover="ShowItem(\'resourcelibrary_submenu\',\'p4\');">Resource Library</a>';
			code += '		<a href="Links.html" id="p5" class="navbartitle"';
			code += '					onmouseout="HideItem(\'links_submenu\',\'p5\');"';
			code += '					onmouseover="ShowItem(\'links_submenu\',\'p5\');">Links</a>';
			code += '		<a href="Contact.html"	id="p6" class="navbartitle"';
			code += '					onmouseout="HideItem(\'contact_submenu\',\'p6\');"';
			code += '					onmouseover="ShowItem(\'contact_submenu\',\'p6\');">Contact</a>';

			code += '		<!-- Home sub-menu, shown as needed  -->';
			code += '		<div class="submenu" id="home_submenu"';
			code += '						onmouseover="ShowItem(\'home_submenu\',\'p1\');"';
			code += '						onmouseout="HideItem(\'home_submenu\',\'p1\');">';
			code += '		</div>';
			code += '		<!-- end of sub-menu for Home -->';

			code += '		<!-- Location sub-menu, shown as needed  -->';
			code += '		<div class="submenu" id="locations_submenu"';
			code += '						onmouseover="ShowItem(\'locations_submenu\',\'p2\');"';
			code += '						onmouseout="HideItem(\'locations_submenu\',\'p2\');">';
			code += '		</div>';
			code += '		<!-- end of sub-menu for Location -->';

			code += '		<!-- Products sub-menu, shown as needed  -->';
			code += '		<div class="submenu" id="products_submenu"';
			code += '						onmouseover="ShowItem(\'products_submenu\',\'p3\');"';
			code += '						onmouseout="HideItem(\'products_submenu\',\'p3\');">';
			code += '			<div class="spaceSubNavTop"></div><!-- end of spaceTop115px -->';
			code += '			<div class="submenubox">';
			code += '				<ul>';
			code += '					<li><a href="ProductGemini.html" class="submenlink">Gemini Proteins</a></li>';
			code += '					<li><a href="ProductPerformanceProtiens.html" class="submenlink">Performance Proteins</a></li>';
			code += '					<li><a href="ProductDairymanEdge.html" class="submenlink">Dairyman\'s Edge</a></li>';
			code += '					<li><a href="ProductDairymanEdgePro.html" class="submenlink">Dairyman\'s Edge Pro</a></li>';
			code += '					<li><a href="ProductPyc14.html" class="submenlink">PYC-14</a></li>';
			code += '					<li><a href="ProductPyc14Con.html" class="submenlink">PYC-14 Concentrate</a></li>';
			code += '					<li><a href="ProductZarmin.html" class="submenlink">Zar-Min</a></li>';
			code += '					<li><a href="ProductMinAd.html" class="submenlink">Min-Ad</a></li>';
			code += '				</ul>';
			code += '			</div>';
			code += '			<!-- end of submenubox for Products -->';
			code += '		</div>';
			code += '		<!-- end of sub-menu for Products -->';

			code += '		<div class="submenu" id="resourcelibrary_submenu"';
			code += '						onmouseover="ShowItem(\'resourcelibrary_submenu\',\'p4\');"';
			code += '						onmouseout="HideItem(\'resourcelibrary_submenu\',\'p4\');">';
			code += '			<div class="spaceSubNavTop"></div><!-- end of spaceTop115px -->';
			code += '			<div class="submenubox">';
			code += '				<ul>';
			code += '					<li><a href="ResourceLibraryGemini.html" class="submenlink">Gemini Proteins</a></li>';
			code += '					<li><a href="ResourceLibraryPerformanceProteins.html" class="submenlink">Performance Proteins</a></li>';
			code += '					<li><a href="ResourceLibraryDairymanEdge.html" class="submenlink">DairyMan\'s Edge</a></li>';
			code += '					<li><a href="ResourceLibraryDairymanEdgePro.html" class="submenlink">DairyMan\'s Edge Pro</a></li>';
			code += '					<li><a href="ResourceLibraryPyc14.html" class="submenlink">PYC-14</a></li>';
			code += '					<li><a href="ResourceLibraryPyc14Con.html" class="submenlink">PYC-14 Concentrate</a></li>';
			code += '					<li><a href="ResourceLibraryZarMin.html" class="submenlink">Zar-Min</a></li>';
			code += '					<li><a href="ResourceLibraryMinAd.html" class="submenlink">Min-Ad</a></li>';
			code += '				</ul>';
			code += '			</div>';
			code += '			<!-- end of submenubox for Resource Library -->';
			code += '		</div>';
			code += '		<!-- end of sub-menu for Resource Library -->';

			code += '	</div>';
			code += '	<!-- end of nav-right -->';
			code += '</div>';
			code += '<!-- end of nav-wrap -->';
			code += '<div id="shadowLine"></div> <!-- end of shadowLine -->';
			code += '<div id="homer">';
			var rand1 = Math.floor(Math.random() * settings.images.length);
			code += '<img class="bgstretcher" src="' + settings.images[rand1] + '" alt="" />';
			//for(i = 0; i < settings.images.length; i++){
				//code += '<img class="bgstretcher" src="' + settings.images[i] + '" alt="" />';
			//}
			//code += '<div id="' + settings.imageContainer + '" class="bgstretcher"><ul>';
			code += '<!-- Main Body Content -->';
			code += '<div id="inner-wrap-homer">';
			code += '	<div id="left">';
			code += '		<div id="left-innertop-home">';
			code += '			<div id="left-1-home">';
			code += '			</div>';
			code += '			<!-- end of left-1 -->';
			code += '			<div id="left-2-home">';
			code += '			</div>';
			code += '			<!-- end of left-2 -->';
			code += '			<div id="left-3-home">';
			code += '				Papillon develops, produces, and markets high quality products for';
			code += '				animal agriculture.  Since 1981, our pledge has been to incorporate';
			code += '				enhanced technologies with the core business values of the industry.';
			code += '			</div>';
			code += '			<!-- end of left-3 -->';
			code += '			<div id="left-4">';
			code += '				<br/>';
			code += '				';
			code += '			</div>';
			code += '			<!-- end of left-4 -->';
			code += '		</div>';
			code += '		<!-- end of left-innertop -->';
			code += '	</div>';
			code += '	<!-- end of left -->';
			code += '   <div id="main">';
			code += '   	<div id="right">';
			code += '   		<div id="right-innerTop">';
			code += '   		</div>';
			code += '   		<!-- end of right-innerTop -->';
			code += '   	</div>';
			code += '   	<!-- end of right -->';
			code += '   	<div id="content">';
			code += '   	</div>';
			code += '   	<!-- end of content -->';
			code += '   </div>';
			code += '   <!-- end of main -->';
			code += '   </div>';
			code += '   <!-- end of inner-wrap -->';
			code += '   </div>';
			code += '   <div id="footer">';
			code += '		<div align="center"><span class="style6">';
			code += '		30 N. Harrison St. &bull;';
			code += '		Suite 204 &bull;';
			code += '		P.O. Box 1161 &bull;';
			code += '		Easton, MD &nbsp;21601<br />';
			code += '		Tel: 410-820-7400 &bull;';
			code += '		Toll Free: 800-888-5688 &bull;';
			code += '		Fax: 410-822-0429 &bull;';
			code += '		<a href="Contact.html">info@papillon-ag.com</a>';
			code += '		</span></div>';
			code += '   </div>';
			code += '   <!-- end of footer -->';
			code += '   </div>';
			code += '<!-- end of wrap -->';
			code += '<div>';
			$(code).appendTo('body');
		};
		
		/*  Start bgStretcher  */
		_build();
	};
	
	$.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(){
		var current = $(containerStr + ' LI.bgs-current');
		var next = current.next();
		if(!next.length){
			next = $(containerStr + ' LI:first');
		}
		
		$(containerStr + ' LI').removeClass('bgs-current');
		next.addClass('bgs-current');
		
		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:             'bgstetcher',
		resizeProportionally:       false,
		resizeAnimate:              false,
		images:                     [],
		imageWidth:                 800,
		imageHeight:                768,
		nextSlideDelay:             3000,
		slideShowSpeed:             'normal',
		slideShow:                  false
	};
	$.fn.bgStretcher.settings = {};
})(jQuery);
