// Copyright 2007 by Visibility Works, Inc. - All Rights Reserved.



var com = window.com || {};
com.visibilityworksinc = com.visibilityworksinc || {};

com.visibilityworksinc.dateclicker = function() {
	//alert( "inside com.visibilityworksinc.dateclicker" );
	
	// set up holding array
	this.selected_dates = new Array();
	
	// expect thse to be set in enter_ride.php, but initialize with some defaults
	this.bg_toggle_off = "#eeeeee";
	this.bg_toggle_on = "#609A5F";
	this.bg_highlight = "#cccc00";
	this.bg_toggle_on_highlight = "#96b32f";
	this.form_dates_field = "ride_dates";
	this.form_time_field = "ride_time";
	
	// highlight today
	var now = new Date();
	var year = now.getFullYear();
	var month = now.getMonth() +1;
	var day = now.getDate();
	
	if( month < 10 ) { month = "0" + month; }
	if( day < 10 ) { day = "0" + day; }
	this.today = "" + year + month + day;	
	this.highlight( this.today );
};

com.visibilityworksinc.dateclicker.prototype.toggle = function( at )
{
	// if date@time passed in, parse it, else read time from form_time_field
	if( at[8] == '@' ) {
		id = at.substr(0,8);
		tm = at.substr(9,8);
		this.toggleat( id, tm );
	} else {
		time_obj = document.getElementById( this.form_time_field );
		if( time_obj ) {
			this.toggleat( at, time_obj.value );
		} else {
			this.toggleat( at, null );
		}
	}
}
	
com.visibilityworksinc.dateclicker.prototype.toggleat = function( id, tm )
{
	// toggle dateclicker date and set tooltip to time value
	obj = document.getElementById( id );
	if( obj ) {
		if( this.selected_dates[id] ) {
			if( id == this.today ) {
				obj.style.backgroundColor = this.bg_highlight;
			} else {
				obj.style.backgroundColor = this.bg_toggle_off;
			}
			obj.title = '';
			this.selected_dates[id] = false;
		} else {
			if( id == this.today ) {
				obj.style.backgroundColor = this.bg_toggle_on_highlight;
			} else {
				obj.style.backgroundColor = this.bg_toggle_on;
			}
			if( tm ) {
				obj.title = this.format_time( tm );
				this.selected_dates[id] = tm;
			} else {
				this.selected_dates[id] = true;
			}
		}
		this.update_form_field( id );
	}
};

com.visibilityworksinc.dateclicker.prototype.highlight = function( id )
{
	// designed to highlight today
	obj = document.getElementById( id );
	if( obj ) {
		obj.style.borderStyle = 'solid';
		obj.style.borderWidth = '1px';
		obj.style.borderColor = this.bg_highlight;
	}
	if( this.selected_dates[id] ) {
		obj.style.backgroundColor = this.bg_toggle_on_highlight;
	} else {
		obj.style.backgroundColor = this.bg_highlight;
	}
};

com.visibilityworksinc.dateclicker.prototype.update_form_field = function( id )
{
	// update form_dates_filed = comma separated list of sorted date@time values
	var result = new Array();
	for( var i in this.selected_dates ) {
		if( this.selected_dates[i] ) {
			result[result.length] = i + "@" + this.selected_dates[i];
		}
	}
	obj = document.getElementById( this.form_dates_field );
	obj.value = result.sort();	// e.g. 20071001@18:30,20071015@17:30
	
	return true;
};

com.visibilityworksinc.dateclicker.prototype.format_time = function( tm )
{
	// convert 13:00:00 to 1:00 pm
	var hr = Number( tm.substr(0,2) );
	var mn = tm.substr(3,2);
	var ampm = 'am';
	if( hr >= 12 ) {
		ampm = 'pm';
	}
	if( hr > 12 ) {
		hr -= 12;
	}
	return "" + hr + ":" + mn + " " + ampm;
}


