 $(document).ready(function(){	
	// Adds a shadow to the body content area
	 //$("#content .container").wrapAll('<div class="shadow"></div>');	 
	
	// Allows all links with this tag to open in a blank window
	 $("a[rel='external'], a[href$='.pdf']").click(function(){
		window.open(this.href);
		return false;
	});
	
	$("a[rel='external'][title]").click(function(){
	pageTracker._trackPageview('/clicks/' + $(this).attr("title"));										 
	});
	
	// Add Word, Excel, and PDF icons to links containing file paths
	$("<img src='images/lock.png' alt='' class='pathType' />").insertAfter($("a[title='locked']:not(:has(img)),a[href*='Documents/members-only']:not(:has(img))"));
	$("<img src='prebuilt/images/pdf.gif' alt='' class='pathType' />").insertAfter($("a[href$='.pdf']:not(:has(img))"));	
	$("<img src='prebuilt/images/excel.gif' alt='' class='pathType' />").insertAfter($("a[href$='.xls']:not(:has(img)),a[href$='.xlsx']:not(:has(img))"));	
	$("<img src='prebuilt/images/word.gif' alt='' class='pathType' />").insertAfter($("a[href$='.doc']:not(:has(img)),a[href$='.docx']:not(:has(img))"));
	
	// Clears the search field if it contains filler text
	$("input.searchSite").focus(function(){
		if ($(this).val()=="Search this site"){
			$(this).val("");
		}
	}).blur(function(){
		if ($(this).val()=="") {
			$(this).val("Search this site");
		}		 
	});
	
	// Clears the search field for Find a Surgeon
	$("#content #fasComponent fieldset label input").focus(function(){
		if ($(this).val()=="enter zipcode"){
			$(this).val("");
		}
	}).blur(function(){
		if ($(this).val()=="") {
			$(this).val("enter zipcode");
		}		 
	});

	// Rotates the press release widget on the home page
	$("#newsTicker div#newsWrapper").cycle( {  width:700,  fit: 1 
	});
	
	// Re-enters the filler text in the search field if it's blank
	$("input#name, textarea#wish").blur(function(){
		if ($(this).val()=="") {
			$(this).val("Enter your " + $(this).attr("id")).removeClass("activeInput");
		}									 
	});

	// Wraps all image lists with paging		   
	$(".videos .floaters.three > div").wrapAll('<div id="results"></div>');
	$('#results').wrapChildren({ childElem : 'div' , sets: 16 });
	$('#results > div').addClass("pageSet");
	$('#results').pager('> div');
	$('#results').append($('#nav'))
	$('#results #nav a[rel="prev"]').addClass('prev');
	$('#results #nav a[rel="next"]').addClass('next');
	$('#results > div').css("minHeight","0").css("height","auto");
	
	// Converts RSS feed text links into live links
	if ($(".rssFeed ul").html()) {
	linkify($(".rssFeed ul"));
	}
	
	// Allows the print icon to print
	$("p#printPage a").click(function(){
		window.print();
		return false;
	});
	
	// Shows and hides videos and content in the #featured area
	$("#featured .floaters a[title]").click(function(){
		$(".floaters div.hiddenMeta:first-child").remove();
		$(this).parent().find(".hiddenMeta").clone().prependTo($(".floaters .four")).show();
		jwplayer("videoContainer").setup({
			file: $(this).attr("rel"),
			image: $(this).attr("title"),
			flashplayer: "prebuilt/js/mediaplayer/player.swf",
			width: '100%',
			height: '275',
            plugins: {
               "gapro-1": { accountid: "UA-547614-1" }
            }
         });
		return false;											 
	});
	$("#featured .floaters a[title]:first").click();
});

//Compare Date. Takes a string as a date. If unable to process date create a new date given the date string provided
function compareDate(dateString) {
	var myDate=new Date(dateString);
	var today=new Date();
	if ( !(isNaN(myDate.toString()))){
		if (myDate > today) {
		  $("#mainContent .dateLine span").html("Embargoed ");
		  }
		else  {
		  $("#mainContent .dateLine span").html("Immediate ");
		  } 		  
	  }
	  else {
		  var a1 = new Array();
		  a1 = dateString.split(",");
		  myDate = new Date( a1[0],a1[1] - 1,a1[2] );
		  if (myDate > today) {
		  $("#mainContent .dateLine span").html("Embargoed ");
		  }
		else  {
		  $("#mainContent .dateLine span").html("Immediate ");
		  }	 
	  }	  
}

function linkify(inputTag) {

	var inputText = inputTag.html();
	var replaceText, replacePattern1, replacePattern2, replacePattern3;
	
	//URLs starting with http://, https://, or ftp://
	replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
	replacedText = inputText.replace(replacePattern1, '<a href="$1" rel="external">$1</a>');
	
	//URLs starting with www. (without // before it, or it'd re-link the ones done above)
	replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
	replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" rel="external">$2</a>');
	
	//Change email addresses to mailto:: links
	replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
	replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
	
	inputTag.html(replacedText);
} 

