// http://plugins.jquery.com/project/ajaxqueue
// http://github.com/nakajima/jquery-ajax-queue/tree/master
// http://www.protofunc.com/scripts/jquery/ajaxManager/
// http://code.google.com/p/jquery-ajaxq/
//				(google code one looks great)
//		QUESTION: which is easier/smaller?

$("html").addClass("js");

// global namespace
var MDA = MDA || {};

MDA.Patients = {
	fadeSpeed : "normal",
	transition: 10000,	 
	current : 0,		 
	count : 1			 
}
MDA.Patients.slide = function(imgSrc, imgAlt, imgUrl, textX, textUrl){
	this.imgSrc = imgSrc;
	this.imgAlt = imgAlt;
	this.imgUrl = imgUrl;
	this.textX = textX;
	this.textUrl = textUrl;
}
MDA.Patients.slide.prototype.loaded = false;
MDA.Patients.allLoaded = function(){
	x = true;
	$.each(MDA.Patients.slides, function(i){
		if ( MDA.Patients.slides[i].loaded === false ) {
			x = false;
		}
	})
	return x;
}
MDA.Patients.showNext = function(x){
	var nextItem;
	// just to peek ahead
	if ( x+1 == MDA.Patients.count) {
		nextItem = 0;
	} else {
		nextItem = x+1;
	}
	$("#banner img:eq(" + x + ")").fadeOut(MDA.Patients.fadeSpeed, function(){
		$("#banner img:eq(" + (nextItem) + ")").fadeIn(MDA.Patients.fadeSpeed, function(){
			if ( nextItem != 0 ) {
				$("#story-link1").attr("href", MDA.Patients.slides[x].imgUrl);
				$("#story-link2").html(MDA.Patients.slides[x].textX);
				$("#story-link2").attr("href", MDA.Patients.slides[x].textUrl);
			} else {
				$("#story-link1").attr("href", $("#story-link1").data("href"));
				$("#story-link2").html($("#story-link2").data("text"));
				$("#story-link2").attr("href", $("#story-link2").data("href"));
			}
			if ( (MDA.Patients.current + 1) >= MDA.Patients.count ) {
				MDA.Patients.current = 0;
			} else {
				MDA.Patients.current++;
			};
			MDA.Patients.loadNext(MDA.Patients.current);
		})
	});
}
MDA.Patients.loadNext = function(x){
	if ( MDA.Patients.allLoaded() === false ) {
		var slideIndex = x;
		var source = MDA.Patients.slides[slideIndex].imgSrc;
		var alt = MDA.Patients.slides[slideIndex].imgAlt;
		$("#banner img:eq(" + (slideIndex+1) + ")").load(function(){
			MDA.Patients.slides[slideIndex].loaded = true;
			setTimeout(function(){
				MDA.Patients.showNext(x);
			}, MDA.Patients.transition);
		})
		$("#banner img:eq(" + (slideIndex+1) + ")").attr("src", source).attr("alt", alt);
	} else {
		setTimeout(function(){
			MDA.Patients.showNext(MDA.Patients.current);
		}, MDA.Patients.transition)
	}
}
MDA.Patients.setup = function(){
	/* original IF statement checked for the homepage - which meant slideshow wouldn't work on any other page. */
	if ( $("#story-link1").size() === 1  && $("#story-link2").size() === 1) {
		/* bug B-63; FF2, the slideshow alters the anti-aliasing of all fonts, this prevents that. */
		if ($.browser.mozilla === true && parseFloat($.browser.version) < 1.9) {
		    $('body').css('opacity', 0.9999);
		}
	
		$("#story-link1").data("href", $("#story-link1").attr("href"));
		$("#story-link2").data("text", $("#story-link2").html());
		$("#story-link2").data("href", $("#story-link2").attr("href"));
		
		MDA.Patients.count = MDA.Patients.slides.length + 1;
		$.each(MDA.Patients.slides, function(i){
			$("#banner img:eq(0)").after("<img title='Patient Story slide' />");
			$("#banner img[title='Patient Story slide']").hide();
		});
		MDA.Patients.loadNext(MDA.Patients.current);
	}
}
$(window).load(function(){
	MDA.Patients.setup();
})
MDA.Patients.slides = [];

