var PricingUi = function(){
	return {
		uid: '',
		pricing: {},
		states: {},
		data: {},
		currentPrice: 0,
		files: 0,
		
		init: function(pricing, uid)
		{
			this.uid = uid;
			this.pricing = pricing;
			var self = this;
			
			$.each(this.pricing, function(setName, set){
				self.states[setName] = {};
				
				$.each(set.options, function(optionName, option){
					self.states[setName][optionName] = false;
				});
			});
		},
		
		serialize: function(){
			this.data.client = $("#input_client").attr('value');
			this.data.client_email = $("#input_client_email").attr('value');
			this.data.summary = $("#input_summary").attr('value');
			this.data.uid = this.uid;
			
			var self = this;
			$.each(this.states, function(set, row){
				self.data[set] = row;
			});
		},
		
		validate: function(){
			this.serialize();
			var messages = new Array();
			
			if( this.data.client == '' )
			{
				messages.push('You must type in your name.');
			}
			
			if( this.data.client_email == '' )
			{
				messages.push('You must type in your email address.');
			}
			
			if( this.data.summary == '' )
			{
				messages.push('Please type in some information about your project.');
			}
			
			var self = this;
			
			$.each(this.states, function(setName, set){
				var hasOne = false;

				$.each(set, function(optionName, option){
					if( option == true )
					{
						hasOne = true;
					}
				});
				
				if( self.pricing[setName]['required'] == true && hasOne == false )
				{
					if( setName == 'service' )
					{
						messages.push('You must select a service.');
					}
					else
					{
						messages.push('You must select a ' + setName + '.');
					}
				}
			});
			
			//$("#errors").slideUp(1000, function(){
				if(messages.length == 0)
				{
				}
				else
				{
					var messagesString = '';
					
					$.each(messages, function(mid, msg){
						messagesString += '<li>' + msg + '</li>';
					});
					
					$("#errors ul").html(messagesString);
					$.fn.colorbox({href: "#errors", open: true, transition: "fade", inline: true});
				}
			///});
			
			return (messages.length == 0);
		},
		
		send: function(){
			this.serialize();
			var self = this;
			
			var qs = "";
			
			$.each(this.data, function(key, value){
				if( typeof(value) == 'string' || typeof(value) == 'number' )
				{
					qs += '&' + key + '=' + escape(value);
				}
				else
				{
					$.each(value, function(childkey, childvalue){
						qs += '&' + key + '[' + childkey + ']=' + escape(childvalue);
					});
				}
			});
			
			qs = qs.substring(1);
			
			$.post('public/index.php/index/ajax-send', qs, function(data, textStatus){
				if( textStatus == 'success' && data.success == true )
				{
					self.resetForm();
										
					$.each(self.states, function(set, options){
						$.each(options, function(option, value){
							self.deactivate(set, option);
						});
					});
					
					$.fn.colorbox({href: '#success', open: true, transition: "fade", inline: true});
				}
				else
				{
					$("#errors").html("<li>We've been unable to send your request.</li>")
					$.fn.colorbox({href: '#errors', open: true, transition: "fade", inline: true});
				}
			}, 'json');
		},
		
		toggle: function(set, option)
		{
			if( this.states[set][option] == true )
			{
				this.deactivate(set, option);
			}
			else
			{
				this.activate(set, option);
			}
		},
		
		activate: function(set, option)
		{
			// IE Workaround
			//$('#input_' + set + '_' + option).css('filter', 'alpha(opacity=50)');
			
			if( this.states[set][option] == false )
			{	
				$('#input_' + set + '_' + option).fadeTo("normal", 0.5);
				this.currentPrice += this.pricing[set]['options'][option]['price'];
			}
			
			if( this.pricing[set]['options'][option]['multiple'] == false )
			{
				var self = this;
				$.each(this.pricing[set].options, function(optionName, optionRow){
					self.deactivate(set, optionName);
				});
			}
			else
			{
				self = this;
				
				$.each(this.pricing[set].options, function(optionName, optionRow){
					if( optionRow['multiple'] == false )
					{
						self.deactivate(set, optionName);
					}
				});
			}
			
			this.states[set][option] = true;
			this.renderPrice();
		},
		
		deactivate: function(set, option)
		{
			if( this.states[set][option] == true )
			{
				$('#input_' + set + '_' + option).fadeTo("normal", 1);
				this.states[set][option] = false;
				this.currentPrice = this.currentPrice - this.pricing[set].options[option].price;
			}
			
			this.renderPrice();
		},
		
		resetForm: function(sel){
			  $(':input', $(sel)).each(function() {
				  var type = this.type;
				  var tag = this.tagName.toLowerCase();
				  if (type == 'text' || type == 'password' || tag == 'textarea')
				    this.value = "";
				  else if (type == 'checkbox' || type == 'radio')
				    this.checked = false;
				  else if (tag == 'select')
				    this.selectedIndex = -1;
				   });
		},
		
		attachClearOnFocus: function(sel){
			$(sel).focus(function(){
				if( this.value == this.defaultValue )
				{ 
					this.value = "";
				} }).blur(function(){
					if( !this.value.length )
					{
						this.value = this.defaultValue;
					}
				}); 
		},
		
		attachSubmit: function(){
			var self = this;
			
			$("#form").submit( function(evt){
				var valid = PricingUi.validate();

				if( valid )
				{
					if( self.files > 0 )
					{
						$("#input_uploader").uploadifyUpload();
					}
					else
					{
						self.send();
					}
				}
				
				evt.stopPropagation();
				return false;
			} );
		},
		
		renderPrice: function(){
			$("#realtime_price").html("$" + this.currentPrice);
		},
		
		renderUploader: function(){
			var self = this;
			
			$("#input_uploader").uploadify({
				'uploader'       : './js/uploadify.swf',
				'script'         : 'public/index.php/index/ajax-upload',
				'scriptData'     : {'uid': this.uid},
				'cancelImg'      : './imgs/cancel.png',
				'queueID'        : 'fileQueue',
				'folder'         : 'uploads/' + this.uid,
				'auto'           : false,
				'multi'          : true,
				'simUploadLimit' : 1,
				'onAllComplete'  : function(event, data){
					self.send();
				},
				'onSelect'       : function(){
					self.files += 1;
				},
				'onCancel'       : function(){
					self.files -= 1;
				}
			});
		}
	};
}();