// implementing a trim function for strings in javascript
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

$().ready(function() {
	// add a class to the first paragraph on the homepage for styling
	$("body.home #content p").eq(0).addClass("first");
	
	// display the search form label if the text box is empty, otherwise hide it
	if ($("#search").val()=="") {
		$("#searchForm label").css("visibility","visible");
	}
	$("#search").focus( function() {
		$("#searchForm label").css("visibility","hidden");
	});
	$("#search").blur( function() {
		if ($("#search").val()=="") {
			$("#searchForm label").css("visibility","visible");
		}
	});
	
	// make image container block level in order to contain caption text
	$(".mt-enclosure-image").each(function() {
		var imgWidth = $(this).find("img").eq(0).width();
		if ($(this).find("a img")) {
			imgWidth = imgWidth + 2;
		}
		var direction = $(this).find("img").eq(0).css("float");
		$(this).width(imgWidth);
		$(this).css("float",direction);
		
	});
	
	
	
	// if the content area has a ".more" section, put it in the 3rd column sidebar
	$("#content .more").insertAfter("#sidebar");
	$("#content .more").remove();
	
	// create year headings on the "scholars" category page
	var gradYearHeading = "";
	$("#content .scholar").each( function() {
		var gradYear = $(this).children(".graduated").children(".year").text();
		if(gradYearHeading!=gradYear) {
			$(this).before("<h3 id=\""+gradYear+"\" class=\"gradYear\">"+gradYear+"</h3>");
			$("#wrapper .more").append("<a href=\"#"+gradYear+"\">"+gradYear+"</a><br />");
			gradYearHeading = gradYear;
		}
	});
	$("#content .scholar .graduated").hide();
	
	// add class to #main if 3-column layout is needed
	var rlCount = $("#wrapper").children(".more").length;
	if (rlCount>0) {
		$("#main").addClass("col3");
	}

	// Capture click on footer newsletter subscribe link and display subscribe form
	$("div.more p.newsletter a").toggle(function(){
		$("div.more #subscribeForm").show();
	},function(){
		$("div.more #subscribeForm").hide();
	});

	// use the subscribe form's label to fade in/out on click
	if ($("#yhtuuj-yhtuuj").val()=="") {
		$("#subscribeForm label").css("visibility","visible");
	}
	$("#yhtuuj-yhtuuj").focus( function() {
		$("#subscribeForm label").fadeOut("fast");
	});
	$("#yhtuuj-yhtuuj").blur( function() {
		if ($("#yhtuuj-yhtuuj").val()=="") {
			$("#subscribeForm label").fadeIn("fast");
		}
	});
	
	// Add a title attribute for links to certain filetypes (PDF, etc)
	$('#main a').each(function() {
		if ($(this).attr("href")) {
			fileExt = $(this).attr("href").trim();
			fileExt = fileExt.substring(fileExt.lastIndexOf(".")+1,fileExt.length)
			fileExt = fileExt.toUpperCase();
			if (fileExt=="PDF") {
				$(this).attr("title",fileExt);
			}
			if (fileExt=="PPT") {
				$(this).attr("title","PowerPoint");
			}
			if (fileExt=="WMV") {
				$(this).attr("title","Windows Media Video");
			}
		}
	});
	// Add filetype icons after links with corresponding title attributes
	$('a[@title^=PDF]').after('<img src="http://blogs.usc.edu/mcnair_scholars_program/assets/images/pdf_icon_new.gif" class="icon" alt="(PDF)" />');
	$('a[@title^=Word]').after('<img src="http://blogs.usc.edu/mcnair_scholars_program/assets/images/word_icon.gif" class="icon" alt="(Word)" />');
	$('a[@title^=Excel]').after('<img src="http://blogs.usc.edu/mcnair_scholars_program/assets/images/excel_icon.gif" class="icon" alt="(Excel)" />');
	$('a[@title^=PowerPoint]').after('<img src="http://blogs.usc.edu/mcnair_scholars_program/assets/images/ppt_icon.gif" class="icon" alt="(PowerPoint)" />');
	$('a[@title^="Windows Media Video"]').after('<img src="http://blogs.usc.edu/mcnair_scholars_program/assets/images/wmv_icon.gif" class="icon" alt="(Windows Media Video)" />');
	$('a[@title^=Quicktime]').after('<img src="http://blogs.usc.edu/mcnair_scholars_program/assets/images/mov_icon.gif" class="icon" alt="(Quicktime movie)" />');
});


