/**
 * Client-site support for the Concert page
 *
 * @author Artem Ploujnikov <artem@htc.ca>
 */
Site.Concert = Class.create(	
	{
		/**
		* The price of a single ticket
		*
		* @var Number
		* @access private
		*/
		ticketPrice : 1,
		
		/**
		* The ID of the ticket purchase form 
		*
		* @var string
		* @access public
		*/
		formId : null,
		
		/**
		* The ticket purchase form
		*
		* @var HTMLElement
		* @access private
		*/
		form : null,
		
		
		/**
		* Creates a new concert object and attaches it
		* to the UI elements
		*
		* @param string formId the ID of the purchase form
		*/
		initialize : function(formId) {
			this.formId = formId;
			this.form = $(formId);
			this.attachEvents();
		},
		
		/**
		* Sets the price of a single ticket ticket to be used
		* in calculations
		*
		* @param Number ticketPrice the new ticket price
		*/
		setTicketPrice : function(ticketPrice) {
			this.ticketPrice = ticketPrice;
		},
		
		/**
		* Attaches event handlers to concert-specific UI
		* elements; namely, the price calculator.
		*/
		attachEvents : function() {
			Event.observe(
				this.form.elements.number_of_tickets,
				'keyup',
				this.updatePrice.bindAsEventListener(this)
			);
						
			Event.observe(
				this.form.elements.number_of_tickets,
				'change',
				this.updatePrice.bindAsEventListener(this)
			);			
		},
		
		/**
		* Returns the price for the number of tickets the user
		* has decided to purchase
		*
		* @return Number
		*/
		getPrice : function() {
			var price = this.ticketPrice * this.form.elements.number_of_tickets.value;
			return isNaN(price) ? null : Site.Format.currency(price);
		},
		
		/**
		* Recalculates the price and updates the corresponding
		* UI element
		*/
		updatePrice : function() {
			var price = this.getPrice();
			if (price === null) {
				price = '';
			}
			$(this.formId + '-calculated-price').update(price);
			
		}
	}
);