//newsAjax 0.4 written by Ryan Senese for King's College
//This javascript operates by building an array of all list-elements (li) contained within the table cell, "BodyText". Once the array is built, we can manipulate it to display however we want.
//By default, if no parameters are passed to the function newsAjax, it will create a list of the latest six press releases; 5 in the "lastest press releases" list and one featured article for the newest press release.
//The first parameter, 'items' controls how many items appear in the "latest press releases" area. By default it is 5, but it can be changed to whatever you want (not exceeding the amount of list elements, obviously)
//The second parameter, 'featured' toggles the featured article area. If you pass a value of 0 through, it will be omitted from the page.
//In order for this to function properly, the HTML on the news pages MUST BE FORMATTED CORRECTLY! Do not use <br> tags or &nbsp; use <p> tags liberally for each paragraph of text.
//ADDED FUNCTIONALITY IN V0.2 - An ajax script feeds in the first image inside td.BodyText and rebuilds it with the appropriate aspect ratio for the Featured Article area.
//ADDED FUNCTIONALITY IN V0.3 - The image script now works in all supported browsers, and doesn't rely on width or height attributes. The resizing script will not allow for enormously huge heights anymore.
//ADDED FUNCTIONALITY IN V0.4 - Alt text and image source will now be converted to proper html in order to avoid single-quote catastrophes.
//For full documentation, please refer to http://www.kings.edu/js/custom/newsAjax/newsAjax_documentation.pdf

function newsAjax(items, featured, featimg){
	$("#filler").hide();
	if(!items){
		var items = 5;
	}
	var geegee = $("td.BodyText ul > *").length;
	$("td.BodyText ul > *").clone().appendTo($("#filler"));
	var ff = new Array();
	
	for(i=0;i<geegee;i++){
		ff[i] = $("#filler li:nth-child(" + (i + 1) + ")").clone();
	};
	if(featured == 0){
		$("#news_title").hide();
		$("#read_more").remove();
		$("#filler").empty();
		for(j=0;j<items;j++){
			ff[j].appendTo($("#filler"));
		};
	}
	else{
		//if ($("#spotlight li")){
		//	var path = $("#spotlight li a").attr("href");
		//}
		//else{
		//	var path = $("#filler li:first a").attr("href");
		//}
		var path = $("#filler li:first a").attr("href");
		$("#news_title a").load(path + " td.BodyText > p:nth-child(2)");
		$("#featured_article").load(path + " td.BodyText > p:nth-child(3)");
		
		if(featimg != 0){
			$.ajax({
				type: "GET",
				url: path,
				cache: false,
				success: function(html){
					$("#ajaxLoader").append(html);
					if($("#ajaxLoader td.BodyText img#main-img").attr("src")){ //Checks to see if there is an image in the featured news article. If no image is found, the accompanying image section is omitted.
						var feat_img_height= $("#ajaxLoader td.BodyText img#main-img").height();
						var feat_img_width= $("#ajaxLoader td.BodyText img#main-img").width();
						if(feat_img_height > feat_img_width){
							var new_height = 275;
							var new_width = Math.floor(((feat_img_width) / (feat_img_height)) * new_height);
							if(new_width > 374){
								var new_width = 374;
							}
						}
						else {
							var new_width = 374;
							var new_height = Math.floor(((feat_img_height) / (feat_img_width)) * new_width);
						}
						var img_path = $("#ajaxLoader td.BodyText img:first").attr("src");
						var alt_txt = $("#ajaxLoader td.BodyText img:first").attr("alt");
						
						img_path = img_path.replace("'","&#39;");
						alt_txt = alt_txt.replace("'","&#39;");

						//var prepended_path = path.split("/", 1); //Can't use this methodology because IE6 is really dumb and spits out an absolute path to the image, even though it's just "guessing" and making really bad "guesses."
						//var full_path = prepended_path + "/" + img_path;
						$("#ajaxCont #featured_image").append("<img src='" + img_path + "' width='" + new_width + "' height='" + new_height + "' alt='" + alt_txt + "' />");
					}
					$("#ajaxLoader").empty();
			}
			});
		}
		$("#news_title a").attr("href",path);
		$("#read_more a").attr("href",path);
		$("#filler").empty();
		for(j=1;j<(items+1);j++){
			ff[j].appendTo($("#filler"));
		};
	}
	$("#filler").show();
};
