function RadEnumerationMaskPart(opts)
{

	this.SetOptions(opts);	
	this.lastOffsetPunched = -1;
	this.selectedForCompletion = 0;
	this.FlipDirection = 0;

	
	this.RebuildKeyBuff();
};



RadEnumerationMaskPart.prototype = new RadMaskPart();

RadEnumerationMaskPart.prototype.SetOptions = function (opts)
{
	this.length = 0;
	this.Options = opts;
	this.optionsIndex = [];
	for (var i = 0; i < this.Options.length; i ++)
	{
		this.length = Math.max(this.length, this.Options[i].length);
		this.optionsIndex[this.Options[i]] = i;
	}
}

RadEnumerationMaskPart.prototype.CanHandle = function ()
{
	return true;
};


RadEnumerationMaskPart.prototype.SetController = function (controller)
{
	this.controller = controller;
	this.InitializeSelection(controller.AllowEmptyEnumerations);
};

RadEnumerationMaskPart.prototype.InitializeSelection = function(allowEmptyEnumerations)
{
	if (allowEmptyEnumerations)
	{
		this.value = "";
		this.selectedIndex = -1;
	}
	else
	{
		this.value = this.Options[0];
		this.selectedIndex = 0;	
	}
};

RadEnumerationMaskPart.prototype.RebuildKeyBuff = function ()
{
	this.keyBuff = [];
	for (i = 0; i < this.length; i ++)
	{
		this.keyBuff[i] = "";
	}
	
	this.keyBuffRebuilt = true;
};

RadEnumerationMaskPart.prototype.IsCaseSensitive = function ()
{
	return true;
};

RadEnumerationMaskPart.prototype.ShowHint = function (hintContainer)
{
	var instance = this;
	
	for (var i = 0; i < this.Options.length; i++)
	{
		var optLink = document.createElement("a");
		
		optLink.index = i;
		optLink.onclick = function()
		{
			instance.SetOption(this.index);
			instance.controller.Visualise();
			return false;
		}
		
		optLink.innerHTML = this.Options[i];
		optLink.href = "javascript:void(0)";
		
		hintContainer.appendChild(optLink);
	}
	
	return true;
};

RadEnumerationMaskPart.prototype.ResetCompletion = function()
{
	this.selectedForCompetion = 0;
}

RadEnumerationMaskPart.prototype.SelectNextCompletion = function()
{
	this.selectedForCompletion ++;
}

RadEnumerationMaskPart.prototype.Store = function (value, offset)
{
	if (this.lastOffsetPunched == offset)
	{
		if (this.keyBuff[offset] == value)
		{
			this.SelectNextCompletion();
		} 
		else
		{
			this.RebuildKeyBuff();
		}
	}
	else
	{
		this.ResetCompletion();
	}
	
	this.lastOffsetPunched = offset;
	this.keyBuff[offset] = value;
}

RadEnumerationMaskPart.prototype.SetNoCompletionValue = function()
{
	if (this.controller.AllowEmptyEnumerations)
	{
		this.SetOption(-1);
	}
}

RadEnumerationMaskPart.prototype.SetValue = function (value, offset)
{
	offset -= this.offset;
	
	this.Store(value, offset);
	
	var completionList = new CompletionList(this.Options, this.PromptChar);
	var completions = completionList.GetCompletions(this.keyBuff, offset);
	
	if (completions.length > 0)
	{
		var selectedItemIndex = this.optionsIndex[completions[this.selectedForCompletion % completions.length]];
		this.SetOption(selectedItemIndex);
	} else
	{
		this.SetNoCompletionValue();
		return false;
	}

	return true;
};


RadEnumerationMaskPart.prototype.GetVisValue = function ()
{
	var v = this.value;
	while (v.length < this.length)
	{
		v += this.PromptChar;
	}
	
	return v;
};

RadEnumerationMaskPart.prototype.GetLength = function ()
{
	return this.length;
};

RadEnumerationMaskPart.prototype.GetSelectedIndex = function ()
{
	return this.selectedIndex;
};

RadEnumerationMaskPart.prototype.SetOption = function (index, up)
{
	var oldValue = this.value;
	
	if (this.controller.AllowEmptyEnumerations)
	{
		if (index < -1)
		{
			index = this.Options.length + index + 1;
			this.FlipDirection = -1;
		}
		else if (index >= this.Options.length)
		{
			
			index = index - this.Options.length -1;
			this.FlipDirection = 1;
		}
	} 
	else
	{
		if (index < 0)
		{
			
			index = this.Options.length + index;
			this.FlipDirection = -1;
		}
		
		else if (index >= this.Options.length)
		{
			
			index = index - this.Options.length;
			this.FlipDirection = 1;
		}	
	}
	this.selectedIndex = index;
	this.value = index == -1 ? "" : this.Options[index];
	
	if (typeof(up) != "undefined")
	{
		if (up)
		{
			this.controller.OnMoveUp(this, oldValue, this.value);
		}
		else
		{
			this.controller.OnMoveDown(this, oldValue, this.value);
		}
	}
	this.controller.OnEnumChanged(this, oldValue, this.value);
	this.FlipDirection = 0;
};

RadEnumerationMaskPart.prototype.HandleKey = function (e)
{
	this.controller.CalculateSelection();
	var eventWrap = new MaskedEventWrap(e, this.controller.field);
	if (eventWrap.IsDownArrow())
	{
		this.SetOption(this.selectedIndex + 1, false);

		this.controller.Visualise();
		this.controller.FixSelection(eventWrap);
		return true;
	}
	else if (eventWrap.IsUpArrow())
	{
		this.SetOption(this.selectedIndex - 1, true);
		this.controller.Visualise();
		this.controller.FixSelection(eventWrap);
		return true;
	}
};

RadEnumerationMaskPart.prototype.HandleWheel = function (e)
{
	this.controller.CalculateSelection();
	var eventWrap = new MaskedEventWrap(e, this.controller.field);	
	
	this.SetOption(this.selectedIndex - e.wheelDelta / 120);
	
	this.controller.Visualise();
	this.controller.FixSelection(eventWrap);
	return false;
};

function CompletionList(options, blankChar)
{
	this.options = options;
	this.blankChar = blankChar;
};

CompletionList.prototype.GetCompletions = function(keys, offset)
{
	var completions = this.options;
	for (var currentPosition = 0; currentPosition <= offset; currentPosition++)
	{
		var currentChar = keys[currentPosition].toLowerCase();
		completions = this.FilterCompletions(completions, currentPosition, currentChar);
	}
	return completions;
};

CompletionList.prototype.FilterCompletions = function(completions, wordIndex, key)
{
	var matches = [];
	for (var completionIndex = 0; completionIndex < completions.length; completionIndex++)
	{
		var currentCompletion = completions[completionIndex];
		var wordChar = currentCompletion.charAt(wordIndex).toLowerCase();
		
		if (this.CharacterMatchesCompletion(key, wordChar))
		{
			matches[matches.length] = currentCompletion;
		}
	}
	return matches;
}

CompletionList.prototype.CharacterMatchesCompletion = function(currentChar, wordChar)
{
	return currentChar == this.blankChar ||
				currentChar == " " ||
				currentChar == wordChar;
};

//BEGIN_ATLAS_NOTIFY
if (typeof(Sys) != "undefined")
{
    if (Sys.Application != null && Sys.Application.notifyScriptLoaded != null)
    {
        Sys.Application.notifyScriptLoaded();
    }
}
//END_ATLAS_NOTIFY

