/*
	** "togSection.js" 
	@ version 1.77
	by Aaron Lademann
	Sisters of Mercy Health System Internet
	---
	
	** MARCH 24, 2008 **
	
	fitted a version of the script for www.mercy.net uses.
	
*/
var $j = jQuery.noConflict();

$j(document).ready(function(){
		
		prepareLinkboxes();
		// toggle everything that has a class of .tog
		//defaultToggles();
		
}); // end document.ready()


// LINKBOX BUILDS - (called at bottom of html) simplified initial HTML structure, recognizes #linkbox2 as needing the inner wrapper.
function prepareLinkboxes(){
		// NEW linkbox functions
		newLboxes = $j("#contributionArea div.linkbox2");
		jQuery.each(newLboxes, function(){
				
				// find li's that don't contain links so they'll match the link styling
				$j(this).find("ul:not(:parent(.selectbox-wrapper))").filter("li:not(:has(a))").each(function(){ $j(this).addClass("noLink"); });
				
				// find and manipulate data enclosed in the linkbox container
				getData(this);

				function getData(ssDat){					
					var dat = ssDat;
					var col = $j(dat).find("ul");
					
					// define width of linkbox
					var lWt = $j(dat).width();
					// define paragraph width UNLESS the paragraph has a class X, then ignore it.
					$j(dat).css("width", lWt + "px").find("p:not(.X)").each(function(){ $j(this).css("width", (lWt-10) + "px"); });

					if(col != "undefined") {
						$j(dat).find("ul:last").css("margin-right", "0");
						//spit out column widths based on how many there are.
						defineColumns(col, lWt);
					} else return;
				}
				
				function defineColumns(ssC, ssW){
					var c = ssC;
					var cTyp = $j(c).attr("class");
				  var w = ssW;
					var i = 0;
					jQuery.each(c, function(){ 											
						i++;
							// integrate title of column if it exists
								var t = $j(this).attr("title");
								$j(this).removeAttr("title").prepend("<li class=title>" + t + "</li>").find("li.title:contains(undefined)").remove();
					});
					var extra = 0;
					
					// if it's a column ul
					if (cTyp == "column") {
						if (i > 2) { var splits = i-(i-1); var extra = 2; } else { var splits = i-1; var extra = 3; }
						var colWt = parseInt( ((w+10) / i) - (5 * splits) - extra);
					} else {
						var colWt = w;
					}
					
					
					
					$j(c).css("width", colWt + "px");
					//.slideDown("slow");
					$j(this).find("a.tog").show().removeClass("loading");
						//test width output
						//$j(c).parent().prepend("((" + (w+10) + ") / " + i + ") - (5 * " + splits + ") - " + extra + " = " + colWt);
				}
				
		});
		
		defaultToggles();
		
}
//

function defaultToggles(){
		ClosedTitle = "Click here to maximize ";
		OpenTitle = "Click here to minimize ";
		
		// variable for all elements matching these criteria
		var toggleAreas = $j("#contributionArea a.tog");
		// every time one is found, do this function	
		jQuery.each(toggleAreas, function(){
				var togType = $j(this).parent().attr("id");
				
				// if toggle type is NOT a linkbox tree-list, look for H1, H2, or H3 as the identifier - and use it's preceding DIV as the folder
				if (togType != "tree-list") {
					var togTitle = $j(this).parent("h1, h2, h3").text();
					var togFolder = $j(this).parent("h1, h2, h3").find("+ div");
				} else {
					// if toggle type IS a linkbox tree-list, use the immediate A found as the identifier - and use it's preceding UL as the folder
					var togTitle = $j(this).text();
					var togFolder = $j(this).find("+ ul");
				}
				var elementID = $j(this).attr("id");
				var folderID = $j(togFolder).attr("id");
				
				// initialize folder based on className
				if( $j(togFolder).is(":hidden") ) { actClosed(this, togFolder); } else { actOpen(this, togFolder); }
		
				$j(this).click(function(){
						
						// if content folder class is ajax, load content dynamically
						var ifAjax = $j(togFolder).attr("class");
						var sourceFile = elementID + ".html";
						if (ifAjax == "ajax"){ $j(togFolder).load(sourceFile, {}, function(){
																		ajaxToggle(togFolder);
																}); }
						// end ajax check
		
						if( $j(togFolder).is(":hidden") ) { actOpen(this, togFolder); } 
						else { actClosed(this, togFolder); }
						
				 });
				


				$j(this).hover(function(){
						$j(this).addClass("Hover");
				}, function(){
						$j(this).removeClass("Hover");	
				});
				
		
				//
				function actClosed(cA, cF) {
					var CclickThis = cA;
					var closeThis = cF;
						$j(closeThis).hide();
						$j(CclickThis).removeClass("Open").addClass("Closed").attr("title", ClosedTitle + "'" + togTitle + "'");
						$j(CclickThis).filter("#sub-section").removeAttr("class").addClass("tog");
				}
				//
				function actOpen(oA, oF) {	
					var OclickThis = oA;
					var openThis = oF;
						$j(openThis).show();
						$j(OclickThis).removeClass("Closed").addClass("Open").attr("title", OpenTitle + "'" + togTitle + "'");
						$j(OclickThis).filter("#sub-section").removeAttr("class").addClass("tog");
				}
				//
		
		}); // end each()
}
//

//function changeAll(t, x){
//	
//		button = t;
//		togWrapper = x;
//		togBoxes = $j(togWrapper).find("div.linkbox2");	
//	
//	$j(button).toggle(function(){				 
//														 
//				var togHeader = $j(togBoxes).find("h1");
//				var togTitle = $j(togHeader).text();
//				//$j(togBoxes).find("div.closed").show();
//				$j(this).find("div.closed").removeClass("closed").addClass("open").show();
//				$j(togHeader).removeClass("Closed").addClass("Open").attr("title", OpenTitle + "'" + togTitle + "'");
//	
//	}, function(){
//				var togHeader = $j(togBoxes).find("h1");
//				var togTitle = $j(togHeader).text();
//				//$j(togBoxes).find("div.closed").hide();
//				$j(this).find("div.open").removeClass("open").addClass("closed").hide();
//				$j(togHeader).removeClass("Open").addClass("Closed").attr("title", ClosedTitle + "'" + togTitle + "'");
//		
//	});;
//	
//	
//}

// ajaxToggle is in function form as well to provide recursion to loop through continual ajax loads if necessary.
function ajaxToggle(x){
	
	var ajaxElem = x;
	
	var embeddedTog = $j(ajaxElem).find("a.tog");
	
			jQuery.each(embeddedTog, function(){
				togFolder = $j(this).parent("h3, h1").find("+ div");
				elementID = $j(this).attr("id");
				folderID = $j(togFolder).attr("id");
				//var folderStat = $j(togFolder).attr("class");
				
				if( $j(togFolder).is(":hidden") ) { actClosed(); } else { actOpen(); }

				$j(this).click(function(){
						
						// if content folder class is ajax, load content dynamically
						var ifAjax = $j(togFolder).attr("class");
						var sourceFile = elementID + ".html";
						if (ifAjax == "ajax"){ $j(togFolder).load(sourceFile, {}, function(){
																		ajaxToggle(togFolder);
																}); }
						// end ajax check

						if( $j(togFolder).is(":hidden") ) { actOpen(); } 
						else { actClosed(); }
						
				 });
				
				$j(this).hover(function(){
						 	$j(this).css("backgroundPosition", "left top");
					}, function(){
						 	$j(this).css("backgroundPosition", "left bottom");						 
					});
				
		
		});
	
}
//