/**
 * TextMarks JavaScript Support Library for selecting keywords.
 * ---------------------------------------------------------------------------
 *
 * Requires jQuery.
 *
 * ---------------------------------------------------------------------------
 * Author: Dan Kamins [d k a m i n s A T t e x t m a r k s D O T c o m]
 * ---------------------------------------------------------------------------
 * Copyright (c) 2010, TextMarks Inc.
 * ---------------------------------------------------------------------------
 */
/** */



// ---------------------------------------------------------------------------


/**
 * Class for dynamic keyword availability checking.
 */
TextMarks_LiveKeywordCheckClient = function () {

	this.seqnum = 0;
	this.url_ajax_keyword_check = '';

	this.input_updated_pre_ajax = function(keyword) {
		// Override this function for page-specific hooks. Called with new keyword prior to making AJAX request. Return false to abort.
	}
	this.input_updated_post_ajax = function(data) {
		// Override this function for page-specific hooks. Called with updated results.
	}
	this.input_updated = function(keyword) {
		this.seqnum++;
		var local_respond_to_seqnum = this.seqnum;
		if (!this.input_updated_pre_ajax(keyword)) { 
			return;
		}
		var lkcc = this;
		function ajax_response_handler(data) {
			if (lkcc.seqnum != local_respond_to_seqnum) {
				return; // (newer callback pending, so ignore this old one)
			}
			function update_prep(data) {
				$('#kwstatus').show();
				$('#kwstatus_head').hide();
				$('#kwstatuses > span').hide();
				$('#kwsuggests_head').hide();
			}
			function update_status_head(data) {
				$('#kw').text(data['body']['keyword']);
				$('#kwstatus_head').show();
			}
			function update_status(data) {
				var status_jq = $('#kwstatuses > #st_UNKNOWN');
				if (data['body']['status'].length) {
					status_jq = $('#kwstatuses > #st_' + data['body']['status']);
					if ((data['body']['status'] == 'available') && (keyword.length <= 4)) {
						status_jq = $('#kwstatuses > #st_available_4char');
					}
				}
				status_jq.show();
			}
			function update_suggestions(data) {
				if (data['body']['suggestions']) {
					$('#kwsuggests').empty();
					for (var s = 0; s < data['body']['suggestions'].length; ++s) {
						$('#kwsuggests').append('<li>' +data['body']['suggestions'][s]+ '</li>');
					}
					$('#kwsuggests_head').show();
					$('#kwsuggests').show();
				} else {
					$('#kwsuggests').hide();
				}
			}
			function input_updated_post_ajax(data) {
				lkcc.input_updated_post_ajax(data);
			}
			update_prep(data);
			if (data['head']['rescode'] != 0) { 
				return; // (error)
			}
			update_status_head(data);
			update_status(data);
			update_suggestions(data);
			input_updated_post_ajax(data);
		}
		$.getJSON(this.url_ajax_keyword_check + "?keyword="+escape(keyword), ajax_response_handler);
	}
	
	this.last_keyword = "";
	this.heartbeat = function() {
		var new_keyword = document.getElementById('id_keyword').value;
		if (new_keyword != this.last_keyword) {
			this.last_keyword = new_keyword;
			this.input_updated(new_keyword);
		}
	}

	this.startbg = function() {
		$('#kwstatuses > span').hide();
		$('#kwsuggests').hide();
		var th = this;
		window.setInterval(function() { th.heartbeat(); }, 500);
	}
}



// ---------------------------------------------------------------------------

/** 
 * Setup JS on page for registering a new keyword (live availability check).
 */
function tm_choose_keyword_setup(url_ajax_keyword_check) {
	$('#id_keyword').keyup(function() {
		$(this).val($(this).val().toUpperCase());
		$(this).val($(this).val().replace(' ', ''));
	});
	$('#id_keyword').focus();

	var lkcc = new TextMarks_LiveKeywordCheckClient();
	lkcc.url_ajax_keyword_check = url_ajax_keyword_check;
	lkcc.input_updated_pre_ajax = function(keyword) { 
		if (keyword == '') {
			$('#kwstatus').hide();
			$("#img_result").removeClass();
			$('.result_block').hide();
			$('#result_intro').fadeIn(500);
			return false;
		}
		$("#img_result").removeClass().addClass("loading");
		return true;
	}
	lkcc.input_updated_post_ajax = function(data) { 
		var status = data['body']['status'];
		$('.result_block').hide();
		if (status == 'available') {
			$("#img_result").removeClass().addClass("tally-check");
			$('#result_good').show();
			$('#result_description').show();
			$('.submit_keyword').removeAttr("disabled");
		} 
		// else if (status == 'authusers') { }
		else {
		 	$("#img_result").removeClass().addClass("tally-x");
			$('#result_bad').show();	
			$('#result_description').hide();
			$('.submit_keyword').attr("disabled","disabled");
		}
	}
	lkcc.startbg();
	return lkcc;
}


