var click_list = 0;

$(document).ready(function(){
//local server version 
$("#autoComplete").autocomplete(env_path + "/users/auto",
//live server version $("#autoComplete").autocomplete("/users/auto",
{
minChars: 2,
cacheLength: 10,
onItemSelect: selectItem,
onFindValue: findValue,
formatItem: formatItem,
 autoFill: false
 });

$("#submit_share_btn").click(function(){
	
	var user = $(".ac_input").val();
	 
	if(user.indexOf(":") != -1)
	{
		$(".ac_input").val("");
		var string = '<div class="related-strat shared-users">';
		string += '<div class="fl strat-name new-shared-user">';
		string += user
		string += '<span class="view-edit-rights"> has been added.</span>'
		string += '</div>';
		/*
		<div class="view-related-strat">
		<a data-id="13" class="share-delete" href="">Delete</a></div></div>
		*/
		if($('.no-shared-users').length != 0)
			$('.no-shared-users').remove();
		$(".added-shared-users").append(string);
	}
})
/* share modal stuff */

$("#strategy-share").click(function(){
	$(".share-strat-modal").modal({
		opacity:70,
		overlayCss: {backgroundColor:"#124F72"},
		overlayClose: true,
		persist: true,
		closeClass: "close-modal"
	})
})

});
 
function shareModalClose(){
	var added = $(".added-shared-users").html();
	$.modal.close();
	$(".newly-added-shares-sidebar").append(added);
}

function selectItem(li) {
    findValue(li);
}
 
function findValue(li) {
    if( li == null ) return alert("No match!");
 
// if coming from an AJAX call, let's use the product id as the value
    if( !!li.extra ) var sValue = li.extra[0];
 
    // otherwise, let's just display the value in the text box
	else
	{	
		var sValue = li.selectValue;
	}

//alert(sValue.indexOf(": ")) ;

$("#UserId2").val(sValue);

// make it pretty 
var string = $("#autoComplete").val();
var location = string.indexOf("<b");
string = string.substring(0,location);
$("#autoComplete").val(string);
}
 
function formatItem(row) {

	row[0] = row[0].replace("()","");
	
    if(row[1] == undefined) {
        return row[0];
    }
    else {
        return row[0] + " <span class=\"auto-complete-id\">(id: " + row[1] + ")</span>";
    }
}
