function addPageTools() {
    var urlToUse = (('https:' == document.location.protocol) ? 'https://s7.addthis.com/js/250/addthis_widget.js#pub=stvincent' : 'http://s7.addthis.com/js/250/addthis_widget.js#pub=stvincent');
    var imgPath = '/imgs/icons/';
    $('#PageToolsWrapper .Dec').append('<div id="PageTools" class="ClearFix">' +
		'<ul>' +
			'<li class="Email"><a href="#">Email</a></li>' +
			'<li class="Print"><a href="#">Print</a></li>' +
			'<li class="AddThis">' +
				'<!-- AddThis Button BEGIN -->' +
					'<a href="http://www.addthis.com/bookmark.php?v=250&amp;pub=stvincent">Add</a>' +
					'<script type="text/javascript" src="' + urlToUse + '"></script>' +
				'<!-- AddThis Button END -->' +
			'</li>' +
		'</ul>' +
	'</div>');
    var clientDomainName = 'jaxhealth.com';
    var clientSiteName = 'St. Vincents 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; });
}

//TEXT SIZER
function addTextSizer() {
	$('#PageToolsWrapper .Dec').append('<div id="TextSizer" class="ClearFix"><h2>Text Size</h2><ul>' +
				'<li><a class="NormalFont" href="javascript:;" title="Small Font Size"></a></li>' +
				'<li><a class="LargeFont" href="javascript:;" title="Medium Font Size"></a></li>' +
				'<li><a class="X-LargeFont" href="javascript:;" title="Large Font Size"></a></li>' +
			'</ul></div>');	
	$('a.NormalFont').click(function() {$('#Content').removeClass('textLarge').removeClass('textXLarge');$.cookie('text_sizer', null, { path: '/' });});
	$('a.LargeFont').click(function() {$('#Content').addClass('textLarge').removeClass('textXLarge');$.cookie('text_sizer', 'large', { path: '/' });});
	$('a.X-LargeFont').click(function() {$('#Content').removeClass('textLarge').addClass('textXLarge');$.cookie('text_sizer', 'xlarge', { path: '/' });});
	$('#TextSizer a').click(function() {$('#TextSizer a').removeClass('active');$(this).addClass('active');});
}

function text_size_checker() {
	var text_sizer_cookie = $.cookie('text_sizer');
	if (text_sizer_cookie==null){$('a.NormalFont').addClass('active')};
	if (text_sizer_cookie=='large'){ $('#Content').addClass('textLarge');$("a.LargeFont").addClass('active') };
	if (text_sizer_cookie=='xlarge'){ $('#Content').addClass('textXLarge');$("a.X-LargeFont").addClass('active') };
}

function dynamicVideoPlayer(){
// markup to use: <a class="VideoLink" href="video/widescreen.mp4" title="height=288;width=512;aspect=16x9;hideControls=true;imageFilename=video/widescreen.jpg;">Video</a>
	String.prototype.startsWith = function(str) {return (this.match("^"+str)==str);}; // this creates a function that can be used to check if a string starts with a value
	// for each a.VideoLink, put a player in place
	$('a.VideoLink').each(function(n){
		var divID = 'videoID'+n;
			$(this).wrap('<div class="DynamicVideoPlayer" id="' + divID + '"></div>');
		var filename = $(this).attr('href');
		var vars = $(this).attr('title').replace(/\s+/g,'');
		var varArray = vars.split(";");
		var height, width, aspect, hideControls, imageFilename, theImage;
		height = width = aspect = hideControls = imageFilename = theImage = '';
		
		var cnt = 0;
		while (cnt < varArray.length) {
			if (varArray[cnt].toLowerCase().startsWith('height=')) { height = varArray[cnt].toLowerCase().replace(/height=/g,''); }
			if (varArray[cnt].toLowerCase().startsWith('width=')) { width = varArray[cnt].toLowerCase().replace(/width=/g,''); }
			if (varArray[cnt].toLowerCase().startsWith('aspect=')) { aspect = '_' + varArray[cnt].toLowerCase().replace(/aspect=/g,''); }
			if (varArray[cnt].toLowerCase().startsWith('hidecontrols=')) { hideControls = varArray[cnt].toLowerCase().replace(/hidecontrols=/g,''); }
			if (varArray[cnt].toLowerCase().startsWith('imagefilename=')) { imageFilename = varArray[cnt].toLowerCase().replace(/imagefilename=/g,''); }
			cnt+=1;
		}
		if (height < 0 || height > 600) { height = "240"; }
		if (width < 0 || width > 800) { width = "360"; }
		if ( aspect.length && !(aspect == '_4x3' || aspect == '_16x9' || aspect == '_1x1') ) { aspect = "_4x3"; }
		if (hideControls !== 'true' && hideControls !== 'false') { hideControls = 'false'; }
		if (imageFilename.length > 0 ) { theImage = 'imageFilename=' + imageFilename + '&'; }
			
		var so = new SWFObject('/video/videoPlayer' + aspect + '.swf?videoURL=' + filename, 'swf', width, height, '7' );
		so.addParam('wmode', 'transparent');
		so.addParam('FlashVars', theImage + 'hideControls=' + hideControls);
		so.addParam('allowscriptaccess', 'always');
		so.addParam('allowfullscreen', 'true');	
		so.write(divID);
	});
}



