

/*
 * what to do when input is focused
 */
function inputFocus(el, zeroState)
{
	//alert($F(el));
	if($F(el) == zeroState)
		el.value = '';
}

/*
 * what to do when input is blured
 */
function inputBlur(el, zeroState)
{
	if($F(el) == '')
		el.value = zeroState;
}

/*
 * Search back in the node tree for the next instance of the specified element
 */
function findNodeBehindByTagName(el, tag)
{
	tag = tag.toUpperCase();
	if(el.parentNode.tagName == tag)
		return el.parentNode;
	else
		return findNodeBehindByTagName(el.parentNode, tag);
}

/*
 *  multi use
 */
function newEl(type)     { return document.createElement(type); }
function newTxt(content) { return document.createTextNode(content); }
function getByTag(tag) { return document.getElementsByTagName(tag); }


function newInput(type, name, id, value)
{
	var i   = newEl('input');
	i.type  = type;
	if(name)  i.name  = name;
	if(id)    i.id    = id;
	if(value) i.value = value;
	return i;
}

/*
 *  multi use
 */
function newPage(id, containerToAppend)
{

	if(typeof(containerToAppend) == 'string')
		containerToAppend = $(containerToAppend);
	
	var title       = newEl('input');
	title.type      = 'text';
	title.name      = 'pages[' + id + '][title]';
	title.className = 'text';
	title.value     = 'Title';

	if(id == 0)
	{
		title.id       = 'newpageTitle';
	}
	var div = newEl('span');
	div.id  = 'page' + id;
	
	div.appendChild(title);
	containerToAppend.appendChild(div);
	window.setTimeout(function() { title.focus() }, 1);

}


function showLoading(elName)
{
	var img = document.createElement('img');
	img.src = sysUrl + 'template/images/spinner.gif';
	img.alt = 'Loading...';
	img.id  = 'loading' + elName;
	if(typeof(elName == 'string'))
	{
		$(elName).appendChild(document.createTextNode(' '));
		$(elName).appendChild(img);		
	}
	else
	{
		elName.appendChild(document.createTextNode(' '));
		$(elName).appendChild(img);
	}

	
}function hideLoading(elName){ $(elName).removeChild($('loading' + elName)); }


function CreateInPlaceEditor(editableContainer, multiLine, table, field, id, inputFieldsOnReturn)
{
	Ajax.InPlaceEditor.defaultHighlightColor = '#f1f1f1';
	if(!multiLine)
	{
		if(field == 'title')
			var r = new Ajax.InPlaceEditor(editableContainer, ajaxUrl + '?ajaxAction=saveInPlaceEditing&table=' + table + '&field=' + field + '&id=' + id, {okText:'Save', onComplete: function() { $('jumpchartNav').innerHTML = ''; showLoading('jumpchartNav'); new Ajax.Updater('jumpchartNav', ajaxUrl + '?ajaxAction=buildNavbar&projectId=' + $F('pageProjectId'))}});
		else
			var r = new Ajax.InPlaceEditor(editableContainer, ajaxUrl + '?ajaxAction=saveInPlaceEditing&table=' + table + '&field=' + field + '&id=' + id, {okText:'Save'});
	}
	else
	{
		// alert(inputFieldsOnReturn);
		if(!inputFieldsOnReturn)
			var inputFieldsOnReturn = 0;
		else
			var inputFieldsOnReturn = 1;
		
		var height   = Element.getHeight(editableContainer);
		var colsRows = Math.round(height/14)+3;
		if(colsRows < 10)
			colsRows = 10;
		
		var r = new Ajax.InPlaceEditor(editableContainer, ajaxUrl + '?ajaxAction=saveInPlaceEditing&table=' + table + '&field=' + field + '&id=' + id + '&multiLine' + '&inputFieldsOnReturn=' + inputFieldsOnReturn, {okText:'Save', rows:colsRows, cols:70, loadTextURL: ajaxUrl + '?ajaxAction=loadTextForInPlaceEditing&table=' + table + '&field=' + field + '&id=' + id});
	}

	return r;
}


function Down(el)  { new Effect.SlideDown(el,{duration:0.4}); }
function Up(el) { new Effect.SlideUp(el,{duration:0.4});}
function Shake(el) { new Effect.Shake(el); }
function Scroll(el) { new Effect.ScrollTo(el, {offset:-10});}
function Fade(el) { new Effect.Fade(el);}


function OverlayWindow(container, content, windowWidth, windowHeight)
{
	if(!windowWidth) var windowWidth = 400;
	if(!windowHeight) var windowHeight = 250;
	
	return new LITBox(container, content, {type: 'window', width: windowWidth, height: windowHeight, overlay: true, resizable: false, draggable: false});
}


function debug(variable)
{
	alert('DEBUG >>> ' + variable);
}

