SJUSERSCHOOSERS_INSTANCE_COUNTER = 0;
SJUSERSCHOOSERS_INSTANCE_RESULTS = null;
SJUSERSCHOOSERS_INSTANCE_CURRENT = null;
SJUSERSCHOOSERS_CURRENT_ID		 = 0;
SJUSERSCHOOSERS_INSTANCES		 = new Object();
SJUSERSCHOOSERS_HOST			 = document.getElementById('RJHost').getAttribute('href');
SJUSERSCHOOSER_LAST_ACTIVE		 = 'Search';
_L_SJUSERSCHOOSERS_BUTTON_SINGLE = 'Choose user';
_L_SJUSERSCHOOSERS_BUTTON_MULTI	 = 'Choose users';
_L_SJUSERSCHOOSERS_LABEL_NONE	 = 'Nobody selected.';
_L_SJUSERSCHOOSERS_LABEL_SINGLE	 = '%1 selected.';
_L_SJUSERSCHOOSERS_LABEL_MULTI   = 'Multiple users selected.';
_L_SJUSERSCHOOSERS_DIALOG_TITLE	 = 'Choose an user / multiple users';
_L_SJUSERSCHOOSERS_LABEL_LOADING = 'Loading...';

dojo.require("dijit.Dialog");

function SJUsersChooser(selectedValues)
{	
	this.host				= document.getElementById('RJHost').getAttribute('href');
	this.imgHost			= this.host;
	if(this.host.substr(this.host.length - 8) == 'control/')
	{
		this.imgHost = this.host.substr(0, this.host.length - 8);
	}

	this.chooserButton;
	this.dialog;

	this.instanceID			= ++SJUSERSCHOOSERS_INSTANCE_COUNTER;
	this.noneIcon			= this.imgHost + 'lib/js/sjtools/res/images/user.none.png';
	this.singleIcon			= this.imgHost + 'lib/js/sjtools/res/images/user.single.png';
	this.multiIcon			= this.imgHost + 'lib/js/sjtools/res/images/user.multi.png';
	this.allowMultiple		= false;
	this.labelInstance		= null;
	this.iconInstance		= null;

	SJUSERSCHOOSERS_INSTANCES[this.instanceID] = this;

	var button = _L_SJUSERSCHOOSERS_BUTTON_SINGLE;
	if(this.allowMultiple)
	{
		button = _L_SJUSERSCHOOSERS_BUTTON_MULTI;
	}
	
	var label = _L_SJUSERSCHOOSERS_LABEL_NONE;
	if(selectedValues != undefined && selectedValues != false)
	{		
		if(SJ.isArray(selectedValues))
		{
			if(selectedValues.length > 1)
			{
				label = _L_SJUSERSCHOOSERS_LABEL_MULTI;
			}
		}
	}
	
	this.open = function() {
		SJUSERSCHOOSER_LAST_ACTIVE = 'Search';
		SJUSERSCHOOSERS_CURRENT_ID = this.instanceID;	
		
		this.dialog		   = dijit.byId('SJUsersChooserDialog_'+this.instanceID);
		this.dialog.show();
		this.dialog.attr('href', this.host+'system/users_chooser.service?view_model=phtml&instance_id='+this.instanceID);
		SJUC.focusTrigger(this.instanceID);
	};
	
	this.print = function() {
		
		document.write('<div title="'+_L_SJUSERSCHOOSERS_DIALOG_TITLE+'" dojoType="dijit.Dialog" class="SJUsersChooser_Dialog" id="SJUsersChooserDialog_'+this.instanceID+'">Test</div>');
		document.write('<div class="SJUsersChooser_View"><img id="SJUsersChooser_Image_'+this.instanceID+'" src="'+this.noneIcon+'" alt="" /><div id="SJUsersChooser_Label_'+this.instanceID+'" class="SJUsersChooser_Label">'+label+'</div><a onclick="return false;" id="SJUsersChooserButton_'+this.instanceID+'" href="">'+button+'</a></div>');
		
		this.chooserButton = document.getElementById('SJUsersChooserButton_'+this.instanceID);
		this.labelInstance = dojo.byId('SJUsersChooser_Label_'+this.instanceID);
		this.iconInstance  = dojo.byId('SJUsersChooser_Image_'+this.instanceID);
		
		dojo.connect(this.chooserButton, "onclick", this, "open");
	}
	
	this.close = function() {
		this.dialog.hide();
	}
	
	this.selectUser = function(id, name) {
		this.labelInstance.innerHTML = name;
		this.iconInstance.src		 = this.singleIcon;
	}
}