$(document).ready(function(){
	addTextSizer();
	text_size_checker();
	addPageTools();
	dynamicVideoPlayer();
	
// ----------- TEMPLATE EXTRAS ------------------//

	
	// Removing Col3 header in microsite interior if no panels exist.
	$('body.LandingInterior #Col3Panels:empty').prev().hide();
	
	// Adding a class of First to the first tab on home page
	$('body.Home ul.Tabnav li:first a').addClass('First');
	
	// Ading first and last class to home filmstrip table tds
	$('table.HomeFilmstrip td:nth-child(1)').addClass('First');
	$('table.HomeFilmstrip td:nth-child(2)').addClass('Second');
	$('table.HomeFilmstrip td:nth-child(3)').addClass('Third');
	$('table.HomeFilmstrip td:nth-child(4)').addClass('Fourth');
	
	// Removing QuickLinks div if no cmspanel resides there.
	$('body.Interior #QuickLinks:empty').parent().addClass('NoQuickLinks');
	$('body.Interior #QuickLinks:empty').remove();
	
	// Removing SlideShowPanel, ColorPreFooterWrapper2 & BelowCol panels if no cmspanel resides there.
	$('#SlideShowPanel .Dec:empty').parent().remove();
	$('#ColorPreFooter:empty').parent().parent().remove();
	$('#BelowCol1:empty').remove();
	$('#BelowCol2:empty').remove();
	$('#Content2Col:empty').remove();
	
	// Hiding containers if empty
	$('div.cmspage:empty').addClass('Hide');
	$('#ContentCol1:empty').addClass('Hide');
	$('#ContentCol2:empty').addClass('Hide');
	
	//adding Active class to top level when drop down is still active (landing/landing interior)
	$('#MainNav ul li').hover(
		function(){$(this).addClass('ActiveParent');},
		function(){$(this).removeClass('ActiveParent');}
	);

	// Main Nav addition of home icon
	$('#MainNav ul').prepend('<li class="Home"><a href="/">Home</a></li>');
	$('body.Home #MainNav li.Home').remove();
	$('body.Landing #MainNav li.Home').remove();
	$('body.LandingInterior #MainNav li.Home').remove();

	// Adding spans to main nav dropdown
	$('#MainNav ul ul li a').wrapInner('<span></span>');
	$('#MainNav ul ul li strong').wrapInner('<span></span>');

	// Moving panel for Proximity Nav after the H2 prox nav header
	$('#ProximityNav h2').after('<div class="NavPanel"></div>');
	$('#ProxNavPanel').appendTo('.NavPanel');
	$('#ProxNavPanel:empty').hide();

	// Hiding top border from first cmspanel in right col (landing interior)
	$('#RightPanel .cmspanel:first').addClass('FirstPanel');

	// Adding class to first cmspanel in column so all panels don't get bkgd color.
	$('#ContentCol1 .cmspanel:first').addClass('FirstPanel');
	$('#ContentCol2 .cmspanel:first').addClass('FirstPanel');
	$('#ContentCol3 .cmspanel:first').addClass('FirstPanel');

	// Making external service link open in a new window
	$('#Services a[href^="http://"]')
        .attr({
            target: "_blank",
            title: "Opens in a new window"
        });

    // Add class name to Status dropdown in Find a Doctor form
    $('.DrSearch fieldset div:nth-child(5)').addClass('Status');

    // Make "Active" the default selected Status item in Find a Doctor form
    $('.DrSearch .Status select').attr('selectedIndex', 1);

// ----------- HACK LIST START ------------------//

	// Removing padding from empty table cells from calendar event detail header
	$('.CalendarEventDetailHeader td:empty').addClass('Empty');

	//adding hack to be fixed after 5.2.7 
	$('.TabContainer').addClass('TabContainerlive');

// ----------- HACK LIST END ------------------//
	
}); // End Document Ready


