function sortEntries(xml, element, authorFilter) {
	var t1 = new Date();
		var blogEntry = [];
		var blogPosts = [];
		
		var count;
		var entryCount;
		
		var blogID;
		var blogName;
		var blogURL;
		var blogIMG;
		var blogAuthorName;
		var blogEntryTitle;
		var blogEntryID;
		var blogEntryDate;
		var blogEntryImage;
		
	
	$(xml).find("Blog").each(function(blogIndex){
		count = $(this).find("BlogEntries").find("BlogEntry").length;
		blogURL = $(this).attr("BlogURL");
		blogID = $(this).attr("BlogID");
		blogIMG = $(this).attr("BlogImageURL");
		blogEntryURL = $(this).attr("BlogEntryURL");
		

		blogName = $(this).attr("BlogName");
		if ( blogName.search(" //") != -1) {
			blogName = blogName.slice( 0 , blogName.search(" //"));
		} else if ( blogName.search(" - ") != -1) {
			blogName = blogName.slice( blogName.search(" - ") + 3, blogName.length);
		}
		
				
		//LOOPS BLOG ENTRIES + STORES ENTRY DATA INTO MASTER ARRAY
		$(this).find("BlogEntries").find("BlogEntry").each(function(e){
			entryCount = blogEntry.length;
			var iconClass = "";
			var bracketText = [];
			blogEntryTitle = $(this).attr("EntryTitle").replace(/\[([^\]]+)\]\s\-\s/g, function() {
			bracketText.push(arguments[1]);
			return "";
			});
			
			if (bracketText[0] != undefined)
			iconClass = bracketText[0];
			else 
			iconClass = "DISCUSSION";

			blogEntryID = $(this).attr("BlogEntryID");
			blogEntryDate = $(this).attr("UTCDateTimePublished");
			blogEntryImage = $(this).find("Images").find("Image").attr("ReducedImageURL");
			blogAuthorName = $(this).attr("AuthorName");
			blogEntryDescription = "<div>" + $(this).find("EntryDescription").text() + "</div>";

			
			//MASTER ARRAY
			blogEntry[entryCount] = new Array(blogEntryTitle, blogID, blogIMG, blogEntryURL, blogName, blogEntryID, blogEntryDate, blogEntryImage, blogAuthorName, iconClass, blogEntryDescription);


							});
							
						});
						//console.log(entryCount);

						blogEntry.sort(function(a, b){
							a = a[6]+a[0];
							b = b[6]+b[0];
							
							return a == b ? 0 : (a < b ? 1 : -1);
						});
						
						
						var blogLength = blogEntry.length;
						//LOOPS THROUGH EVERY ENTRY WITHIN FEED
						for (i=0; i < blogLength; i++) { //blogEntry.length; i++) {
							
							//Set Dynamic Regular expression for Author Match
							var authorMatch = new RegExp(authorFilter, 'gi');
							
							//COLLECTS ENTRY PARAMETERS
							var entryTitle = blogEntry[i][0];
							var entryID = blogEntry[i][5];
							var pubDate = blogEntry[i][6];
							var entryIMG = blogEntry[i][7];
							var entryURL = blogEntry[i][3];
							var authorName = blogEntry[i][8];
							var iconClass = blogEntry[i][9];
							var blogEntryDescription = blogEntry[i][10];
							
							//Convert EMMIS UTC Date to a Readable timestamp using date.js
							pubDate = Date.parse(pubDate.slice(0, pubDate.search(/\T/gi)));
							//console.log(pubDate);
							pubDate = pubDate.toString('ddd, MMM d, yyyy');
							
							
							//COLLECTS BLOG PARAMETERS
							var blogName = blogEntry[i][4]; 
							var ID = blogEntry[i][1]; 
							var IMG = blogEntry[i][2];
							
							
							//If authorFilter is set to All authors (all OR ALL OR All) then it will display all entries
							if (authorFilter == "all" || authorFilter == "All" || authorFilter == "ALL") {
								if (iconClass == "LINK") {
									var blogEntryLink = $(blogEntryDescription).find("a:eq(0)").attr("href");
									$("ul#" + element).append("<li class=\"blogteaser bt-" + blogEntry[i][9] + "\" title=\"" + entryTitle + "\"><h5><a target='_blank' href=\"" + blogEntryLink + "?BlogEntryID=" + entryID + "\" title=\"" + entryTitle + "\">" + entryTitle + "</a></h5></li>");
									
								}
								
								else if (iconClass == "GALLERY" || iconClass == "PAGE") {
									var blogEntryLink = $(blogEntryDescription).find("a:eq(0)").attr("href");
									$("ul#" + element).append("<li class=\"blogteaser bt-" + blogEntry[i][9] + "\" title=\"" + entryTitle + "\"><h5><a href=\"" + blogEntryLink + "\" title=\"" + entryTitle + "\">" + entryTitle + "</a></h5></li>");
								}
								
								else if (iconClass == "PLAYER") {
									var blogEntryLink = $(blogEntryDescription).find("a:eq(0)").attr("href");
									$("ul#" + element).append("<li class=\"blogteaser bt-" + blogEntry[i][9] + "\" title=\"" + entryTitle + "\"><h5><a class=\"popup av-popup\" href=\"" + blogEntryLink + "\" title=\"" + entryTitle + "\">" + entryTitle + "</a></h5></li>");
									
			$(".av-popup").popup({
				width: 675,
				height: 320,
				titlebar: true,
				status: true,
				resizeable: false,
				toolbar: false,
				scrollbars: false,
				menubar: false
			});
			
								}
																
								else {
								
								$("ul#" + element).append("<li class=\"blogteaser bt-" + blogEntry[i][9] + "\" title=\"" + entryTitle + "\"><h5><a href=\"" + entryURL + "?BlogEntryID=" + entryID + "\" title=\"" + entryTitle + "\">" + entryTitle + "</a></h5></li>");
								}
							} 
							
							//If authorFilter isn't set to All then it will only display entries matching the authorFilter
							else if (authorName.search(authorMatch) != -1) {
								if (iconClass == "LINK") {
									var blogEntryLink = $(blogEntryDescription).find("a:eq(0)").attr("href");
									$("ul#" + element).append("<li class=\"blogteaser bt-" + blogEntry[i][9] + "\" title=\"" + entryTitle + "\"><h5><a target='_blank' href=\"" + blogEntryLink + "?BlogEntryID=" + entryID + "\" title=\"" + entryTitle + "\">" + entryTitle + "</a></h5></li>");
									
								} else {
								$("ul#" + element).append("<li class=\"blogteaser bt-" + blogEntry[i][9] + "\" title=\"" + entryTitle + "\"><h5><a href=\"" + entryURL + "?BlogEntryID=" + entryID + "\" title=\"" + entryTitle + "\">" + entryTitle + "</a></h5></li>");
								}
							}	
													
						}

						$("ul.latest-author-blogs li").addClass("hidden");
						showMoreItems();					
						var t2 = new Date();
						var time = t2 - t1;
						//alert(time);
					}

