//The calendar nav
//2007-11-12, Luke McKenzie theclockspot.com

//This .js generates a playlist navigation calendar
//will be placed in dom @ <div id='calendarnavdiv'>
//"parent" .js should handle onChange with onCalendarNav()

//include function for including the calendar stuff into here
//http://www.chapter31.com/2006/12/07/including-js-files-from-within-js-files/
function include(file) {
	var script  = document.createElement('script'); script.src = file; script.type = 'text/javascript'; //script.defer = true;
	document.getElementsByTagName('head').item(0).appendChild(script);
}
//pathtohome must be defined in parent page
//include(pathtohome+'data/calendar/calendar.js');
//include(pathtohome+'data/calendar/lang/calendar-en.js');
//include(pathtohome+'data/calendar/lang/calendar-setup.js');

//initialization---
//Why do these weird "init" functions exist? - see showselector.js

function initCalendarNavForAll() { //you can diversify this in case different pages have different needs
	initCalendarNav();
}

function initCalendarNavForDJ(inputfield) {

	Calendar.setup({
		inputField : inputfield, // id of the input field
		ifFormat : "%Y-%m-%d", // format of the input field
		onUpdate : onCalendarNavInternal
	});
}

function initCalendarNavForMGMT () {
	document.getElementById("concertdate").value='';
	document.getElementById("giveawaydate").value='';

	Calendar.setup({
		inputField : "concertdate", // id of the input field
		ifFormat : "%Y-%m-%d", // format of the input field
		onUpdate : onCalendarNavInternal
	});
	Calendar.setup({
		inputField : "giveawaydate",
		ifFormat : "%Y-%m-%d",
		onUpdate : onCalendarNavInternal
	});
}

function initCalendarNav() { //not intended to be public
	//builds code for nav tools, places it in parent document in <div id='calendarnavdiv'>
	constvar = ''; //constructionvar! tonka truck rrrrrrrarrr
	constvar += '<form action="#" method="get">\n';
	constvar += 'From: <input name="startdate" id="startdate" type="text" readonly=true onKeyDown="return false;" size="10" value="">&nbsp;&nbsp;&nbsp;\n';
	constvar += 'To (optional): <input name="enddate" id="enddate" type="text" readonly=true onKeyDown="return false;" size="10" value="">\n';
	constvar += '&nbsp;&nbsp;&nbsp;<input type="button" value="Reset dates" onClick="resetCalendarNav(1);">\n';
	constvar += '</form>';
	document.getElementById("calendarnavdiv").innerHTML = constvar;
	
	Calendar.setup({
		inputField : "startdate", // id of the input field
		ifFormat : "%Y-%m-%d", // format of the input field
		onUpdate : onCalendarNavInternal
	});
	Calendar.setup({
		inputField : "enddate",
		ifFormat : "%Y-%m-%d",
		onUpdate : onCalendarNavInternal
	});
}

function resetCalendarNav(andUpdate) {
	d=new Date(); dst=d.getFullYear()+'-'+doubledigit((d.getMonth()+1))+'-'+doubledigit(d.getDate());
	document.getElementById("startdate").value=dst;
	document.getElementById("enddate").value='';
	if(andUpdate) onCalendarNav(true); //PARENT FN
}
function doubledigit(x) {
	//expecting number between 0 and 99, inclusive
	if(x<10) return '0'+x; else return x;
}

function onCalendarNavInternal() {
	//for the calendar; launches onCalendarNav false to indicate this is a specific date, not a reset
	onCalendarNav(false); //PARENT FN
}

function getStartDate() {
	return document.getElementById("startdate").value;
}
function getEndDate() {
	return document.getElementById("enddate").value;
}
