//sets up the jQueryUI datepicker
//uses the dates Array to determine the available dates.
//	all dates not in 'dates' are not available

/*
$(document).ready( function()
{
	var dates = new Array();
	dates[0] = '2010-06-01';
	dates[1] = '2010-06-02';
	dates[2] = '2010-06-03';

	$('#datepicker').NewDatePicker({
									dates : dates
								});
});
*/

(function($)
{
	$.fn.NewDatePicker = function( options )
	{
		if( options == undefined )
		{
			options = new Object();
		};
		
		var globals = options;
		if( options.dates != undefined )
		{
			globals.dates = cleanDates( options.dates );
		}
		else
		{
			globals.dates = new Array();
		};

		return this.each( function()
		{
			this.options = globals;
			var earliest_date = getEarliestDate( this.options.dates );
			$(this).datepicker({
									showTime : globals.showTime,
									time24h : globals.time24h,
									constrainInput : false,
									dateFormat : 'yy-mm-dd',
									beforeShowDay : isSelectable,
									defaultDate : earliest_date,
									duration : ''
								});

			//unset the default:
			$('a.ui-state-active').removeClass( 'ui-state-active' );
			$('a.ui-state-hover').removeClass( 'ui-state-hover' );
		});
	};

})(jQuery);


function cleanDates( dates )
{
	for( var i = 0 ; i < dates.length ; i++ )
	{
		
		dates[i] = parseISO8601( dates[i] );
		dates[i].setMinutes( dates[i].getMinutes() + dates[i].getTimezoneOffset() );
	};
	
	return dates;
}

function isSelectable( date )
{
	var selectable;
	selectable = new Array();
	
	if( this.options.dates.length != 0 )
	{
		selectable[0] = containsDate( this.options.dates , date );
	}
	else
	{
		selectable[0] = true;
	};
	
	selectable[1] = '';
	selectable[2] = '';
	
	return selectable;
}

//check if Array of Dates a contains Date b
function containsDate( dates , date )
{
	for( var i = 0 ; i < dates.length ; i++ )
	{
		if( compareDates( dates[i], date ) )
		{
			return true;
		};
	};
	
	return false;
}

function compareDates( a , b )
{
	return a.getDate() == b.getDate() && a.getMonth() == b.getMonth() && a.getYear() == b.getYear();
}

function getEarliestDate(dates)
{
	if( dates == undefined )
	{
		return new Date();
	};
	
	if( dates.length == 0 )
	{
		return new Date();
	};
	
	
	var earliest = parseISO8601( '3000-12-31' );
	if( dates.length <= 1 )
	{
		earliest = dates[0];
	}
	else
	{
		for(var i = 0; i < dates.length - 1; i++ )
		{
			if( dates[i] < earliest )
			{
				earliest = dates[i];
			};
		};
	};
	
	return earliest;
}

  /**Parses string formatted as YYYY-MM-DD to a Date object.
   * If the supplied string does not match the format, an 
   * invalid Date (value NaN) is returned.
   * @param {string} dateStringInRange format YYYY-MM-DD, with year in
   * range of 0000-9999, inclusive.
   * @return {Date} Date object representing the string.
   */
function createDateISO( dateStringInRange )
{
	return parseISO8601( dateStringInRange );
};
   
   
   
  function parseISO8601(dateStringInRange) {
    var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
        date = new Date(NaN), month,
        parts = isoExp.exec(dateStringInRange);

    if(parts) {
      month = +parts[2];
      date.setFullYear(parts[1], month - 1, parts[3]);
      if(month != date.getMonth() + 1) {
        date.setTime(NaN);
      }
    }
    return date;
  }
