//client stuff
function addPageTools()
{
	var urlToUse = (("https:" == document.location.protocol) ? "https://secure.addthis.com/js/200/addthis_widget.js" : "http://s7.addthis.com/js/200/addthis_widget.js");
	var imgPath = '/imgs/icons/';
	$("#PageToolsWrapper").append("<div id=\"PageTools\" class=\"ClearFix\">" +
		"<h2 class=\"Hide\">Page Tools</h2>" +
		"<ul>" +
			"<li class=\"Email\">" +
				"<a href=\"#\">Email</a>" +
			"</li>" +
			"<li class=\"Print\">" +
				"<a href=\"#\">Print</a>" +
			"</li>" +
			"<li class=\"AddThis\">" +
				"<!-- AddThis Button BEGIN -->" +
					"<script type=\"text/javascript\">var addthis_pub  = \'adventisthealthcare\';</script>" +
					"<a href=\"http://www.addthis.com/bookmark.php?v=20\" onmouseover=\"return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')\" onmouseout=\"addthis_close()\"onclick=\"return addthis_sendto()\">" +
					"Add This" +
					"</a>" +
					"<script type=\"text/javascript\" src='" + urlToUse + "'></script>" +
				"<!-- AddThis Button END -->" +
			"</li>" +
		"</ul>" +
	"</div>");
	var clientDomainName = 'adventisthealthcare.com';
	var clientSiteName = 'Adventist HealthCare';
	var titleEl = document.getElementsByTagName('title')[0];
	var currentPageTitle = "";
	if (titleEl.text)
	{
		currentPageTitle = titleEl.text;
	}
	else
	{
		currentPageTitle = title.data;
	}
	currentPageTitle = escape(currentPageTitle);
	var currentPageUrl = document.location;
	var emailSubject = clientDomainName + ": " + currentPageTitle;
	var emailHref = "mailto:?subject=" + emailSubject + "&body=I thought you would be interested in a page on the " + clientSiteName + " Web site: " + currentPageTitle + " - " + currentPageUrl;
	$("li.Email a").attr({ href: emailHref});
	$("li.Print a").click(function() { print(); return false; });
}
function addTextSizer()
{
	var theHref = 'href="#"';
	$("#PageToolsWrapper").append("<div id=\"TextSizer\" class=\"ClearFix\">" +
			"<h2>Text Size</h2>" +
			"<ul>" +
				"<li>" +
					"<a class=\"Normal\"" + theHref + ">Small Font Size</a>" +
				"</li>" +
				"<li>" +
					"<a class=\"Large\"" + theHref + ">Medium Font Size</a>" +
				"</li>" +
				"<li>" +
					"<a class=\"X-Large\"" + theHref + ">Large Font Size</a>" +
				"</li>" +
			"</ul>" +
		"</div>");
	$("a.Normal").click(function() { setFontSize('normal'); return false; });
	$("a.Large").click(function() { setFontSize('large'); return false; });
	$("a.X-Large").click(function() { setFontSize('x-large'); return false; });
}
$(addTextSizer);
$(addPageTools);

//Home page accordion
function initAccordion() {
$('.Accordion ul').hide();
$('.Accordion ul:first').show();
$('.Accordion ul:first').parent().addClass('Open');
$('.Accordion li a').click(
	function() {
		var checkElement = $(this).next();
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('.Accordion ul:visible').slideUp('normal');
			$('.Accordion ul:visible').parent().removeClass('Open');
			checkElement.slideDown('normal');
			checkElement.parent().addClass('Open');
			return false;
		}
		}
	);
}
$(document).ready(function() {initAccordion();});

$(document).ready(function(){
	//remove background (divider) on first panel in left col
	$('#Col1 #LeftPanels div.cmspanel:first').css('background','none');
	//remove border bottom on last panel in right col
	$('#Col3 div.cmspanel:last').css('border-bottom','none');
	
	//hide cmspage div when empty and/or hide Row3 div when empty
	$('#Home div.cmspage:empty').hide();
	$('#Home #Row3Col1:empty').hide();
	$('#Home #Row3Col2:empty').hide();
	$('#Home #Row3Col3:empty').hide();

	// toggle slide
	$('#Tab').click(function(){
		$('#IndexWrapper').slideToggle();
		$('#Tab').toggleClass('Open');
		return false;
	});
	
	//show the tab if index container .cmspanel exists  
	if ($('#IndexWrapper .cmspanel').length > 0) {
		$('#IndexTabWrapper').css('display','block');
	}
	
	//class of last on on accordion li
	$('.Accordion li:last-child').addClass('Last');
	
	//moving flyout panels to be within the MainNav
	$('#MainNav li:nth-child(2)').addClass('Second');
	$('#MainNav li:nth-child(3)').addClass('Third');
	$('#MainNav li:nth-child(4)').addClass('Fourth');
	$('#MainNav li:nth-child(5)').addClass('Fifth');
	$('#MainNav li:nth-child(6)').addClass('Sixth');
	$('#MainNav li:nth-child(7)').addClass('Seventh');
	
	$('#Flyout1').appendTo('#MainNav li.First');
	$('#Flyout2').appendTo('#MainNav li.Second');
	$('#Flyout3').appendTo('#MainNav li.Third');
	$('#Flyout4').appendTo('#MainNav li.Fourth');
	$('#Flyout5').appendTo('#MainNav li.Fifth');
	$('#Flyout6').appendTo('#MainNav li.Sixth');
	$('#Flyout7').appendTo('#MainNav li.Seventh');
	
	//remove the main nav drop down if .cmspanel doesn't exist or is empty
	$('.Flyout .FlyoutInner:empty').parent().parent().parent().remove();
	$('.Flyout .FlyoutInner .cmspanel:empty').parent().parent().parent().parent().remove();
	
});





function selectCode(a)
{
	// Get ID of code block
	var e = a.parentNode.parentNode.getElementsByTagName('CODE')[0];

	// Not IE
	if (window.getSelection)
	{
		var s = window.getSelection();
		// Safari
		if (s.setBaseAndExtent)
		{
			s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
		}
		// Firefox and Opera
		else
		{
			if (window.opera && e.innerHTML.substring(e.innerHTML.length - 4) == '<BR>')
			{
				e.innerHTML = e.innerHTML + '&nbsp;';
			}

			var r = document.createRange();
			r.selectNodeContents(e);
			s.removeAllRanges();
			s.addRange(r);
		}
	}
	// Some older browsers
	else if (document.getSelection)
	{
		var s = document.getSelection();
		var r = document.createRange();
		r.selectNodeContents(e);
		s.removeAllRanges();
		s.addRange(r);
	}
	// IE
	else if (document.selection)
	{
		var r = document.body.createTextRange();
		r.moveToElementText(e);
		r.select();
	}
}
