/**
*User Interface scripts copyright 2007-08 Alex Geppert alex.geppert@gmail.com
*please don't steal this code - give me work instead!
*/
var WindowRegistry =
{
	panes:[],
	/*
	The following two arrays are for functions which occur when ANY window is opened or closed
	*/
	oncloseAll:[],
	onopenAll:[],
	register:function(closeCallback,div)
	{
		this.panes.push(
		{
		close:closeCallback,
		div:div
		});
	},
	notifyOpen:function(div)
	{
		var l = this.onopenAll.length;

		for(var i = (l-1);i>-1;i--)
		{
			this.onopenAll[i]();		
		}

		for (var n = (this.panes.length-1),i=n;i>-1;i--)
		{
			var pane = this.panes[i];
			if(pane.div != div)
			{
				pane.close();
			}
		}
	},
	notifyClose:function(div)
	{
		var l = this.oncloseAll.length;
		for(var i = (l-1);i>-1;i--)
		{
			this.oncloseAll[i]();		
		}
		
	
	},
	registerGlobalOpen:function(func)
	{
		this.onopenAll.push(func);
	},
	registerGlobalClose:function(func)
	{
		this.oncloseAll.push(func);
	}

}


function DateField(containerDiv)
{
	this.container = document.getElementById(containerDiv);
}

DateField.prototype.init = function(datetime)
{
	


}

DateField.prototype.refresh = function()
{

}

function Calendar()
{
	this.DAYS=DateTime.DAYS_SHORT;
	this.MONTHS=DateTime.MONTHS_LONG;
	this.calendarDiv="dtCalendar";
	this.nextMonthDisabled = false;
	this.prevMonthDisabled = false;
	this.now = new Date();
	this.startOfWeek = 0;
	this.activeDate = new Date(this.now.getTime());
}

Calendar.prototype.nextMonthLabel = "&raquo;";
Calendar.prototype.prevMonthLabel = "&laquo;";	


Calendar.prototype.nextMonth = function()
{
	this._getMonth(true);
}

Calendar.prototype.prevMonth = function()
{
	this._getMonth(false);
}
Calendar.prototype._getMonthNavigation = function(nextmonth)
{
	var label = (nextmonth)?this.nextMonthLabel:this.prevMonthLabel;
	var enabled = ((nextmonth && !this.nextMonthDisabled)
						||(!nextmonth && !this.prevMonthDisabled));
						
	var monthNav = document.createElement("div");
	monthNav.className = (nextmonth)?"month_nav_next":"month_nav_prev";
	if(enabled)
	{
		var link = document.createElement("a");
		var calendar = this;
		if(nextmonth)
		{
			link.onclick = function()
			{
				calendar.nextMonth();
			};
		}
		else
		{
			link.onclick = function()
			{
				calendar.prevMonth();
			};
		}
		link.innerHTML = label;
		monthNav.appendChild(link);
	}
	else
		monthNav.innerHTML = label;	

	return monthNav;
}
Calendar.prototype._getMonth = function(forward)
{
	var month = ((forward)?1:-1)+this.activeDate.getMonth();
	//bugfix for end of month skipping
	this.activeDate.setDate(1);
	this.activeDate.setMonth(month);
	this.refresh();
}


/**
this is the init function
*/
Calendar.prototype.refresh = function()
{

	
	var workingDate = new Date();
	workingDate.setTime(this.activeDate.getTime());

	var cDiv = this.activeDiv;

	while(cDiv.firstChild)
	{
		cDiv.removeChild(cDiv.firstChild);			
	}
	calendarMonth = workingDate.getMonth();
	var table = document.createElement("table");
	var cTBody = document.createElement("tbody");
	table.appendChild(cTBody);
	var tr = document.createElement("tr");
	cTBody.appendChild(tr);
	td = tr.insertCell(day);
	td.colSpan = this.DAYS.length;
	td.innerHTML = this.MONTHS[calendarMonth]+" "+this.activeDate.getFullYear();
	td.className = "monthName";

	var tr = document.createElement("tr");
	cTBody.appendChild(tr);
	var width = parseInt(100/this.DAYS.length);
	var remainder = 100-(width*this.DAYS.length);

	for(var i = 0,n=this.DAYS.length;i<n;i++)
	{
		var th = document.createElement("th");
		th.setAttribute("scope","vertical");
		if(i == this.DAYS.length-1) width += remainder;
		th.setAttribute("width",width+"%");
		th.innerHTML = this.DAYS[i];
		tr.appendChild(th);
	}
	//move to first day of month
	workingDate.setDate(
	(workingDate.getDate()-((workingDate.getDate())))
		  );
	//then zero the bastard 
	workingDate.setDate(
	(workingDate.getDate()-((workingDate.getDay()))+1)
		  );
	
	for(var i=0;i<35;i++)
	{
		var day = workingDate.getDay();

		if(day == 1)
		{
			tr = document.createElement("tr");
			cTBody.appendChild(tr);
		}
		else if(day == 0) day = 7;
		day -= 1;

		td = tr.insertCell(day);

		this.addDay(td,i,workingDate);
		workingDate.setDate(workingDate.getDate()+1);

	}
	
	cDiv.appendChild(table);	
	cDiv.appendChild(this._getMonthNavigation(true));
	cDiv.appendChild(this._getMonthNavigation(false));
	

}

