function queryBlogs(listID, listTitle) {
	linkClass = "audio";
	switch (listTitle) {
		case "FrontPg":
			blog_ID_Query = "1001330,1001331,1001332,1001333,1001334,1001335";
			authorFilter = "All";
			archiveLink = "";
			break;		
		case "Villanova":
			blog_ID_Query = "1001334";
			authorFilter = listTitle;
			archiveLink = "";
			break;
		case "Madee":
			blog_ID_Query = "1001336";
			authorFilter = listTitle;
			archiveLink = "";
			break;
		case "Cumberland":
			blog_ID_Query = "1001336";
			authorFilter = listTitle;
			archiveLink = "";
			break;
		case "Jefferson":
			blog_ID_Query = "1001336";
			authorFilter = listTitle;
			archiveLink = "";
			break;			
		case "Seltzer":
			blog_ID_Query = "1001330";
			authorFilter = listTitle;
			archiveLink = "";
			break;
		case "Paolantonio":
			blog_ID_Query = "1001330";
			authorFilter = listTitle;
			archiveLink = "";
			break;
		case "DeCamara":
			blog_ID_Query = "1001330,1001331,1001333";
			authorFilter = listTitle;
			archiveLink = "";
			break;
		case "This Week in Pro Football":
			blog_ID_Query = "1001330,1001331,1001332,1001333,1001334,1001335,1001472";
			authorFilter = "McManus";
			archiveLink = "";
			break;
		case "Stark":
			blog_ID_Query = "1001331";
			authorFilter = listTitle;
			archiveLink = "";
			break;
		case "Daulton":
			blog_ID_Query = "1001331";
			authorFilter = listTitle;
			archiveLink = "";
			break;			
		default:
			blog_ID_Query = "1001330,1001331,1001332,1001333,1001334,1001335,1001472";
			authorFilter = listTitle;
			archiveLink = "";
			break;
		
	}
	return [blog_ID_Query, authorFilter, archiveLink, linkClass];
}

