//TextSizer and PageTools
function addPageTools() {
	var clientDomainName = '[clientDomainName]';
	var clientSiteName = '[clientSiteName]';
	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 = currentPageTitle;
	var emailHref = "mailto:?subject=" + emailSubject + "&body=I thought you would be interested in the following page: " + currentPageTitle + " - " + currentPageUrl;
	$('#PageToolsWrapper .Dec').append('<div id="PageTools" class="ClearFix">' +
		'<ul>' +
			'<li class="EmailLink"><a href="#">Email</a></li>' +
			'<li class="PrintLink"><a href="#">Print</a></li>' +
		'</ul>' +
	'</div>');
	$('li.PrintLink a').click(function() { print(); return false; });
	$('li.EmailLink a').attr({ href: emailHref});
}

//TEXT SIZER
function addTextSizer() {
	$('#PageToolsWrapper .Dec').append('<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 ------------------//

	$('#fastfind').change(function(){
		alert('test');
		window.open( $("#fastfind option:selected").value);
	});

// ----------- HACK LIST ------------------//
						   
	//adding comma after doctor name on doctor detail page
	$('#DoctorDetail span.DoctorName').after("<span>,</span>");

	//remove empty cmspage for Corp Homepage to remove IE space
    $('.cmspage:empty').remove();
	
	//removing empty table cells in calendar
    $('.CalendarEventDetailHeader td:empty').addClass('Empty');
    
    //adding hack to be fixed after 5.2.7
    $('.TabContainer').addClass('TabContainerlive');
	
// ----------- HACK LIST END ------------------//

});
