/*
Copyright (c) 2011 Chris Abrams, http://chrisabrams.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the ÒSoftwareÓ), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ÒAS ISÓ, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

(function($) {
	$.fn.entry = function(options) {	
		
		var defaults = {
			delegate: '', //The container that houses the input that we will be checking
			filepath: '', //The path to the file we will have check the DB for the entry
			input: false, //Is the return element an input?
			returnelem: '',
			returntext: '',
			speed: 1000
		};
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var input = '#' + $(this).attr('id'); //Get the ID of the input the plugin was attached to

			var typewatch = (function() {
				var timer = 0;
				return function(callback, ms) {
					clearTimeout (timer);
					timer = setTimeout(callback, ms);
				}  
			})();
			
			$(options.delegate).delegate(input, 'keyup', function() {			  	
			  	var userinput = $(this).val(); //Get the value of the input
			  	
			  	typewatch(function () {
					$.post(options.filepath,{userinput: userinput}, 
					function(theResponse) {
						
						switch(parseInt(theResponse)) {
							case 0:
								$(options.returnelem).css('color', '#f00').html('The ' + options.returntext + ' is not available.');
								break;
								
							case 1:
								$(options.returnelem).css('color', '#008000').html('The ' + options.returntext + ' is available!');
								break;
								
							case 2:
								$(options.returnelem).css('color', '#f00').html('The ' + options.returntext + ' is invalid.');
								break;
						}
						
						/*if(parseInt(theResponse) == 1) {//Entry is available
							$('#returnwrap').removeClass();
							$('#returnwrap').addClass('available');
							var returnResponse = '<i>' + userinput + '</i> is available!';
						} else {//Entry is not available
							$('#returnwrap').removeClass();
							$('#returnwrap').addClass('unavailable');
							var returnResponse = '<i>' + userinput +  '</i> is not available.';
						}

						if(options.input == true) {//If return element is an input
							$(options.returnelem).attr('value',returnResponse);
						} else {//If return element is a container such as a div or span
							$(options.returnelem).html(returnResponse); //Send the value to the mimic element
						}*/
						//return parseInt(theResponse);
						//return theResponse;
					});
			  	}, options.speed);
			});
		});
	};
})(jQuery);
