function closePopup(whatDiv) {
	if(document.getElementById(whatDiv)) {
		var popups = document.getElementById(whatDiv);
		$("#"+whatDiv).slideUp("fast");
//		popups.style.display = "none";
	}
}

function initNav(what) {
	if(!document.getElementById(what)) return false;
	var navHolder = document.getElementById(what);
	var lis = navHolder.getElementsByTagName('li');
	for(i=0; i<lis.length; i++) {
		lis[i].onmouseover = function() {
			if(undefined!==window.closeTheThing) {
				clearTimeout(closeTheThing);
			}
			var liPartialName = this.id.substring(3, this.id.length);
			var popupName = "s"+liPartialName;
			if((document.getElementById(popupName))) {
				var popups = document.getElementById(popupName);
//				alert(popupName);
				$("#"+popupName).show(150);
//				popups.style.display = "block";

			}
			for(j=0; j<lis.length; j++) {
				
				var liPName = lis[j].id.substring(3, lis[j].id.length);
				var pName = "s"+liPName;
				
				if(document.getElementById(pName)) {
//alert(popups);
					var ps = document.getElementById(pName);
					if(ps!=popups) {
//						alert(popups + " - " + pName);
						if(pName!="sfilter") { // Hack to fix filter
							$("#"+pName).slideUp("fast");
						}
//						ps.style.display = "none";
					}
				}
			}
		}		
		lis[i].onmouseout = function() {
			var liPartialName = this.id.substring(3, this.id.length);
			var popupName = "s"+liPartialName;
			var closeIt = "closePopup('"+popupName+"')";
			closeTheThing = setTimeout(closeIt, 60)  
		}
	}
	
	// to keep boxes open //
	var navPop = document.getElementsByTagName('div');
	for(i=0; i<navPop.length; i++) {
		var navClass = navPop[i].className.substring(0, 6);
		if(navClass=="navpop") {
			navPop[i].onmouseover = function() {
				if(undefined!==window.closeTheThing) {
					clearTimeout(closeTheThing);
				}
				this.style.display = "block";
				thisID = this.id;
				linkID = "np-"+thisID.substring(1, thisID.length);
				
				if(document.getElementById(linkID)) {
					linkClass = document.getElementById(linkID);
					linkClass.className = "hovered";
				}
			}
			navPop[i].onmouseout = function() {
				thisID = this.id;
				linkID = "np-"+thisID.substring(1, thisID.length);
				if(document.getElementById(linkID)) {
					linkClass = document.getElementById(linkID);
					linkClass.className = "";
				}
				var closeIt = "closePopup('"+thisID+"')";
				closeTheThing = setTimeout(closeIt, 60);
			}
		}
	}
}


function animateSearch() {
	$("#i-search").click(function() {
		$("#searchbox").slideToggle(150);
		$("#navinput").focus();
		return false;
	});
	
	
	
	$("#searchclose").click(function() { $("#searchbox").slideUp(150); return false; });
}

function makeLinksWork() {
	// LINK ACTIONS
	$('a[href^="mailto:"]').addClass('imail');
	$('a[href^="http://"]').addClass('iext');
	$('a[href$=".pdf"]').addClass('ipdf');
	$('a[href$=".doc"]').addClass('iword');
	$('a[href$=".ppt"]').addClass('ipowerpoint');
	
	$('a[rel="external"]').click(function(){
		window.open(this.href); return false;
	});
	
	$('a[rel="externalSized"]').click(function(){
		window.open(this.href,"mywindow","menubar=0,status=0,resizable=1,width=600,height=400"); return false;
	});

	$('.video').click(function(){
		window.open(this.href,"videowindow","menubar=0,status=0,resizable=0,width=441,height=295");
		return false;
	});	
}

function dropDowns() {
	
	if($("#f-classSelect")) {
		$("#f-classSelect").change(function() {
			var classType = $("#f-classType").val();
			window.open("/guide/biblestudies/?g=" + this.value + "&t=" + classType, "_parent");
		});
	}
	
	if($(".jumpTo")) {
		$(".jumpTo").change(function() {
			window.open(this.value, "_parent");
		});
	}
	
}

function mediaLink() {
	var popupName = "sidebar-cats";

	$("#categoriesButton").mouseover(function(){
		$("#sidebar-cats").show(200);
	});
	
	$("#sidebar-cats").mouseover(function() {
		if(undefined!==window.closeTheThing) {
			clearTimeout(closeTheThing);
		}
	});
	
	$("#sidebar-cats").mouseout(function(){
		var closeIt = "closePopup('"+popupName+"')";
		closeTheThing = setTimeout(closeIt, 50)  
	});
}

function footerAction() {
	if($.browser.msie) {
		return false;	
	}
	
	$("#footer").mouseover(function(){
		if(undefined!==window.closeTheThing) {
			clearTimeout(closeTheThing);
		}		
		$("#footer").animate({ opacity: 1 },150);
	});
	
	$("#footer").mouseout(function(){
		var closeIt = '$("#footer").animate({ opacity: .5 },300)';
		closeTheThing = setTimeout(closeIt,50)
	});
}

function sundaySchool() {
	if($('.ssBody')) {
		$('.ssBody').hide();
		$('.ssTitle a').click(function() {

			$(this).toggleClass("paged");
			var openTitle = "ssBody"+this.title;
			$('#'+openTitle).slideToggle(200);
			return false;
		});
	}	
}

function searchFocus() {
	$("#navinput").focus(function() {
		$("#search").addClass("bkg-move");
		$("#navinput").addClass("nav-input-dark");
		if($("#navinput").val()=="search...") {
			$("#navinput").val("");
		}
	});
	
	$("#navinput").blur(function() {
		$("#search").removeClass("bkg-move");
		$("#navinput").removeClass("nav-input-dark");
		if($("#navinput").val()=="") {
			$("#navinput").val("search...");
		}
	});
}

$(document).ready(function() {
	initNav("nav");
	makeLinksWork();
	dropDowns();
	mediaLink();
	footerAction();
	sundaySchool();
	searchFocus();
	seriesReflect();
});


// cufon

Cufon.set('fontFamily', 'Myriad').replace('h2')('#sidebar h3');


// cufon

// reflect
function seriesReflect() {
	$("#seriesList img").reflect({height:0.25,opacity:0.3});
	$("#seriesList a img").css({
		opacity: .7
	});
	$("#seriesList a img").mouseover(function() {
		$(this).animate({
			opacity: 1
		},200);
	});
	
	$("#seriesList a img").mouseout(function() {
		$(this).animate({
			opacity: .7
		},400);
	});
}