MDA.Tools = {
	fontMed : "med",
	fontLarge : "large"
}
MDA.Tools.setCookie = function(sz){
	$.cookie('mdatxtsize', sz);
	return;
}
MDA.Tools.build = function(){
	$("#nav").append('<ul id="nav-tools" />');
	$("#nav-tools").append('<li id="tool-email" title="Email page link" />')
		.append('<li id="tool-print" title="Print this page" />')
		.append('<li id="tool-font-label"><div id="tool-font-minus" title="Decrease font size"></div><div id="tool-font-plus" title="Increase font size"></div></li>')
	$("#tool-email").click(
		function(){
			location.href = "mailto:?subject=" + MDA.CSS.txtEmailSubject + "&body=" + location.href;
		}
	);
	$("#tool-print").click(
		function(){
			window.print();
		}
	);
	$("#tool-font-plus").click(
		function(){
			if ( $("html").hasClass(MDA.Tools.fontLarge) ) {
				return true;
			}
			else if ( $("html").hasClass(MDA.Tools.fontMed) ) {
				$("html").addClass(MDA.Tools.fontLarge).removeClass(MDA.Tools.fontMed);
				MDA.Tools.setCookie(MDA.Tools.fontLarge);
			} else if ( !$("html").hasClass(MDA.Tools.fontLarge) && !$("html").hasClass(MDA.Tools.fontMed) ) {
				$("html").addClass(MDA.Tools.fontMed);
				MDA.Tools.setCookie(MDA.Tools.fontMed);
			}
		}
	);
	$("#tool-font-minus").click(
		function(){
			if ( $("html").hasClass(MDA.Tools.fontLarge) ) {
				$("html").addClass(MDA.Tools.fontMed).removeClass(MDA.Tools.fontLarge);
				MDA.Tools.setCookie(MDA.Tools.fontMed);
			} else if ( $("html").hasClass(MDA.Tools.fontMed) && !$("html").hasClass(MDA.Tools.fontLarge) ) {
				$("html").removeClass(MDA.Tools.fontMed);
				MDA.Tools.setCookie(null);
			} else {
				return true;
			}
		}
	);
}

MDA.slides = {
	delay : 1600,
	slide : 800,
	slideShowsList : [],
	addToSSList : function(that){
		if ( $(that).size() > 0 ) {
			$(that).find("a").each(function(){
				MDA.slides.slideShowsList.push(this)
			})
		}
	}
}

MDA.slides.setUp = function(){
	$(".type1-slides-container").slideThis({
		slideTime : MDA.slides.delay,
		slideFXTime : MDA.slides.slide,
		classSlideBox : "type1-slide-container",
		classSlide : "type1-slides-class",
		classIndexOn : "on",
		transitionType : 'slide'
	});
	MDA.slides.addToSSList(".type1-slides-class");
	MDA.slides.addToSSList(".highlights-list");
	$(".type2-slides-container").slideThis({
		slideTime : MDA.slides.delay,
		slideFXTime : MDA.slides.slide,
		classSlideBox : "type2-slides-container",
		classSlide : "type2-slides-class",
		classIndexOn : "on",
		transitionType : "slide"
	})
	MDA.slides.addToSSList(".type2-slides-class");
	$(".type3-slides-container").slideThis({
		slideTime : MDA.slides.delay,
		slideFXTime : MDA.slides.slide,
		classSlideBox : "type3-slides-container",
		classSlide : "type3-slides-class",
		classIndexOn : "on",
		transitionType : "slide"
	})
	MDA.slides.addToSSList(".type3-slides-class");
	$(".type4-slides-container").slideThis({
		slideTime : MDA.slides.delay,
		slideFXTime : MDA.slides.slide,
		classSlideBox : "type4-slides-container",
		classSlide : "type4-slides-class",
		classIndexOn : "on",
		transitionType : "slide"
	})
	MDA.slides.addToSSList(".type4-slides-class");
	$(".type5-slides-container").slideThis({
		slideTime : MDA.slides.delay,
		slideFXTime : MDA.slides.slide,
		classSlideBox : "type5-slides-container",
		classSlide : "type5-slides-class",
		classIndexOn : "on",
		transitionType : "slide"
	})
	MDA.slides.addToSSList(".type5-slides-class");
	$(".type6-slides-container").slideThis({
		slideTime : MDA.slides.delay,
		slideFXTime : MDA.slides.slide,
		classSlideBox : "type6-slide-container",
		classSlide : "type6-slides-class",
		classIndexOn : "on",
		transitionType : 'slide'
	});
	MDA.slides.addToSSList(".type6-slides-class");
	MDA.slides.putControls();
}

