var LOL = (function() {
	var bind = {};
	var lol = {};
	
	bind.inputClick = function() {
		$.each($('.textInput input'), function() {
			var $this = $(this),
				label = $('label[for=' + $this.attr('name') + ']');
			
			if ($this.val() !== '') {
	            label.css('visibility', 'hidden');
	        }
			
			$this.focus(function() {
	            label.css('visibility', 'hidden');
	        });
	        $this.blur(function() {
	            if ($this.val() === '') {
	                label.css('visibility', 'visible');
	            }
	        });
		});
	};
	
	bind.labelClick = function() {
		$.each($('label'), function() {
			var $this = $(this),
				textInput = $('input[name=' + $this.attr('for') + ']');
			
			$this.click(function() {
				textInput.focus();
			});
			
		});
	};
	
	bind.selectBox = function() {
		$('select').easySelectBox();
	};
	
	bind.searchSubmit = function() {
		var searchForms = $('form[name=recipe_search]');
		
		searchForms.submit(function(e) {
			e.preventDefault();
			var form = $(this),
				action = '/search/?',
				params = '',
				keyword = form.find('#name').val(),
				param = '';
			
			// add the value for each select box to the param string
			$.each($(this).find('select'),function() {
				var $this = $(this),
					currentVal = $this.val(),
					// assumes the first option is a placeholder
					defaultVal = $this.find('option').eq(0).text();
					
				if(currentVal!==defaultVal) {
					param = $this.attr('name')+'='+currentVal;
					params += (params!=='') ? '&'+param : param;
				}
			});
			
			// add the keyword string to the param string
			if(keyword!=='') {
				param = 'name='+keyword;
				params += (params!=='') ? '&'+param : param;
			}
			
			// redirect to the search page, passing params to the url
			window.location = action + params;
		});
	};
	
	bind.tabs = function() {
		$("ul.tabs").tabs("div.panes > div", { initialIndex: 0 });
	};
	
	bind.nav = function() {
		var url = document.URL.split('/').slice(3).join(''),		
		navLinks = $('#nav > a');
		navLinks.removeClass('active');
		$.each(navLinks,function() {
			var $this = $(this),
				justLink = $this.attr('href').replace(/\//g,'');
			if(justLink==url) {
				$this.addClass('active');
			}
		});
	};
	
	// document ready
	lol.docReady = function() {
		bind.inputClick();
		bind.labelClick();
		bind.selectBox();
		bind.searchSubmit();
		bind.tabs();
		bind.nav();
	};
	
	return lol;
	
})();

$(function() {
	LOL.docReady();
});

