// globals
versionID = "1.0";
//
var defaults = [];
defaults["font-weight"] = "normal";
defaults["font-size"] = "10pt";
defaults["background-color"] = "#FFFFFF";
//
var IsHomePage = true;
var xml_data = null;
var news_div = [];
//
// Globals
//
var IsSampleSite = false;

//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
//																																									 				setNewsDivCharacteristics()
//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
function setNewsDivCharacteristics(){
	//
	// Control that toggles the news div visibility
	//
	$("#_news_slider").css({"cursor":"pointer"});
	$("#_news_slider").click( function(){
		if ( $("#_news_items").is(":visible") === true  ){
			$("#_news_items").hide();
			this.src = 'images/showitems.gif';
			this.title = "Show all news items";
		}
		else {
			$("#_news_items").show();
			this.src = 'images/hideitems.gif';
			this.title = "Hide news items";
		}
	});
	$("#_news_items").css({"margin":"0", "padding":"0", "font-size":"xx-small"});
	//
	// Define the CSS properties for the _news_headlines DIV
	//
	news_div.css = {
		"font-size":defaults["font-size"], 
		"text-align":"right",
		"background-color":defaults["background-color"]
	};
	news_div.anchor = {
		"font-size":defaults["font-size"], 
		"font-weight": defaults["font-weight"],
		"color":"#990000",
		"text-decoration":"none", 
		"font-family" : 'verdana,"Trebuchet MS", Arial,Helvetica, San Serif',	
		"background-color":defaults["background-color"]
	};
	news_div.anchor_link = {
		"font-size":defaults["font-size"], 
		"font-weight": defaults["font-weight"],
		"color":"#990000",
		"text-decoration":"none",
		"background-color":defaults["background-color"]
	};
	news_div.anchor_visited = {
		"font-size":defaults["font-size"], 
		"font-weight": defaults["font-weight"],
		"color":"#990000",
		"text-decoration":"none",
		"background-color":defaults["background-color"]
	};
	news_div.anchor_hover = {
		"font-size":defaults["font-size"], 
		"font-weight": defaults["font-weight"],
		"background-color":"#ffffcc"
	};
	news_div.article_summary = {
		"font-size":defaults["font-size"], 
		"font-weight": defaults["font-weight"],
		"text-align":"left", 
		"padding":"1em", 
		"border":"1px solid navy",
		"background-color":"#f5f5dc",
		"margin":".5em 10% .5em 10%"
	};
	news_div.article_body = {
		"font-size":defaults["font-size"], 
		"font-weight": defaults["font-weight"],
		"text-align":"left", 
		"padding":"1em", 
		"background-color":"#f4f4f4",
		"margin-top":".5em"
	};	
	//
	// Implement the property definitions
	//
	$("#_news_headlines").css(news_div.css);
	$("#_news_headlines a").css(news_div.anchor);
	$("#_news_headlines a").click(function(){
		$(this).blur();
	});
	$("#_news_headlines a:link").css(news_div.anchor_link);
	$("#_news_headlines a:visited").css(news_div.anchor_visited);
	$("#_news_headlines a").mouseover(function() {
		$(this).css(news_div.anchor_hover);
	});
	$("#_news_headlines a").mouseout(function() {
		$(this).css(news_div.anchor_visited);
	});
	$("#_news_headlines .article_summary").css(news_div.article_summary);
	$("#_news_headlines .article_body").css(news_div.article_body);
	//
	// This next line is only used on the updater program; not the site web page
	//
	if (IsHomePage !== true){
		$("#chhdr").css({"font-size":"small","font-weight":"bold","margin-bottom":"1em","margin-top":"1em","text-align":"left"});
	}
}

//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
//																																													 	getLocalNewsFileLocation()
//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
function getLocalNewsFileLocation() {
	ans = ""
	//
	// If this is a sample location then adjust the file path
	//
	if ( IsSampleSite === true ){
		ans = "../xml/news.xml";
		IsHomePage = true;
	} 
	else if (IsHomePage){
		ans = "xml/news.xml";
	}
	else {
		//
		// Get the location from the config file
		//
		$.ajax({
			cache: false,
			async: false,
			url: config_file_location,
			dataType: "xml",
			success: function(xml_data, status){
				$(xml_data).find("site_config").each( function(){
					ans = $(this).find("local > news_file").text();
					ans = jQuery.trim(ans);
				});
			}//END: xml success
		});//END .ajax()
	}//END: else not a sample site
	jQuery.trim(ans);
	return ans;
}
//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
//																																										 				 			getXMLDataFromFile()
//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
function getXMLDataFromFile() {
	var localNewsfileLocation = getLocalNewsFileLocation();
	var err = false;
	var ans = "";
	if (localNewsfileLocation === "") { err = true; }
	if (err === false){
		$.ajax({
			cache: false,
			dataType: "xml",
			async : false,
			url: localNewsfileLocation,
			error: function(XMLHttpRequest, textStatus, errorThrown){
				err = true;
				var response = XMLHttpRequest.responseText;
				if (IsHomePage === false){
					// A "404" in the textStatus means that the file requested does not exist or cannot be found.
					if ( response.indexOf('404') >= 0 ) {
						//
						// If the news file does not exist, give the user the option to create it
						//
						ans += "<div class='err_msg'>";
						ans += "ERROR: " + localNewsfileLocation + " does not exist.";
						ans += getNewNEWSButton(localNewsfileLocation);
						ans += "</div>";
					}
					else {
						ans = "<div class='err_msg'>ERROR (" + textStatus + "): ";
						ans += response;
						ans += "</div>";
					}
				}
			},
			success: function(xml){
				xml_data = xml;
			}
		});//.ajax
	}//if
	if (err === true) {
		xml_data = null;
	}
	return ans;
}

