// Copyright (c) 2012 by Stephen C. Dewhurst

function deptcode_to_deptname_long(code, dept_map) {
	for (var i = 0; i != dept_map.length; ++i) {
		if (dept_map[i][0] == code)
			return dept_map[i][1];
	}
	return "Unknown Department";
}

function deptcode_to_deptname_short(code, dept_map) {
	for (var i = 0; i != dept_map.length; ++i) {
		if (dept_map[i][0] == code)
			return dept_map[i][2];
	}
	return "Unknown";
}

function deptcode_to_dept_dir(code, dept_map) {
	for (var i = 0; i != dept_map.length; ++i) {
		if (dept_map[i][0] == code)
			return dept_map[i][4];
	}
	return "";
}

//
//	These are for removing headlines.
//
function getid(headline_array) {
	var index_element = document.getElementById('headline_index');
	var index = index_element.value;
	var id = headline_array[index][7];
	var id_element = document.getElementById('headline_id');
	id_element.value = id;
}

function write_headlines_precis(headline_array) {
	for (var i = 0; i != headline_array.length; ++i) {
		//var department = headline_array[i][0];
		var posted = headline_array[i][1];	// date the item was posted
		//var start = headline_array[i][2];	// date to start posting
		var stop = headline_array[i][3];	// date after which to stop posting
		var headline = headline_array[i][4];
		//var unused = headline_array[i][5];
		//var description = headline_array[i][6];
		//var id = headline_array[i][7];

		var line = "<h3>" + i + ": " + "posted " + posted
			+ ", expires " + stop
			+ "&nbsp;&nbsp;<i>\"" + headline + "\"</i></h3>";

		document.write(line);
	}
}

function write_today() {
	var today = new Date();
	var todayStr = "<h3>Today is " + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear() + "</h3>";
	document.write(todayStr);
}

function write_form_line(action_location) {
	var line = "<form method=\"POST\" action=\""
			+ action_location
			+ "\" id=\"headline_form\">";
	document.write(line);
}

//
//	These are for adding or displaying headlines.
//
function makeDate(d) {	// d must be string as mm/dd/yyyy
	//if (d.length != 10) ;
	//if (d.substr(2,1) != "/") ;
	//if (d.substr(5,1) != "/") ;
	var month = d.substr(0,2);
	var day = d.substr(3,2);
	var year = d.substr(6,4);
	var theDate = new Date(year, month-1, day-1);
	return theDate;
}

function posted_comp(a, b) {
	// compare date posted, sort most recent (greater date) first
	var da = makeDate(a[1]);
	var db = makeDate(b[1]);
	if (da > db) return -1;
	if (da < db) return 1;
	return 0;
}

function can_publish(headline) {
	var today = new Date();
	var firstdate = makeDate(headline[2]);	// date to start posting
	var lastdate = makeDate(headline[3]);	// date after which to stop posting
	return (firstdate <= today) && (today <= lastdate);
}

function count_headlines(listArr) {
	var count = 0;
	for (var i = 0; i != listArr.length; ++i) {
		if (null == listArr[i])
			continue; // cover errant commas
		else if (can_publish(listArr[i]))
			++count;
	}
	return count;
}

function process_headline_array(headlines) {
	var new_headlines = new Array();
	for (var i = 0; i != headlines.length; ++i) {
		if (null == headlines[i])
			continue;	// cover errant commas
		else if (can_publish(headlines[i]))
			new_headlines.push(headlines[i]);
	}
	if (new_headlines.length > 0)
		new_headlines.sort(posted_comp);
	return new_headlines;
}

function formatHeadlines(listArr, max, length, show_dept, dept_map) {
	var ITEM_START = "<hr width=80% align=left size=1>";
	var ITEM_END = "<br>";
	var HEAD_BEGIN = "";
	if (length == "long")
		HEAD_BEGIN = "<span class=\"headline\"><strong>"
				+ "<span class=\"mouseoffheadline\" onmouseover=\"this.className='mouseonheadline'\" onmouseout=\"this.className='mouseoffheadline'\">";
	var HEAD_END = "";
	if (length == "long")
		HEAD_END = "</span></strong></span>";
	var DESC_BEGIN = "<div class=\"description\">";
	var DESC_END = "</div>";
	var ATT_START = "<br><i>Attachments:</i>";
	var ATT_END = "";
	var DATE_POSTED_BEGIN = "&nbsp;[<i>posted ";
	var DATE_POSTED_END       = "</i>]";
	if (length == "long") {
		DATE_POSTED_BEGIN = "<br>&nbsp;&nbsp;&nbsp;&nbsp;<i>Posted on ";
		DATE_POSTED_END       = "</i>";
	}

	var listStr = "";

	listArr = process_headline_array(listArr);

	try {
		if (listArr.length == 0) {
			listStr = HEAD_BEGIN + "There are no current headlines." + HEAD_END;
		}
		else {
			var counter = 0;
			for (var i = 0; i != listArr.length; ++i) {
				var dept_code = listArr[i][0];
				var department = "";
				if (length == "short") 
					department = deptcode_to_deptname_short(dept_code, dept_map);
				else
					department = deptcode_to_deptname_long(dept_code, dept_map);
				var posted = listArr[i][1];	// date the item was posted
				//var start = listArr[i][2];	// date to start posting
				//var stop = listArr[i][3];	// date after which to stop posting
				var headline = listArr[i][4];
				var attachments = listArr[i][5];// link and file attachments
				var description = listArr[i][6];
				//var identifier = listArr[i][7];
				var listLine = ITEM_START;
				if (show_dept == "yes") {
					listLine += "<a href=\"http://www.carverma.org/carverma_admin/" + dept_code + "/do_department_headlines.php\">";
					listLine += "<b>" + department + "</b>";
					listLine += "</a>: ";
				}
				listLine += HEAD_BEGIN + headline + HEAD_END;
				if (length == "long") {
					listLine += DESC_BEGIN + description;
					if (attachments != "" && attachments != "PLACEHOLDER")
						listLine += ATT_START + attachments + ATT_END;
					listLine +=  DESC_END;
				}
				listLine += DATE_POSTED_BEGIN + posted + DATE_POSTED_END;
				listLine += ITEM_END;
				listStr += listLine;
				if (++counter >= max)
					break;
			}
		}
	}
	catch (e) {
	}

	return listStr;
}

function writeHeadlines(headlineArray, max, length, show_dept, dept_map) {
	var hdlnStr = formatHeadlines(headlineArray, max, length, show_dept, dept_map);
	//if (""!=hdlnStr) document.getElementById("headlines_holder").style.display="block";
	return hdlnStr;
}

$(document).ready(function() {
	$('.description').hide();
	$('.headline').toggle(
		function() {
			$(this).next('.description').fadeIn();
			$(this).addClass('close');
		},
		function() {
			$(this).next('.description').fadeOut();
			$(this).removeClass('close');
		}
	); // end toggle
});

