/**
* jQuery username lookup plugin
* Adapted from the jpassword code here: http://www.rvdevsign.net/ressources/javascript/jpassword-plugin-jquery.html
* originally written by:	Hervé GOUCHET [ contact(at)rvdevsign(dot)net ]
*
* $("input").alookup();
*/
(function($){
	$.fn.alookup = function(settings){
		var jElements			= this;
		var settings			= $.extend({}, $.fn.alookup.defaults, settings);
		var template			= '<div class="jpassword"><div><p class="jpassword-meter">&nbsp;</p><p class="jpassword-info">&nbsp;</p></div></div>';
		
		return jElements.each(function(){
			// Manage all inputs type password
			if($(jElements).is("input")){ aLookup( $(jElements) ); }
		});
		
		// Construct password meter
		function aLookup(jInput){
			// Create tooltip
			var unikId			= "alookup_" + parseInt(Math.random()*1000);
			var jTooltip		= $(template).attr("id", unikId);
			if(settings.flat == false){
				// Define position of the tooltip
				var pos			= jInput.offset();
				var win			= getWindow();
				var dir			= "right";
				var top			= pos.top;
				var left		= (pos.left + jInput.width());
				jTooltip.appendTo(document.body);
				if((left + jTooltip.width()) > (win.left + win.width)){ left -= (jTooltip.width() + jInput.width()); dir = "left"; }
				if((top + jTooltip.height()) > (win.top + win.height)){ top -= (jTooltip.height() - (jInput.height()*1.5)); dir += "bottom"; }else{ dir += "top"; }
				jTooltip.css({ left: left + "px", top: top + "px", display: "none" });
				jTooltip.addClass("jpassword-" + dir);
			}else{
				// Insert after the input
				jTooltip.insertAfter(jInput);
				jTooltip.css({ position: "relative", display: "block" });
				jTooltip.addClass("jpassword-flat");
			}
			// Event handler
			jInput.bind("keyup", function(e){ verifyUser(jInput, jTooltip); });
			jInput.bind("focus", function(e){
				verifyUser(jInput, jTooltip);
				// Show tooltip
				if(settings.flat == false){ tooltip(jTooltip, "show"); }
				// Function called when the tooltip is shown
				if($.isFunction(settings.onShow)){ settings.onShow(jInput, jTooltip); }	
			});
			jInput.bind("blur", function(e){
				// Hide tooltip
				if(settings.flat == false){ tooltip(jTooltip, "hide"); }
				// Function called when the tooltip is hided
				if($.isFunction(settings.onHide)){ settings.onHide(jInput, jTooltip); }	
			});
			// Function called when process is completed
			if($.isFunction(settings.onComplete)){ settings.onComplete(jInput, jTooltip); }
		}
		
		// Verified username and update the progress meter
		function verifyUser(jInput, jTooltip){
			var val				= jInput.val();
			var meter			= jTooltip.find(".jpassword-meter");
			var info			= jTooltip.find(".jpassword-info");
			if (val.length == 0){
				meter.css("background-position", "0 0");
				info.html("Please enter a username<br/>A-Z, a-z, 1-9, \".\" dot, or \"_\" underscore only.");	
			} else {
				$.ajax({url: "ajaxckusername.do",
					data: "uname=" + val,
					dataType: "text",
					success: function(msg){
						var stat = msg.match(/(true|false)/);
						if (stat[0] == 'true'){
							meter.css("background-position", "0 -10px");
							info.html("Username Already Exists");
						} else {
							meter.css("background-position", "0 -30px");
							info.html("Username Available");
						}
					}});
					
			}
		}
		// Show or hide tooltip 
		function tooltip(jTooltip, effect){
			if(effect == "show"){ jTooltip.fadeIn(); }else{ jTooltip.fadeOut(); }
		}
		// Get window size
		function getWindow(){
			var m				= document.compatMode == "CSS1Compat";
			return {
				left : (window.pageXOffset || (m ? document.documentElement.scrollLeft : document.body.scrollLeft)),
				top : (window.pageYOffset || (m ? document.documentElement.scrollTop : document.body.scrollTop)),
				width : (window.innerWidth || (m ? document.documentElement.clientWidth : document.body.clientWidth)),
				height : (window.innerHeight || (m ? document.documentElement.clientHeight : document.body.clientHeight))
			};
		}
	};

	// Default settings
	$.fn.alookup.defaults = {
		flat: false,													// Add jPassword after input or show it on demand
		onShow: function(){},											// Function called when the tooltip is shown (return: jQuery of input and tooltip)
		onHide: function(){},											// Function called when the tooltip is hided (return: jQuery of input and tooltip)
		onKeyup: function(){},											// Function called when writing the password (return: jQuery of input)
		onComplete: function(){}										// Function called when the process is done (return: jQuery of input and tooltip)
	};
})(jQuery);

