$('html').removeClass('no-js');
$('html').addClass('js');
var addthis_config = {
	services_compact: 'print, email, digg, stumbleupon, reddit, delicious, google, blogger, wordpress,more'
}
var elsToCycle = 0;
var elParent = '.homeQuestions li'; // What are we going to cycle?
function fadeEngine(x) {
	var y = x;
	if(x == elsToCycle) y = 1; else y++;
	$("#fadeQuestions" + x).css("display","none");
	$("#fadeQuestions" + y).fadeIn("slow");
	$('#answerTarget').attr('href', $("#fadeQuestions" + y + " a").attr('href'));
	setTimeout('fadeEngine(' + y + ')',6000);
}
$(document).ready(function() {
	$('.viewModal').click(function() {
		if (!$('html').hasClass('windowNarrow')) {
			// $('#headSite').focus();
			var itemSource = $(this).attr('href');
			var wrapHeritageHeight = $('#wrapHeritage').height();
			var modalStart = '<div id="view-modal" class="modal" style="height:' + wrapHeritageHeight + 'px"><div class="modalBlock"><div class="modalWrap"><div class="modalClose" onclick="$(\'.modal\').remove()">Close</div><div class="modalContent">'
			var modalEnd = '</div></div></div></div>'
			if ($(this).hasClass('chartFrame')) {
				$('body').append(modalStart + '<div class="modalImg"><img src=' + itemSource + ' /></div>' + modalEnd);
			} else {
				$.ajax({
					url: itemSource,
					cache: false,
					success: function(html){
						$('body').append(modalStart + html + modalEnd);
					}
				});
				$(document).keypress(function(e) {
				    if (e.keyCode == 27) {
						$('.modal').remove();
				    }
				});
			}
			window.location = "#wrapHeritage";
			return false;
		} else {
			return true;
		}
		
	});
	adjustAsideTiles = function() {
		if ($(window).width() > 480) {
			var highestCol = Math.max($('.asideArticle > .tile:nth-child(1)').height(), $('.asideArticle > .tile:nth-child(2)').height(), $('.asideArticle > .tile:nth-child(3)').height(), $('.asideArticle > .tile:nth-child(4)').height(), $('.asideArticle > .tile:nth-child(5)').height(), $('.asideArticle > .tile:nth-child(6)').height(), $('.asideArticle > .tile:nth-child(7)').height());
			$('.asideArticle > .tile').css('min-height', highestCol);
		} else {
			$('.asideArticle > .tile').css('min-height','1px');
		}
	};
	pageHeight = function() {
		$('#wrapHeritage').css('min-height', $(window).height())
	};
	asignNarrow = function() {
		if ($(window).width() <= 420) {
			$('html').addClass('windowNarrow');
		};
	};
	adjustModalTop = function() {
		if ($(window).height() <= 700) {
			$('html').addClass('windowShort');
		} else {
			$('html').removeClass('windowShort');
		};
	};
	init = function() {
		$('.faqQuestionAnswer dd').append('<a href="#main" class="backTop"><span>Back to Top</span></a>')
		$(elParent).attr("id", function (arr) {
			elsToCycle++;
			return "fadeQuestions" + (arr + 1);
		})
		fadeEngine(0);
		$('.headPage nav > ul > li').mouseover(function() {
			$(this).addClass('hovered');
		}).mouseout(function() {
			$(this).removeClass('hovered');
		});
		adjustAsideTiles();
		pageHeight();
		asignNarrow()
		adjustModalTop()
	}
	init();
	$(window).resize(function() {
		pageHeight();
		asignNarrow()
		adjustModalTop()
		adjustAsideTiles();
	});
	if (!$('html').hasClass('windowNarrow')) {
		var $faqGroup  = $('.faqGroup');
		$faqGroup.hide();
		$faqGroup.first().show();
		var $faqCategories = $('.faqCategories');
		$faqCategories.find('li').first().addClass('active');
		$faqCategories.find('a').click(function() {
			$faqCategories.find('li').removeClass('active');
			$(this).parent().addClass('active');
			var targetGroup = $(this).attr('href');
			$faqGroup.hide();
			$(targetGroup).show();
			return false;
		});
		if(window.location.hash) {
			$faqGroup.hide();
			var targetQuestion = window.location.hash;
			$(targetQuestion).removeClass('faqUnHighlighted');
			$(targetQuestion).addClass('faqHighlighted');
			setTimeout(function() {
				$(targetQuestion).addClass('faqUnHighlighted');
				$(targetQuestion).removeClass('faqHighlighted');
			}, 3000);
			$(targetQuestion).parent().parent().show();
			var targetGroupName = $(targetQuestion).parent().parent().attr('id');
			$faqCategories.find('li').removeClass('active')
			$faqCategories.find('li[data-group='+ targetGroupName +']').addClass('active');
		};
	}
	$('.faqQuestions a').click(function() {
		var $selfHash = $(this).attr('href');
		$($selfHash).removeClass('faqUnHighlighted');
		$($selfHash).addClass('faqHighlighted');
		setTimeout(function() {
			$($selfHash).addClass('faqUnHighlighted');
			$($selfHash).removeClass('faqHighlighted');
		}, 3000);
	});
});