MDA.slides.putControls = function(){
	$("ul.slideIndex").each(function(){
		var x = $(this).position();
		var nextWidth = 35;
		var dotWidth = 13;
		var bufferWidth = 14;
		
		var count = $(this).find('li').size()

		if ( $("body.publications.enterprise").size() > 0 && $(this).parent(".type5-slides-container").size() > 0 ) {
			$(this).css("left", x.left-((count-1)*dotWidth)-nextWidth - bufferWidth + "px");
		} else if ( $("body.home").size() < 1 ) {
			$(this).css("left", x.left-((count-1)*dotWidth)-nextWidth + "px");
		} else {
			$(this).css("left", x.left+((count-1)*dotWidth)-nextWidth + "px");
		}

		x = $(this).position(); // get new position
		var y = $(this).outerWidth();
		var z = $(".slidePrev a").width();

		$(this).next(".slideButtons").css({
			"position" : "absolute",
			"top" : "0px"
		}).find(".slidePrev").css({
			"position" : "absolute",
			"left" : (x.left - z - 3) + "px",	//3 is just an eyeballed adjustment
			"top" :	 x.top - 2 + "px"
		})
		$(this).next(".slideButtons").find(".slideNext").css({
			"position" : "absolute",
			"left" : (x.left + y + 3) + "px",	// 3 is just an eyeballed adjustment
			"top" : x.top - 2 + "px"
		})
	});
}

MDA.CSS = {
	classBodyPatient : "patient",
	classBodyEducation : "education",
	classBodyEnterprise : "enterprise",
	isInteriorPage : function(){
		return (( $("body." + this.classBodyPatient).size() == 0 && $("body." + this.classBodyEducation).size() == 0 && $("body." + this.classBodyEnterprise).size() == 0) ? false : true);
	},
	isInside : false,
	txtEmailSubject : "MD Anderson Page of Interest",
	intFontSize : 0 // deprecated
}

MDA.CSS.corners = {};

MDA.CSS.cornerType = function(propList, classOf){
	this.props = propList;
	this.classOf = classOf || "";
}

MDA.CSS.corners.types = {
	t1 : new MDA.CSS.cornerType({ // 2 column home page specific
		caps : true,
		thisBox : "type1" // applies this class to ID
	}, ".home .type1"),
	t2 : new MDA.CSS.cornerType({ // solid dark gray corners
		right : false,
		left : false
	}, ".type2"),
	t3 : new MDA.CSS.cornerType({ // solid white corners
		right : false,
		left : false
	}, ".type3"),
	t4 : new MDA.CSS.cornerType({ // gray outer transparent lines (white fill)
		corners : true
	}, ".type4"),
	t400 : new MDA.CSS.cornerType({
		corners: true
	}, ".type400"),
	t500 : new MDA.CSS.cornerType({ // no transparency solid gray on white / was type5
		corners: true
	}, ".type500"),
	t6 : new MDA.CSS.cornerType({ // white transparent lines
		corners : true
	}, ".type6"),
	t7 : new MDA.CSS.cornerType({ // white transparent solid inner
		corners : true
	}, ".type7"),
	t8 : new MDA.CSS.cornerType({ // white transparent lines only on bottom
		topLeft : false,
		topRight : false,
		corners : true
	}, ".type8"),
	t9 : new MDA.CSS.cornerType({ // rounded, solid, patient care dark green like patient care essentials
		right : false,
		left : false
	}, ".type9"),
	t10 : new MDA.CSS.cornerType({ // solid white only on bottom
		corners : true,
		topLeft : false,
		topRight : false
	}, ".type10"),
	t11 : new MDA.CSS.cornerType({ // solid light blue inner, gray line, trans outer
		corners : true
	}),
	t12 : new MDA.CSS.cornerType({ // solid gray inner, gray line, trans outer
		corners : true
	}),
	t13 : new MDA.CSS.cornerType({ // solid white bottom only
		corners : true,
		topLeft : false,
		topRight : false
	}, ".type13"),
	t14 : new MDA.CSS.cornerType({
		corners : true
	}, ".type14"),
	t15 : new MDA.CSS.cornerType({	// rounded, solid, research dark blue top white bottom
		right : false,
		left : false
	}, ".type15"),
	t16 : new MDA.CSS.cornerType({	// rounded, solid, education light blue top and bottom
		right : false,
		left : false,
		corners : true
	}, ".type16"),
	t17 : new MDA.CSS.cornerType({ // rounded gray border on white bottom bright red gradient on top
		right : false,
		left : false
	}, ".type17"),
	t18 : new MDA.CSS.cornerType({ // rounded gray border white bottom bright purple gradient top
		right : false,
		left : false
	}, ".type18")
	
}

