$(document).ready(function(){
	
	var checkNumber = 0;
	var checkTimer;
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	var widthCheck = 0;
	$('#home #wrapper').css('width',winWidth+"px").css('height',winHeight+"px");

	$(window).resize(function(){
		checkWindow();
	});

	checkWindow = function(){
		winWidth = $(window).width();
		winHeight = $(window).height();
		$('#home #wrapper').css('width',winWidth+"px").css('height',winHeight+"px");
		if(widthCheck != winWidth){
			resizeSlideImage();	
			widthCheck = winWidth;
		}	
		if(checkNumber == 60){
			window.clearInterval(checkTimer);
		}
		if(winWidth < 1100){
			$('#blurb').css('width','75%');
			$('#blurb').css('right','25%');
		}
		if(winWidth < 960){
			$('.subpage-sidebar #primary_nav').css('position','relative');
		} else {
			$('.subpage-sidebar #primary_nav').css('position','fixed');
		}
	}
	
	resizeSlideImage = function(){
		if(winWidth > 960){
			var imgHeight = $('#slider .img-wrapper.current img').height();
			var imgWidth = $('#slider .img-wrapper.current img').width();
			var loX = Math.floor((winWidth - imgWidth) / 2);
			var loY = Math.floor(((winHeight - imgHeight) / 2) * -1);
			$('#slider .img-wrapper.current').css('left',loX+'px').css('top',loY+'px');
		
			// move blurb
			var blurbPos = Math.floor(winHeight / 3);
			$('#blurb').css('top',''+blurbPos+'px');
		}
	}

	$('#slider .slide').each(function(i){
		$('div.block-wrapper').prepend('<a class="nav-block" href="javascript:void(0);">&nbsp;</a>');
		var curTitle = $('.slide img').eq(i).attr('alt');
		$('#mantle-nav').append('<div class="location"><h2>LOCATION: <span>&nbsp;'+curTitle+'</span></h2></div>');
	});

	postLoader = function(){
		checkTimer = setInterval(function(){
			checkWindow();
			checkNumber++;
		}, 250);
	}

	// set easing for primary nav
	jQuery.easing.def = "easeOutBack";

	// // primary nav slide in
	$('#nav li.topnav').each(function(i){
	
		$(this).hover(
			function(){
				$('li.topnav ul').hide();
				$('li.topnav ul li a').css('right','-1000px');
				$('li.topnav ul li a').stop();
				$('ul', this).show();
				$('ul li a', this).each(function(subi){
					$(this).animate({
						right: '-100'
					}, 350*(subi+1));
				});
			},
			function(){
				$('li.topnav ul li a').stop();
				$('li.topnav ul li a').animate({
					right: '-800'
				}, 100, function(){
					$('li.topnav ul').hide();
				});				
			}
		);
		
	});
	
	// var instantiation
	var current = 0;
	var numSlides = $('#slider .slide .img-wrapper').length;
	$('#slider .slide .img-wrapper').hide();
	$(window).load(function(){
		checkWindow();
		$('#preloader').fadeOut(600);
		$('#blurb').show();
		$('#slider .slide .img-wrapper').eq(0).fadeIn(2400,'linear',
			function(){
				autoRotate();
			}
		); 
		$('#mantle-nav .location h2').eq(0).show();
		$('#mantle-nav a.nav-block').eq(0).addClass('active');
		$('#slider .slide .img-wrapper').eq(0).addClass('current');
	});
	
	// controls the action for slider
	sliderAction = function(cur) { 
		$('#mantle-nav a.nav-block').removeClass('active');
		$('#mantle-nav a.nav-block').eq(cur).addClass('active');
		$('.location h2').fadeOut(100);
		$('#slider .slide .img-wrapper.current').fadeOut(500,
			function(){
				$('.location h2').eq(cur).fadeIn(100, 'linear');
				$('#slider .slide .img-wrapper').removeClass('current');
				$('#slider .slide .img-wrapper').eq(cur).fadeIn(500, 'linear').addClass('current');
				resizeSlideImage();
				if(cur == 0){
					window.clearInterval(slideTimer);
				}
			}
		);
	}
	
	var slideTimer;
	autoRotate = function() {
		slideTimer = setInterval(function(){
			if(current == (numSlides - 1)){
				current = 0;
			} else {
				current++;
			}
			sliderAction(current);

		},7000);
	}

	// change slide on click
	$('#mantle-nav a.nav-block').each(function(i){
		$(this).click(function(){
			if(!$(this).hasClass('active')){
				$('#mantle-nav a.nav-block').removeClass('active');
				$(this).addClass('active');
				sliderAction(i);

				$('.location h2').fadeOut(300);
				$('.location h2').delay(300);
				$('.location h2').eq(i).fadeIn(500);
				resizeSlideImage();
				window.clearInterval(slideTimer);
			}
		});
	});
	
	$('.subpage').scrollTo($('.subpage-content'), 1500, {offset: {left: 0, top: -100}, easing: "easeInOutQuad" });

	// subpage scroller
	$('ul#sidebar li a').each(function(i){
		$(this).eq(i).addClass('active');
		var scrollToMe = $('.subpage-content #'+$(this).attr('rel')+'');
		$(this).click(function(){
			$('ul#sidebar li a').removeClass('active');
			$('ul#sidebar li span').css('color','#FFFFFF');
			$(this).addClass('active');
			$('#sidearrow').animate({top: ''+52*i+'px'}, 500, "easeOutQuad");
			$.scrollTo(scrollToMe, 1200, {offset: {left: 0, top: -100}, easing: "easeOutExpo" });
		});
	});
	
	$('.timeline-entry').each(function(i){
		var overHeight = $('.over', this).height();
		$('.arrow',this).css('top', ((overHeight - 30)/2)+'px');
		
		$(this).hover(
			function(){
				$('.timeline-entry .over').stop();
				$('.timeline-entry .arrow').stop();
				$('.timeline-entry .over').css('left','0px');
				$('.timeline-entry .arrow').css('right','20px');
				if(!$(this).hasClass('first')){
					$('.over',this).animate({ left: '600px'}, 1000, "easeOutQuad");
					$('.arrow',this).animate({ right: '-600px'}, 1000, "linear");
				}
			},
			function(){
				$('.arrow',this).animate({ right: '20px'}, 250, "easeOutQuad");
				$('.over',this).animate({ left: '0px'}, 200, 'linear');
			}
		);
	});
	
	$('h3.expand-head a').each(function(index){
		$(this).append('<span>expand <img src="/assets/images/arrow_expand.jpg" /></span><span style="display: none;">collapse <img src="/assets/images/arrow_collapse.jpg" /></span>');
		$(this).click(function(){
			$('span',this).toggle();
			$('.expand-content').eq(index).slideToggle(200);
		});
	});
	
	$('p.chart-button a').click(function(){
		if(!$(this).hasClass('active')){
			$('.integration div').hide();
		}
	});
	
	$('a#marec').click(function(){
		chartClick($('#mamap'));
		showSolutions(0);
		$('#relation-to').html('to M&amp;A Advisory');
	});
	$('a#partrec').click(function(){
		chartClick($('#partmap'));
		showSolutions(1);
		$('#relation-to').html('to Partnership');
	});
	$('a#caprec').click(function(){
		chartClick($('#capmap'));
		showSolutions(2);
		$('#relation-to').html('to Capital');
	});
	$('a#consultrec').click(function(){
		chartClick($('#consultmap'));
		showSolutions(3);
		$('#relation-to').html('to Marketing &amp; Consulting');
	});
	$('a#mamap').click(function(){
		chartClick($(this));
		showSolutions(0);
		$('#relation-to').html('to M&amp;A Advisory');
	});
	$('a#partmap').click(function(){
		chartClick($(this));
		showSolutions(1);
		$('#relation-to').html('to Partnership');
	});
	$('a#capmap').click(function(){
		chartClick($(this));
		showSolutions(2);
		$('#relation-to').html('to Capital');
	});
	$('a#consultmap').click(function(){
		chartClick($(this));
		showSolutions(3);
		$('#relation-to').html('to Marketing &amp; Consulting');
	});
	
	// Chart changes on click
	var chartInit = false;
	chartClick = function(chartPiece){
		$('.dropper-downer').hide();
		$('#chart a.active').removeClass('active');
		chartPiece.addClass('active');
		$('a.linkmap').each(function(i){
			$(this).css({backgroundPosition:'0px -'+($(this).height()*2)+'px'});
		});
		$('a.smallrec').each(function(i){
			$(this).css({backgroundPosition:'0px -'+($(this).height()*2)+'px'});
		});
		chartPiece.next('.smallrec').css({backgroundPosition:'0px 0px'});
		chartPiece.css({backgroundPosition:'0px 0px'});
		if(!chartInit){
			$('.chart-subsection .column').show();
			$('#no-solution').hide();
			startHover();
			chartInit = true;
		}
	}
	
	// Chart hovers 
	startHover = function(){
		$('a.linkmap').each(function(i){
			$(this).hover(
				function(){
					if(!$(this).hasClass('active')){
						$(this).css({backgroundPosition:'0px -'+$(this).height()+'px'});
						var childRec = $('.smallrec').eq(i);
						childRec.css({backgroundPosition:'0px -'+childRec.height()+'px'});
					}
				},
				function(){
					if(!$(this).hasClass('active')){
						var childRec = $('.smallrec').eq(i);
						$(this).css({backgroundPosition:'0px -'+($(this).height()*2)+'px'});
						childRec.css({backgroundPosition:'0px -'+(childRec.height()*2)+'px'});
					}
				}
			);
		});
		$('a.smallrec').each(function(i){
			$(this).hover(
				function(){
					if(!$('.linkmap').eq(i).hasClass('active')){
						$(this).css({backgroundPosition:'0px -'+$(this).height()+'px'});
						var parentRec = $('.linkmap').eq(i);
						parentRec.css({backgroundPosition:'0px -'+parentRec.height()+'px'});
					}
				},
				function(){
					if(!$('.linkmap').eq(i).hasClass('active')){
						var parentRec = $('.linkmap').eq(i);
						$(this).css({backgroundPosition:'0px -'+($(this).height()*2)+'px'});
						parentRec.css({backgroundPosition:'0px -'+(parentRec.height()*2)+'px'});
					}
				}
			);
		});
	}
	
	showSolutions = function(solutionIndex){
		var curSolution = solutionIndex + 1;
		$('div.solution').not(curSolution).hide();
		$('div.solution').eq(curSolution).show();
		$('div.integration').not(curSolution).hide();
		$('div.integration').eq(curSolution).show();
	}
	
	$('#chart .integration').each(function(initIndex){
		$('#chart a.dropper').each(function(index){
			$(this).eq(initIndex).click(function(){
				$('span',this).toggle();
				$(this).parents('.integrations').find('.integration div').eq(index).slideToggle(200,'linear');
			});
		});
	});
	
	$('a.job-title').each(function(index){
		$(this).append('<span>expand <img src="/assets/images/arrow_expand.jpg" /></span><span style="display: none;">collapse <img src="/assets/images/arrow_collapse.jpg" /></span>');
		$(this).click(function(){
			$(this).parents('.content-block').find('.job-description').eq(index).slideToggle(500,'linear');
			$('span',this).toggle();
		});
	});

	$('#closure ul li a:eq(2)').css('border','0px');
	
	//postLoader runs for ten seconds making sure the window is the right size.
	postLoader();
});
