var articles;
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];

// Calendar Settings
prettytd = false;
calurl = "http://" + location.host + "/newspaper/archive/nav";

var repcode;

function reloadCalendar() {
	if (! dateselectionvalid())
		return false;

	var theForm = document.browse;
	yy = parseInt( theForm.year.options[ theForm.year.selectedIndex ].value );
	mm = parseInt( theForm.month.options[ theForm.month.selectedIndex ].value );	
	
	initCalendar("archive" + (repcode?"/"+repcode:""),yy,mm,1);
	//addFeatures( articles );
}

function dateselectionvalid() {

	var theForm = document.browse;

	var today = new Date();
	today.setDate(today.getDate()-7)

	if ( theForm.year.options.length > 0
		&& theForm.year.selectedIndex == theForm.year.options.length-1
		&& theForm.month.selectedIndex > today.getMonth()) {
		theForm.month.selectedIndex = today.getMonth();
		return false;
	} else if (theForm.year.selectedIndex == 0
		&& theForm.month.selectedIndex < 2) {
		theForm.month.selectedIndex = 2;
		return false;
	}

	return true;
}

function moveCalendarForw() {
	if (! dateselectionvalid())
		return false;

	var theForm = document.browse;

	if( theForm.month.selectedIndex == 11
		&& theForm.year.selectedIndex < theForm.year.options.length) {
		theForm.month.selectedIndex = 0;
		theForm.year.selectedIndex++;
	} else {
		theForm.month.selectedIndex++;
	}
	reloadCalendar();
	return false;
}

function moveCalendarBack() {
	if (! dateselectionvalid())
		return false;

	var theForm = document.browse;
	if( theForm.month.selectedIndex == 0 
		&& theForm.year.selectedIndex > 0) {
		theForm.month.selectedIndex = 11;
		theForm.year.selectedIndex--;
	} else {
		theForm.month.selectedIndex--;
	}
	reloadCalendar();
	return false;
}

function initYearList() {
	var yearlist = document.getElementById( 'yearlist' );

	yearlist.length = 0;

	for(i=startyear;i<=endyear;i++) {
		yearlist.options[ yearlist.options.length ] = new Option( i, i );
	}

	loadFeatures();
}

function addFeatures( articles ) {
	theForm = document.browse;
	yy = theForm.year.options[ theForm.year.selectedIndex ].value;
	mm = theForm.month.options[ theForm.month.selectedIndex ].value;
	epoch = new Date( yy, mm-1, 1 ).getTime();

	/*
	for(var i=1;i<=3;i++) {
		$('fi'+i+'_date').setHTML( '' );
		$('fi'+i+'_headline').setHTML( '' );
		$('fi'+i+'_description').setHTML( '' );
		$('fi'+i+'_link').setProperty( 'href', '' );
	}

	counter = 1;

	articles.each(function(article) {

		if (article.epoch >= epoch) {
			if (counter > 3) return;
			$('fi'+counter+'_date').setHTML( prettydate( article.epoch ) );
			$('fi'+counter+'_headline').setHTML( article.headline );
			$('fi'+counter+'_description').setHTML( article.description );
			$('fi'+counter+'_link').setProperty( 'href', article.link );
			counter++;
		}

	});
	*/

}

function loadFeatures() {
	var url = 'http://' + location.host + '/search/timeline.js';
	var request = new Json.Remote(url, {
		method: 'get',
		onComplete: function(jsonObj) {

			articles = jsonObj;

			articles.each(function(article) {
				article.epoch = iso9660toepoch( article.date );
			});
			articles.sort(compareEpoch);
			addFeatures( articles );

		}
	}).send();
}

function iso9660toepoch( iso ) {
	var datepart = iso.substring(0,10).split('-');
	return new Date( datepart[0], datepart[1]-1, datepart[2] ).getTime();
}

function prettydate( epoch ) {
	var mydate = new Date( epoch );
	var prettydate = months[ mydate.getMonth() ];
	prettydate += " " + mydate.getDate()
	prettydate += ", " + mydate.getFullYear()
	return prettydate;
}

function compareEpoch(a,b) { return a.epoch - b.epoch; }

var publist = [ 'wit', 'tpl', 'pic', 'ipl' ];
var firstlist = [ '1876,1,1', '1941,12,6', '1955,6,18', '1957,8,10' ];
var lastlist = [ '1941,11,29', '1955,6,11', '1957,8,3', '1958,03,29' ];

var startyear = 1859;
var endyear = (new Date()).getFullYear();
var csection = "archive";

//eoneill - new function to switch publications using a dropdown instead of tabs
function switchPub(){

	var theForm = document.browse;
  	i = parseInt( theForm.pub.options[ theForm.pub.selectedIndex ].value );
	var first, last;
			if (i == 1) {
				repcode = "";
				calurl = 'http://'+location.host+'/newspaper/archive/nav';
				first = '1859,3,1';
				last = (new Date()).getFullYear() + ",1,1";
				csection = "archive";
			} else {
				repcode = publist[i-2];
				calurl = 'http://'+location.host+'/newspaper/archive/'+repcode+'/nav';
				first = firstlist[i-2];
				last = lastlist[i-2];
				csection = "archive/" + publist[i-2];
			}
			var fdate = first.split(",");
			var ldate = last.split(",");

			startyear = fdate[0];
			endyear = ldate[0];

			initCalendar(csection,fdate[0],fdate[1],fdate[2]);
			initYearList();

			document.browse.month.selectedIndex = fdate[1]-1;
}