Calendar.prototype.addDay = function(td,index,workingDay)
{
	classname = (index & 1 == 1)? "oddMonth":"evenMonth";

	if(workingDay.getMonth() !== this.activeDate.getMonth()) 
	{
		classname = "notMonth";
	}
	else if(workingDay.getTime() == this.now.getTime())
	{
		classname = "today";
	}

	td.innerHTML = workingDay.getDate();
	//td.innerHTML  = "i"+index+"d "+workingDay.getDate()+"m "+workingDay.getMonth();
	td.className = classname;
}
/**
overrideable by child classes
*/
Calendar.prototype.init = function()
{
	this.activeDiv = document.getElementById(this.calendarDiv);
	this.activeDiv.className = "dtCalendar";
		
	this.refresh();
}


function TimeInput()
{
	this.time = null;
	this.timeValid = false;
	this.AMPM = ["AM","PM"];
	this.input = null;
	this.select = null;
}

TimeInput.prototype.appendToParent = function(parent)
{
	var thisObj = this;
	
	this.input = document.createElement("input");
	this.input.setAttribute("size",5);
	this.input.setAttribute("maxlen",5);
	this.input.className = "timeInput";	
	this.select = document.createElement("select");
	this.select.className = "timeInput";	
	
	for(var i=0;i<this.AMPM.length;i++)
	{
		option = document.createElement("option");
		option.setAttribute("value",i);
		option.innerHTML = this.AMPM[i];
		this.select.appendChild(option);		
	}
	
	this.select.onchange = function()
	{
		thisObj._setTime();
	}	

	this.input.onblur = function()
	{
		thisObj._setTime();
	}
	
	parent.appendChild(this.input); 
	parent.appendChild(this.select); 

}

TimeInput.prototype._setTime = function()
{
	this.timeValid = false;
		
	var matcher = /([1-2]?[0-9])[:\.]([0-5][0-9])/
	if(matcher.test(this.input.value))
	{
		time = matcher.exec(this.input.value);
		hours = parseInt(time[1]);
		min = parseInt(time[2]);
		if((this.select.options[this.select.selectedIndex].value == "1"))
		{
			if(hours < 12)
				hours = hours+12;
			else if(hours > 12)
			this.timeValid = false;
		}
		else if(hours == 12) hours = 0;
		
		this.time.setMinutes(min);
		this.time.setHours(hours);
		this.timeValid = true;

	}	
	else if((!isNaN(this.input.value))&&
			(this.input.value > 0) &&
			(this.input.value < 13))
	{
		this.input.value = this.input.value+":00";
		this._setTime();
	}	
	
	if(!this.timeValid)
		this.input.value = '';
}

TimeInput.prototype.setTime = function(date)
{
	this.time = date;
	this._setTime();
}
/**
Window functions

Pane - Pane x creates a container of rootDivName at the bottom of the document.
This should use a lot of crap from slideshow - ie formatting and positions 
Pane takes a callback which is used to init its content 
init/close/Callback will have the pane sent to the object for further work
*/

function Pane(initCallback, closeCallback,paneId,alignTopId,alignLeftId)
{
	this.initCallback = initCallback;
	this.closeCallback = closeCallback;
	this.paneId  = paneId;
	this.alignTopId = alignTopId;
	this.alignLeftId = alignLeftId;
	this.pane = null;
	this.content = null;
}

Pane.prototype.paneClassName = null;

Pane.prototype.show = function(e)
{	

	if(!this.pane)
	{
		this.pane = document.getElementById(this.paneId);
		if(this.pane)
		{
			this.content = this.pane.lastChild;
		}
	}
	var me = this;
	
	if(!this.pane)
	{
		this.pane = document.createElement("div");
		this.pane.setAttribute("id",this.paneId);
		this.pane.className = "pane";

		this.content = document.createElement("div");
		this.content.className = "content";
		

		var alignTop = document.getElementById(this.alignTopId);
		
		this.pane.style.top = DOMTools.getOffsetTop(alignTop)+"px";
		
		var alignLeft = ((this.alignTopId == this.alignLeftId)
			||(!this.alignLeftId))?alignTop:document.getElementById(this.alignLeftId);
		
		var left = DOMTools.getOffsetLeft(alignLeft);
		
		if((document.all)&&(!window.addEventListener))
		{
			left ++;
			
		}

		left += 2;
		
		this.pane.style.left = left+"px";

		var titleBar = document.createElement("div");
		titleBar.className = "header";
		var closeButt = document.createElement("div");		
		closeButt.className = "closeButton";
		
		var hide = function(e)
		{
			me.pane.style.display = "none";
			if(me.closeCallback)
				me.closeCallback(me);
		}
		
		WindowRegistry.register(hide,me.pane);
	
		EventUtils.attachClick(closeButt,hide);		

		titleBar.appendChild(closeButt);
		
		this.pane.appendChild(titleBar);
		this.pane.appendChild(this.content);
		this.initCallback(this,e);
		document.body.appendChild(this.pane);
					
	}
	else
	{
		this.initCallback(this,e);
	}
	WindowRegistry.notifyOpen(me.pane);	
	
	this.pane.style.display = "inline";				
	
}

