//TEXT SIZER
function addTextSizer() {
    var urlToUse = (('https:' == document.location.protocol) ? 'https://secure.addthis.com/js/200/addthis_widget.js' : 'http://s7.addthis.com/js/200/addthis_widget.js');
	$('#TopContentWrapper').append('<div id="TextSizer" class="ClearFix"><h2 class="Hide">Text Size</h2><ul>' +
			    '<li class="AddThis">' +
					'<script type="text/javascript">var addthis_pub="mercymarketing1";</script>' +
					'<a href="https://www.addthis.com/bookmark.php?v=20" onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()" onclick="return addthis_sendto()">' +
					'<img src="https://s7.addthis.com/static/btn/sm-plus.gif" width="16" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="https://s7.addthis.com/js/200/addthis_widget.js"></script>' +
				'</li>' +
				'<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 addPageTools() {
	$('#PageToolsWrapper').append('<div id="PageTools" class="ClearFix">' +
		'<ul class="Horiz">' +
			'<li class="Email"><a href="#">Email</a></li>' +
			'<li class="Print"><a href="#">Print</a></li>' +
		'</ul>' +
	'</div>');
	var clientDomainName = 'MercyCR.com';
	var clientSiteName = 'Mercy CR';
	var titleEl = document.getElementsByTagName('title')[0];
	var currentPageTitle = '';
	if (titleEl.text) { currentPageTitle = escape(titleEl.text); } else { currentPageTitle = escape(title.data); }
	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 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 ------------------//

	//add Last class to last td/th of row within tables
	$('table tr td:last-child').addClass('Last');
	$('table tr th:last-child').addClass('Last');
	
	//adding divs within Table4 td for styling
	$('table.Border4 td').wrapInner('<div class="Dec1"></div>');
	
// ----------- HACK LIST START ------------------//

	//adding class to each panel in Col3 - except first panel
	$('#Col3 .cmspanel:not(#Col3 .cmspanel:first)').addClass('BorderTop');
	
	//temporary fix for tabs to contain floated elements without them breaking out of tab container
	$('div.Tab').addClass('ClearFix');
	
	//removing empty table cells in calendar
	$('.CalendarEventDetailHeader td:empty').addClass('Empty');

// ----------- HACK LIST END ------------------//
	

});


// Popup Windows
function strOpenWindowFeatures(iWindowWidth, iWindowHeight) {
	var iMouseX = 10;
	var iMouseY = 10;
	if ( window.event != null ) {
		iMouseX = window.event.screenX;
		iMouseY = window.event.screenY; }
	var iScreenX = window.screen.availWidth;
	var iScreenY = window.screen.availHeight;
	var iWindowLeft = iMouseX;
	if ( iWindowLeft + iWindowWidth > iScreenX ) { iWindowLeft = iMouseX - iWindowWidth - 10; }
	if ( iWindowLeft < 10 ) { iWindowLeft = 10; }
	var iWindowTop = iMouseY;
	if ( iWindowTop + iWindowHeight > iScreenY - 50 ) { iWindowTop = iMouseY - iWindowHeight - 60; }
	if ( iWindowTop < 10 ) { iWindowTop = 10; }
	return 'width=' + iWindowWidth + ', height=' + iWindowHeight + ', left=' + iWindowLeft + ', top=' + iWindowTop + ', resizable=yes';
}
function fnOpenWindow(strWindowURL, strWindowName, iWindowWidth, iWindowHeight) {
	var objNewWindow = window.open(strWindowURL, strWindowName, strOpenWindowFeatures(iWindowWidth, iWindowHeight), false);
	if ( objNewWindow != null ) {
		objNewWindow.focus();
	}
}

// Simplified Popup
// Usage: <a onclick="return popUp(this.href,250,250);" href="popup.aspx">Link</a>
function popUp(theLink, popUpWidth, popUpHeight) {
	var popUpWindow = window.open(theLink, 'popUp', 'width=' + popUpWidth +
	', height=' + popUpHeight +
	', left=50, top=50, resizable=yes, scrollbars=yes', false);
	if ( popUpWindow != null ) {
		popUpWindow.focus();
	}
	return false;
}
function closePopUp() {
window.close();
}

function closeSurveyPopUp() {
	// Cookie setter
	var expireDate = new Date();
	expireDate.setMonth(expireDate.getMonth() + 6);
	document.cookie = 'hasClicked=True;expires=' + expireDate.toGMTString() + ';path=/;';
	document.getElementById('SurveyPopUp').style.display = 'none';
}
// Cookie getter
function getCookieValue() {
	var cookieName = 'hasClicked';
	var cookieValue = document.cookie;
	var cookieStartsAt = cookieValue.indexOf(' ' + cookieName + '=');
	if (cookieStartsAt == -1) {
		cookieStartsAt = cookieValue.indexOf(cookieName + '=');
	}
	if (cookieStartsAt == -1) {
		cookieValue = null;
	}
	else {
		cookieStartsAt = cookieValue.indexOf('=', cookieStartsAt) + 1;
		var cookieEndsAt = cookieValue.indexOf(';', cookieStartsAt);
		if (cookieEndsAt == -1) {
			cookieEndsAt = cookieValue.length;
		}
		cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
	}
	return cookieValue;
}
// Survey Psuedo-Popup
function showSurveyPopUp() {
	var hasClicked = getCookieValue();
	if (hasClicked == null) {
		document.getElementById('SurveyPopUp').style.display = 'block';
	}
}
addLoadEvent(showSurveyPopUp);