/*
 *	TypeWatch 2.0 - Original by Denny Ferrassoli / Refactored by Charles Christolini
 *
 *	Examples/Docs: www.dennydotnet.com
 *	
 *  Copyright(c) 2007 Denny Ferrassoli - DennyDotNet.com
 *  Coprright(c) 2008 Charles Christolini - BinaryPie.com
 *  
 *  Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
*/

(function(jQuery) {
	jQuery.fn.typeWatch = function(o){
		// Options
		var options = jQuery.extend({
			wait : 750,
			callback : function() { },
			highlight : true,
			captureLength : 2
		}, o);
			
		function checkElement(timer, override) {
			var elTxt = jQuery(timer.el).val();
		
			// Fire if text > options.captureLength AND text != saved txt OR if override AND text > options.captureLength
			if ((elTxt.length > options.captureLength && elTxt.toUpperCase() != timer.text) 
			|| (override && elTxt.length > options.captureLength)) {
				timer.text = elTxt.toUpperCase();
				timer.cb(elTxt);
			}
		};
		
		function watchElement(elem) {			
			// Must be text or textarea
			if (elem.type.toUpperCase() == "TEXT" || elem.nodeName.toUpperCase() == "TEXTAREA") {

				// Allocate timer element
				var timer = {
					timer : null, 
					text : jQuery(elem).val().toUpperCase(),
					cb : options.callback, 
					el : elem, 
					wait : options.wait
				};

				// Set focus action (highlight)
				if (options.highlight) {
					jQuery(elem).focus(
						function() {
							this.select();
						});
				}

				// Key watcher / clear and reset the timer
				var startWatch = function(evt) {
					var timerWait = timer.wait;
					var overrideBool = false;
					
					if (evt.keyCode == 13 && this.type.toUpperCase() == "TEXT") {
						timerWait = 1;
						overrideBool = true;
					}
					
					var timerCallbackFx = function()
					{
						checkElement(timer, overrideBool)
					}
					
					// Clear timer					
					clearTimeout(timer.timer);
					timer.timer = setTimeout(timerCallbackFx, timerWait);				
										
				};
				
				jQuery(elem).keydown(startWatch);
			}
		};
		
		// Watch Each Element
		return this.each(function(index){
			watchElement(this);
		});
		
	};

})(jQuery);

$(document).ready(function()
{
	var typeWatchOptions = {
		callback:function() { chkNicknameAvailability(); },
		wait:400,
		captureLength:0
	}

	$("#nickname").typeWatch(typeWatchOptions);

	$("#nickname").keypress(function() {
		$('#ajax_msg').text('');
	});
});

function chkNicknameAvailability()
{
	$('#ajax_msg').text('Checking availability...');
	$('#ajax_msg').addClass('orange');
	$('#ajax_msg').removeClass('green');
	$('#ajax_msg').removeClass('red');
	$('#submit').attr("disabled", "disabled");

	$.post("/include/ajax.php", { action: "chknname", nname: $('#nickname').val() }, function(data)
	{
		if (data == '0' || data == '-1')
		{
			if (data == '0')
				$('#ajax_msg').text('Nickname already taken');
			else
				$('#ajax_msg').text('Only letters, numbers, _ and space are allowed in nicknames.');
			
			$('#ajax_msg').addClass('red');
			$('#ajax_msg').removeClass('green');
			$('#ajax_msg').removeClass('orange');
		}
		else
		{
			$('#ajax_msg').text('Nickname available!');
			$('#ajax_msg').addClass('green');
			$('#ajax_msg').removeClass('orange');
			$('#ajax_msg').removeClass('red');
			$('#submit').removeAttr("disabled");
		}
	});
}

function vote(w_id, u_nonce)
{
	$.post("/include/ajax.php", { action: "vote", wid: w_id, nonce: u_nonce }, function(data) {
		$('#vote strong').html(data);
	});

	$('#vote_link').html('<span>voted</span>');
}
