// daily.js
// by Malcolm Lyon
//
// JavaScript used for the daily devotion component (com_daily)
// To display the calendar, as well as including this js file you must also have:
//
//  - a <span id="calendar">anything</span> - this is where the calendar will be displayed
//  - a <form name="calform" method="post" action=""> - the action is set when the form is submitted
//  - a <input name="date" id="date" type="hidden" value=""> within this form
//  - a call to the function calendarInit("yyyy-mm-dd") to set everything going

var dayobj = new Date();
var year = dayobj.getFullYear();
var month = dayobj.getMonth();
var day = dayobj.getDate();
var curryear = year;
var currmonth = month;
var currday = day;
var incurrent = 1;
var daystr = Array(
	"الأحد",
	"الإثنين",
	"الثلاثاء",
	"الأربعاء",
	"الخميس",
	"الجمعة",
	"السبت"
);
var dayabbv = Array(
	"أح",
	"إث",
	"ثل",
	"أر",
	"خم",
	"جم",
	"سب"
);
var monthmestr = new Array(
	"كانون الثاني",
	"شباط",
	"آذار",
	"نيسان",
	"أيار",
	"حزيران",
	"تمّوز",
	"آب",
	"أيلول",
	"تشرين الأول",
	"تشرين الثاني",
	"كانون الأول"
);
var monthegstr = new Array(
	"يناير",
	"فبراير",
	"مارس",
	"أبريل",
	"مايو",
	"يونيو",
	"يوليو",
	"أغسطس",
	"سبتمبر",
	"أكتوبر",
	"نوفمبر",
	"ديسمبر"
);
var monthnastr = new Array(
	"جانفي",
	"فيفري",
	"مارس",
	"أفريل",
	"ماي",
	"جوان",
	"جويلية",
	"أوت",
	"سبتمبر",
	"أكتوبر",
	"نوفمبر",
	"ديسمبر"
);
var clickonprev = "لسماع التأمّلات إضغط على اليوم المناسب";

function calendarInit(dstr)
{
	year = dstr.substr(0,4);
	month = dstr.substr(5,2) - 1;
	day = dstr.substr(8,2);
	calendarShow();
	return;
}

function calendarShow()
{
	dayobj.setFullYear(year);
	dayobj.setMonth(month);
	dayobj.setDate(1);
	var dfirst = dayobj.getDay();
	var lastdayobj = new Date(year,month+1,0);
	var last = lastdayobj.getDate();
	var incurrent = (month == currmonth && year == curryear ? 1 : 0);
	var monthstr = monthmestr[month]+" / "+monthegstr[month];
	if (monthnastr[month] != monthegstr[month]) monthstr += " / "+monthnastr[month];
	var output = ""; 
	output += "<table align='left'>";
	output += "<tr align='center'>";
	output += "<td onclick='lastMonth();' style='cursor:pointer;'>&lt;&lt;</td>";
	output += "<td colspan='5'>"+monthstr+" "+year+"</td>";
	if (incurrent) output += "<td><br /></td>";
	else output += "<td onclick='nextMonth();' style='cursor:pointer;'>&gt;&gt;</td>";
	output += "</tr>";
	output += "<tr align='center'>";
	for (d=0; d<7; d++) output += "<td width='14%'><font size=1>"+daystr[d]+"</font></td>";
	output += "</tr>";
	output += "<tr align='center'>";
	for (d=1; d<=dfirst; d++) output +="<td><br /></td>"
	for (nday=1; nday<=last; nday++)
	{
		if (d % 7 == 1) output += "<tr align='center'>";
		bgcol = (nday == day ? '#f4a460' : '#ffebcd');
		if (incurrent && nday > currday) output += "<td style='background:#ddd'>"+nday+"</td>";
		else output += "<td onclick='changeDay("+nday+")' style='cursor:pointer; background:"+bgcol+";'>"+nday+"</td>";
		if (d % 7 == 0) output += "</tr>";
		d++;
	}
	if (d % 7 != 1)
	{
		while (d % 7 != 1)
		{
			output += "<td><br /></td>"
			d++;
		}
		output += "</tr>";
	}
	output += "<tr><td colspan='7' align='center'><font size=1>"+clickonprev+"</font></td></tr>";
	output += "</table>";
	document.getElementById("calendar").innerHTML = output;
	return;
}

function calendarSmallInit(dstr)
{
	year = dstr.substr(0,4);
	month = dstr.substr(5,2) - 1;
	day = dstr.substr(8,2);
	calendarSmallShow();
	return;
}

function calendarSmallShow()  // This version is used in the sidebar link module
{
	dayobj.setFullYear(year);
	dayobj.setMonth(month);
	dayobj.setDate(1);
	var dfirst = dayobj.getDay();
	var lastdayobj = new Date(year,month+1,0);
	var last = lastdayobj.getDate();
	var monthstr = monthmestr[month]+" / "+monthegstr[month];
	if (monthnastr[month] != monthegstr[month]) monthstr += " / "+monthnastr[month];

	var output = ""; 
	output += "<table align='center' cellpadding='1' onclick='window.open(\"index.php?option=com_daily&view=daily&Itemid=215\",\"_self\");' style='cursor:pointer;' title='تأمّل اليوم'>";
	output += "<tr align='center'>";
	output += "<td colspan='7'>"+monthstr+" "+year+"</td>";
	output += "</tr>";
	output += "<tr align='center'>";
	for (d=0; d<7; d++) output += "<td width='14%'><font size=1>"+dayabbv[d]+"</font></td>";
	output += "</tr>";
	output += "<tr align='center'>";
	for (d=1; d<=dfirst; d++) output +="<td><br /></td>"
	for (nday=1; nday<=last; nday++)
	{
		if (d % 7 == 1) output += "<tr align='center'>";
		bgcol = (nday == day ? '#f4a460' : '#ffebcd');
		if (incurrent && nday > currday) output += "<td style='background:#ddd; padding:2px;'>"+nday+"</td>";
		else output += "<td style='cursor:pointer; background:"+bgcol+"; padding:2px;'>"+nday+"</td>";
		if (d % 7 == 0) output += "</tr>";
		d++;
	}
	if (d % 7 != 1)
	{
		while (d % 7 != 1)
		{
			output += "<td><br /></td>"
			d++;
		}
		output += "</tr>";
	}
	output += "</table>";
	document.getElementById("calendar").innerHTML = output;
	return;
}

function changeDay(n)
{
	newday = n;
	month++;
	if (month < 10) month = "0"+month;
	if (newday < 10) newday = "0"+newday;
	dstr = year+"-"+month+"-"+newday;
	document.getElementById("date").value = dstr;
	document.calform.action = "index.php?option=com_daily&day="+dstr+"&Itemid=215";
	document.calform.submit();
	return;
}

function lastMonth()
{
	month--;
	if (month < 0)
	{
		month = 11;
		year--;
	}
	day = null;
	calendarShow();
	return;
}

function nextMonth()
{
	month++;
	if (month > 11)
	{
		month = 0;
		year++;
	}
	day = null;
	calendarShow();
	return;
}

