/**
* UI support for the Auction page, which makes it possible to
* dynamically add auction items
*/
Site.Auction = Class.create(
	{
		/**
		* The ID of the purchase form
		* 
		* @var string
		* @access public
		*/ 
		formId : null,
		
		/**
		* The purchase form to which the auction
		* support object is bound
		*
		* @var HTMLElement
		* @access public
		*/
		form: null,
		
		/**
		* The index of the last auction item appearing on
		* the form
		*
		* @var Number
		* @access private
		*/
		lastItemIndex : 0,
		
		/**
		* Creates a new auction support object
		* and attaches it to a form
		*
		* @param string formId the ID of the form to which the
		* 	support object will be attached
		*/
		initialize : function(formId) {
			this.formId = formId;
			this.form = $(formId);
			this.attachEvents();
		},
		
		/**
		* Attaches event handlers to UI elements
		*/
		attachEvents : function() {
			Event.observe(
				this.getAddLink(),
				'click',
				function(event) {
					Event.stop(event);
					this.addItem();
				}.bind(this)
			);
            Event.observe(
                this.getContactMethodPhone(),
                'click',
                function(event) {
                    this.setContactMethod('phone');
                }.bind(this)
            );
            Event.observe(
                this.getContactMethodEmail(),
                'click',
                function(event) {
                    this.setContactMethod('email');
                }.bind(this)
            );
		},
		
		/**
		* Returns the container for auction items
		* on the auction form
		*
		* @access public
		*/
		getItemsContainer : function() {
			return $(this.formId + '-items');
		},
		
        getContactMethodPhone : function(){
            return $(this.formId + '-contact-method-phone');
        },
        
        getContactMethodEmail : function(){
            return $(this.formId + '-contact-method-email');
        },
        
        setContactMethod : function(method) {
            $(this.formId + '-contact-method').value = method;
        },
        
		/**
		* Returns the link that will be used to
		* add new items
		*
		* @access public		
		*/
		getAddLink : function() {
			return $(this.formId + '-add-item-link');
		},
		
		/**
		* Adds an item at the bottom of the form
		*
		* @access public		
		*/
		addItem : function() {
			this.lastItemIndex++;
			var container = this.getItemsContainer();
			
			var descriptionRow = container.insertRow(container.rows.length);
			this.buildDescriptionRow(descriptionRow);
			
			var valueRow = container.insertRow(container.rows.length);
			this.buildValueRow(valueRow);
						
		},
		
		/**
		* Returns the full name of an item-specific field
		*
		* @param string name a partial field name
		*
		* @return string
		* @access public
		*/
		getFieldName : function(name) {
			return 'items[' + this.lastItemIndex + '][' + name + ']';
		},
		
		
		/**
		* Builds the row for the description of the auction item
		*
		* @param HTMLElement row an empty table row
		*
		* @access private
		*/
		buildDescriptionRow : function(row) {
			var labelCell = row.insertCell(0);
			$(labelCell).addClassName('label-form');
			$(labelCell).update('Item Description');
			
			var fieldCell = row.insertCell(1);
			$(fieldCell).addClassName('field');
			var field = document.createElement('textarea');		
			field.name = this.getFieldName('description');
			field.rows = 8;
			field.cols = 50;
			fieldCell.appendChild(field);
		},
		
		/**
		* Builds the row for the approximate value of the auction item
		*
		* @param HTMLElement row an empty table row
		*
		* @access private
		*/
		buildValueRow : function(row) {
			var labelCell = row.insertCell(0);
			$(labelCell).addClassName('label-form');
			$(labelCell).update('Approximate Value');
			
			var fieldCell = row.insertCell(1);
			$(fieldCell).addClassName('field');
			var field = document.createElement('input');
			field.type = 'text';
			field.name = this.getFieldName('value');
			field.size = 10;
			field.maxlength = 10;
			fieldCell.appendChild(field);
		}
		
	}
);