/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *	EVERYMOMENT NOW : http://everymomentnow.com
 *	Javascript Tomfoolery, 2008          *
 *  (c) Craig Mod : http://craigmod.com  *
 *                   * *   *             *
 *     *           * * * * *       *     * * *
 *   * *         * * * * * * *     * *   * * * * *
 * * * * * * *   * * * * * * * * * * * * * * * * * * * * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
	var filterList		= [];
	var current_date	= "";

	function newsproject_init(autoLoad, auto_current_date, base_keyword) {
	
		current_date = auto_current_date;
		tooltip("graph");
		
		// - - - - - - - - - - - - - - - - - - - - - - - -
		// setup graph hover states
		//
		// NOTE: this will not work for multiple keywords ... Yappari, we need to
		// rewrite the chart generation, however slightly! 
		//

//
// Note on this commenting out:
// We're turning off Ajax loads for clicks on the chart for launch 
// because we want people to get unique urls when they click on the chart states
// right now, if they enter election08 with a date, and then click around
// a descrpency builds between the url and the data ... which I think it misleading
// so by turning off the next couple of functions, we turn this off for now
//
/*
		$("#graph td.dateData").hover(function () {

			color 		= $(this).css("background-color");
			$(this).css({ backgroundColor: "#efefef", cursor: "pointer" });
			
		  }, 
		  function () {
			$(this).css({ backgroundColor: color, cursor: "default" });
			//$(this).contents().find("a").css({ backgroundColor: link_color });
		  }
		);

		$("#graph td.dateData").click(function (event) {
			$(this).contents().find("a").trigger('click');
			$(this).contents().find("a").addClass("red");
			filterList		= [];								// this is to clear any active filters we may have turned on ..
		});

		
		// - - - - - - - - - - - - - - - - - - - - - - - -
		// setup graph load states
		//
		$("#graph li a").click(function(event) {
			event.preventDefault();
			
			$("#graph li a").removeClass("red");
			$(this).addClass("red");
			
			$("#articleListing").html("<div class=\"loading\">loading <img src=\"/images/ajax-loader.gif\" alt=\"Loading ...\" /></div>");
			$("#keywordListing").html("<div class=\"loading\">loading <img src=\"/images/ajax-loader.gif\" alt=\"Loading ...\" /></div>");

			current_date = $(this).parent().attr("id");
		
			$.ajax({
				type: "GET",
				url: "/get_day_tags/" + current_date + "/" + base_keyword + "/",
				success: function(html) {
					display_tags(html);
				}
			});

			$.ajax({
				type: "GET",
				url: "/get_day_articles/" + current_date + "/" + base_keyword + "/",
				success: function(html) {
					display_articles(html);
				}
			});

		});
*/
		
		// - - - - - - - - - - - - - - - - - - - - - - - -
		// If we're autoloading data (i.e., a date is being
		// passed in the URL)
		//		
		if (autoLoad) {
		
			$("#graph #" + auto_current_date + " a").addClass("red");

			$("#articleListing").html("<div class=\"loading\">loading <img src=\"/images/ajax-loader.gif\" alt=\"Loading ...\" /></div>");
			$("#keywordListing").html("<div class=\"loading\">loading <img src=\"/images/ajax-loader.gif\" alt=\"Loading ...\" /></div>");

			$.ajax({
				type: "GET",
				url: "/get_day_tags/" + auto_current_date + "/" + base_keyword + "/",
				success: function(html) {
					display_tags(html);
				}
			});
			
			$.ajax({
				type: "GET",
				url: "/get_day_articles/" + auto_current_date + "/" + base_keyword + "/",
				success: function(html) {
					display_articles(html);
				}
			});
			
		}
		
	}
	
	function display_articles(html) {
		
		$("#articleListing").html(html);
		$("li#jq_shortsummary a").addClass("selected");

		$("li#jq_nosummary").click(function(event) {
			event.preventDefault();
			$("div.articleSummaries").hide("slow");
			$("#articleListing li a").removeClass("selected");
			$("li#jq_nosummary a").addClass("selected");
		});
		
		$("li#jq_shortsummary").click(function(event) {
			event.preventDefault();
			$("div.articleSummaries p.longsummary").hide("slow");
			$("div.articleSummaries").show("slow");
			$("div.articleSummaries p.shortsummary").show("slow");
			$("#articleListing li a").removeClass("selected");
			$("li#jq_shortsummary a").addClass("selected");
		});
		
		$("li#jq_longsummary").click(function(event) {
			event.preventDefault();
			$("div.articleSummaries p.shortsummary").hide("slow");
			$("div.articleSummaries").show("slow");
			$("div.articleSummaries p.longsummary").show("slow");
			$("#articleListing li a").removeClass("selected");
			$("li#jq_longsummary a").addClass("selected");

		});
		
		$("#moreArticles a").click(function(event) {
			event.preventDefault();
			$("#moreArticles").hide();
			$("#extendedArticles").show("slow");
		});
		
	}
	
	function display_filtered_articles(html) {
		$("#articleListingBody #filters").hide("slow");
		$("#articleListingBody").html(html);

		$("#filters a.removeFilter").click(function(event) {
			event.preventDefault();
			
			var filter_key = $(this).attr("id");
			
			var filter_text = build_filter(filter_key);				// this holds the text for the filtering notice[1], and the html for the filter url[0], 
																	// and also if there are any filters at all [2] (bool)
			process_filter(filter_text, filter_key);
			tooltip(); 												// remove lingering tooltip -- just clears all #tooltip ps
		});
		
		$("#moreArticles a").click(function(event) {
			event.preventDefault();
			$("#moreArticles").hide();
			$("#extendedArticles").show("slow");
		});
		
		tooltip("filters");
	}
	
	function display_tags(html) {
		$("#keywordListing").html(html);
		
		$(".sparkline").sparkline('html', { lineColor: '#000', spotColor: '#e33636', spotRadius: '2', fillColor: '#efefef', minSpotColor: '', maxSpotColor: '' });
		
		$("a.filter").click(function(event) {
			
			event.preventDefault();
			
			var filter_key = $(this).parent().attr("id");			// this is the keyword we're adding or removing from the filter

			var filter_text = build_filter(filter_key);				// this holds the text for the filtering notice[1], and the html for the filter url[0], 
																	// and also if there are any filters at all [2] (bool)

			process_filter(filter_text, filter_key);
			
			tooltip(); 												// remove lingering tooltip -- just clears all #tooltip ps

		});
		
		tooltip("keywordListing");
		
	}
	
	//--------------------------------------------------------------------------------------
	//
	// Handles the logic of processing a filter (so we can filter from various points:
	// i.e., from the tags column or from the filter list ..
	//
	// Notes on FILTER_TEXT varible:			
	// this holds the text for the filtering notice[1], and the html for the filter url[0], 
	// and also if there are any filters at all [2] (bool)
	//
	//
	function process_filter(filter_text, filter_key) {
	
		var filter_url = "";									// this holds the final filter url

		// Toggle keyword active / inactive state
		//
		if ($("#keywordListing li#"+filter_key+" a.filter").hasClass("red")) {
			$("#keywordListing li#"+filter_key+" a.filter").removeClass("red");
			//$("#keywordListing li#"+filter_key+" a.filter").attr({ title: "Filter article list with this keyword" });
		}
		else {
			$("#keywordListing li#"+filter_key+" a.filter").addClass("red");
			//$("#keywordListing li#"+filter_key+" a.filter").attr({ title: "Remove this filter from article list" });
			//$("#keywordListing li#"+filter_key+" a.filter").attr("title", "Remove this filter from article list");
		}
			
		$("#articleListingBody #filters").addClass("filtering");

		// are there any active filters?
		if (filter_text[2]) { 
		
			$("#articleListingBody #filters").html("<p>Filtering with <span>" + filter_text[1] + "</span> ...</p>");
			filter_url = current_date + "/" + base_keyword + "/_filter_/" + filter_text[0];
		
		}
		
		// If no active filters ... 
		else {
			$("#articleListingBody #filters").html("<p>Removing filters ...</p>"); 
			filter_url = current_date + "/" + base_keyword + "/";
		}


		$("#articleSnippets").slideUp("slow");
		
		// If there are active filters, then display the filter function ...
		//
		if (filter_text[2]) {
			$.ajax({
				type: "GET",
				url: "/get_day_articles/" + filter_url,
				success: function(html) {
					display_filtered_articles(html);
				}
			});
		}
		
		// Else, just act like we're displaying articles for the base_keyword
		//
		else {
			$.ajax({
				type: "GET",
				url: "/get_day_articles/" + filter_url,
				success: function(html) {
					display_articles(html);
				}
			});
		}

	}
	
	function build_filter(filter_key) {
		var inArray = false;
		
		// - - - - - - - - - - - - - - - - - - - - - - - -
		// Build out filter array
		//
		
		filter_key = filter_key.replace(/_/, " ");			// We need to replace the '_', used for valid element ids to
															// a space, which is how the words are stored in the backend
		
		// Do we have any current filter keywords?
		//
		if (filterList.length > 0) {
			
			// Loop through all keywords, if we find a match, toggle it off (remove from array via splice)
			//
			for (var i = 0; i < filterList.length; i++) {
				if(filterList[i] == filter_key) {
					filterList.splice(i, 1);
					inArray = true;
				}
			}
			
			// If we didn't find it as a currently active filter, add to the filterList
			//
			if (!inArray)
				filterList[filterList.length] = filter_key;
		}
		
		// If no current keywords, just add this as the first key to the array
		//
		else {
			filterList[filterList.length] = filter_key;
		}
		
		// - - - - - - - - - - - - - - - - - - - - - - - -
		// Build our filter texts
		//
		var filterText = [];
		filterText[0] = "";			// the url for the filter
		filterText[1] = "";			// the 'text' represnetation of the filter
		filterText[2] = false;		// whether or not we have anything in the filter

		if (filterList.length > 0)
			filterText[2] = true;
		
		for (var i = 0; i < filterList.length; i++) {

			// Add each filter word + '/' to the filter url
			//
			filterText[0] = filterText[0] + filterList[i] + "/";

			// Add commas between words, but not last (-1) word)
			//
			if (i != filterList.length -1)
				filterText[1] = filterText[1] + filterList[i] + ", ";
			else
				filterText[1] = filterText[1] + filterList[i];

		}
		
		return filterText;
		
	}
	
