$(function () {

	P.module('collapsedSections', function () {
		var sections = {},
			curSection = null;		
		
		function Section (specs) {
			var $el = $(specs.el),
				$trigger = $(specs.trigger),
				isCollapsed = true,
				self = {};
			
			function expand () {
				isCollapsed = false;
				if (curSection !== self) collapseSelected();
				$el.slideDown();
				$trigger.addClass('selected').find('.toggle').text('-');
				curSection = self;
				return this;
			}
			
			function collapse () {
				isCollapsed = true;
				$el.slideUp();
				$trigger.removeClass('selected').find('.toggle').text('+');
				return this;
			}
			
			function toggle () {
				return !isCollapsed? collapse(): expand();
			}
			
			return P.augment(self, {
				expand: expand,
				collapse: collapse,
				toggle: toggle,
				isCollapsed: function () {
					return isCollapsed;
				},
				init: function () {
					$el[isCollapsed? 'hide': 'show']();
					$trigger.click(function () {
						toggle();
						return false;
					});
					return true;
				}()
			});
		}
			
		function addSection (name, specs) {
			sections[name] = new Section(specs);
			return this;
		}
		
		function addBatch (batch) {
			var b;
			for (b in batch) {
				addSection(b, batch[b]);
			}
			return this;
		}
		
		function collapseSelected () {
			if (curSection === null) {
				return;
			} else {
				curSection.collapse();
			}
		}
					
		return {
			addSection: addSection,
			addBatch: addBatch,
			init: function () {
				addBatch({
					faq: {
						trigger: '#faq_toggle',
						el: '#faq_section'
					},
					links: {
						trigger: '#links_toggle',
						el: '#links_section'
					}
				});
				return this;
			}()
		};
	});
	
	P.module('visualSection', function () {
		var $logoPreview = $('#visual_section_logopreview'),
			$embedCode = $('#visual_section_embed').add('label[for=visual_section_embed]'),
			$radios = $('#visual_section input[type=radio]');
			
		function updateLogo (src) {
			$logoPreview.attr('src', src);
			return this;
		}
		
		function showLogo () {
			$logoPreview.show();
			hideEmbed();
			return this;
		}
		
		function hideLogo () {
			$logoPreview.hide();
			return this;
		}
		
		function showEmbed () {
			$embedCode.show();
			hideLogo();
			return this;
		}

		function hideEmbed () {
			$embedCode.hide();
			return this;
		}
		
		function showRelevantForm (val) {
			if (val === 'logo') {
				showLogo();
			} else {
				showEmbed();
			}
			return this;
		}
			
		return {
			init: function () {
				showRelevantForm($radios.filter(':checked').val());
				$radios.click(function () {
					showRelevantForm($radios.filter(':checked').val());
				});
			}()
		};
	});
	
	P.module('replaceableFields', function () {		
		function Field (el) {
			var $el = $(el),
				firstVal = $el.val();
			$el.focus(function () {
				if ($el.val() === firstVal) {
					$(this).val('');
				}
			}).blur(function () {
				if ($el.val().length === 0) {
					$el.val(firstVal);
				}
			});
			return $el;
		}
		
		function add (el) {
			Field(el);
			return this;
		}
		
		function addBatch (els) {
			var i;
			for (i = 0; i < els.length; i++) {
				add(els[i]);
			}
			return this;
		}
		
		return {
			add: add,
			addBatch: addBatch,
			init: function () {
				addBatch([
					'#ipt_faq1_q',
					'#ipt_faq2_q',
					'#ipt_faq3_q',
					'#ipt_faq4_q'
				]);
			}()
		};
	});
	
	P.module('maxLength', function () {
		function checkLength () {
			var val = $(this).val();
			if (val.length > $(this).attr('maxlength')) {
				$(this).val(val.slice(0, $(this).attr('maxlength')));
			}
		}
		return {
			init: function () {
				$('input[maxlength]').keyup(checkLength);
				return true;
			}()
		};
	});
	
});