function sortEntries(xml, element, authorFilter, displayIcons, entriesToDisplay, blogVariables, displayDate, displayBlogAuthor, archiveLink, linkClass) {
	
	//LOOPS BLOG ENTRIES + STORES ENTRY DATA INTO MASTER ARRAY
	$(xml).find("BlogEntries").find("BlogEntry").each(function(e){
		
		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";
		
		//PUSHES ENTRY VARIABLES/JSON OBJECT INTO MASTER ARRAY
		blogVariables.push( {	
				"blogURL": $(this).parents("Blog").attr("BlogURL"),
				"blogID" : $(this).parents("Blog").attr("BlogID"),
				"blogIMG"  : $(this).parents("Blog").attr("BlogImageURL"),
				"blogEntryURL": $(this).parents("Blog").attr("BlogEntryURL"),
				"blogName" : $(this).parents("Blog").attr("BlogName"),
				"blogEntryTitle": blogEntryTitle,
				"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>",
				"blogIconClass" : iconClass }
		);
		
		
		if (blogVariables[e].blogName.search(" //") != -1) {
			blogVariables[e].blogName = blogVariables[e].blogName.slice( 0 , blogVariables[e].blogName.search(" //"));
		} else if ( blogName.search(" - ") != -1) {
			blogVariables[e].blogName = blogVariables[e].blogName.slice( blogVariables[e].blogName.search(" - ") + 3, blogVariables[e].blogName.length);
		}
		
	});

	//SORT ARRAY
	blogVariables.sort(function(a, b){
		a = a.blogEntryDate;
		b = b.blogEntryDate;
		
		return a == b ? 0 : (a < b ? 1 : -1);
	});



	//LOOPS THROUGH EVERY ENTRY WITHIN ARRAY
	for (i=0; i < blogVariables.length; i++) {
		
		//Set Dynamic Regular expression for Author Match
		var authorMatch = new RegExp(authorFilter, 'gi');
		
		//Convert EMMIS UTC Date to a Readable timestamp using date.js
		var pubDate = blogVariables[i].blogEntryDate;
		pubDate = Date.parse(pubDate.slice(0, pubDate.search(/\T/gi)));
		pubDate = pubDate.toString('ddd, MMM d, yyyy');
		
		if (displayIcons == true) {
			entryImage = "<span class=\"latest-blog-thumbnails\"><img src=\"" + blogVariables[i].blogIMG + "\" /></span>";
			entryLinkSpan = "<span class=\"latest-blog-text-info\">";
			entryLink = "<a href=\"" + blogVariables[i].blogEntryURL + "?BlogEntryID=" + blogVariables[i].blogEntryID + "\" class=\"" + linkClass + "\">";
		} else if (displayIcons == false) {
			entryImage = "";
			entryLinkSpan = "<span class=\"latest-blog-text-info no-entry-image \">";
			entryLink = "<a href=\"" + blogVariables[i].blogEntryURL + "?BlogEntryID=" + blogVariables[i].blogEntryID + "\" class=\"" + linkClass + "\">";
		}
		if (displayDate == true) {
			entryDate = "<br />" + pubDate;
		} else if (displayDate == false) {
			entryDate = "";
		}
		if (displayBlogAuthor == true) {
			entryBlogAuthor = "<span class=\"blog-author\"><br />" + blogVariables[i].blogAuthorName + " // " + blogVariables[i].blogName + "</span>";
		} else if (displayBlogAuthor == false) {
			entryBlogAuthor = "";
		}
		if (blogVariables[i].blogIconClass == "LINK" || blogVariables[i].blogIconClass == "GALLERY" || blogVariables[i].blogIconClass == "PAGE") {
			entryLink = "<a href=\"" + $(blogVariables[i].blogEntryDescription).find("a:eq(0)").attr("href") + "\" target=\"_blank\" class=\"" + linkClass + "\">";
		} else if (blogVariables[i].blogIconClass == "PLAYER") {
			entryLink = "<a class=\"popup av-popup\" href=\"" + $(blogVariables[i].blogEntryDescription).find("a:eq(0)").attr("href") + "\" class=\"" + linkClass + "\">";
		}
		
		entryListItem = "<li title=\"" + blogVariables[i].blogEntryTitle + "\" class=\"" + blogVariables[i].blogName.cleanBlogTitle().toLowerCase() + " bt-" + blogVariables[i].blogIconClass + "\">" + entryImage + entryLinkSpan + entryLink + blogVariables[i].blogEntryTitle + "</a>" + entryBlogAuthor + entryDate + "</span></li>";
		

		//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") {
			$("ul#" + element).append(entryListItem);

		} 

		//If authorFilter isn't set to All then it will only display entries matching the authorFilter
		else if (blogVariables[i].blogAuthorName.search(authorMatch) != -1) {
			$("ul#" + element).append(entryListItem);

		}
	
	}
	
	//No Matches then display all blog links
	if ($("ul#" + element + " li").length <= 0) {
		$("ul#" + element).append("<p class=\"largeheader\"><strong>Sorry. No recent matches found. Please browse through our different Blogs.</strong></p>" + 
		"<li><span class=\"latest-blog-thumbnails\"><img src=\"http://origin-www.975thefanatic.com/Pics/Blogs/1001330/eagles-001.gif\" /></span><span class=\"latest-blog-text-info\"><a href=\"http://www.975thefanatic.com/teams/eagles/home.aspx\">Eagles // NFL</a><br />All the Latest Eagles Information</span></li>"
		+ 
		"<li><span class=\"latest-blog-thumbnails\"><img src=\"http://origin-www.975thefanatic.com/Pics/Blogs/1001331/phillies.gif\" /></span><span class=\"latest-blog-text-info\"><a href=\"http://www.975thefanatic.com/teams/phillies/home.aspx\">Phillies // MLB</a><br />All the Latest Phillies Information</span></li>"
		+ 
		"<li><span class=\"latest-blog-thumbnails\"><img src=\"http://origin-www.975thefanatic.com/Pics/Blogs/1001332/flyers.gif\" /></span><span class=\"latest-blog-text-info\"><a href=\"http://www.975thefanatic.com/teams/flyers/home.aspx\">Flyers // NHL</a><br />All the Latest Flyers Information</span></li>"
		+ 
		"<li><span class=\"latest-blog-thumbnails\"><img src=\"http://origin-www.975thefanatic.com/Pics/Blogs/1001333/sixers.gif\" /></span><span class=\"latest-blog-text-info\"><a href=\"http://www.975thefanatic.com/teams/sixers/home.aspx\">76ers // NBA</a><br />All the Latest 76ers Information</span></li>"
		+ 
		"<li><span class=\"latest-blog-thumbnails\"><img src=\"http://origin-www.975thefanatic.com/Pics/Blogs/1001334/ncaa.gif\" /></span><span class=\"latest-blog-text-info\"><a href=\"http://www.975thefanatic.com/teams/college/home.aspx\">Collegiate Sports // NCAA</a><br />All the Latest Collegiate Sport Information</span></li>"
		+ 
		"<li><span class=\"latest-blog-thumbnails\"><img src=\"http://origin-www.975thefanatic.com/Pics/Blogs/1001335/generic.gif\" /></span><span class=\"latest-blog-text-info\"><a href=\"http://www.975thefanatic.com/news/home.aspx\">News // All</a><br />All the Latest in the World of Sports</span></li>"
		+ 
		"<li><span class=\"latest-blog-thumbnails\"><img src=\"http://origin-www.975thefanatic.com/Pics/Blogs/1001472/generic.gif\" /></span><span class=\"latest-blog-text-info\"><a href=\"http://www.975thefanatic.com/features/home.aspx\">Fanatic Features // All</a><br />All the Latest 97.5 The Fanatic Features</span></li>");
	}
}

