// Content - client side script used by all pages except bulletins (aspx) pages.
// Mainly resizing and hiding/showing page items.

var content;		// content in pages (div)
var mnuLeft; 		// left menu (div)
var showVia; 		// hidden input to say whether using via
var viaList; 		// select via (div)
var journeyResults; // (table)

// Via search entry items
var viaSearchEntry;
var addViaButton;
var hideViaButton;

// Selection list boxes
var orgPlace;
var dstPlace;
var viaPlace;

// Route timetable select list box
var routeSelect;

// Date and time vars
var d;
var m;
var today;
var day;
var year;
var end;

// Date and time
d = new Array("Sunday",	"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",	"Saturday");
m = new Array("January", "February", "March", "April", "May", "June", "July", "August",
					"September", "October", "November", "December");
today = new Date();
day = today.getDate();
year = today.getYear();
if (year < 2000)    // Y2K Fix, Isaac Powell
year = year + 1900; // http://onyx.idbsu.edu/~ipowell
end = "th";
if (day == 1 || day == 21 || day == 31) end = "st";
if (day == 2 || day == 22) end = "nd";
if (day == 3 || day == 23) end = "rd";
day += end;

// Setup variables and initially display or hide via search entry items.
function InitialiseMisc()
{

	// Set remaining vars
	content = document.getElementById("content");
	mnuLeft = document.getElementById("menu_left");
	showVia = document.getElementById("ShowVia");
	viaList = document.getElementById("via_list");
	viaSearchEntry = document.getElementById("via_search_entry");
	addViaButton = document.getElementById("AddVia");
	hideViaButton = document.getElementById("HideVia");
	orgPlace = document.getElementById("orgPlace");
	dstPlace = document.getElementById("dstPlace");
	viaPlace = document.getElementById("viaPlace");
	routeSelect = document.getElementById("routeSelection");
	journeyResults = document.getElementById("tableMain");

	if (showVia)
	{
		DisplayVia(showVia.value);

		if ((showVia.value == 0) && (viaList)) viaList.style.display = "none";
	}
}

// Journey select page and timetable select page, adjust the height of the list boxes, default size
// is 7 lines but if less than 7 items are shown then shrink height of list box
// appropriately.
function AdjustSelectBoxHeights()
{
	var DEFAULT_SIZE = 7;
	var MIN_SIZE = 2;
	var length = 0;

	if (orgPlace)
	{
		length = orgPlace.tags("option").length;
		if (length < MIN_SIZE) length = MIN_SIZE;
		if (length < DEFAULT_SIZE) orgPlace.size = length;
	}

	if (dstPlace)
	{
		length = dstPlace.tags("option").length;
		if (length < MIN_SIZE) length = MIN_SIZE;
		if (length < DEFAULT_SIZE) dstPlace.size = length;
	}

	if (viaPlace)
	{
		length = viaPlace.tags("option").length;
		if (length < MIN_SIZE) length = MIN_SIZE;
		if (length < DEFAULT_SIZE) viaPlace.size = length;
	}
}

// Set the left border of the content (div tag) item that encloses page content
// excluding menus. Left border changes when text size changes.
function RepositionContent()
{
	if (content && mnuLeft)
		content.style.borderLeftWidth = mnuLeft.offsetWidth;
}


// Resize the place/stop select boxes according to the window size.
function ResizeSelectBoxes()
{
	if (mnuLeft)
	{
		if (orgPlace) orgPlace.style.pixelWidth = window.document.body.clientWidth - mnuLeft.offsetWidth - 310;
		if (dstPlace) dstPlace.style.pixelWidth = window.document.body.clientWidth - mnuLeft.offsetWidth - 310;
		if (viaPlace) viaPlace.style.pixelWidth = window.document.body.clientWidth - mnuLeft.offsetWidth - 310;
	}
}

// Used on the route timetables page.
function ResizeSelectList()
{
	if (routeSelect && mnuLeft)
		routeSelect.style.pixelWidth = window.document.body.clientWidth - mnuLeft.offsetWidth - 310;
}

// Journey search page - called when the 'Add Via' button or 'Hide Via' button is pressed.
function SwapViaButton(addVia)
{
	if (addViaButton && hideViaButton)
	{
		if (addVia)
		{
			hideViaButton.style.display = "none";
			addViaButton.style.display = "";
		}
		else
		{
			addViaButton.style.display = "none";
			hideViaButton.style.display = "";
		}
	}
}

// Journey search page - called when the 'Add Via' button or 'Hide Via' button is pressed.
// Hides or shows the via search entry items.
function DisplayVia(display)
{
	if (viaSearchEntry)
	{
		if (display == 1)
		{
			viaSearchEntry.style.display = "";
			if (showVia) showVia.value = 1;
			SwapViaButton(false);
		}
		else
		{
			viaSearchEntry.style.display = "none";
			if (showVia) showVia.value = 0;
			SwapViaButton(true);
		}
	}
}


function PrintPage()
{
	if (window.print) window.print();
}


// Resizes the journey results (table tag) according to window size.
function ResizeJourneyResults()
{
	if (mnuLeft && journeyResults)
	{
		// Set journey results table width
		journeyResults.style.pixelWidth = window.document.body.clientWidth - mnuLeft.offsetWidth - 190;
	}
}
