function searchCalendar(year,month,newLocation,variation) {
	//console.log("searchCalendar:");
	//console.log(year);
	//console.log(month);
	//console.log(newLocation);

	if (year === null){
		year  = $('year').getValue();
		month = $('month').getValue();
	} 

	var navItem = $('calNavMonth_' + year + "_" + month);
	if (navItem === null) {
		//console.log("Month Not Available");
		return;
	}

	$('location').setValue(newLocation);
	$('calendarContainer').hide();
	$('calendarLoadingMessage').show();
	
	$$('.calNavMonth').invoke('removeClassName','tCalTabNavOn');

	navItem.addClassName('tCalTabNavOn');
	
//	$$('.calNavYear').each(function(item){item.removeClassName('tCalYearTabNavOn');});
//	$('calNavYear' + year).addClassName('tCalYearTabNavOn');	
	
//	$$('.calNavLocation').each(function(item){item.removeClassName('tCalLocationTabNavOn');});
//	$('calNavLocation' + newLocation).addClassName('tCalLocationTabNavOn');	
	
	
	new Ajax.Updater('calendarContainer', '/h/tessitura/ShowCalendar', 
    { 	evalScripts: false, 
        method: 'get', 
        parameters: {nonav:'1',year:year,month:month,location:newLocation,variation:variation},
		onSuccess: function(response){
			$('calendarLoadingMessage').hide();
			$('calendarContainer').show();
			// Create a date object on the 5th to avoid timezone related issues
			var d = new Date(year,month-1,5);
			$('calNavCurrentMonthLabel').update(formatDateForNavTab(d));
		}
	});
    calSetNewMonth(year, month);
}

function nextMonth(variation) {

	year  = parseInt($('year').getValue(),10);
	month = parseInt($('month').getValue(),10);
	
	month = month + 1;
	
	if (month == 13) {
		month = 1;
		year = year + 1;
	}
	monthpad = month >= 10 ? month : '0' + month; 
	
	searchCalendar(year,monthpad,$('location').value, variation);
	
}

function prevMonth(variation) {
	year  = parseInt($('year').getValue(),10);
	month = parseInt($('month').getValue(),10);
	
	month = month - 1;
	
	if (month === 0) {
		month = 12;
		year = year - 1;
	}
	monthpad = month >= 10 ? month : '0' + month; 
	
	searchCalendar(year,monthpad,$('location').value, variation);
}

function calSetNewMonth(year, month) {
	// Set the month and year
	$('year').setValue(year);
	$('month').setValue(month);
	
	
  var d = new Date(year,month-1,5);
  $('ddCalValue').update(formatDateForNavTab(d));

  $('tCalMonthTabNavDropdown').hide();
}

function formatDateForNavTab(d){
	var monthname=new Array(7);
	monthname[0]="January";
	monthname[1]="Februrary";
	monthname[2]="March";
	monthname[3]="April";
	monthname[4]="May";
	monthname[5]="June";
	monthname[6]="July";	
	monthname[7]="August";	
	monthname[8]="September";	
	monthname[9]="October";	
	monthname[10]="November";	
	monthname[11]="December";	

	var monthNum = d.getUTCMonth();
	var yearNum  = d.getUTCFullYear();
	return monthname[monthNum] + " " + yearNum;
}