// Quick links toggle slide
$(document).ready(function(){
	$('#QuickLinksHome :header').click(function(){
		$('#QuickLinksHome ul').slideToggle(function(){
		
		  if ($(this).is(':hidden')) {
		   $('#QuickLinksHome h3').css({"background":"url(../../imgs/quicklinks-arrow-closed.gif) no-repeat left center"});
		  } else {
		   $('#QuickLinksHome h3').css({"background":"url(../../imgs/quicklinks-arrow.gif) no-repeat left center"});
		  }
		});
			
		//$('#QuickLinksHome').toggleClass('Open');
		return false;
	});
});


// Auto Rotating Tabs
(function($) {
    $.fn.equalHeights = function() {
        tallest = 0;
        this.each(function() {
            if ($(this).height() > tallest) {
                tallest = $(this).height();
            }
        });
        return this.each(function() {
            $(this).height(tallest);
        });
    }
})(jQuery);

	var rotateSpeed = 5000; // Milliseconds to wait until switching tabs.
	var currentTab = 0; // Set to a different number to start on a different tab.
	var numTabs; // These two variables are set on document ready.
	var autoRotate;

	function openTab(clickedTab) {
		var thisTab = $(".tabbed-box .tabs a").index(clickedTab);
		$(".tabbed-box .tabs li a").removeClass("active");
		$(".tabbed-box .tabs li a:eq("+thisTab+")").addClass("active");
		$(".tabbed-box .tabbed-content").hide();
		$(".tabbed-box .tabbed-content:eq("+thisTab+")").show();
		currentTab = thisTab;
	}

	function rotateTabs() {
		var nextTab = (currentTab == (numTabs - 1)) ? 0 : currentTab + 1;
		openTab($(".tabbed-box .tabs li a:eq("+nextTab+")"));
	}

	$(document).ready(function() {
		$(".tabbed-content").equalHeights();
		numTabs = $(".tabbed-box .tabs li a").length;
		$(".tabbed-box .tabs li a").click(function() { 
			openTab($(this)); return false; 
		});
		$(".tabbed-box").mouseover(function(){clearInterval(autoRotate)})
		.mouseout(function(){autoRotate = setInterval("rotateTabs()", rotateSpeed)});
		
		$(".tabbed-box .tabs li a:eq("+currentTab+")").click()
		$(".tabbed-box").mouseout();
		
	});
	
	
/* Animated List */
function theRotator() {
	//Set the opacity of all images to 0
	$('div.AnimatedListContainer ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div.AnimatedListContainer ul li:first').css({opacity: 1.0});
		
	//Call the Rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',5400);
}

function rotate() {	
	//Get the first image
	var current = ($('div.AnimatedListContainer ul li.show')?  $('div.AnimatedListContainer ul li.show') : $('div.AnimatedListContainer ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.AnimatedListContainer ul li:first') :current.next()) : $('div.AnimatedListContainer ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: '1.0'}, 2500);
	

	//Hide the current image
	current.animate({fontSize: '0em'}, 800)
	current.animate({opacity: '0.0'}, 2500)
	.removeClass('show')
	.animate({fontSize: '3em'}, 100);
	
	/* Link Hover Animation 
    $(function(){
        $(".AnimatedList li a").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
	});*/
};

$(document).ready(function() {		
	//Load the slideshow
	theRotator();
});