/***************************************************************************
 *
 * Copyright (C) 2006 by Korgault Studios
 * http://www.kgstudios.net
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

/**
 * Controlador de calendarios
 *
 * @package	phppr0.Shared.plugins.Calendar
 * @class Calendar
 * @author Korvus
 * @date 01-01-2007
 * @version 1.0
 */

function Calendar( sObjectId )
{
	
	// ==================== Atributos ==========================
	
	this.id					=			sObjectId;
	this._handleFor			=			null;
	this._display 			=			"none";
	
	this._day				=			new Date().getDate();
	this._month				=			new Date().getMonth();
	this._year				=			new Date().getYear();
		
	this._x;
	this._y;
	
	this.images				=			{ 
										 prev:  APP_VARS[ "COMMONPATH" ] + "plugins/Calendar/images/prev.gif",
										 next:  APP_VARS[ "COMMONPATH" ] + "plugins/Calendar/images/next.gif",
										 first: APP_VARS[ "COMMONPATH" ] + "plugins/Calendar/images/first.gif",
										 last:  APP_VARS[ "COMMONPATH" ] + "plugins/Calendar/images/last.gif"
										};
	
	// ================ Metodos SET / ADD ======================
	
	this.setDate = function( nDay, nMonth, nYear )
	{
	 this._day = parseInt( nDay.length == 2 && nDay.charAt( 0 ) == "0" ? nDay.charAt( 1 ) : nDay );
	 this.setMonth( parseInt( nMonth.length == 2 && nMonth.charAt( 0 ) == "0" ? nMonth.charAt( 1 ) : nMonth ) - 1 );
	 this.setYear( nYear );
	 
	 /**
	  * Si se ha definido un objeto sobre el que se trabaja, guardamos la fecha en dicho objeto
	  */
	 
	 if( typeof( this._handleFor ) == "object" )
	 {
	  this._handleFor.value  = ( this.getYear() < 10 ? "0" + this.getYear() : this.getYear() ) + "-";
	  this._handleFor.value += ( this.getMonth() < 9 ? "0" + ( this.getMonth() + 1 ) : ( this.getMonth() + 1 ) ) + "-";
	  this._handleFor.value += this._day < 10 ? "0" + this._day : this._day;
	 
	  /**
	   * Si el objeto asociado al calendario posee el evento 'onchange', lo invocamos
	   */
	 
	  if( typeof( this._handleFor.onchange ) == "function" )
	   this._handleFor.onchange();
	 }
	
	 /**
	  * Oculta el calendario
	  */

	 this.hide();
	};
	
	// Setea el mes activo, teniendo en cuenta el overflow, y recalculando
	// el aņo y mes correctos en funcion del mes indicado por 'nMonth'
	
	this.setMonth = function( nMonth )
	{ 	
	 if( nMonth >= 12 )
	 { this._year += parseInt( ( nMonth + 1 ) % 12 ); nMonth = 0; }
	 
	 if( nMonth < 0 )
	 { this._year += parseInt( nMonth % 12 ); nMonth = 11; }

	 this._month = nMonth; 
	 this.refresh(); 
	};
	
	this.setYear = function( nYear )
	{ this._year = parseInt( nYear ); this.refresh(); };
	
	this.setHandleFor = function( oHandleFor )
	{ this._handleFor = oHandleFor; };

	// ====================== Metodos GET ======================

	this.getDay   = function(){ return( parseInt( this._day ) ); };	
	this.getMonth = function(){ return( parseInt( this._month ) ); };
	this.getYear  = function(){ return( parseInt( this._year ) ); };
	this.getHandleFor = function(){ return( this._handleFor ); };

	// ==================== Otros metodos ======================

	this.show = function( nLeft, nTop )
	{
	 if( typeof( this._node ) != "object" )
	  this._node = document.getElementById( this.id );
	 
	 if( this.getHandleFor().value.match( new RegExp( "[0-9]{4}\-[0-9]{2}\-[0-9]{2}" ) ) )
  	 {
	  this.setDate( this._handleFor.value.split( "-" )[ 2 ], this._handleFor.value.split( "-" )[ 1 ], this._handleFor.value.split( "-" )[ 0 ] );	 
	  this.getHandleFor().blur();
	 }
	 
	 this._node.style.display = this._display = "block";
	 this._node.style.left = this._x = new String( nLeft + "px" );
	 this._node.style.top = this._y = new String( nTop + "px" );
	};
	
	this.hide = function()
	{ this._node.style.display = this._display = "none"; };
	
	/**
	 * Actualiza el contenido del calendario, llamando al propio metodo
	 * 'toString'. La propiedad 'outerHTML' solo funcion en internet explorer
	 */
	
	this.refresh = function()
	{ this._node.innerHTML = this.toString( true );	};
	
	this.toString = function( bIgnoreParentNode )
	{ 
	 var oCalendarBody = new Array();
	 var oCalendarHeader = new Array();
	 
 	 var nFirstDay = new Number();
 	 var nLastDay = new Date( this.getYear(), this.getMonth(), 31 )
	 var oTempDate = new Date( this.getYear(), this.getMonth(), 1 );
 
 	 nLastDay  = ( nLastDay.getDate() != 31 ) ? ( 31 - nLastDay.getDate() ) : nLastDay.getDate();
 	 nFirstDay = ( oTempDate.getDay() == 0 ) ? 7 : oTempDate.getDay();
 
 	 for( var i = 0; i < nFirstDay - 1; i++ )
	 {
	  oCalendarBody.push( "<td>" );
	  oCalendarBody.push( "</td>" );	
	 }
	 
 	 for( var j = 1; j <= nLastDay; j++, i++)
	 {
	  if( i == 7 )
	  { oCalendarBody.push( "</tr><tr>" ); i = 0; } 
	
	  oCalendarBody.push( '<td class="phppr0-plugins-Calendar-day-' + ( j == this._day ? "selected" : "default" ) + '" onclick="getObject( \'' + this.id + '\' ).setDate( \'' + j + '\', \'' + ( this.getMonth() + 1 ) + '\', \'' + this.getYear() + '\' );">' + j + '</td>' );	
	 }
	 
	 if( bIgnoreParentNode != true )
	  oCalendarHeader.push( '<div id="' + this.id + '" style="left: ' + this._x + '; top: ' + this._y + '; position: absolute; display: ' + this._display + '" onclick="getObject( \'' + this.id + '\' ).hide();">' );
	 
	 oCalendarHeader.push( '<table class="phppr0-plugins-Calendar" cellspacing="1" cellpadding="2"><tr><td colspan="7">' );
	 oCalendarHeader.push( '<table class="phppr0-plugins-Calendar-header" width="100%" cellspacing="0" cellpadding="0" style="height: 100%;">' );
	 oCalendarHeader.push( '<tr><td class="phppr0-plugins-Calendar-selector" onclick="getObject( \'' + this.id + '\' ).setYear( ' + ( this.getYear() - 1 ) + ' ); if( window.event ){ window.event.cancelBubble = true; }"><img src=\"' + this.images[ "first" ] + '\" /></td>' );
	 oCalendarHeader.push( '<td class="phppr0-plugins-Calendar-selector" onclick="getObject( \'' + this.id + '\' ).setMonth( ' + ( this.getMonth() - 1 ) + ' ); if( window.event ){ window.event.cancelBubble = true; }"><img src="' + this.images[ "prev" ] + '" /></td>' );
	 oCalendarHeader.push( '<td class="phppr0-plugins-Calendar-title" style="width: 100%;" align="center">' + ( this.getMonth() + 1 ) + '/' + this.getYear() + '</td>' );
	 oCalendarHeader.push( '<td class="phppr0-plugins-Calendar-selector" onclick="getObject( \'' + this.id + '\' ).setMonth( ' + ( this.getMonth() + 1 ) + ' ); if( window.event ){ window.event.cancelBubble = true; }"><img src="' + this.images[ "next" ] + '" /></td>' );
	 oCalendarHeader.push( '<td class="phppr0-plugins-Calendar-selector" onclick="getObject( \'' + this.id + '\' ).setYear( ' + ( this.getYear() + 1 ) + ' ); if( window.event ){ window.event.cancelBubble = true; }"><img src="' + this.images[ "last" ] + '" /></td>' );
	 oCalendarHeader.push( '</tr></table></td></tr>' + oCalendarBody.join( "" ) + '</table>' );
	 
	 if( bIgnoreParentNode != true )
	  oCalendarHeader.push( '</div>' );
				
	 return( oCalendarHeader.join( "" ) );
	};
}