// Scroll the .scroll
scrollTimer = 0;

function scrollScroll(elid, dir) {
	if (!dir || !dir.length) {
		dir = "down";
	}
	
	var delta = 1;
	
	switch(dir){
		case "up":
			delta = -1 * delta;
			break;
			
		case "down":
		default:
			delta = delta;
	}
	
	var ul = $("#" + elid).children("ul");
	var top = ul.scrollTop();

	var li_last = $("#" + elid).children("ul").children("li:last");
	var li_last_pos = li_last.position();

	ul.scrollTop(top + delta);
	
	if (top > 0) {
		$("#" + elid).children(".top").addClass("top_arrow");
	} else {
		$("#" + elid).children(".top").removeClass("top_arrow");
	}
	if (li_last_pos.top > 130) {
		$("#" + elid).children(".bottom").addClass("bottom_arrow");
	} else {
		$("#" + elid).children(".bottom").removeClass("bottom_arrow");
	}
}

$(document).ready(function(){
// Place "hint" labels into their items
	$("label.hint").each(function(){
		var text = $(this).text();
		var item = $(this).attr("for");
		if (item.length > 0) {
			$item = $("[name="+item+"]");
			$item
				.focus(function(){ if($(this).val() == text) { $(this).val(""); } })
				.blur(function(){ if($(this).val() == "") { $(this).val(text); } });
				
			var value = $item.val();
			if (value === "") { $item.val(text); }
		}
	}).hide();

// Add #nav drop downs
	$("#nav > ul > li").hover(function(){
			$(this).addClass("hover");

			var $ul = $(this).find(".scroll ul");
			if ($ul.length){
				$ul.scrollTop(0);
	
				var li_last_pos = $(this).children(".scroll:first").children("ul").children("li:last").position();
				if (li_last_pos.top > 130) {
					$(this).children(".scroll:first").children(".bottom").addClass("bottom_arrow");
				}
			}
		}, function(){
			$(this).removeClass("hover");
		});
		
	$("#nav > ul > li").click(function(){
		$(this).addClass("click");
	});
	
// And make it scroll
	$(".scroll", "#nav")
		.addClass("scroll_js")
		.children(".bottom")
			.hover(function(){
				var elid = $(this).parent().attr("id");
				scrollTimer = setInterval("scrollScroll('" + elid + "')", 10); 
			}, function(){
				clearTimeout(scrollTimer);
			})
			.end()
		.children(".top")
			.hover(function(){
				var elid = $(this).parent().attr("id");
				scrollTimer = setInterval("scrollScroll('" + elid + "', 'up')", 10);
			}, function(){
				clearTimeout(scrollTimer);
			})
			.end()
		.children("ul")
			.children("li")
				.hover(function(){
					$(this).addClass("hover");
					$(this).find(".scroll ul").scrollTop(0);
				}, function(){
					$(this).removeClass("hover");
				})
				.each(function(){
					var pageid = $(this).attr("id").split("_")[1];
					var that = this;
					$.get("/classlibrary/page/sitenavigation/ajax_childpages.cfml?page=" + pageid, function(d){
						var data = eval(d);
						if (data.length){
							var html = "<div id=\"scroll_" + pageid + "\" class=\"scroll scroll_js scroll_child\"><div class=\"top\"></div><ul>";
							for (var i = 0; i < data.length; i++){
								var page = data[i];
								html += "<li><a href=\"" + page.url + "\">" + page.pagename + "</a></li>";
							}
							html += "</ul><div class=\"bottom\"></div></div>";
							
							$(that).addClass("parent").append(html);
							
							$("#scroll_" + pageid)
								.children(".bottom")
									.hover(function(){
										var elid = $(this).parent().attr("id");
										scrollTimer = setInterval("scrollScroll('" + elid + "')", 10);
									}, function(){
										clearTimeout(scrollTimer);
									})
									.end()
								.children(".top")
									.hover(function(){
										var elid = $(this).parent().attr("id");
										scrollTimer = setInterval("scrollScroll('" + elid + "', 'up')", 10);
									}, function(){
										clearTimeout(scrollTimer);
									})
									.end();
						}
					});
				});
	
// Hide Quick Links
	$(".quicklinks_hide ul")
		.hide()
		.prev("h3")
			.addClass("toggle_blue_closed toggle_blue")
			.click(function(ev){
				ev.preventDefault();
				$(this)
					.toggleClass("toggle_blue_closed")
					.next("ul")
						.slideToggle();
			});
			
// Simple tabs (see the "Services" section)
	$(".simpletabs")
		.each(function(){
			$(this)
				.addClass("js")
				.prepend("<div class=\"tabbar\"></div>")
				.append("<div class=\"tabtext\"></div>")
				.children(".tab")
					.wrapAll("<div class=\"tabcontent\"></div>")
					.each(function(i){
						var active = 0;
						if (location.hash.split("_").length > 1) {
							active = location.hash.split("_")[1];
						}
						var IsFirst = (i === 0);
						var IsActive = (i === parseInt(active));
						var cssclass = "active";
						
						if (!IsActive) {
							$(this).hide();
							cssclass = "";
						}
						
						var head = $(this).children("h3").text();
						head = head.replace(/ /g, "&nbsp;");
						var html = "<span class=\"" + cssclass + "\"><q><a href=\"#tab_" + i + "\">" + head + "</a></q></span>";
						
						$(this).parent().prev(".tabbar").append(html);
						if (!IsFirst) { html = "<span>|</span>" + html; }
						$(this).parent().next(".tabtext").append(html);
					})
					.end()
				.children(".tabbar, .tabtext")
					.children("span")
						.click(function(){
							var tabs = $(this).parent("div").siblings(".tabcontent").children(".tab");
							var active = $(this).children("q").children("a").attr("href").split("_")[1];
							var links = $(this).parents(".simpletabs").children(".tabbar, .tabtext").find("a");
							
							tabs
								.not(":eq(" + active + ")")
									.hide()
									.end()
								.eq(active)
									.show();
							links
								.each(function(){
									var href = $(this).attr("href").split("_")[1];
									
									if (active === href) {
										$(this).parents("span").addClass("active");
									} else {
										$(this).parents("span").removeClass("active");
									}
								});
		
						});
		});
});