function SJUCInstance() {
	
	this.callUser = function(userID, userName) {
		var chooser = SJUSERSCHOOSERS_INSTANCES[SJUSERSCHOOSERS_CURRENT_ID];
		chooser.selectUser(userID, userName);
		chooser.close();
	};
	
	this.callContent = function(content)
	{
		var prefix = 'SJUsersChooserContent_';
		dojo.removeClass(dojo.byId(prefix+SJUSERSCHOOSER_LAST_ACTIVE), 'SJUsersChooserActiveContent');
		dojo.removeClass(dojo.byId('SJUsersChooser_Tab_'+SJUSERSCHOOSER_LAST_ACTIVE), 'Active');
		dojo.addClass(dojo.byId(prefix+content), 'SJUsersChooserActiveContent');
		dojo.addClass(dojo.byId('SJUsersChooser_Tab_'+content), 'Active');
		SJUSERSCHOOSER_LAST_ACTIVE = content;
		return false;
	}
	
	this.focusTrigger = function(instanceID) {
		if(!dojo.byId('SJUsersChooser_UserSearch_'+instanceID))
		{
			window.setTimeout('SJUC.focusTrigger('+instanceID+')', 250);
			return;
		}
		SJUSERSCHOOSERS_INSTANCE_RESULTS = dojo.byId('SJUsersChooser_UserResults_'+instanceID);
		SJUSERSCHOOSERS_INSTANCE_CURRENT = dojo.byId('SJUsersChooser_UserSearch_'+instanceID);
		SJUSERSCHOOSERS_INSTANCE_CURRENT.focus();
		dojo.connect(SJUSERSCHOOSERS_INSTANCE_CURRENT, 'onkeyup', SJUC.searchUser);
	}
	
	this.searchUser = function(e) {
		console.log(dojo.attr(SJUSERSCHOOSERS_INSTANCE_CURRENT, 'value'));
		SJUC.searchUserByTerm(dojo.attr(SJUSERSCHOOSERS_INSTANCE_CURRENT, 'value'));
	}
	
	this.searchUserByTerm = function(term) {
		SJUC.startLoading();
		dojo.xhrGet({
			url: SJUSERSCHOOSERS_HOST+'system/search_user.service?term='+term,
			handleAs: 'json',
			load: function(response, ioArgs) {
				SJUC.stopLoading();
				if(response['status'] == 1)
				{
					var id = dojo.attr(SJUSERSCHOOSERS_INSTANCE_CURRENT, 'id');
					for(user in response['users'])
					{
						SJUSERSCHOOSERS_INSTANCE_RESULTS.innerHTML+= '<div><a href="javascript:SJUC.callUser(\''+response['users'][user]['id']+'\',\''+response['users'][user]['name']+'\');">'+response['users'][user]['name']+'</a></div>';
					}
				}
				else
				{
					SJUSERSCHOOSERS_INSTANCE_RESULTS.innerHTML = '<div>'+response['message']+'</div>';
				}
				return true;
			},
		});
	}
	
	this.startLoading = function() {
		SJUSERSCHOOSERS_INSTANCE_RESULTS.innerHTML = '<div>'+_L_SJUSERSCHOOSERS_LABEL_LOADING+'</div>';
	}
	
	this.stopLoading = function() {
		SJUSERSCHOOSERS_INSTANCE_RESULTS.innerHTML = '<div></div>';
	}
}

var SJUC = new SJUCInstance();
