/***********************************************************************************************************************
 File		: AutoCalendar.TmplBased.js
 File Type	: Javascript (Internet Explorer > 5.5)

 Copyright 	: xtendo technologies Kurt Schwedes, 76185 Karlsruhe, Germany
			  All rights reserved
.......................................................................................................................
 Created		: 11.01.06
 Author			: Kurt Schwedes, xtendo technologies
 Last Change	: 12.01.06
.......................................................................................................................

 Create monthlx calendar based on a template
 
.......................................................................................................................
 Develeped for 	: xtendo technologies Kurt Schwedes, 76185 Karlsruhe, Germany
 Contact		: kurt.schwedes@xtendo.de
 Documentation 	: ---
***********************************************************************************************************************/
// DEFINITIONS
try {
/*----------------------------------------------------------------------------------------------------------------------
 CHECK PACKAGE REQUIREMENTS
----------------------------------------------------------------------------------------------------------------------*/

if (! window.TmplObj) throw ('Required common JS package "TmplObj" not available!');

/*----------------------------------------------------------------------------------------------------------------------
 CLASS CONSTANTS/DEFINITIONS
----------------------------------------------------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------------------------------------------------
 CONSTRUCTOR OF CLASS, INHERITANCE
----------------------------------------------------------------------------------------------------------------------*/

function ACTB (def)
{
	if (! def || typeof def != 'object') def = {};
	this.CLASS	= ACTB;
	
	this.date 			= def.date 				|| new Date();
	this.day			= 0;
	
	this.tmpl 			= def.tmpl 				|| null;
	this.container 		= def.container 		|| null;
	this.table			= null;
	
	this.fmtMonth 		= def.fmtMonth 			|| '%M %Y';
	this.fmtDay 		= def.fmtDay 			|| '%e';
	this.fmtWeek 		= def.fmtWeek 			|| '%v';
	this.firstDayOfWeek = def.firstDayOfWeek 	|| this.date.firstDayOfWeek;
	
	this.tokenId		= def.tokenId 			|| 'idDay';
	this.tokenCss		= def.tokenCss 			|| 'cssDay';
	this.tokenTxt		= def.tokenTxt 			|| 'txtDay';
	
	this.prefixId		= def.prefixId 			|| 'day';
	this.todayId		= def.todayId 			|| 'today';
	
	this.cssWeekday		= def.cssWeekday 		|| '';
	this.cssSunday	 	= def.cssSunday 		|| '';
	this.cssSaturday	= def.cssSaturday 		|| '';
	this.cssHolidays	= def.cssHolidays 		|| '';
	
	this._build();
	return this;
}

/*----------------------------------------------------------------------------------------------------------------------
 PUBLIC OBJECT METHODS
----------------------------------------------------------------------------------------------------------------------*/

ACTB.prototype.create = function ()
{
	with (this){
		if (tmpl && typeof tmpl == 'object' && tmpl.CLASS == window.TmplObj){
			// initial day number
			var iday = date.getDate();
			// actual day number
			var today = TODAY.getSerial();
			
			tmpl.output = '';
			var w = tmpl.sectors.Week;
			if (w){
				date.setDate(1);
				var pos = date.getDay();
				var last = date.getLastDayOfMonth();
				w.resetTokens();
				for (var i = 0; i < last; i++){
					if (pos == firstDayOfWeek){
						w.tokens.cw = date.byFormat(fmtWeek);
						if (i) {
							w.resolveTokens();
							tmpl.resolveSector('Week', true);
							w.resetTokens();
						}
					}
					
					date.setDate(i + 1);
					w.tokens[tokenId + pos] = (today == date.getSerial() ? todayId : '') || (prefixId + (i + 1));
					w.tokens[tokenCss + pos] = (pos == 5 ? cssSaturday : pos == 0 ? cssSunday : '') || cssWeekday || 'none';
					w.tokens[tokenTxt +pos] = date.byFormat(fmtDay);
					pos++;
					pos = pos % 7;
				}
				w.tokens.cw = date.byFormat(fmtWeek);
				w.resolveTokens();
				tmpl.resolveSector('Week', true);
			}

			tmpl.tokens.month = date.byFormat(fmtMonth);
			tmpl.resolveTokens();

			if (container){
				container.innerHTML = tmpl.output;
				container.style.display = 'block'
				table = container.getElementsByTagName('TABLE')[0];
			}
			
			date.setDate(iday);
		}
	}
	return this;
}

ACTB.prototype.showNext = function ()
{
	with (this){
		if (! date) date = new Date();
		_changeMonth(true);
		create();
	}
	return this;
}

ACTB.prototype.showPrevious = function ()
{
	with (this){
		if (! date) date = new Date();
		_changeMonth(false);
		create();
	}
	return this;
}

ACTB.prototype.showToday = function ()
{
	with (this){
		date = new Date();
		create();
	}
	return this;
}

/*----------------------------------------------------------------------------------------------------------------------
 PRIVATE OBJECT METHODS
----------------------------------------------------------------------------------------------------------------------*/

ACTB.prototype._build = function ()
{
	with (this){
		if (tmpl && typeof tmpl == 'object' && tmpl.innerHTML){
			var t = tmpl;
			tmpl = new TmplObj({content: t.innerHTML});
			if (! container){
				container = t;
				t.innerHTML = '';
			}
		}
	}
	return this;
}

ACTB.prototype._changeMonth = function (up)
{
	with (this){
		if (!day) day = date.getDate();
		var m = date.getMonth();
		var y = date.getFullYear();

		if (up) {
			m += 1;
			if (m > 11) {
				y += 1;
				m = 0;
			}
		}
		else {
			m -= 1;
			if (m < 0) {
				y -= 1;
				m = 11;
			}
		}
		date.setDate(1);
		date.setMonth(m);
		date.setFullYear(y);
	}
	return this;
}

/*----------------------------------------------------------------------------------------------------------------------
 GERNERIC ERROR HANDLER
----------------------------------------------------------------------------------------------------------------------*/
}
catch(e) {
	if (typeof e == 'object')
		e = e.description;
	alert ('[Notifications] INTERNAL ERROR:\n' + e);
}