//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
// 																																					 					   		update_news_headlines_div()
//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
function update_news_headlines_div(xml_data) {
	var ans = '<img id="_news_slider" src="images/hideitems.gif" title="click here to hide the news items" />\n';
	ans += '<div id="_news_items"> \n';
	var err = false;
	var item_cnt = 0;
	var today = new Date();
	if (IsHomePage === false ){
		ans  += "<div id='chhdr'>The following items will be displayed TODAY :</div>\n";
	}
	//
	// Traverse the xml file and only show records that fall within the current timeframe
	//
	$(xml_data).find("news_articles").each(function(){
		//
		// Run this block for each news item
		//
		$(this).find('article').each( function (){
				var _id = $(this).attr('id');
				var _posted = $(this).attr('posted');
				var _start = $(this).attr('start');
				var _expires = $(this).attr('expires');
				var _author_email = $(this).attr('author_email');
				//
				// check the dates
				//
				var start_date = new Date(_start);
				var expires_date = new Date(_expires);
				if ((start_date <= today) && (expires_date >= today)) {
					// init  for current articles
					item_cnt++;
					start_anchor = "";
					end_anchor = "";
					ans += '<div>\n';
					// link
					$(this).find('link').each( function(){
						if ($(this).text() !== ''){
							start_anchor = '<a href="' + $(this).text() + '">';
							end_anchor = '</a>';
						}
					});
					// headline
					$(this).find('headline').each( function(){
						ans += '<div id="headline_' + _id + '" >\n' + start_anchor + $(this).text() + end_anchor;
						summary_span_id = "summary_" + jQuery.trim(_id);
						body_span_id = "body_" + jQuery.trim(_id);
						// toggle summary button
						var img_tag = '<img hspace="6" id="' + summary_span_id + 'viewer" class="viewer_button" src="images/summary_icon.gif" align="bottom" ';
						img_tag += ' title="click here to toggle the summary for this headline" alt="toggle summary" ';
						img_tag += ' onclick="$(' + "'#" + summary_span_id + "'" + ').toggle();" ';
						img_tag += '/>';
						ans += img_tag;
						// toggle boddy button
						img_tag = '<img hspace="3" id="' + body_span_id + 'viewer" class="viewer_button" src="images/body_icon.gif" align="bottom" ';
						img_tag += ' title="click here to toggle the body for this headline" alt="toggle article body" ';
						img_tag += ' onclick="$(' + "'#" + body_span_id + "'" + ').toggle();" ';
						img_tag += '/>';
						ans += img_tag + "";
					});
					// summary
					$(this).find('summary').each( function(){
						var tsummary = jQuery.trim($(this).text());
						if (tsummary !== ''){
							ans += '      <div id="' + summary_span_id + '" class="article_summary">' + $(this).text() + "\n</div>";
							ans += '\n<script language="javascript" type="application/javascript">';
							ans += '$("#' + summary_span_id + '").hide();';
							ans += '\n</script>';
						}
						else {
							ans += '\n<script language="javascript" type="application/javascript">';
							ans += '$("#' + summary_span_id + 'viewer").hide();';
							ans += '\n</script>\n';
						}
					});
					// body
					$(this).find('body').each( function(){
						var tbody = jQuery.trim($(this).text());
						if (tbody !== ''){
							ans += '<div id="' + body_span_id + '" class="article_body">' + $(this).text() + "</div>\n";
							ans += '<script language="javascript" type="application/javascript">\n';
							ans += '$("#' + body_span_id + '").hide();';
							ans += '\n</script>\n';
						}
						else {
							ans += '<script language="javascript" type="application/javascript">\n';
							ans += '$("#' + body_span_id + 'viewer").hide();';
							ans += '\n</script>\n';
						}
					});
					ans += '</div>\n';
				}// if 
		});
	});
	ans += "</div>\n";
	ans += "</div>\n";
	$("#_news_headlines").html(ans);
	setNewsDivCharacteristics();
	if (item_cnt < 1){
		$("#_news_slider").hide();
	}
	$("#_news_headlines").show();
}

//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
//																																				 					   				 displayCurrentArticles()
//-   -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -     -   -
function displayCurrentArticles(){
	// initialize
	xml_data = null;
	var ans = getXMLDataFromFile();
	if (xml_data !== null ) {
		ans = update_news_headlines_div(xml_data);
	}
}
