/// <reference path="../../../vendor/jquery/1.4.4/jquery.js"/>

function selectRange(input, start, end)
{
	if( input.createTextRange )
	{
		var selRange = input.createTextRange();
		selRange.collapse(true);
		selRange.moveStart("character", start);
		selRange.moveEnd("character", end);
		selRange.select();
	}
	else if( input.setSelectionRange )
	{
//		input.setSelectionRange(start, end);
	}
	else
	{
		if( input.selectionStart )
		{
			input.selectionStart = start;
			input.selectionEnd = end;
		}
	}
	input.focus();
}

function makeList(data, options)
{
	var options = options;
	var ul = document.createElement("ul");
	 
	ul.AddListItem = addListItem;
	
	for (var i =0; i< data.length; i++)
	{
		addListItem(data[i])
	}
	return ul;
	
	function addListItem(itemData)
	{
		var li = document.createElement("li");
		var a = document.createElement("a");
		a.innerHTML = (options.formatItem) ? options.formatItem(itemData) : data[i];

		if (options.itemClickCallback)
			$(li).click(function(e) {e.preventDefault(); e.stopPropagation(); options.itemClickCallback(e, li, li.data);});
		li.data = itemData;
		li.appendChild(a);            
		ul.appendChild(li);
	}
}

function appendUrlParams(url, params)
{
	if (params)
	{
		url += (url.indexOf('?') >= 0 ? '&' : '?') + params;
	}
	return url;
}


function eventIsEnterKey(e)
{
	var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
	return (key == 13);
}

/*
 * createNButtonAlert
 * 
 * A util for creating an alert popup with a title, message and a set of N buttons to render in the dialog
 *
 * title:		dialog title
 * message:		message to be displayed in dialog
 * options:		dialog options according to the jQuery.ui spec
 * buttonHash:	hash<buttonText, buttonOptions>
 * 
 * The buttonOptions can be either a single function to be attached to the click method of the button or a hash with any of the following options
 *		buttonClass:		the css class to apply to the button
 *		iconClass:			the css class to apply to the icon inside the button
 *		click:				the click function for the button. The function takes the form function(event, buttonCallback, closeDialogCallback)
 *								event:					the click event
 *								buttonCallback:			a callback supplied by the clickable plugin that removes loaders and active styles
 *								closeDialogCallback:	a callback supplied by the NButtonAlert that closes and destroys the dialog
 *							if a button performns some action that leaves the dialog open then it should just call the buttonCallback 
 *							if the button also wants to close the dialog once it completes it action then it should call both buttonCallback and closDialogCallback
 *
 */
function createNButtonAlert ( title, message, options , buttonHash)
{
	// create div main div
	var dialogDiv = $("<div></div>").addClass("dialog").attr("title", title);	
	var messageDiv = $("<div></div>").addClass("message").html(message);
	var buttonsDiv = $("<div></div>").addClass("buttons");
	
	for (var buttonText in buttonHash)
	{
		var buttonOptions = buttonHash[buttonText];
		if ($.isFunction(buttonHash[buttonText]))
		{
			buttonOptions = { click: buttonOptions };
		}
		//reproduce the html that would be created by the Button ViewComponent with slightly fewer options, see button.vm
		var buttonContainer = $("<span></span>").addClass("button-container").addClass(buttonOptions.buttonClass);

		// The options don't have a reference to the dialog to construct the close function so we need an option to signal that behavior and attach it here
		var closeDialogCallback = function() { dialogDiv.dialog("close"); };
		
		var buttonSpan = $("<span></span>")
			.addClass("button");

		//could maybe add error checking here, makes no sense to have a button without a click function
		//wrap the supplied click function in a function that matches the signature of the clickable.doClick function
		//in the process we need to create an anon function to get the right closure behavior inside the loop over the hash
		var buttonClick = function(optionClick)
		{
			return function(e, buttonCallback)
			{
				optionClick.apply(buttonSpan, [e, buttonCallback, closeDialogCallback]);
			};
		} (buttonOptions.click);
		buttonSpan.clickableButton({ addLoader:false, doClick: buttonClick });

		var buttonRight = $('<span></span>').addClass("button-right");
		buttonSpan.append(buttonRight);
		buttonRight.append($("<a>" + buttonText + "</a>"));
		if (buttonOptions.iconClass)
		{
			buttonRight.append($('<span class="icon ' + buttonOptions.iconClass + '"/>'));
		}
		buttonContainer.append(buttonSpan);
		buttonsDiv.append(buttonContainer);
	}
	// add message div
	dialogDiv.append(messageDiv);
	// add buttons div
	dialogDiv.append(buttonsDiv);
	// create dialog
	dialogDiv.dialog(getDialogOptions(options));
	return dialogDiv;
}

function showErrorMessage(widget, message)
{
	$(".errortext", widget).text(message);
	widget.show();
}

function clearErrorMessage(widget)
{
	widget.hide();
}

// Enum of possible results of filter validation. Depending on context, empty may or may
// not be considered valid.
var ValidationResult = {
	invalid: -1,
	empty: 0,
	valid: 1
};

function pointIsInRect(x, y, left, top, width, height)
{
	return x >= left && x < left + width
	    && y >= top  && y < top + height;
}

// Returns true if str is null, 0 length, or all whitespace
function stringIsEmpty(str)
{
	if (str == null)
		return true;
	
	for (var ii=0; ii<str.length; ii++)
	{
		if (!isSpace(str[ii]))
			return false;
	}
	
	return true;
}

function isSpace(ch)
{
	return (ch==' ' || ch=='\n' || ch=='\t' || ch=='\r');
}


