

/*
 * 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;
}

function removeElement(el)
{
	Element.remove(el);
}

/*
 *  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;
	$(elName).appendChild(document.createTextNode(' '));
	$(elName).appendChild(img);
	
}function hideLoading(elName){ $(elName).removeChild($('loading' + elName)); }


function CreateInPlaceEditor(editableContainer, multiLine, table, field, id)
{
	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
	{
		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', {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 openPopUp(el, width, height, appendix) 
{
	if(typeof(appendix) == 'undefined') var appendix = 'resizable=yes,toolbar=no,location=no,status=no';
	if(typeof(width) == 'undefined') var width = '700';
	if(typeof(height) == 'undefined') var height = '580';

	window.open(el.href,'','scrollbars=yes,menubar=no,height=' + height + ',width=' + width + ',' + appendix);
}
