/**
 * 
 * @param {Object} local_conf
 */
jQuery.fn.nickAvailabilityCheck = function(local_conf) {
	//
    var conf = {};

	conf.triggerJQ = jQuery([]);
	conf.stateJQ = jQuery([]);

	conf.triggerText = '';
	conf.stateText = '';

	conf.checkAjaxUrl = null;

	conf.states = {
		loading: true,
		free: true,
		invalid: true,
		reserved: true,
		error: true};

	conf.loadingState = 'loading';
	conf.errorState = 'error';

	//
	var standard = {};

	standard.states = {
		loading: {
			inputClass: 'loading',
			stateText: '',
			stateClass: 'loading'},
		free: {
			inputClass: 'free',
			stateText: '',
			stateClass: 'free'},
		invalid: {
			inputClass: 'invalid',
			stateText: '',
			stateClass: 'invalid'},
		reserved: {
			inputClass: 'reserved',
			stateText: '',
			stateClass: 'reserved'},
		error: {
			inputClass: 'error',
			stateText: '',
			stateClass: 'error'}};

	//
	var inputJQ = this;
	var parentJQ = inputJQ.parent().get(0);

	var triggerJQ = jQuery([]);
	var stateJQ = jQuery([]);
	
	var inputClasses = '';
	var stateClasses = '';
	
	/**
	 * 
	 */
	var init = function (local_conf) {
		conf = jQuery.extend(conf, local_conf);

		//			
		if(typeof(conf.triggerJQ) !== 'undefined' && typeof(conf.triggerJQ.length) !== 'undefined' && conf.triggerJQ.length === 0) {
			triggerJQ = jQuery('<a>').attr('href', '').addClass('trigger').html(conf.triggerText).insertAfter(inputJQ);
		}
		else {
			triggerJQ = conf.triggerJQ.wrap(jQuery('<a>').attr('href', ''));
		}

		if(typeof(conf.stateJQ) !== 'undefined' && typeof(conf.stateJQ.length) !== 'undefined' && conf.stateJQ.length === 0) {
			stateJQ = jQuery('<div>').addClass('state').html(conf.stateText).insertAfter(triggerJQ);
		}
		else {
			stateJQ = conf.stateJQ;
		}

		//
		for(var state in conf.states) {
			if(typeof(standard.states[state]) !== 'undefined') {
				if(typeof(conf.states[state]) === 'object' || conf.states[state] === true) {
					conf.states[state] = jQuery.extend(true, standard.states[state], conf.states[state]);
				}
				else {
					delete conf.states[state];
				}
			}

			inputClasses += inputClasses.length === 0 ? '' : ' ';
			inputClasses += conf.states[state].inputClass;

			stateClasses += stateClasses.length === 0 ? '' : ' ';
			stateClasses += conf.states[state].stateClass;
		}

		//	
		triggerJQ.bind(
			'click',
			function(){
				check();
				return false;
			});
	}
	
	/**
	 * 
	 */
	var check = function () {
		inputJQ
			.removeClass(inputClasses)
			.addClass(conf.states[conf.loadingState].inputClass);

		stateJQ
			.show()
			.removeClass(stateClasses)
			.addClass(conf.states[conf.loadingState].stateClass)
			.html(conf.states[conf.loadingState].stateText);														

        jQuery.ajax({
            type: 'GET',
            url: conf.checkAjaxUrl,
            data: {nick: inputJQ.val()},
            dataType: 'json',
            success: function (json, textStatus) {
				inputJQ
					.removeClass(inputClasses)
					.addClass(conf.states[json.state].inputClass);

				stateJQ
					.removeClass(stateClasses)
					.addClass(conf.states[json.state].stateClass)
					.html(conf.states[json.state].stateText);														
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
				inputJQ
					.removeClass(inputClasses)
					.addClass(conf.states[conf.errorState].inputClass);
				
				stateJQ
					.show()
					.removeClass(stateClasses)
					.addClass(conf.states[conf.errorState].stateClass)
					.html(conf.states[conf.errorState].stateText);														
            }
		});
	}
	
	//
	jQuery(document).ready(
    	function () {
        	init(local_conf);
    	}
    );			
}

