// MOBILE CHANGES
	$(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(/genesishealth.com/, document.domain);
		  this.href = this.href.replace(/www./, '');
		  this.href = this.href.replace(/services.urgent-care/, 'mobile/UrgentCare');
	   });
	
	});  //END DOC READY
	

//TEXT SIZER
function addTextSizer() {
	$('#PageToolsWrapper').append('<div id="TextSizer" class="ClearFix"><h2>Text Sizer</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 addPageTools()
{
	var urlToUse = (('https:' == document.location.protocol) ? 'https://secure.addthis.com/js/250/addthis_widget.js?pub=genesishealth' : 'http://s7.addthis.com/js/250/addthis_widget.js?pub=genesishealth');
	var imgPath = '/imgs/icons/';
	$('#PageToolsWrapper #TextSizer').after('<div id="PageTools">' +
		'<ul>' +
			'<li class="Print"><a href="#">Print</a></li>' +
			'<li class="Email"><a href="#">Email</a></li>' +
			'<li class="AddThis">' +
				'<!-- AddThis Button BEGIN -->' +
				'<script type="text/javascript">var addthis_pub = "genesishealth";</script>' +
					'<a href="http://www.addthis.com/bookmark.php?v=250&pub=genesishealth" onclick="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()"onclick="return addthis_sendto()" onclick="pageTracker._trackEvent(\'AddThis\', \'Open\');">' +
					'<img src="/imgs/icons/add-this.gif" width="16" height="16" alt="Bookmark and Share" style="border:0"/>' +
					'</a>' +
					'<script type="text/javascript" src="' + urlToUse + '"></script>' +
				'<!-- AddThis Button END -->' +
			'</li>' +
		'</ul>' +
	'</div>');
	
	var clientDomainName = 'genesishealth.com';
	var clientSiteName = 'Genesis Health System';
	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 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 ------------------//

    //Removes Footer Top and/or Banner if no cmspanel available
    $('#FooterTop:empty').remove();
    $('#Banner:empty').remove();

    //adds div within each cell of .Section table
    $('table.Sections td').wrapInner('<div class="SectionDec"></div>');

    //adds class of ClearFix to the SectionDec class for landing pages so image doesn't overflow area
    $('.SectionDec').addClass('ClearFix');

    // Ading class names to the tabnav links for styles
    $('#Home .Tabnav li:nth-child(1) a').addClass("First");
    $('#Home .Tabnav li:nth-child(2) a').addClass("Second");
    $('#Home .Tabnav li:nth-child(3) a').addClass("Third");
    $('#Home .Tabnav li:nth-child(4) a').addClass("Fourth");
    $('#Home .Tabnav li:nth-child(5) a').addClass("Fifth");
    $('#Home .Tabnav li:nth-child(6) a').addClass("Sixth");
    $('#Home .Tabnav li:nth-child(7) a').addClass("Seventh");
    $('#Home .Tabnav li:nth-child(8) a').addClass("Eigth");
    $('#Home .Tabnav li:nth-child(9) a').addClass("Ninth");
    $('#Home .Tabnav li:nth-child(10) a').addClass("Tenth");
    $('#Home .Tabnav li:last a').addClass("Last");
    
    // ----------- HACK LIST START ------------------//

    //Adding PanelWrappers around Bottom cmspanels - for right col template
    $('#LowerPanels .cmspanel').wrap('<div class="PanelWrapper"></div>');
    $('#LowerPanels .PanelWrapper').wrap('<div class="PanelWrapper2"></div>');

    //Adding class to first header in each cmspanel in col3 and bottom panels on Right Col Only template
    $('#Col3 .cmspanel :header:first-child').not('thead :header').addClass('PanelHeader');
    $('#Home #Col3 .cmspanel :header:first-child').removeClass('PanelHeader');
    $('#LowerPanels .cmspanel :header:first-child').not('thead :header').addClass('PanelHeader');

    //Adding class to cmspanel containing portal login for styling
    $('#Col3 .Portal-Login').parent().parent().parent().addClass('PortalPanel');

    //Temporary fix for Tabs to include ClearFix class on tab for floating elements within (should be fixed in core)
    $('.Tab').addClass('ClearFix');
    $('#Col3 .cmspanel').addClass('ClearFix');

    //removing padding from empty table cells from calendar event detail header
    $('.CalendarEventDetailHeader td:empty').addClass('Empty');

    // Adding print link for Google API Maps within the Locations and Doctors Details
    $('.MapWrapper').after('<br /><img src="/imgs/icons/print_map.gif" class="printmap" alt="print" title="print" onclick="window.open(\'/print.html\')" />');
    $('#DrDetail .Locations').after('<br /><img src="/imgs/icons/print_map.gif" class="printmap" alt="print" title="print" onclick="window.open(\'/print.html\')" />');

    //adding hack to be fixed after 5.2.7 
    $('.TabContainer').addClass('TabContainerlive');

    // ----------- HACK LIST END ------------------//


});   // END DOC READY


