/*
Written by Marco A. Gonzalez
Copyright (c) 2009 Amagavi, Inc.

web: http://www.amagavi.com/

this javascript depends on AmagaviHTML.js
*/

function isDateBetween(testDate, startDate, endDate){
	var result = false;
	if (startDate < testDate && testDate < endDate ){
		result = true;
	}	
	return result;
}


function dateFromTeaserDateString(dateString){
	var theDate;
	
	theDate = new Date(	dateString.substring(0,4), 
						dateString.substring(5,7)-1, 
						dateString.substring(7,9) );			
	// alert("dateFromTeaserDateString(): dateString = "+theDate.toString());
	return theDate;
}

function showHomeTeasers(id, teaserJSONfn){
	var html = "";
	
	var teasers = eval(teaserJSONfn);
	
	var today = new Date();
	var startDate;
	var endDate;
	
	var item;
	
	/*
	var currentMonthNumber = today.getMonth()+1 //get current month (1-12)
	var currentYearNumber = today.getFullYear() //get current year
	var currentDayNumber = today.getDate();
	*/
	
	var numberOfItems = teasers.length;
	for (item = 0; item < numberOfItems; item++){
		startDate = dateFromTeaserDateString(teasers[item].startDate);
		stopDate = dateFromTeaserDateString(teasers[item].stopDate);

		if (isDateBetween(today, startDate, stopDate)){
			onClickAttribute = "";
			if (teasers[item].linkTo != ""){
				onClickAttribute = asHTMLAttribute("onclick", "parent.location='"+teasers[item].linkTo+"'");
			}
		
			html += asHTMLOpenTag("div", 
								asHTMLAttribute("class", "newsEventEntry") +
								onClickAttribute
								);
			
			tableAttribute = "";
			if (teasers[item].linkTo == ""){
				tableAttribute = asHTMLAttribute("class", "noHover");
			}
			html += asHTMLOpenTag("table", tableAttribute);
			
			html += asHTMLOpenTag("tr", "");
			
			// icon column
			html += asHTMLOpenTag("td", asHTMLAttribute("class", "icon"));	
			
			if (teasers[item].iconPath.length > 0){
			html += asHTMLContainer("img", 
						asHTMLAttribute("src", teasers[item].iconPath) + 
						asHTMLAttribute("alt", ""), "");
			}
							
			html += asHTMLCloseTag("td");
			
			// column 2
			html += asHTMLOpenTag("td", asHTMLAttribute("class", "description"));
			
			if (teasers[item].contentForColumn2.length > 0){
						html += asHTMLContainer("div", 
						asHTMLAttribute("class", "newsitem"), 
						teasers[item].contentForColumn2);
			}			
			html += asHTMLCloseTag("td");
			
			
			// column 3
			html += asHTMLOpenTag("td", asHTMLAttribute("class", "when"));
			
			if (teasers[item].contentForColumn3.length > 0){
						html += asHTMLContainer("div", 
						asHTMLAttribute("class", "newsitem2"), 
						teasers[item].contentForColumn3);
			}
			html += asHTMLCloseTag("td");
			
			html += asHTMLCloseTag("tr");
			html += asHTMLCloseTag("table");
			
		
			html += asHTMLCloseTag("div");
		} // end if
	}// end for
	document.getElementById(id).innerHTML = html;	
}// end 


// depends on AmagvaCalendar.js
function showScheduledEventForToday(id, calendarJSONfn, h1text){
	var calendarJSON = eval(calendarJSONfn);	// actually call the method to get the JSON data
	
	var today = new Date();
	var month = today.getMonth()+1; //get current month (1-12)
	var year = today.getFullYear(); //get current year
	var day = today.getDate();
	var fieldName = "teaser";
	
	todayText = getTextForMonthDayYear(calendarJSON, month, day, year, fieldName);
	if (todayText.length > 0){
		html = "";
		html += asHTMLOpenTag("div", asHTMLAttribute("class", "CalendarItemForTodayOnHomePage"));	
		html += asHTMLContainer("h1", "", h1text);
	
		textLines = todayText.split("\n");
		html+=asHTMLOpenTag("ul", "");
		for(q=0; q<textLines.length; q++){
			if(textLines[q].length > 0){
				html+=asHTMLContainer("li", "", textLines[q]);
			}
		}
		html+=asHTMLCloseTag("ul");			
		
		html += asHTMLCloseTag("div");
		document.getElementById(id).innerHTML = html;	
	}
}



// depends on AmagvaCalendar.js
function showScheduledEventForTomorrow(id, calendarJSONfn, h1text){
	var calendarJSON = eval(calendarJSONfn);	// actually call the method to get the JSON data
	
	var tomorrow = new Date();
	tomorrow.setDate(tomorrow.getDate() + 1);
	
	var month = tomorrow.getMonth()+1; //get current month (1-12)
	var year = tomorrow.getFullYear(); //get current year
	var day = tomorrow.getDate();
	
	var fieldName = "teaser";
	var href="";
	
	todayText = getTextForMonthDayYear(calendarJSON, month, day, year, fieldName);
	if (todayText.length > 0){
		html = "";
		html += asHTMLOpenTag("div", asHTMLAttribute("class", "CalendarItemForTomorrowOnHomePage"));	
		html += asHTMLContainer("h1", "", h1text);
	
		textLines = todayText.split("\n");
		html+=asHTMLOpenTag("ul", "");
		for(q=0; q<textLines.length; q++){
			if(textLines[q].length > 0){
				href=asHTMLContainer("a", 
					asHTMLAttribute("href", "RossSchoolCalendar.html"),
					 textLines[q]);
				html+=asHTMLContainer("li", "", href);
				// html+=asHTMLContainer("li", "", textLines[q]);
			}
		}
		html+=asHTMLCloseTag("ul");			
		
		html += asHTMLCloseTag("div");
		document.getElementById(id).innerHTML = html;	
	}
}