MDA.CSS.corners.build = function(){
	for (var key in MDA.CSS.corners.types) {
		$(MDA.CSS.corners.types[key].classOf).cornerMe(MDA.CSS.corners.types[key].props);
	}
}

MDA.Events = {};

MDA.Events.filter = function(){
	var formHTML = '<form action="#" id="event-form">' + 
	'<p><label>Filter by event type: <select id="event-filter" name="event-filter">' + 
			'<option value="">Select event type</option>' + 
		'</select>' + 
		'</label></p>' +	
	'</form>';
	var typesOf = [];

	//var arrayPatient = ["Community", "Prevention &amp; Screening", "Cancer Treatment Information", "Coping &amp; Support", "Wellness", "For New Patients", "For Caregivers &amp; Families", "For Kids &amp; Teens", "For Volunteers"];
	var arrayPatient = ["Cancer Treatment Information","Community","Coping &amp; Support","For Caregivers &amp; Families","For Kids &amp; Teens","For New Patients","For Volunteers","Prevention &amp; Screening","Wellness"];
	//var arrayEdu = ["Awards", "Conferences/Symposia", "Continuing Education", "Grand Rounds", "Lectures/Seminars", "For Students &amp; Alumni", "For Research Professionals", "For Clinical Professionals"];
	var arrayEdu = ["Awards","Conferences/Symposia","Continuing Education","For Clinical Professionals","For Research Professionals","For Students &amp; Alumni","Grand Rounds","Lectures/Seminars"];

	if ( $("body.patient").size() > 0 ) {
		 typesOf = arrayPatient;
	} else if ( $("body.education").size() > 0 ) {
		typesOf = arrayEdu;
	} else {
		typesOf.concat(arrayPatient, arrayEdu);
	}
	typesOf = typesOf.sort();
	var z = "";
	for ( y in typesOf ) {
		z += '<option>' + typesOf[y] + '</option>';
	}
	var work = "";
	$("body.events #content-highlights div.foreground")
		.append(formHTML)
		.find("#event-filter")
		.append(z).change(function(){
			work = $(this).val();
			if ( $(this).val() == "" ) {
				$("table.events-month tr").removeClass("hide");
				return true;
			}
			$("table.events-month tr").removeClass("hide");
			$("table.events-month tr[title]").each(function(){
				if ( $.trim($(this).attr("title")) != $.trim(work) ) {
					$(this).addClass("hide");
				}
			})
		});
}

MDA.CSS.setHeights = function(){
	var heights = [];
	var biggie = 0;
	if ( $("body.drill").size() > 0 && $("body.section").size() < 1 ) {
		var x = [".drill #content-col1", ".drill #content-col2", ".drill #nav-section"];
		for (var i=0; i < x.length; i++) {
			heights.push($(x[i]).height());
		};
		heights.sort(function(a,b){return a - b}); // get biggest by putting it in the last place
		var zzz = 0;
		var c = 0;
		for (var i=0; i < x.length-1; i++) {
 			zzz = heights[heights.length-1] - heights[i]; // get difference between biggest and current
			while (c < x.length) { 
				if ( $(x[c]).height() == heights[i] ) {
					$(x[c]).append("<div class='fixer'></div>").find("div.fixer").css("height", zzz);
				}
				c++;
			}
			c = 0;
		}
	} else { }
}

