/* Author: 

*/


$(function() {

	function setHistory(data) {
		alert(data);
	}

	$('.external').attr('target', "_blank");
	
	var windowHeight = $(window).height();
	if(windowHeight > 550) {
		$("html, body").css('overflow','hidden');
	}
	
	$('nav ul ul').hide();
	$("nav ul li a[href='#fabric']").addClass('current');
	
	$('a.scroller').each(function() {
		$(this).click( function() {
			linkDest = $(this).attr('href');
			$('#offering .innerWrapper').scrollTo( linkDest , 500 );
			$('li.nav1 ul li a').removeClass('current');
			$('nav ul li a[href='+linkDest+']').addClass('current');
		});
	});
	
	$('nav > ul > li > a').each(function() {
		$(this).click( function() {
		 	
		 	link = $(this).attr('href');
			
			$('#outer').scrollTo( $(this).attr('href') , 1000 );
			
			$('nav > ul > li > a').removeClass('current');
			$(this).addClass('current');
			if(link == "#offering") {
				$('nav ul .nav1 ul').slideDown();
			} else {
				$('nav ul .nav1 ul').slideUp();
			}
			if(link == "#contact") {
				$('nav ul .nav2 ul').slideDown();
			} else {
				$('nav ul .nav2 ul').slideUp();
			}
			
			
			
			
			switch(link) {
			case "#fabric":
				$('.bg-1').animate({top: 0}, 500);
				$('.bg-2').animate({top: windowHeight}, 500);
				$('.bg-4').animate({top: windowHeight}, 500);
				$("nav ul li a[href='#fabric']").addClass('current');
				break;
			case "#offering":
				$('.bg-1').animate({top: -windowHeight}, 500);
				$('.bg-2').animate({top: 0}, 500);
				$('.bg-4').animate({top: windowHeight}, 500);
				$('li.nav1 ul li a').removeClass('current');
				break;
			case "#contact1":
				$('.bg-1').animate({top: -windowHeight}, 500);
				$('.bg-2').animate({top: -windowHeight}, 500);
				$('.bg-4').animate({top: windowHeight}, 500);
				break;
			case "#contact":
				$('.bg-1').animate({top: -windowHeight}, 500);
				$('.bg-2').animate({top: -windowHeight}, 500);
				$('.bg-4').animate({top: 0}, 500);
				$(".nav2 li a").removeClass('current');
				$("nav ul li a[href='#potentialcustomers']").addClass('current');
				$('#contact .innerWrapper').scrollTo( "#potentialcustomers" , 500 );
				break;
			case "#locations":
				
				$('.bg-1').animate({top: -windowHeight}, 500);
				$('.bg-2').animate({top: -windowHeight}, 500);
				$('.bg-4').animate({top: 0}, 500);

				$('.nav2 ul').slideDown();
				$(".nav2 li a").removeClass('current');
				$('#contact .innerWrapper').scrollTo( "#locations" , 500 );
				$("nav ul li a[href='#locations']").addClass('current');
				break;
			default:
				$('.bg-1').animate({top: 0}, 500);
				$('.bg-2').animate({top: windowHeight}, 500);
				$('.bg-4').animate({top: windowHeight}, 500);
				break;
			}
			
			
			return false;
		});
	});
	
	

	
	//$('nav ul li ul li a:first').addClass('current');
	$('.nav1 ul li a').each(function() {
	
		$(this).click( function() {
			
			$('#outer').scrollTo( $('#offering') , 500 );
			$('#offering .innerWrapper').scrollTo( $(this).attr('href') , 500 );
			
			var index = navigator.userAgent.indexOf("Chrome");
			if (index != -1) {
				//$('#offering .innerWrapper').height(500);
			}
			
			$('li.nav1 ul li a').removeClass('current');
			$(this).addClass('current');
			/*
			$('.bg-1').animate({top: -windowHeight}, 500);
			$('.bg-2').animate({top: 0}, 500);
			$('.bg-4').animate({top: windowHeight}, 500);
			*/
			
			return false;
			
		});
		
	});
	$('.nav2 ul li a').each(function() {
	
		$(this).click( function() {
			
			$('#outer').scrollTo( $('#contact') , 500 );
			$('#contact .innerWrapper').scrollTo( $(this).attr('href') , 500 )
			
			var index = navigator.userAgent.indexOf("Chrome");
			if (index != -1) {
				//$('#contact .innerWrapper').height(500);
			}
			
			$('li.nav2 ul li a').removeClass('current');
			$(this).addClass('current');
			//$('.bg-1, .bg-2').animate({top: -windowHeight}, 500);
			//$('.bg-4').animate({top: 0}, 500);
			//return false;
			
		});
		
	});
	/*
$('p.nav a').each(function() {
	
		$(this).click( function() {
			
			$('#offering .innerWrapper').scrollTo( $( $(this).attr('href') ), 500 );
			return false;
			
		});
		
	});
*/
	
	$('a[href=#offering]').click( function() {
			
			$('#offering .innerWrapper').scrollTo( $( '#offering-intro' ), 500 );
			
	});
	
	
	var bgWrapper = $('.bg-wrapper');
	
	var sectionHeight = $(window).height() - 192;
	
	$('section').css('min-height', ( sectionHeight ) + "px");
	bgWrapper.css('min-height',$(window).height()+"px");
	
	function backgroundResize() {
		var headerPos = $('header').offset(),
		headerWidth = $('header').outerWidth(),
		fabricPos = $('#offering').offset(),
		contactPos = $('#contact').offset();
		
		$('.overlay').width($(window).width()+"px").css('margin-left', headerPos.left + headerWidth);
	}
	backgroundResize();
	
	$(window).resize(function() {
		backgroundResize();
	});
	
	$('.external').each(function() {
		$(this).attr('target','_blank')
	});
		
	/*
	var posContainer = $('.container').offset();
	$('header').css('left', posContainer.left + 'px')
	
	$('.bg-wrapper').height($(window).height()).css('overflow', 'hidden');
	
	var pos = $('header').offset(), hWidth = $('header').outerWidth();

	var overlayLeft = pos.left + hWidth;
	
	htmlHeight = $('html').height();
	bgCount = $('.bg-wrapper').length;
	
	totalHeight = htmlHeight * bgCount;
*/
	
	/* $('.main').css('position','relative'); */
	
	
	/* $('.overlay').css('marginLeft', overlayLeft+'px').css('height', totalHeight ); */
	

	if( window.location.hash == '#offering-true' ) {
		$('nav ul li a.offering').click();
	}

	if( window.location.hash == '#contact-true' ) {
		$('nav ul li a.contact').click();
	}

	
});





