String.prototype.cleanBlogTitle = function() {
	return this.replace(/\s/g,"-").replace('+', '').replace('%20', '').replace('*', '').replace('/', '').replace('@', '').replace('\'', '');
};

function getAuthorBlogs(element, authorFilter, entriesRequested, displayIcons, blog_ID_Query, entriesToDisplay, displayDate, displayBlogAuthor, archiveLink, linkClass) {
		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>' + entriesRequested + '</NumberOfItems>' +
		   '   <StartDate></StartDate> '+
			'</GetXMLBlogsContent>'+
		 ' </soap:Body>'+
		'</soap:Envelope>';
		var blogVariables = [];
		$.ajax({
			   type:'POST',
			   url:'http://' + window.location.hostname + '/_shared/content/public/blogservice.asmx?op=GetXMLBlogsContent',
			   dataType:"xml",
			   contentType: "text/xml; charset=\"utf-8\"",
			   data:payload,
			   success: function(xml){
				sortEntries(xml, element, authorFilter, displayIcons, entriesToDisplay, blogVariables, displayDate, displayBlogAuthor, archiveLink, linkClass);
				//$("ul#" + element).append(xml);
				
				if (entriesToDisplay == 15 || entriesToDisplay == 5) {
					$("ul#" + element + ".latest-author-blogs li").addClass("hidden");
					showMoreItems(entriesToDisplay, archiveLink, element);
				}
				
				$("ul.latest-author-blogs li:odd").addClass("alt");
			   },
			   error: function() {
					console.log("Error");
				}
			   }, "xml");
	}

