﻿var g_oCurCal = null;

function PhDropDownCalendar( width, height )
{
	this.m_iLeft		= 0;
	this.m_iTop			= 0;
	this.m_iWidth		= width;
	this.m_iHeight		= height;

    this.m_oMonth       = null;
    this.m_oControls    = null;
    this.m_oDates       = null;
    
    this.m_strMonths	= new Array( 'Jan', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.' );
    this.m_dtCurDate	= new Date();
	this.m_iTimeoutId   = 0;
	this.InitObject();
}

PhDropDownCalendar.m_oCurCal	= null;
PhDropDownCalendar.prototype	= new PhControl();

PhDropDownCalendar.prototype.InitObject = function()
{
	var me		= this;
	var wrapper	= null;
	
	this.m_oContainer	= document.createElement( 'div' );
    this.m_oMonth       = document.createElement( 'div' );
    this.m_oControls    = document.createElement( 'div' );
    this.m_oDates       = document.createElement( 'div' );
    wrapper				= document.createElement( 'div' );
    
	if( this.m_oContainer != null )
	{
		this.m_oContainer.style.position	= 'absolute';
		this.m_oContainer.style.left		= '0px';
		this.m_oContainer.style.top			= '0px';
		this.m_oContainer.style.width		= this.m_iWidth+'px';
		this.m_oContainer.style.height		= this.m_iHeight+'px';
		this.m_oContainer.style.visibility	= 'hidden';
		this.m_oContainer.className			= 'png';
		this.m_oContainer.zIndex			= 1;
		
		this.m_oContainer.style.border		= 'solid 2px #AAAAAA';
		this.m_oContainer.style.backgroundColor	= '#FEFEFE';
		
		this.InitHeader();
		this.InitControls();
		this.InitDates();
		
        this.m_oContainer.appendChild( this.m_oMonth );
        this.m_oContainer.appendChild( this.m_oControls );
        this.m_oContainer.appendChild( this.m_oDates );
        
        g_oClient.AttachEvent( this.m_oContainer, 'mouseout', function(){me.HideCalendar();} );	
        g_oClient.AttachEvent( this.m_oContainer, 'mouseover', function(){me.ShowCalendar();} );
        g_oClient.DisableSelection( this.m_oContainer );
	}
}

PhDropDownCalendar.prototype.AttachToElement = function( parent )
{
	if( this.m_oContainer == null )
	{
		alert( 'control not yet created' );
		return;
	}
	
	var me			= this;
	this.m_oParent	= (typeof( parent ) == 'string')? document.getElementById( parent ) : parent;
	
	if( this.m_oParent != null )
	{
		g_oClient.AttachEvent( parent, 'focus', function(){me.ShowCalendar(); me.HideCalendar();} );
		g_oClient.AttachEvent( parent, 'click', function(){me.ShowCalendar(); me.HideCalendar();} );
		//g_oClient.AttachEvent( parent, 'blur', function(){me.HideCalendar();} );
		g_oClient.AttachEvent( parent, 'keydown', function(e){me.HandleTab(e?e:event);} );
		
		document.getElementsByTagName( 'body' )[0].appendChild( this.m_oContainer );
	}
	else
		alert( 'failed to find parent object ('+parent+')' );
}

PhDropDownCalendar.prototype.HandleTab = function( evt )
{
	var key = (evt.keyCode || evt.charCode || evt.which || 0 );
	
	if( key == 9 )
		this.HideCalendarNow();
}

PhDropDownCalendar.prototype.ShowCalendar = function()
{
	var pos	= g_oClient.GetElementPosition( this.m_oParent );
	
	if( g_oCurCal != null )
		g_oCurCal.HideCalendarNow();
		
	g_oCurCal = this;
			
	this.m_iLeft	= (pos.X+this.m_oParent.clientWidth)-this.m_iWidth-0;
	this.m_iTop		= pos.Y+this.m_oParent.clientHeight+10;	
	
	clearTimeout( this.m_iTimeoutId );
	
	this.m_oContainer.style.top			= this.m_iTop+'px';
	this.m_oContainer.style.left		= this.m_iLeft+'px';
	this.m_oContainer.style.visibility	= 'visible';
}

PhDropDownCalendar.prototype.HideCalendar = function()
{
	var me = this;
	
	if( this.m_iTimoutId != 0 )
	{
		clearTimeout( this.m_iTimoutId );
		this.m_iTimeoutId = 0;
	}
	
	if( this.m_oContainer != null )
		this.m_iTimeoutId = setTimeout( function(){me.HideCalendarNow();}, 1000 );
}

PhDropDownCalendar.prototype.HideCalendarNow = function()
{
	this.m_oContainer.style.visibility	= 'hidden';
}

PhDropDownCalendar.prototype.InitHeader = function()
{
	this.m_oMonth.setAttribute( 'id', 'calendar_month' );
    this.m_oMonth.innerHTML = this.m_strMonths[ this.m_dtCurDate.getMonth() ]+' '+this.m_dtCurDate.getFullYear(); 
    
	g_oClient.DisableSelection( this.m_oMonth );
}

PhDropDownCalendar.prototype.InitControls = function()
{
	var me		= this;
	var t		= new PhTable( 1, 5, 0, 0 );
	var path	= 'system/controls/drop_down_calendar/images/';
	
	t.Table.style.width		= '200px';
	t.Table.style.margin	= 'auto';
	
	this.m_btnPrevYear	= new PhImgButton( path+'prevYearBtn.jpg', 
										   path+'prevYearBtn_over.jpg', 
										   path+'prevYearBtn_down.jpg', 25, 22,
										   function(){me.IncrementYear( -1 );} );
								   	
	this.m_btnPrevMonth = new PhImgButton( path+'prevMonthBtn.jpg', 
										   path+'prevMonthBtn_over.jpg', 
										   path+'prevMonthBtn_down.jpg', 25, 22, 
										   function(){me.IncrementMonth( -1 );} );
										   
	this.m_btnToday		= new PhImgButton( path+'todaySmlBtn.jpg', 
										   path+'todaySmlBtn_over.jpg', 
										   path+'todaySmlBtn_down.jpg', 101, 22, 
										   function(){me.ResetDate();} );										   
		
	this.m_btnNextMonth = new PhImgButton( path+'nextMonthBtn.jpg', 
										   path+'nextMonthBtn_over.jpg', 
										   path+'nextMonthBtn_down.jpg', 25, 22,
										   function(){me.IncrementMonth( 1 );} );
																				
	this.m_btnNextYear	= new PhImgButton( path+'nextYearBtn.jpg', 
										   path+'nextYearBtn_over.jpg', 
										   path+'nextYearBtn_down.jpg', 24, 22,
										   function(){me.IncrementYear( 1 );} );
										   							   	
	this.m_btnPrevYear.AttachToElement( t.Cells[0][0] );					   
	this.m_btnPrevMonth.AttachToElement( t.Cells[0][1] );
	this.m_btnToday.AttachToElement( t.Cells[0][2] );
	this.m_btnNextMonth.AttachToElement( t.Cells[0][3] );		
	this.m_btnNextYear.AttachToElement( t.Cells[0][4] );							   
	
	this.m_oControls.appendChild( t.Table );
}

PhDropDownCalendar.prototype.InitDates = function()
{
	this.m_oDates.setAttribute( 'id', 'calendar_dates' );
	this.m_oDatesTable = new PhTable( 6, 7, 0, 2 );
	
	this.m_oDatesTable.Table.style.width		= '200px';
	this.m_oDatesTable.Table.style.margin	= 'auto';	
	
	var me	= this;
	var i	= 0;
	var j	= 0;
	
	var cur_date = new Date( (this.m_dtCurDate.getMonth()+1)+'/1/'+this.m_dtCurDate.getFullYear() );
	
	if( cur_date.getDay() != 0 )
		cur_date = this.AddDays( cur_date, -1*(cur_date.getDay()) );
	
	for( i = 0; i < 6; i++ )
	{
		for( j = 0; j < 7; j++ )
		{
			this.m_oDatesTable.Cells[i][j].innerHTML	= cur_date.getDate();
			this.m_oDatesTable.Cells[i][j].onclick		= function(){me.OnDateSelected( this );};
			this.m_oDatesTable.Cells[i][j].date			= cur_date;	
				
			if( cur_date.getMonth() != this.m_dtCurDate.getMonth() )
				this.m_oDatesTable.Cells[i][j].className = 'other_month';
				
			else if( cur_date.getDay() == 0 || cur_date.getDay() == 6 )
				this.m_oDatesTable.Cells[i][j].className = 'weekend';
				
			else
				this.m_oDatesTable.Cells[i][j].className = 'cur_month';
				
			if( cur_date.getFullYear() == this.m_dtCurDate.getFullYear() &&
				cur_date.getMonth() == this.m_dtCurDate.getMonth() &&
				cur_date.getDate() == this.m_dtCurDate.getDate() )
			{
				this.m_oDatesTable.Cells[i][j].className += ' today';
				this.m_oCurDate = this.m_oDatesTable.Cells[i][j];
			}				
			
			cur_date = this.AddDays( cur_date, 1 );
		}
	}
	
	g_oClient.RemoveElement( this.m_oDates.getElementsByTagName( 'table' )[0] );
	this.m_oDates.appendChild( this.m_oDatesTable.Table );
	
	g_oClient.DisableSelection( this.m_oDates );
}

PhDropDownCalendar.prototype.ResetDate = function()
{
	this.m_dtCurDate = new Date();
	
	this.InitHeader();
	this.InitDates();
}

PhDropDownCalendar.prototype.IncrementYear = function( count )
{
	var month	= this.m_dtCurDate.getMonth();
	var year	= this.m_dtCurDate.getFullYear();
	var day		= this.m_dtCurDate.getDate();
	
	year += count;
		
	this.m_dtCurDate.setFullYear( year, month, day );
	
	this.InitHeader();
	this.InitDates();
}

PhDropDownCalendar.prototype.IncrementMonth = function( count )
{
	var month	= this.m_dtCurDate.getMonth();
	var year	= this.m_dtCurDate.getFullYear();
	var day		= this.m_dtCurDate.getDate();
	
	month += count;
	
	if( month < 0 )
	{
		year--;
		month += 12;
	}
		
	if( month > 11 )
	{
		year++
		month -= 12;
			
	}
		
	this.m_dtCurDate.setFullYear( year, month, day );
	
	this.InitHeader();
	this.InitDates();
}

PhDropDownCalendar.prototype.AddDays = function( date, number_of_days )
{
	var temp = new Date( date );
	temp.setDate( date.getDate()+number_of_days );
	
	return temp;
}

PhDropDownCalendar.prototype.OnDateSelected = function( elm )
{
	this.m_oCurDate.className = this.m_oCurDate.className.split( ' ' )[0];	
	
	this.m_dtCurDate	= elm.date;
	this.m_oCurDate		= elm;
	this.m_oCurDate.className += ' today';
	
	this.m_oParent.value = (this.m_dtCurDate.getMonth()+1)+'/'+this.m_dtCurDate.getDate()+'/'+this.m_dtCurDate.getFullYear();
	this.HideCalendarNow();
	
	this.m_oParent.focus();
}

PhDropDownCalendar.prototype.GetSelectedDate = function()
{
	return this.m_dtCurDate;
}