/* ============ PRELOADER PLUGIN =========== */
(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
					.attr("src", imgArr[i])
				);
			}
			
		}
	});
})(jQuery);


/* ============ PRELOADER SET =========== */	

$(function() {
		var step = 0;
		var stepHeight;
		$.preload([
			"img/activities-photo.jpg",
			"img/contact-photo.jpg",
			"img/costs-photo.jpg",			
			"img/join-joinnow.png",
			"img/join-photo.jpg",
			"img/nav-aim-logo.png",
			"img/nav-bmf-logo-black.png",
			"img/nav-bmf-logo-white.png",
			"img/nav-mam-logo.png",
			"img/nav-next.png",
			"img/nav-prev.png",
			"img/rides-photo.jpg",
			"img/scrollbar-down.gif",
			"img/scrollbar-up.gif",
			"img/start-enter.png",
			"img/start-photo.jpg",
			"img/started-photo.jpg",
			"img/training-photos.jpg",
			"img/where-map.jpg",
			"img/where-photo.jpg",
			"img/who-photo.jpg"		
		], {
			init: function(loaded, total) {
				step = $(window).width()/total;
			},
			loaded: function(img, loaded, total) {
			 
				//$("#loaderBar").animate({
				 //   width: $("#loaderBar").width() + step
				// }, 50 );
			
			
				$("#loaderBar").width($("#loaderBar").width() + step);


			},
			loaded_all: function(loaded, total) {

				if (!$.browser.msie) {
						$("#loaderWrapper").fadeOut(200,function(){
							initSite();
							$("#pageFrame").fadeIn(600,function(){loadScrollers();});
							$("#nav").fadeIn(600);
							
						});
				} else {

						$("#loaderWrapper").fadeOut(0,function(){
							initSite();
							$("#pageFrame").fadeIn(0,function(){loadScrollers();});
							$("#nav").fadeIn(0);
						});
				}		
							
			}
		});
	
	});



// Adjust body width and height to window's size //


        var theWindow        = $(window),
			theBody 		 = $('body'),
			pageFrame		 = $('#pageFrame'),
			pageSlider		 = $('#pageSlider'),
			pageLenght		 = $('.page').length,
			pageSpeed		 = 400;


		function resizeBody() {
		
			var windowHeight = (theWindow.height() < 750) ? 750 : theWindow.height();
			
			var windowWidth = (theWindow.width() < 1185) ? 1185 : theWindow.width();

			if (theWindow.height() < 750) {
				$('.aimLogo').width(69).css('margin','15px 0 0 30px');
				$('.bmfLogo').width(68).css('margin','15px 0 0 31px');				
			} else {
				$('.aimLogo').attr('style', '');
				$('.bmfLogo').attr('style', '');
			}

			theBody.height(windowHeight);
			theBody.width(windowWidth);
			$('.start .pageBg').width(windowWidth).height(windowHeight);

			pageFrame.height(windowHeight);
			pageFrame.width(windowWidth);
			$('.page').width(windowWidth);
			pageSlider.height(windowHeight);		
			pageSlider.width(windowWidth * pageLenght);
			var cIndex = $('.currentPage').index();
			pageSlider.css('left',-1 * cIndex * windowWidth + 'px');
		
       
        }

		function scaleHeight($elem,ratio) {
        	$elem.height(pageSlider.height());
        	pageBgWidth = $elem.find('img').width();
			contentWidth = $elem.parent().find('.pageContent').outerWidth(true);
			pageWidth = $('.page').width();

			if(pageBgWidth+contentWidth < $('.page').width()) {
				if (pageBgWidth > 0) {

					$elem.width(pageBgWidth);
				} else {
					if (ratio == 'ratio1') {
						pageBgWidth = pageSlider.height() * 0.66;
					} else if (ratio == 'ratio2') {
						pageBgWidth = pageSlider.height() * 0.54;
					}
					$elem.width(pageBgWidth);
				}
			} else {
				$elem.width(pageWidth-contentWidth);
			}
        	
 			
        	//pageBgWidth = ;
        	//if($elem.width() > $elem.find('img').width()) {
        		//$elem.width($elem.find('img').width());
        	//}

        
        }
        
        function scaleWidth($elem) {
        	$elem.width(theWindow.width());
        
        }         

        

        
        function swapLogo ($ep) {
        	
        	if ($ep.hasClass('start') || $ep.hasClass('who') || $ep.hasClass('training')) {
        		logoColor = 'white';	
        	} else logoColor = 'black';
        
        	$bmfLogo = $('.bmfLogo');
        	if (!$bmfLogo.hasClass(logoColor)) {
        		$bmfLogo.attr('src','img/nav-bmf-logo-' + logoColor + '.png');
        		if(logoColor=='black') {
        			$bmfLogo.removeClass('white');
        		} else {
        			$bmfLogo.removeClass('black');
        		}
        		$bmfLogo.addClass(logoColor);
        	}
        
        }
        
        function highlightNav(navTarget) {
        	
			$selected = $('#nav ul li a.selected');
       		
        	if (navTarget == 'prev') {
        		$selected.parent().prev().find('a').addClass('selected');


        	} else if (navTarget == 'next') {
        		$selected.parent().next().find('a').addClass('selected');


        	
        	} else {
        		$tobeSelected = $('#nav ul li a[href$="' + navTarget + '"]');
        		$tobeSelected.addClass('selected');
        	}
        	if($selected.length>0) {
        		$selected.removeClass('selected');
        	}
        }
        
        
        function gotoPage(pageTarget) {
        	var $currentPage = $('.currentPage');
        	var currentIndex = $currentPage.index();
        	//clicked on next
        	highlightNav(pageTarget);
        	if((pageTarget == 'next') && (currentIndex+1 < pageLenght)) {
				$toPage	= $currentPage.next();
        		swapLogo($toPage);
        		$currentPage.toggleClass('currentPage');
        		$toPage.toggleClass('currentPage');
				pageSlider.animate({
					left: '-=' + theBody.width()
				}, pageSpeed, "easeOutQuad", function() {
					

				});        	
        	
        	} else if((pageTarget == 'prev') && (currentIndex > 0)) { //clicked on prev
						$toPage	= $currentPage.prev();
						swapLogo($toPage);
		        		$currentPage.toggleClass('currentPage');
		        		$toPage.toggleClass('currentPage');						
							pageSlider.animate({
							left: '+=' + theBody.width()
						}, pageSpeed, "easeOutQuad", function() {
							// Animation complete.
							
						});	        	        	        	
        	} else if((pageTarget != 'next') && (pageTarget != 'prev')) { //clicked on nav item

		        	var $toPage = $('.' + pageTarget);
		        	var toIndex = $toPage.index();						        		
		        	if (currentIndex != toIndex) {
		        		$currentPage.toggleClass('currentPage');
		        		$toPage.toggleClass('currentPage');
		        	}
		        	
	        		if ((currentIndex>=0) && (currentIndex+1 <= pageLenght) && (currentIndex!=toIndex)) {

						var speedRatio = Math.abs((currentIndex - toIndex)/2);
						swapLogo($toPage);
						var toDirection = '-=';
	        			if (currentIndex > toIndex) {
	        				toDirection = '+=';
	        			}
			   				var pageOffset = Math.abs(currentIndex - toIndex);
						pageSlider.animate({
							left: toDirection + (theBody.width() * pageOffset)
							
						}, pageSpeed * speedRatio, "easeOutQuad", function() {
							// Animation complete.
						});         				
        		
        		}
        		
        	} //end of else		
        	
        	
        } //end of gotoPage
        