function showMoreItems(entriesToDisplay, archiveLink, element) {
	var itemCount = $("ul.latest-author-blogs li.hidden").size();
	
	if (itemCount != 0) {
	$("ul#" + element + ".latest-author-blogs li.hidden:lt(" + entriesToDisplay + ")").removeClass("hidden");
	itemCount = $("ul#" + element + ".latest-author-blogs li.hidden").size();
		if (itemCount == 0) {
			//console.log(archiveLink);
			if (archiveLink == "") {
				$("span#show-more-items").remove();
			} else {
				$("span#show-more-items").html("Search the Archives").unbind("click", function(){
					showMoreItems();											 
				}).bind("click", function() { location.href= archiveLink; });
			}
			
			
		}
	}

	
}


$(document).ready(function() {
	
	$("ul.author-list-icons-date, ul.author-list-icons, ul.author-list-date, ul.author-list, ul.author-teaser-icons-date, ul.author-teaser-icons, ul.author-teaser-date, ul.author-teaser, ul.author-front-page").each(function(i) {
		var listID = $(this).attr("id");
		var listTitle = $(this).attr("title");
		var listClass = $(this).attr("class");
		
		if (listClass.search("latest-author-blogs") > 2)
		{
			blogListType = listClass.slice(0, listClass.search(" "));
		} else if (str.search("latest-author-blogs") < 2) {
			blogListType = listClass.slice(listClass.search(" "), listClass.length);
		}
		
		//Returns Blog Query and Blog Author Filter
		var blogInfo = queryBlogs(listID, listTitle);
		switch (blogListType) {
			case "author-list-icons-date" :
				entriesToDisplay = 50;
				displayIcon = true;
				displayDate = true;
				displayBlogAuthor = true;
				break;
			case "author-list-icons" :
				entriesToDisplay = 50;
				displayIcon = true;
				displayDate = false;
				displayBlogAuthor = true;
				break;
			case "author-list-date" :
				entriesToDisplay = 50;
				displayIcon = false;
				displayDate = true;
				displayBlogAuthor = true;
				break;
			case "author-list" :
				entriesToDisplay = 50;
				displayIcon = false;
				displayDate = false;
				displayBlogAuthor = true;
				break;
			case "author-teaser-icons-date" :
				entriesToDisplay = 5;
				displayIcon = true;
				displayDate = true;
				displayBlogAuthor = true;
				break;
			case "author-teaser-icons" :
				entriesToDisplay = 5;
				displayIcon = true;
				displayDate = false;
				displayBlogAuthor = true;
				break;
			case "author-teaser-date" :
				entriesToDisplay = 5;
				displayIcon = false;
				displayDate = true;
				displayBlogAuthor = true;
				break;
			case "author-teaser" :
				entriesToDisplay = 5;
				displayIcon = false;
				displayDate = false;
				displayBlogAuthor = true;
				break;
			case "author-front-page" :
				entriesToDisplay = 15;
				displayIcon = false;
				displayDate = false;
				displayBlogAuthor = false;
				break;
			default:
				break;
			
		}
		//Sends Element ID, Author Filter, Number of Entries to Query, Display Icons Boolean, Blog IDs to Query, Entries to Display, Display Date Boolean, and Archive Link
		getAuthorBlogs(listID, blogInfo[1], 50, displayIcon, blogInfo[0], entriesToDisplay, displayDate, displayBlogAuthor, blogInfo[2], blogInfo[3]);
		
		//if (blogListType == "author-front-page") {
			$("ul#front-page-audio").after("<span id=\"show-more-items\">Get " + 15 + " More Items</span><span class=\"items-archive\" style=\"display: none;\">" + blogInfo[2] + "</span>");
			// Create the dummy search box in the "latest crap" box
			$("ul#" + listID).before("<div id='lc-search-box'><form id='lc-search-form'><input type='text' id='q2' name='q2' class='googleCSEwatermark' /><input type='submit' name='sa2' id='sa2' value='Search'/></form></div>");
		//}
	});
	
	$("span#show-more-items").live("click", function(){
			showMoreItems(15, $("span.items-archive").text(), $(this).prev("ul").attr("id"));		
	});
	
	// 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');
	});

});