﻿function PhLargeCalendar( parent, width, height, current_date, on_date_select )
{
    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( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
    this.m_dtCurDate	= current_date;
    this.m_oCurDate		= null;
    this.m_oDatesTable	= null;
    this.m_btnPrevYear	= null;
    this.m_btnPrevMonth	= null;
    this.m_btnNextMonth	= null;
    this.m_btnNextYear	= null;
    
    this.m_oParent			= document.getElementById( parent );
    this.m_fpDateSelected	= on_date_select;
    
    this.InitObject();
}

PhLargeCalendar.prototype = new PhControl();

PhLargeCalendar.prototype.InitObject = function()
{
    this.m_oContainer   = document.createElement( 'div' );
    this.m_oMonth       = document.createElement( 'div' );
    this.m_oControls    = document.createElement( 'div' );
    this.m_oDates       = document.createElement( 'div' );

    if( this.m_oContainer != null )
    {
        this.m_oContainer.style.width   = this.m_iWidth+'px';
        this.m_oContainer.style.height  = this.m_iHeight+'px';
        
        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 );
    }
}

PhLargeCalendar.prototype.InitHeader = function()
{
	this.m_oMonth.setAttribute( 'id', 'large_calendar_month' );
    this.m_oMonth.innerHTML = this.m_strMonths[ this.m_dtCurDate.getMonth() ]+' '+this.m_dtCurDate.getFullYear(); 
    
	g_oClient.DisableSelection( this.m_oMonth );
}

PhLargeCalendar.prototype.InitControls = function()
{
	var me		= this;
	var t		= new PhTable( 1, 5, 0, 0 );
	var path	= 'system/controls/calendars/images/';
	
	t.Table.style.width		= '227px';
	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+'todayBtn.jpg', 
										   path+'todayBtn_over.jpg', 
										   path+'todayBtn_down.jpg', 152, 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 );
}

PhLargeCalendar.prototype.InitDates = function()
{
	this.m_oDates.setAttribute( 'id', 'large_calendar_dates' );
	this.m_oDatesTable = new PhTable( 6, 7, 0, 3 );
	
	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 );
}

PhLargeCalendar.prototype.GetSelectedDate = function()
{
	return this.m_dtCurDate;
}

PhLargeCalendar.prototype.ResetDate = function()
{
	this.m_dtCurDate = new Date();
	
	this.InitHeader();
	this.InitDates();
}

PhLargeCalendar.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();
}

PhLargeCalendar.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();
}

PhLargeCalendar.prototype.AddDays = function( date, number_of_days )
{
	var temp = new Date( date );
	temp.setDate( date.getDate()+number_of_days );
	
	return temp;
}

PhLargeCalendar.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';
	
	if( this.m_fpDateSelected != null )
		this.m_fpDateSelected( elm.date );
}