// 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");
	}
	
}

// When the document is ready...
$(document).ready(function(){
	$("#nav a[href=/findadoctor/]").click(function () {
		doctorsWin = window.open(this.href,'doctors','height=630,width=980,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
		doctorsWin.focus();
		return false;
	});
						   
// 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.val(text)
					.focus(function(){ if($(this).val() == text) { $(this).val(""); } })
					.blur(function(){ if($(this).val() == "") { $(this).val(text); } });
		}
	}).hide();

// Add #nav drop downs
//	$("#nav > ul > li").hover(function(){ $(this).addClass("hover"); }, function(){ $(this).removeClass("hover"); });

// 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];
							// Subsite addition to remove initial folder from URL
								var url = page.url.substring(1);
								var url = url.substring(url.indexOf("/"));
								html += "<li><a href=\"" + 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();
						}
					});
				});
});
