/*
 * tokeninput-facebook.js
 *
 * Copyright (c) 2010 NextMark, Inc.
 * 2 Buck Road, Suite 8, Hanover, NH 03755, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * NextMark, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with NextMark.
 *
 * $Id: tokeninput-facebook.js,v 1.4 2011-07-22 16:23:20 jsmith Exp $
 *
 * Description: 
 * Defines a wrapper function around jQuery tokenInput() that does two things:
 * 1. Configures the UI style to look like facebook's
 * 2. Prevents tokens from being added twice to the list of selections (like facebook's)
 *
 * Function Parameters:
 *   selector - jQuery selector for inputs to initialize
 *   url - a URL for the ajax request, which should return a JSON string
 *   initialJSONValue - (optional) the initial JSON value to populate in the input
 *   hintText (optional) - The text to display as a hint
 *
 * Example Usage:
 * $(document).ready(function() {
 *     initTokenInput("input[name=myTokenInputName]","/url/to/ajax_handler_json.jsp");
 * });
 *
 * $(document).ready(function() {
 *     initTokenInput("input","/ajax_json.jsp",'[{"id":"US","name":"United States"}]','Enter a country');
 * });
 *
 */

function initTokenInput(selector, url, initialJSONValue, hintTxt) {
    $(selector).tokenInput(url, {
	prePopulate: (initialJSONValue ? initialJSONValue : []),
	hintText: hintTxt,
	classes: {
	    // this stuff makes it look like facebook.
	    tokenList: "token-input-list-facebook",
	    token: "token-input-token-facebook",
	    tokenDelete: "token-input-delete-token-facebook",
	    selectedToken: "token-input-selected-token-facebook",
	    highlightedToken: "token-input-highlighted-token-facebook",
	    dropdown: "token-input-dropdown-facebook",
	    dropdownItem: "token-input-dropdown-item-facebook",
	    dropdownItem2: "token-input-dropdown-item2-facebook",
	    selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
	    inputToken: "token-input-input-token-facebook"
	} 
	/*
	    THIS IS NOT WORKING PROPERLY YET

	, onResult: function(results) {
	    // remove values already set from results
	    var selected = $(selector).val();
	    for (var i = results.length - 1; i >= 0; i--) {
		if (selected.indexOf(results[i].id) != -1) {
		    results.splice(i,1);
		}
	    }
	    return results;
	}
	*/
    });
    
    
}    

function initTokenInputNarrow(selector, url, initialJSONValue, hintTxt) {
    $(selector).tokenInput(url, {
	prePopulate: (initialJSONValue ? initialJSONValue : []),
	hintText: hintTxt,
	classes: {
	    // this stuff makes it look like facebook.
	    tokenList: "token-input-list-facebook-narrow",
	    token: "token-input-token-facebook",
	    tokenDelete: "token-input-delete-token-facebook",
	    selectedToken: "token-input-selected-token-facebook",
	    highlightedToken: "token-input-highlighted-token-facebook",
	    dropdown: "token-input-dropdown-facebook-narrow",
	    dropdownItem: "token-input-dropdown-item-facebook",
	    dropdownItem2: "token-input-dropdown-item2-facebook",
	    selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
	    inputToken: "token-input-input-token-facebook"
	} 
	/*
	    THIS IS NOT WORKING PROPERLY YET

	, onResult: function(results) {
	    // remove values already set from results
	    var selected = $(selector).val();
	    for (var i = results.length - 1; i >= 0; i--) {
		if (selected.indexOf(results[i].id) != -1) {
		    results.splice(i,1);
		}
	    }
	    return results;
	}
	*/
    });

    
}