function getAuthorBlogs(element, authorFilter, blog_ID_Query) {
		payload = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' + 
		' xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> ' +
		 ' <soap:Body> ' +
		   '  <GetXMLBlogsContent xmlns="http://www.emmisinteractive.com/"> ' +
		   '   <BlogIDs>' + blog_ID_Query + '</BlogIDs> ' +
		   '   <NumberOfItems>' + "50" + '</NumberOfItems>' +
		   '   <StartDate></StartDate> '+
			'</GetXMLBlogsContent>'+
		 ' </soap:Body>'+
		'</soap:Envelope>';

		$.ajax({
			   type:'POST',
			   url:'http://www.wmmr.com/_shared/content/public/blogservice.asmx?op=GetXMLBlogsContent',
			   dataType:"xml",
			   contentType: "text/xml; charset=\"utf-8\"",
			   data:payload,
			   success: function(xml){

				sortEntries(xml, element, authorFilter);
				//$("ul#" + element).append(xml);
				$("div.contentblock_m01 ul.latest-author-blogs li:odd").addClass("alt");
			   },
			   error: function() {
				   //alert("error");
					//console.log("Error");
				}
			   }, "xml");
	}

function showMoreItems() {
	var itemCount = $("ul.latest-author-blogs li.hidden").size();
	//console.log($("ul.latest-author-blogs li.hidden").size());
	if (itemCount != 0) {
	$("ul.latest-author-blogs li.hidden:lt(15)").removeClass("hidden");
	itemCount = $("ul.latest-author-blogs li.hidden").size();
		if (itemCount == 0) {
			$("div.teaser-caption").html("Search the Archives").unbind("click", function(){
				showMoreItems();											 
			}).bind("click", function() { location.href="http://www.wmmr.com/shows/preston-and-steve/blogarchive.aspx"; });
		}
	}

	
}

$(document).ready(function() {
	
		//Creates Latest Crap Box w/ Emmis Skin
		$("table#maintop_wrapper tr:eq(1)").after("<tr id='latest-crap'><td colspan='2'>" + "<div class='skin skin_m05'>" + "<div class='label_text label_text_m05'><h2>The Latest Crap:</h2><img alt='' class='m05_bars' src='/images/skins/skin_m05_bars.png'></div><div class='contentblock contentblock_m05'>" + "<div class='control blogs blogs_aggregated author-teaser'><ul class='latest-author-blogs' title='all' id='blog-teaser-1'></ul><div class='teaser-caption'></div></div></div></div>" + "</td></tr>");
	
		$("ul.latest-author-blogs").each(function(i) {
			
			var listID = $(this).attr("id");
			var listTitle = $(this).attr("title");
			blog_ID_Query = "1001193, 1001194, 1001195, 1001196, 1001322";

			getAuthorBlogs(listID, listTitle, blog_ID_Query);
						
			
		});
		
	// Create the dummy search box in the "latest crap" box
	$("div.blogs_aggregated").prepend("<div id='lc-search-box'><form id='lc-search-form'><input type='text' id='q2' name='q2' class='googleCSEwatermark' /><input type='button' name='sa2' id='sa2' value='Search'/></form></div>");
	
	// Mirror the text from the dummy search box in the real search box
	$("div#lc-search-box input#q2").keypress(function() {
			var searchTerm = $(this).val();		
			$('input#q').val(searchTerm);
	});
	
	// Trigger the real search box when the "submit" button on the dummy search box is pressed
	$("div#lc-search-box form#lc-search-form").bind("submit", function(event) {
			event.preventDefault();
			$("input#sa").trigger('click');
	});

	// Create button, bind to action that shows 15 more on each click
	$("div.teaser-caption").addClass("getmore-button").html("Get 15 More Items");
	
	$("div.teaser-caption").live("click", function(){
			//alert("I've been clicked!");
			showMoreItems();		
	});


});	


/*Blogs with IDs

P&S Main - 1001193
P&S Interviews - 1001194
P&S Lost - 1001195
P&S Troops - 1001196
P&S Show Topics - 1001322
***************8*/