MDA.CSS.setUp = function(){
	MDA.CSS.isInside = MDA.CSS.isInteriorPage();
	$("#nav-main li:first-child").addClass("first");
	$("#nav-main li:last-child").addClass("last");
	$("#nav-main ul li").not("li:last-child").append("<span />");
	MDA.Tools.build();
	if ( $.cookie('mdatxtsize') ) {
		var mdatxtsize = $.cookie('mdatxtsize');
		$("html").addClass(mdatxtsize);
	}
	if (! ($.browser.msie && $.browser.version < 7) ) {
		MDA.CSS.corners.build();
	}
	$("#site-info-inner li + li.label").addClass("label-break");
	$(".section.education #content-col2").addClass("min");
	if ( $("#event-detail img.event-banner").size() < 1 ) {
		$("#event-detail").addClass("no-img");
	}
	var $canTypes = $("#alpha ul.alpha-list");
	if ( $canTypes.size() > 0 ) {
		$canTypes.find("li").each(function(i){
			if ( i == "9" || i == "18" || i == "24") {
				$(this).addClass("edge")
			}
		})
	}
	$("#cancer-topics div.vert-tabs > ul li:last-child").addClass("last");
	$("#patient-care ul.newslist li, #research-info ul.newslist li").append('<div class="over-ess" />');
	$("#patient-care ul.newslist li, #research-info ul.newslist li").click(function(){
		location.href = $(this).find("a:eq(0)").attr("href");
	})
	$("ul.newslist li").addClass("clearfix");
	// cch adjustments
	if ( $("body.cch").size() > 0 ) {
		$(".cch #tab-gui ul.ui-tabs-nav li:eq(0)").addClass("first");
		$(".cch #tab-gui ul.ui-tabs-nav li:eq(1)").addClass("second");
		$(".cch #tab-gui ul.ui-tabs-nav li:eq(2)").addClass("third");
	}
	// mozilla adjustments
	if ( $.browser.mozilla && navigator.userAgent.indexOf("Mac") != -1 ) {
		if ( $("body.section").size() > 0 ) {
			setTimeout(function(){
				$("#cancer-topics .ui-tabs-panel").addClass("mozAdj01");
			}, 500); // make sure tabs are built and set height before adjusting
		} else {
			setTimeout(function(){
				$("#cancer-topics .ui-tabs-panel").addClass("mozAdj");
			}, 500); // make sure tabs are built and set height before adjusting
		}
	} else if ( $.browser.mozilla && navigator.userAgent.indexOf("Windows") != -1 ) {
		$("#cancer-trials fieldset").addClass("mozAdj");
		setTimeout(function(){
			$(".education #cancer-topics .ui-tabs-panel").addClass("mozAdj")
		}, 500);
	}
	// IE adjustments
	if ( $.browser.msie ) {
		if ( $("body.drill.extra #content-col1").size() > 0 ) {
			$("body.drill.extra #content-col1").addClass("clearfix");
		}
		$("#nav-section ul li:last-child, #nav-section ul li li:last-child").addClass("last");
	}
	if ( $.browser.msie && $.browser.version < 7 ) {
		$("#tab-gui ul.ui-tabs-nav li:first-child").addClass("first");
		$("#content-wrapper .type4-slides-class > li").addClass("adjIE");
		$("table").attr("cellspacing", 0);
	}
	// JES 20090425 - working on the styling of the breadcrumbs
	if ( $("#nav-bread li").size() > 4 ) {
		//var liCount = $("#nav-bread li").size();
		$("#nav-bread li").each(function(idx, obj) {
			if (idx < $("#nav-bread li").size()-3) {
				var content = obj.innerHTML;
				content=content.replace(/[\r\n\t]/g,"");
				content=content.replace(/ +/g," ");
				if (idx == 0) {
					content=content.replace(/(<[^>]*>) *([^<]*?) *(<\/a>).*/,"$1$2$3");
				} else {
					content=content.replace(/(<[^>]*>) *([^<]*?) *(<\/a>).*/,"$1<img src=\"/resources/static/img/bread-bullet.gif\" border=\"0\" style=\"position:relative; top:-1px\"/>$3");
				}
				//alert(idx + " - " + content);
				obj.innerHTML = content;
			}
		});
	}
}

