var hashTagElements = new Array(),
	flexiSlideHeight = 0,
	flexiNextSlide = false,
	flexiAnimateTime = 300,
	flexiTimer = false,
	flexiOverState = "out",
	flexiAutoSlideTime = 7000;
		
$(document).ready(function(){
	var subHeaderHeight = $('#sub_header').height();
	
	//If the user cameto the site with a hash tag scroll to it
	$.localScroll.hash({duration: 200, onBefore: function(e, anchor) {
		this.offset = {top: +- ($(anchor).find("h3").outerHeight(true) + 80)};
	}});
	
	//Handle the flexi
	if ($("#flexi_banner").length) {
		flexiSlideHeight = $("#flexi_home").innerHeight();
		flexiUpdateVisible();
		
		flexiOut();
		$("#flexi_banner").overNOut({
			over: flexiOver,
			overDelay: 0,
			out: flexiOut,
			outDelay: 300,
			mouseOutOnLast: false
		});
		
		//Setup click events
		$("#flexi_banner #flexi_wrap #browserbox_control li a").click(flexiChangeTo);
	}
	
	//Init sticky and scroll to hash tag for #subnav
	//also gather hashTagElements
	if ($("#subnav").length) { //Only if #subnav exists otherwise pointless
		$("#subnav")
			.sticky({
				topSpacing: subHeaderHeight, 
				placeHolder: true
			})
			.localScroll({
				hash:true, 
				duration: 200, 
				onBefore: function(e, anchor) {
					this.offset = {top: +- ($(anchor).find("h3").outerHeight(true) + 80)};
					
					return true;
				}
			})
			.find("a").each(function() { //Loop through all the subnav links
				var hashTag = $(this).attr("href"),
					hashTagElement;
					
				if (hashTag.length > 1 && hashTag.startsWith("#")) { //If this link is considered a valid hashtag
					hashTagElement = $(hashTag);
					if (hashTagElement.length) { //If the hashTagElement exists
						hashTagElements[hashTagElements.length] = hashTagElement; //Append the new found hash tag element to the array
					}
				}
			});
		
		//Create event for scrolling to change active subnav link based on current scroll position
		$(window).scroll(function() {
			var currentScrollTop = $(window).scrollTop();
			
			for (var i = hashTagElements.length; i--; i >= 0) {
				var hashTagElement = hashTagElements[i];
				
				if (hashTagElement.position().top - 142 <= currentScrollTop) {
					var hashTagLink = $("#subnav ul li a[href='#" + hashTagElement.attr("id") + "']");
				
					if (!hashTagLink.parent().hasClass("current")) {
						$("#subnav ul li.current").removeClass("current");
						hashTagLink.parent().addClass("current");
					}
					
					break; //Jump out of loop so it doesn't trickle back up to the first link
				}
			}
		});
	}
	
	//Hide drop_boxes
	$(".dropdown_box").hide();
	
	$(window).trigger("scroll");
});
