$(document).ready(function() {
	$('#featured').append('<div id="pager-container"><ul id="pager">');
	$('#featured ul.news').cycle({ fx: 'fade', speed: 'fast', pause: 1, pauseOnPagerHover: 1, fit: 1, random: 1, height: '305', timeout: 0, pager:  '#pager', 
		// callback fn that creates a thumbnail to use as pager anchor 
		pagerAnchorBuilder: function(idx, slide) {
			var slidesrc = $(slide).find("img").attr("src");
			var slidetitle = $(slide).find("h4").text();
			if (slidesrc) {
			slidesrc = slidesrc.replace("/thumb/", "/thumb/tn-");
			slidesrc = '<img src="' + slidesrc + '" />';
			//slidesrc = ' style="background-image: url(' + slidesrc + ');"';
			} else {
			slidesrc = "";
			}
			return '<li><a href="#" title="' + slidetitle + '">'+ slidesrc +'</a></li>'; 
		} 
	});
	
	
	$("#featured ul.news li").each(function() {
	$(this).prepend('<div class="hd"><h2>&nbsp;</h2></div>');
	$(this).append('<div class="ft">&nbsp;</div>');
	var thisFeatPepper = $(this).attr("class").split(/\s/);
	thisFeatPepper = thisFeatPepper[thisFeatPepper.length -1];
		if (thisFeatPepper == "pepper-AcademicDistinction") {
			$(".hd h2",this).html('Academic Distinction');
			$(".ft",this).html('See Also: <a href="/academics/index.aspx">Academic Values</a> &bull; <a href="/academics/socal-learning/index.aspx">Learning in SoCal</a>');
		}
		else if (thisFeatPepper == "pepper-DistinguishedAlumni") {
			$(".hd h2",this).html('Distinguished Alumni');
			$(".ft",this).html('See Also: <a href="/alumni/index.aspx">Alumni</a> &bull; <a href="/news/notable-alumni/index.aspx">Notable Alumni Blog</a>');
		}
		else if (thisFeatPepper == "pepper-Athletics") {
			$(".hd h2",this).html('Athletics');
			$(".ft",this).html('See Also: <a href="/athletics/index.aspx">Scores & Schedules</a> &bull; <a href="/athletics/teamlist.aspx">Team Pages</a>');
		}
		else if (thisFeatPepper == "pepper-CollegeNews") {
			$(".hd h2",this).html('College News');
			$(".ft",this).html('See Also: <a href="/academics/college/index.aspx">College Guidance</a> &bull; <a href="/academics/college/coll-blog/index.aspx">Director\'s Blog</a>');
		}
		else if (thisFeatPepper == "pepper-Advancement") {
			$(".hd h2",this).html('Advancement');
			$(".ft",this).html('See Also: <a href="/support/index.aspx">The Impact of Giving</a> &bull; <a href="/support/webb-fund/index.aspx">The Webb Fund</a>');
		}
		else if (thisFeatPepper == "pepper-StudentLife") {
			$(".hd h2",this).html('Student Life');
			$(".ft",this).html('See Also: <a href="/student-life/clubs-act/index.aspx">Clubs &amp; Activities</a> &bull; <a href="/student-life/boarding/index.aspx">Boarding @Webb</a>');
		}
		else {
			$(".hd h2",this).html('Around Campus');
			$(".ft",this).html('See Also: <a href="/about/index.aspx">The Webb Experience</a> &bull; <a href="/student-life/index.aspx">Student Life</a>');
		}
	});
	
	$("#featured ul.news li p").each(function() {
		var text = $(this).html();
		text = $.trim(text);
		if (text.length > 490){
			text = text.substring(0, 490);
			text = text.substring(0, text.lastIndexOf(' '))+ "...";
			$(this).html(text);
		}
		var first_letter = text.substr(0,1);
		if ( /[a-zA-Z]/.test(first_letter) ) {
		  $(this).html('<span class="dropcap">' + first_letter + '</span>' + text.slice(1));
		}
	});
	$("div.col1 ul.news li p").each(function() {
		var text = $(this).html();
		text = $.trim(text);
		if (text.length > 230){
			text = text.substring(0, 230);
			text = text.substring(0, text.lastIndexOf(' '))+ "...";
			$(this).html(text);
		}
	});
	
	$("#featured ul.news li:not(:has(.thumb))").each(function() {
		$(".hd h2",this).css('margin-left','auto').css('margin-right','auto');
		$("h4",this).css('margin-left','auto').css('margin-right','auto');
		$("h5",this).css('margin-left','auto').css('margin-right','auto');
		$(".ft",this).css('margin-left','auto').css('margin-right','auto');
	});
	
	var numSlides = $("#featured ul.news").children().length;
	if (numSlides < 5) {
		$("#featured #pager").css('width',(numSlides*110));
	}
	
	$("#calendar-today").css('height',($("#upcomingevents").height()-60));
	
	// Insert titles for tooltip
	$("#pager li a").qtip({
		show: { delay: 0, effect: { length: 0 } },
		hide: { delay: 0, effect: { length: 0 }, fixed: true },
		style: { name: 'dark', textAlign: 'center', background: '#FFFFFF', color: '#333333', border: { width: 0 } }, //, tip: { corner: 'bottomMiddle', color: '#FFFFFF', size: { x: 18, y: 10 } } 
		position: { corner: { target: 'bottomMiddle', tooltip: 'topMiddle' }, adjust: { screen: false, x: 0, y: 3 } }
	});
	
});