MDA.nav = {
	timeout : 300,
	handleOff : null,
	handleOn : null,
	idx : null,
	ddnav : ["#clinic-options", "#prevention-options", "#prof" , ".jumpmenu"]
}

MDA.nav.setDDNav = function(dest){
	// drop down navigation, enable click and go for these items
	var argu = (typeof dest == 'undefined') ? 'undefined' : dest;	
	if (argu != 'undefined') {
		$(dest).change(function(){
			var z = $(this).val();
			if ($.trim(z) != "" && $.trim(z) != "#") {
				location.href = $(this).val();
			}
		});
	} else {
		for (var i=0; i < MDA.nav.ddnav.length; i++) {
			$(MDA.nav.ddnav[i]).change(function(){
				var z = $(this).val();
				if ($.trim(z) != "" && $.trim(z) != "#") {
					location.href = $(this).val();
				}
			})
		};
	}
}

MDA.nav.setUp = function(){
	// number nav items for IE6
	if ( $.browser.msie && $.browser.version ) {
		$("#nav-main ul li").each(function(i){
			$(this).addClass("nav" + i);
		})
	}
	if ( $("#nav-main li li a").size() > 0 && !MDA.CSS.isInside ) {
		// mark the default one on for this page
		MDA.nav.idx = $("#nav-main>li>a.on").addClass("default").get(0);
		// main menus
		$("#nav-main>li>a").mouseover(function(){
			MDA.nav.mainOver(this);
		}).focus(function(){
			MDA.nav.mainOver(this);
		}).mouseout(function(){
			MDA.nav.mainOff(this);
		}).blur(function(){
			MDA.nav.mainOff(this);
		});
		// sub menus
		$("#nav-main li li a").mouseover(function(){
			MDA.nav.subOn(this);
		}).focus(function(){
			MDA.nav.subOn(this);
		}).mouseout(function(){
			MDA.nav.subOff(this);
		}).blur(function(){
			MDA.nav.subOff(this);
		});
	}
	MDA.nav.setDDNav();
}

MDA.nav.on = function(obj){
	$(obj).addClass("on").next("ul").removeClass("off").addClass("on");
};

MDA.nav.off = function(obj){
	$(obj).removeClass("on").next("ul").removeClass("on").addClass("off");
};

MDA.nav.start = function(that){
	window.clearTimeout(MDA.nav.handleOff);
	MDA.nav.off("#nav-main>li>a");
	MDA.nav.on(that)
};

MDA.nav.stop = function(that){
	if ($(that).get(0) != MDA.nav.idx) {
		window.clearTimeout(MDA.nav.handleOff);
		MDA.nav.off(that);
	}
	MDA.nav.on(MDA.nav.idx);
};

MDA.nav.mainOver = function(obj){
	if ($(obj).hasClass("on")) {
		window.clearTimeout(MDA.nav.handleOff);
		return;
	} else {
		window.clearTimeout(MDA.nav.handleOn);
		window.clearTimeout(MDA.nav.handleOff);
		var n = obj;
		MDA.nav.handleOn = window.setTimeout(function(){MDA.nav.start(n)},MDA.nav.timeout);

		// var handleDelay = ( MDA.nav.handleOn != null ) ? MDA.nav.timeout : 0;
		// MDA.nav.handleOn = window.setTimeout(function(){MDA.nav.start(n)},handleDelay);
	};
};

MDA.nav.mainOff = function(obj){
	window.clearTimeout(MDA.nav.handleOn);
	var x = obj;
	MDA.nav.handleOff = window.setTimeout(function(){MDA.nav.stop(x)},MDA.nav.timeout);
};

MDA.nav.subOn = function(obj){
	if ($(obj).parent().parent().parent().find("a.on")) {
		window.clearTimeout(MDA.nav.handleOff);
		window.clearTimeout(MDA.nav.handleOn);
	};
}

MDA.nav.subOff = function(obj){
	window.clearTimeout(MDA.nav.handleOn);
	// if this sub-item's parent is on and not the default stop it
	if ($(obj).parent().parent().parent().find("a.on").get(0) != MDA.nav.idx) {
		var z = $(obj).parent().parent().parent().find("a.on");
		MDA.nav.handleOff = window.setTimeout(function(){MDA.nav.stop(z)},MDA.nav.timeout);
	};
}

