// Mobile
$(document).ready(function() {
	
	// For Mobile Template: if any div.cmspage is empty, let's remove it -- avoids padding on empty pages.			
		$('#Mobile div').each(function(i, el) {
		// Remove white space
		var html = $(el).html().replace(/\s+/, '');
		// Check if element is :empty or length of html is zero
		if ($(el).is(':empty') || html.length == 0)
			$(el).remove();
		});
	
	$('body#Mobile .ERWTRow span a')
	   .each(function() { 
		  this.href = this.href.replace(/southernregional.org/, document.domain);
		  this.href = this.href.replace(/www./, '');
		  this.href = this.href.replace(/services.urgent-care/, 'mobile/UrgentCare');
	   });

});  //END DOC READY


function addPageTools() {
	var urlToUse = (('https:' == document.location.protocol) ? 'https://secure.addthis.com/js/250/addthis_widget.js?pub=srhsatlanta' : 'http://s7.addthis.com/js/250/addthis_widget.js?pub=srhsatlanta');
	$('#PageToolsWrapper').append('<div id="PageTools">' +
		'<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 = "srhsatlanta";</script>' +
					'<a href="http://www.addthis.com/bookmark.php?v=250&pub=srhsatlanta" 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 = 'southernregional.org';
	var clientSiteName = 'Southern Regional Health System';
	var titleEl = document.getElementsByTagName('title')[0];
	var currentPageTitle = '';
	if (titleEl.text) { currentPageTitle = titleEl.text; } else { currentPageTitle = 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.Print a').click(function() { print(); return false; });
	$('li.Email a').attr({ href: emailHref});
}

//TEXT SIZER
function addTextSizer() {
	$('#PageToolsWrapper #PageTools').before('<div id="TextSizer" class="ClearFix"><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(){
	addPageTools();	
	addTextSizer();
	text_size_checker();
	dynamicVideoPlayer();
	
// ----------- TEMPLATE EXTRAS ------------------//

	$('#QuickLinks div.cmspanel li:last').addClass('Last');
	
	//hiding empty abstract div
	$('.Abstract:empty').hide();
	
	//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');
	
	$('#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');
	
	//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();
	
	//adding Advertiser OpenX script to career and nursing sections
	var thisDir = $.url.attr('directory')
    if (thisDir.indexOf('careers') > 0) {  // if the word 'careers' exists in the url path anywhere, then do this
        $('#Footer').append('<script src="http://ad.yieldmanager.com/pixel?id=926101&t=1" type="text/javascript"></script>');
    }
	if (thisDir.indexOf('nursing-careers') > 0) {  // if the word 'nursing-careers' exists in the url path anywhere, then do this
        $('#Footer').append('<script src="http://ad.yieldmanager.com/pixel?id=926103&t=1" type="text/javascript"></script>');
    }
	
	
// ----------- HACK LIST START ------------------//

	//removing empty table cells in calendar
	$('.CalendarEventDetailHeader td:empty').addClass('Empty');
	//adding TabContainerlive back to tabs
	$('div.TabContainer').addClass('TabContainerlive');

// ----------- HACK LIST END ------------------//

}); // End Document Ready
