
var debugSlider = false; // enable display of various debug info.
var sliderColor = ['rgb(66,54,35)', 'rgb(217,82,4)']; // active state, inactive state
var isScrolling = false; // if the user clicked a menu link and we're animating a scroll, transition link colors faster.

var sliderLinks = [
	// css selector, id of element containing content, scroll offset
	['#menu li.page-item-2 a', '#header', 0],
	['#menu li.page-item-8 a', '#aboutUs', 150],
	['#menu li.page-item-10 a', '#programs', 150],
	['#menu li.page-item-12 a', '#apply', 150],
	['#menu li.page-item-14 a', '#resources', 150],
	['#menu li.page-item-16 a', '#contact', ($(window).height()/1.5)]
];

$(document).ready(function() {

	if(debugSlider) $("body").append('<div id="slider-debug" style="position: fixed; padding: 10px; top: 10px; left: 10px; width: 300px; height: auto; background: rgba(0,0,0,0.5); color: #fff; font: 12px/12px arial,helvetica,sans-serif"></div>');

	for(var e in sliderLinks) {
		$(sliderLinks[e][0]).attr('href', sliderLinks[e][1]);

		$(sliderLinks[e][0]).click(function() {
			var pos = $($(this).attr('href')).offset().top;
			isScrolling = true;
			$('html, body').stop(true,false).animate({scrollTop: pos}, {duration: 1000, easing: 'easeOutExpo'}, function() { isScrolling = false; });
			return false;
		});
	}

	$("#menu li:odd").addClass("odd"); // Apply different tab styles to sidebar.
	updateMenuColors();

	$(window).scroll(function() {
		updateMenuColors();
	});

	$("#courseButton img:first").hover(function() {
		$(this).attr("src", "wp-content/themes/WaNic/images/courseBrowserOver.png");
	}, function() {
		$(this).attr("src", "wp-content/themes/WaNic/images/courseBrowser.png");
	});

	$("#careerTab img:first").hover(function() {
		$(this).attr("src", "wp-content/themes/WaNic/images/careerContactTabOver.png");
	}, function() {
		$(this).attr("src", "wp-content/themes/WaNic/images/careerContactTab.png");
	});

	$("#districtTab img:first").hover(function() {
		$(this).attr("src", "wp-content/themes/WaNic/images/districtContactTabOver.png");
	}, function() {
		$(this).attr("src", "wp-content/themes/WaNic/images/districtContactTab.png");
	});
	
	$("#courseLink").click(function() {
		var pos = $("#apply").offset().top;
		$('html, body').stop(true,false).animate({scrollTop: pos}, {duration: 1000, easing: 'easeOutExpo'});
		return false;
	});

});

function updateMenuColors() {
	var wt = $(window).scrollTop();
	var wh = $(window).height();
	var ea = [];

	if(debugSlider) {
		$("#slider-debug").html('');
		$("#slider-debug").append('Window Top: ' + wt + '<br />');
		$("#slider-debug").append('Window Height: ' + wh + '<br /><br />');
	}

	for(var e in sliderLinks) {
		try {
			var et = $(sliderLinks[e][1]).offset();
			if(typeof et.top === 'undefined' ||typeof et.top === 'null') continue;
			et = et.top;

			if(debugSlider) {
				$("#slider-debug").append(sliderLinks[e][1] + '<br />');
				$("#slider-debug").append('&nbsp; Top Offset: ' + (et - sliderLinks[e][2]) + '<br />');
			}

			if(wt >= (et - sliderLinks[e][2])) {
				ea = [sliderLinks[e][0], sliderLinks[e][1]];
			}
		} catch(err) {
		
		}
	}

	if(!ea) ea = [sliderLinks[0][0], sliderLinks[0][1]];

	for(var e in sliderLinks) {
		var e = sliderLinks[e][0];
		if(typeof e === 'undefined' ||typeof e === 'null') continue;

		if(e == ea[0]) {
			if(!$(e).hasClass("slider-menu-active").length) {
				$(e).addClass("slider-menu-active");
				$(e).parent().addClass("slider-menu-active");

				if(isScrolling) {
					$(e).stop(true, true).animate({color: sliderColor[1]}, 100);
				} else {
					$(e).stop(true, false).animate({color: sliderColor[1]}, 100);
				}

				if(debugSlider) $("#slider-debug").append('<br />Applied class to ' + ea[1] + '<br />');
			}
		} else {
			if($(e).hasClass("slider-menu-active")) {
				$(e).removeClass("slider-menu-active");
				$(e).parent().removeClass("slider-menu-active");

				if(isScrolling) {
					$(e).stop(true, true).animate({color: sliderColor[0]}, 100);
				} else {
					$(e).animate({color: sliderColor[0]}, 100);
				}
			}
		}
	}
}
