

/*
 * 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, destroyContainerBefore)
{
	if(!$(elName))
		return false;
	
	if(typeof(destroyContainerBefore) != 'undefined' && destroyContainerBefore == true)
		$(elName).update('');

	$(elName).appendChild(document.createTextNode(' '));
	$(elName).appendChild(Builder.build('<img src="' + sysUrl + 'template/images/spinner.gif" alt="loading..." id="loading' + elName + '" />'));
		
}
function hideLoading(elName)
{ 	
	if(!$(elName))
		return false;
	$(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);
}

/* -----------------------------------*/
/* --->>> onDOMReady Extension <<<----*/
/* -----------------------------------*/


Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});


/*
 * A fix for focusFirstElement on IE
 */
function focusFirst(formName) {
	window.setTimeout( function() { $(formName).focusFirstElement(); }, 10 );
}

function fancyFocus(el) {
	window.setTimeout( function() { $(el).focus(); }, 10 );
}