$(document).ready(function() {    

        //resize page container and body
       

		$('.next,.prev,#nav ul li a,.slideToPage').click(function() {
			toTarget = $(this).attr('href');
			toTarget = toTarget.replace('#', "");
			gotoPage(toTarget);
		});

		resizeBody();
		
		scaleHeight($('.who .pageBg'),'ratio1');
		scaleWidth($('.started .pageBg'));
		scaleHeight($('.costs .pageBg'),'ratio2');		
		scaleHeight($('.rides .pageBg'),'ratio1');
		scaleHeight($('.training .pageBg'),'ratio1');
		scaleHeight($('.activities .pageBg'),'ratio2');		
		scaleWidth($('.where .pageBg'));
		scaleWidth($('.join .pageBg'));
		scaleHeight($('.contact .pageBg'),'ratio1');	
					
        theWindow.resize(function() {
                
                resizeBody();
				scaleHeight($('.who .pageBg'),'ratio1');
				scaleWidth($('.started .pageBg'));
				scaleHeight($('.costs .pageBg'),'ratio2');		
				scaleHeight($('.rides .pageBg'),'ratio1');
				scaleHeight($('.training .pageBg'),'ratio1');
				scaleHeight($('.activities .pageBg'),'ratio2');		
				scaleWidth($('.where .pageBg'));
				scaleWidth($('.join .pageBg'));
				scaleHeight($('.contact .pageBg'),'ratio1');
        });


});

function initSite() {

	$('.start .pageBg').bgStretcher({
		images: ['img/start-photo.jpg'], imageWidth: 1160, imageHeight: 750
	});
	var url=window.location;
	var anchor=url.hash.substring(1); 
	if(anchor.length) {
		gotoPage(anchor);
		
	}	else {
		$('#nav ul li:nth-child(2) a').addClass('selected');
	}	
	
	
}

function loadScrollers() {
	$(".who #mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
	$(".costs #mcs2_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
	$(".activities #mcs3_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
	$(".faqs #mcs4_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",10);
}






