/**
*	@package		Generic CMS, (GCMS)
*	@subpackage		Javascript
*	@author			Ben Sekulowicz-Barclay
*	@copyright		Copyright 2008, Outside Line.
*	@version		9.01
*
************************************************************************************************************************ **/

var gcms_formTimestamp = Class.create({	
	
	vars: { format: 'html' },
	
	/* ****************************************************************************************************************** */
	
	initialize: function(format) {
		this.vars.format = (format != null)? format: this.vars.format;
		
		Event.observe(window, 'load', function(e) {
		
			// Get all of the YMD timestamp fields
			$$('input.DOB').each(function(i) {
				// Change the type of this input ... BROWSER SNIFFING MADNESS!
				(Prototype.Browser.IE == true)? i.hide(): i.setAttribute('type', 'hidden');
				
				// Update the file information
				this.drawTimestampInfo(i);	
					
			}.bind(this));					
		}.bindAsEventListener(this));
	},
	
	/* *********************************************************************************************************************
	
	DRAW
	
	********************************************************************************************************************* */
	
	drawTimestampInfo: function(i) {
		// Define what type of SELECT we are using ...
		if (i.hasClassName('gcms_formTimestampYmdhi')) {
			i.setAttribute('dates', 'ymdhi');
		} else {
			i.setAttribute('dates', 'ymd');	
		}
		
		// Construct an AJAX request to get the file information ...
   		new Ajax.Updater(i, site_url + this.vars.format + '/ajax/getFormTimestampInfoRegistration', {
   			insertion: Insertion.Before,
			parameters: { dates: i.getAttribute('dates'), name: i.getAttribute('name'), value: i.getAttribute('value') },
   			onComplete: function() {
				
				// Attach events - Select Existing File
				$$('select[rel=' + i.getAttribute('name') + ']').each(function(s) {					
					s.observe('change', this.onChangeSelect.bindAsEventListener(this));					
				}.bind(this));
								
			}.bind(this)
   		});
	},
	
	/* *********************************************************************************************************************
	
	CHANGE
	
	********************************************************************************************************************* */
	
	onChangeSelect: function(e) {
		// Find the trigger
		var s = Event.findElement(e, 'select');
		
		// Build up our value string based on the new value ...
		var v = $$('select[name=' + s.getAttribute('rel') + '_y]').first().value;
		v += '-';
		v += $$('select[name=' + s.getAttribute('rel') + '_m]').first().value;
		v += '-';
		v += $$('select[name=' + s.getAttribute('rel') + '_d]').first().value;
		v += ' ';
		v += $$('select[name=' + s.getAttribute('rel') + '_h], input[name=' + s.getAttribute('rel') + '_h]').first().value;
		v += ':';
		v += $$('select[name=' + s.getAttribute('rel') + '_i], input[name=' + s.getAttribute('rel') + '_i]').first().value;
		v += ':00';		
		
		// update the (now) hidden field ...
		$$('input[name=' + s.getAttribute('rel') + ']').first().setAttribute('value', v);
	}
	
	/* ****************************************************************************************************************** */
});