MDA.Treeview = {
	options: {
		collapsed : true,
		animated : "fast",
		control : "#tab-tools"
	}
};

MDA.Treeview.setUp = function(){
	try { // script not always on the page
		$("#sitemap .tree .col1 > ul").treeview(MDA.Treeview.options);
		$("#sitemap .tree .col2 > ul").treeview(MDA.Treeview.options);
		$("#sitemap .tree .col3 > ul").treeview(MDA.Treeview.options);
	}
	catch(err) { return true; }
}

$(window).load(function(){
	MDA.CSS.setHeights();
})

$(function(){
	MDA.CSS.setUp();
	MDA.nav.setUp();
	MDA.slides.setUp();
	MDA.Events.filter();
	MDA.alphaShadow = null;
	if ( $("#sitemap") ) {
		MDA.Treeview.setUp();
	}

	// set up alphabet pop-up layer
	if ( $("body.home").size() > 0 || $("body.section").size() > 0 || $("body.drill").size() > 0) {
		if ( $("body.patient").size() > 0 ) {
			$("#content-highlights").append("<div id='bnr-overlay' />");
		}
		$("#alphabet").hide();
		if ( $("#cancer-types").size() > 0 ) {
			var wii = $("#cancer-types").outerWidth();
			var hii = $("#cancer-types").outerHeight();
			var pos = $("#cancer-types").position();
			
			$("#cancer-types").css({
				"position" : "absolute",
				"top" : pos.top + 1 + "px",
				"left" : pos.left + "px",
				"z-index" : 30
			});
			/* since #cancer-types shows up on body.section too we need to adjust the TOP value. */
			$('body.drill:not(body.section) #cancer-types').css({
				'top': pos.top + 14 + "px"
			});
			$("#cancer-types").before("<div />").prev("div").css({
				"height" : hii + "px",
				"width" : wii + "px",
				"float" : "left",
				"margin-bottom" : 10 + "px"
			});
			/* since #cancer-types shows up on body.section too we need to adjust the TOP value. */
			$('body.drill:not(body.section) #cancer-types').prev('div').css({
				'margin-bottom': 40 + "px"
			});
			if ( $.browser.msie && $.browser.version < 7 ) {
				$("#cancer-types").prev("div").addClass("adjIE6");
			}
			$("#cancer-types ul").eq(0).before("<p class='hider'>Close</p>");
			$(".home #cancer-types ul li,.section #cancer-types ul li").each(function(i){
				if ( i == "8" || i == "17" || i == "24") {
					$(this).addClass("edge")
				}
			});
			$('.drill #cancer-types ul li').each(function(i){
				if( i==10 || i==21 || i==24){
					$(this).addClass('edge');
				}
			});
			$("#cancer-types li a").click(
				function(e){
					$("#alpha-drill").remove();
					$("#cancer-types form").find("p.alphas").remove();
					$("#cancer-types form").append('<select id="alpha-drill" size="7" />').find("#alpha-drill").hide();
					MDA.nav.setDDNav("#alpha-drill");
					$("#alphabet optgroup[label=" + $(this).text() + "]").find("option").clone().appendTo("#alpha-drill");
					if ( $("#alpha-drill option").eq(0).text() == "[none]" ) {
						$("#alpha-drill").replaceWith("<p class='alphas'>Currently, no cancer types begin with this letter.</p>");
					} else {
						$("#alpha-drill").show();
					}
					if ( !$.browser.msie ) {
						if ( $("#overlay").size() < 1 ) {
							$("body").append('<div id="overlay" />');
						}
					}
					$("#cancer-types").addClass("on");
					if ( MDA.alphaShadow === null ) {
						MDA.alphaShadow = $("#cancer-types.on").dropShadow({
							top: 1,
							left: 1,
							blur: 6,
							opacity: .6
						})
					}
					e.preventDefault();
				}
			);
			$("#cancer-types p.hider").click(
				function(){
					$("#alpha-drill").empty().hide();
					$("#cancer-types").removeClass("on").find("p.alphas").remove();
					$("#cancer-types").removeShadow();
					$("#overlay").remove();
					MDA.alphaShadow = null;
				}
			);
		}
		$("#cancer-topics .vert-tabs > ul").tabs();			
	}
	$("#tab-gui.horiz-tabs > ul.ui-tabs-nav").tabs();
	$("#tab-page.horiz-tabs > ul.ui-tabs-nav").tabs();
	/* bug B-57; Just For You tabs */
	$(".cch #tab-gui ul.ui-tabs-nav").tabs({
		show: function() {
			$(this)
				.find('li')
				.removeClass('firstOn')
				.removeClass('secondOn')
				.removeClass('thirdOn')
				.end()
				.find('li.ui-tabs-selected')
				.addClass($(this).find('li.ui-tabs-selected').attr('class').replace(' ui-tabs-selected', '') + 'On');
		}
	});
	
	var $horizTabs = $(".horiz-tabs");
	// need to make sure this doesn't run on site map or education home or other GUI looking tabs; just the page tabs
	if ( $horizTabs.size() > 0 && $("#sitemap").size() < 1 && $("body.drill.section.education").size() < 1 && $("body.cch.drill.extra").size() < 1) {
		if (!($.browser.msie && $.browser.version < 7)) {
			var horizCount = $horizTabs.find(".ui-tabs-nav li").size();
			var horizWidth = $horizTabs.width();
			var offSetBottom = $horizTabs.find("li:last-child").offset().top
			var horizWidthLis = 0;
				$horizTabs.find(".ui-tabs-nav li").each(function(){
					horizWidthLis += $(this).outerWidth() - $(this).find("a:eq(0)").outerWidth();
				});
			var horizWidthAs = $horizTabs.find(".ui-tabs-nav li>a").size();
			var offsetwidth = horizWidthAs + horizWidthLis;
			var horizSize = (($(".ui-tabs-nav").width() - offsetwidth)/horizCount);
			var horiz = (horizSize*horizCount) + offsetwidth;
			$horizTabs.find(".ui-tabs-nav li a").css("width", horizSize + "px");
			if ( $.browser.msie ) {
				x = 0
				// increase the size of the last LI by one pixel until the item wraps to next line
				while ( x < 50 ) {
					if ( $horizTabs.find("li:last-child").offset().top == offSetBottom ) {
						$horizTabs.find(".ui-tabs-nav li:last-child a").css("width", $horizTabs.find(".ui-tabs-nav li:last-child a").width() + 1 + "px")
					} else {
						//	then, reduce the size by one so it goes back to the previous line. 
						// 		no, i'm not kidding. IE can't do math.
						$horizTabs.find(".ui-tabs-nav li:last-child a").css("width", $horizTabs.find(".ui-tabs-nav li:last-child a").width() - 1 + "px")
						break;
					}
					x++;
				}
			}
		}
	}
	$("#tab-page.horiz-tabs > ul.ui-tabs-nav > li > a").click(function(){
		$(this).parent().siblings().removeClass("adj");
		$(this).parent().prev("li").addClass("adj");
	})
	
	$("#search label").click(function(){
		$(this).hide().next("input").focus();
	}).blur(function(){
		if ($(this).next("input").val() == "") {
			$(this).show().next("input").blur();
		}
	});
	$("#search input").focus(function(){
		$(this).prev("label").hide()
	}).blur(function(){
		if ($(this).val() == "") {
			$(this).prev("label").show();
		}
	});

	// rebuild tab indexes after half second...
	setTimeout(function(){
		// @todo: this is just bad
		var xxx = $("a");
		var iii = 0;
		$.each(MDA.slides.slideShowsList, function(){
			xxx = $(xxx).not(this);
		});
		$(xxx).each(function(i){
			$(this).attr("tabIndex", i)
			iii = i;
		});
		iii++;
		// JES 20090518 - Added the if body.hasClass(app) test to NOT give application forms the tabIndex treatment.
		if (! $("body").hasClass("app")) {
			$("form *").not("input[type=hidden]").each(function(){
				if ( $(this).is(":input") || $(this).is(":submit") || 
						$(this).is(":image") || $(this).is(":reset") || 
						$(this).is(":file") ||	$(this).is(":checkbox") || 
					$(this).is(":radio") ) {
							$(this).attr("tabIndex", iii)
							iii++;
						}
			});
		};
	}, 500);

});

