// JavaScript Document
$(document).ready(function(){
	if($('ul.nested').length>0) nestedListNav();
});

function nestedListNav() {
	var oppsList = $("ul.nested");
	addCSS(oppsList);
	oppsList.find("h3").mouseenter(function(){
		$(this).css('cursor','pointer');
	}).each(function(){
			$(this).closest("li").children("div").prepend('<h3 class="static">'+$(this).html()+'</h3>');
		});
	oppsList.css({'height':getMaxHeight(oppsList)});
	displayCurrentList(oppsList.children("li:first-child"),oppsList);
	oppsList.find("h3:not('.static')").click(function(){
		displayCurrentList($(this).closest("li"),oppsList);
	}).append(':');
}

function displayCurrentList(c,o) {
	c.find('h3').css({'color':'#888'});
	if($("ul.nested li.current").length>0) $("ul.nested li.current").removeClass("current");
	c.addClass("current");
	o.children("li:not('.current')").children("div").fadeOut();
	c.children("div").fadeIn();
	o.css('backgroundPosition',getBGPos());
	o.css({'background-image':'url("/images/give/gift-catalog/tab-swirls.png")','background-repeat':'no-repeat','backgroundPosition':getBGPos()});
	o.find('li h3').css({'color':'#888'}).mouseenter(function(){
		$(this).css({'color':'#900'})
	}).mouseleave(function(){
		$(this).css({'color':'#888'})
	});
	c.find('h3:not(".static")').css({'color':'#900'}).mouseenter(function(){
		$(this).css({'color':'#900'})
	}).mouseleave(function(){
		$(this).css({'color':'#900'})
	});
	o.find('h3.static').css({'color':'#333'}).mouseenter(function(){
		$(this).css({'color':'#333'})
	}).mouseleave(function(){
		$(this).css({'color':'#333'})
	});
}

function getBGPos() {
	return 70+'px '+(($("li.current h3:not('.static')").position().top)-70)+'px';
}

function getMaxHeight(o) {
	var h = 0;
	o.find("div.resources ul").each(function(){
		h = Math.max(h,$(this).height());
	});
	return h + 175;
}

function addCSS(o) {
	$('.quad #main ul.nested li div.resources').css({'position':'absolute','top':'40px','height':'30px','width':'435px'});
	o.css({'width':'470px','paddingLeft':'180px'});
	o.find('ul').css({'width':'470px'});
	o.find('ul li').css({'border-top-width':'1px','border-top-style':'dotted','border-top-color':'#333'});
	o.find('ul ul li,ul li:first-child').css({'border':'0'});
	o.find('ul li p,ul li h4').css({'marginRight':'10px'});
	o.find('h3').css({'position':'relative','left':'-210px','top':'70px','width':'180px','fontSize':'1.2em','color':'#888','lineHeight':' 30px','textAlign':'right','margin':'0'});
}

