$(document).ready(function(){
	
	// The index variable will keep track of which image is currently showing
	var index = 0;
	
	//carousel interchange balls
	$('.clickTransit').click(function() {
		clearAll();
		$(this).attr('src', 'image/carouselBall1.png');
		index = Number(this.id.substr(4));
		$.backstretch(images[index], {speed: 500});
		$('h2 img').attr('src', imagesTitle[index]);
		$('.blurb').text(blurbs[index]);
	});
	
	function clearAll() {
		for (i=0;i<5;i++) {
			$('#ball'+i).attr('src', 'image/carouselBall2.png');
		}
	}
	
	/*
	if($("#container_sub").length > 0) {
		var tempHeight = $("#container_sub").css('height');
		var newTempHeight = tempHeight.split("px");
		tempHeight = (0-newTempHeight[0]/2);
		if(newTempHeight[0]<900) {			
			$("#container_sub").css('margin-top', tempHeight);
		} else {
			$("#container_sub").css('top', 0);
			$("#container_sub").css('margin-top', 0);
		}
	}
	*/
	
	$(window).resize(function() {
	 	useFilter();
	});
	$('#Blur_container').hide();
	useFilter();
	//check content height to determine if using filter
	function useFilter() {
		if($("#content").length > 0) {
			var noLessThanHeight = $(window).height()-185;
			var contentHeight = $("#content").css('height');
			var newContentHeight = contentHeight.split("px");
			if(newContentHeight[0] > noLessThanHeight) {
				$('#Blur_container').fadeIn();
			} else {
				$('#Blur_container').hide();
			}
		}
	};
	
	$("#MainMenu_items").load("1menu.htm");
	$(".footer").load("1footer.htm");
	
	/*
	// super fish
	$("ul.sf-menu").superfish({ 
		pathClass:  'current' 
	}); 
	*/
	// animating bg
	// Create an array of images that you'd like to use
	var images = [
		"image/home_banner1.jpg",
		"image/home_banner2.jpg",
		"image/home_banner3.jpg",
		"image/home_banner4.jpg",
		"image/home_banner5.jpg"
	];
	var imagesTitle = [
		"image/home_title1.png",
		"image/home_title2.png",
		"image/home_title3.png",
		"image/home_title4.png",
		"image/home_title5.png"
	];
	var blurbs = [
		"AEM takes pride in providing innovative engineering-focused solutions to cater to today’s manufacturing needs through our global engineering service support network and innovative people.",
		"Our customer-centric approach means we are always committed to forming long standing partnerships with our clients and associates.",
		"Technology-focused. Engineering-focused. People-focused.",
		"We consider ourselves a sum of all parts, and none more important than our team of people.",
		"Guided by our core values, the Group envisage in becoming amongst the world's leading companies that provide solutions in equipment systems; precision components; substrates packaging and related manufacturing services across various industries."
	];
	
	// A little script for preloading all of the images
	// It's not necessary, but generally a good idea
	$(images).each(function(){
	   $('<img/>')[0].src = this; 
	});
	$(imagesTitle).each(function(){
	   $('<img/>')[0].src = this; 
	});
	

	
	/*
	// Call backstretch for the first time,
	// In this case, I'm settings speed of 500ms for a fadeIn effect between images.
	$('h2').hide();
	$('.blurb').hide();
	$.backstretch(images[index], {speed: 500}, function() {
		$('h2').fadeIn(800, function() {
			$('.blurb').text(blurbs[index]);
			$('.blurb').fadeIn(800);
		});
		// Set an interval that increments the index and sets the new image
		// Note: The fadeIn speed set above will be inherited
		setInterval(function() {
			$('.blurb').fadeOut(800, function() {
				$('h2').fadeOut(800, function() {
					index = (index >= images.length - 1) ? 0 : index + 1;
					$('h2 img').attr('src', imagesTitle[index]);
					$('.blurb').text(blurbs[index]);
					$.backstretch(images[index], function() {
						$('h2').fadeIn(800, function() {
							$('.blurb').fadeIn(800);
						});
					});
				});
			});
		}, 5000);		
	});	
	*/
	
	// Call backstretch for the first time,
	// In this case, I'm settings speed of 500ms for a fadeIn effect between images.
	$('h2').hide();
	$('.blurb').hide();
	$.backstretch(images[index], {speed: 500});
	$('h2').fadeIn(800);
	$('.blurb').text(blurbs[index]);
	$('.blurb').fadeIn(800);
	// Set an interval that increments the index and sets the new image
	// Note: The fadeIn speed set above will be inherited
	setInterval(function() {
		$('.blurb').fadeOut(800);
		$('h2').fadeOut(800, function() {
			index = (index >= images.length - 1) ? 0 : index + 1;
			$('h2 img').attr('src', imagesTitle[index]);
			$('.blurb').text(blurbs[index]);
			clearAll();
			$('#ball'+index).attr('src', 'image/carouselBall1.png');
			$.backstretch(images[index], function() {
				$('h2').fadeIn(800);
				$('.blurb').fadeIn(800);
			});
		});
	}, 6000);		
		
});	
