
var ifTools = new Object();
ifTools.getObject = function(name)
{
	try {
		return this[name];
	} catch(e) 
	{
		alert(name+' ifTools Object Not Found!');
	}
}

/* PAGER */
function ifToolsPager(name, params)
{
	this.name = name;
	this.settings = params;
	ifTools[name] = this;
	
	this.settings.useMaint = odef(this.settings.useMaint, true);
}

ifToolsPager.prototype.setOffset = function(page, offset)
{
	form = this.settings["form"];

	frmiForms[form].setHidden('pager['+this.name+'][page]', page);
	frmiForms[form].setHidden('pager['+this.name+'][offset]', offset);
	
	if (this.settings.useMaint)
		maintSetAction(frmiForms[form].getForm(), this.settings["setOffsetAction"], 1, true, false);
	else
		frmiForms[form].submit();
}
ifToolsPager.prototype.jumpPage = function()
{
	form = this.settings["form"];

	j = frmiForms[form].getValue('pager['+this.name+'][jump]');
	
	if (j == '')
		return;

	j--;
	mp = this.settings["maxpages"];
	wind = this.settings["window"];
	
	if (j < 0)
		j = 0;
	if (j >= mp)
		j = (mp-1);
		
	o = j*this.settings["step"];
	
	if (j>(mp-wind))
		j = mp < wind ? 0:mp-wind;

	this.setOffset(j,o);
}


/* LIST */
function ifToolsList(name, params)
{
	this.name = name;
	this.settings = params;
	ifTools[name] = this;
}

ifToolsList.prototype.setPosition = function(dir)
{
	form = this.settings["form"];

	
	frmiForms[form].setHidden('list['+this.name+'][pdir]', dir);
	frmiForms[form].setHidden('list[_]', this.name);
	maintSetAction(frmiForms[form].getForm(), this.settings["setPositionAction"], 1, true, false);
}

ifToolsList.prototype.setListView = function()
{
	form = this.settings["form"];

	frmiForms[form].setHidden('list[_]', this.name);
	maintSetAction(frmiForms[form].getForm(), this.settings["setListViewAction"], 1, true, false);
}

ifToolsList.prototype.setListItems = function()
{
	form = this.settings["form"];

	frmiForms[form].setHidden('list[_]', this.name);
	maintSetAction(frmiForms[form].getForm(), this.settings["setListItemsAction"], 1, true, false);
}

ifToolsList.prototype.setItemsStatus = function()
{
	form = this.settings["form"];

	frmiForms[form].setHidden('list[_]', this.name);
	maintSetAction(frmiForms[form].getForm(), this.settings["setItemsStatusAction"], 1, true, false);

}

ifToolsList.prototype.deleteItems = function()
{
	form = this.settings["form"];

	frmiForms[form].setHidden('list[_]', this.name);
	try {
		maintActions.activeAction.doAction(this.settings["deleteItemsAction"], form);
	} catch(e) {
		maintSetAction(frmiForms[form].getForm(), this.settings["deleteItemsAction"], 1, true, false);
	}
}

ifToolsList.prototype.setChecks = function(cbobj)
{
	iname = 'cb_list['+this.name+'][rows]';
	for(i=0; i<cbobj.form.elements.length; i++)
	{
		e = cbobj.form.elements[i];
		if (e.name.substr(0,iname.length+1) == (iname+'['))
		{
			e.checked = cbobj.checked;

			if (e.onclick)
				e.onclick();
		}
	}
}

/* ORDERING */
function ifToolsOrder(name, params)
{
	this.name = name;
	this.settings = params;
	this.columns = new Array();
	ifTools[name] = this;
}

ifToolsOrder.prototype.addColumn = function(id, params)
{
	var col = new Object();
	col.settings = params;
	this.columns[id] = col;
}

ifToolsOrder.prototype.chOrder = function(id)
{
	var col = this.columns[id];
	
	dir = col.settings['direction'];
	
	if (dir == 'asc')
		col.settings['direction'] = 'desc';
	else if ((dir == 'desc') || (dir == 'noord') || !dir)
		col.settings['direction'] = 'asc';
		
	if (this.settings["single"])
	{
		this.clearOther(id);
		col.settings['nr'] = 1;
	}

	form = this.settings["form"];

	frmiForms[form].setHidden('order['+this.name+'][fields]['+id+'][dir]', col.settings['direction']);
	frmiForms[form].setHidden('order['+this.name+'][fields]['+id+'][nr]', col.settings['nr']);
	maintSetAction(frmiForms[form].getForm(), this.settings["chOrderAction"], 1, true, false);
}
ifToolsOrder.prototype.clearOther = function(id)
{
	form = this.settings["form"];
	
	for(colid in this.columns)
	{
		if (colid != id)
		{
			frmiForms[form].setHidden('order['+this.name+'][fields]['+colid+'][dir]', '');
			frmiForms[form].setHidden('order['+this.name+'][fields]['+colid+'][nr]', '');
		}
	}
}

/* TABS */
function ifToolsTab(name, params)
{
	this.name = name;
	this.settings = params;
	this.tabs = new Array();
	ifTools[name] = this;
}

ifToolsTab.prototype.addTab = function(id, params)
{
	var tab = new Object();
	tab.settings = params;
	this.tabs[id] = tab;
}

ifToolsTab.prototype.chTab = function(id,onload)
{
	if (onload) {
		name = this.name;
		Html.addEvent(window, 'load', function () { ifTools[name].chTab(id); });
		return;
	}
	
	var tab = this.tabs[id];
	
	if (this.tabs[id].settings["active"])
		return;

	this.hideOther(id);
	
	if (o = document.getElementById(id))
		o.className = '';
		
	this.tabs[id].settings["active"] = true;
		
	if (this.settings["menu"])
	{
		mi = document.getElementById(tab.settings["menuItemID"]);
		mi.className = this.settings["menuItemClass"]+'-noclick';
	}
	
	form = this.settings["form"];
	frmiForms[form].setHidden('tab['+this.name+'][lasttab]', id);
}

ifToolsTab.prototype.hideOther = function(id)
{
	form = this.settings["form"];
	
	for(tabid in this.tabs)
	{
		if (tabid != id)
		{
			if (o = document.getElementById(tabid))
				o.className = 'hide';

			this.tabs[tabid].settings["active"] = false;

			if (this.settings["menu"])
			{
				mi = document.getElementById(this.tabs[tabid].settings["menuItemID"]);
				mi.className = this.settings["menuItemClass"];
			}
		}
	}
}

/* FORMS */
function ifToolsForm(name, params)
{
	this.name = name;
	this.settings = params;
	ifTools[name] = this;
}

ifToolsForm.prototype.addControl = function(control)
{
	this.settings['controls'][this.settings['controls'].length] = control;
}

ifToolsForm.prototype.changeLang = function(newlang)
{
	form = this.settings['form'];
	
	if (!newlang)
		newlang = frmiForms[form].getAllValue('form['+this.name+'][actlang]');
	frmiForms[form].setAllValue('form['+this.name+'][actlang]', newlang);
	lang = newlang;

	if (frmiForms[form].hasElement('form['+this.name+'][showdef]'))
	{
		showdeflang = frmiForms[form].getForm().elements['form['+this.name+'][showdef]'].checked;
	} else
		showdeflang = false;
	deflang = this.settings['deflang'];
	
	for(i=0; i < this.settings['controls'].length; i++)
	{
		cntl = this.settings['controls'][i];
		for(l=0; l < this.settings['langs'].length; l++)
		{
			al = this.settings['langs'][l];
			if (o = document.getElementById(cntl+'['+al+']'))
			{
				o.className = (al == lang || (showdeflang && al == deflang)) ? '' : 'hide';
			}
		}
	}
}

/* INFODIV */
function ifToolsInfoDiv(name, params)
{
	this.name = name;
	this.settings = params;
	ifTools[name] = this;

	var o = Html.getElementById('info'+name);
	if (o)
	{
		o.int_name = name;
		
		idiv = document.createElement('DIV');
		idiv.name = 'infopanel'+name;
		idiv.id = 'infopanel'+name;
		idiv.innerHTML = this.settings['infotext'];
		idiv.className = this.settings['infoclass'];
		Html.setStyle(idiv, 'position', 'absolute');
		Html.setVisibility(idiv, false);
		
		document.body.appendChild(idiv);

		Html.addEvent(o, 'mousemove', this.onMove);
		Html.addEvent(o, 'mouseout', this.onOut);
	}
}

ifToolsInfoDiv.prototype.onMove = function(e)
{
	//return;
	var so = (e.srcElement ? e.srcElement : e.target);

	while(!(name = so.int_name) && so.parentNode)
		so = so.parentNode;

	var o = document.getElementById('infopanel'+name);
	if (o)
	{
		p = Html.getOffset(so);
		if (Html.isMSIE)
		{
			p[0] += e.offsetX + 20;
			p[1] += e.offsetY + 20;
		} else
		{
			p[0] = e.layerX + 20;
			p[1] = e.layerY + 20;
		}
		
		// check location...
		st = document.documentElement.scrollTop;
		sl = document.documentElement.scrollLeft;
		ch = document.documentElement.clientHeight;
		cw = document.documentElement.clientWidth;
		if ((p[0] + o.offsetWidth - sl) > cw)
			p[0] -= o.offsetWidth + 30;
		if ((p[1] + o.offsetHeight - st) > ch)
			p[1] -= o.offsetHeight + 30;
		
		Html.setOffset(o,p);
		
		Html.setVisibility(o, true);
		SelectRemover.process();
	}
}

ifToolsInfoDiv.prototype.onOut = function(e)
{
	var so = (e.srcElement ? e.srcElement : e.target);

	while(!(name = so.int_name) && so.parentNode)
		so = so.parentNode;

	if (o = document.getElementById('infopanel'+name))
	{
		Html.setVisibility(o, false);
		SelectRemover.process();
	}
}

/* MENU */

function ifToolsMenu(name, params)
{
	this.name = name;
	this.settings = params;
	this.submenus = new Object();
	ifTools[name] = this;
	
	this.settings.openAuto = odef(this.settings.openAuto, true);

	if (!this.settings.noinit && this.settings.openAuto)
		Html.addEvent(window, 'load', function () { Html.addEvent(document.body, 'mouseover', function() { ifTools[name].closeAll(); } ); });
}
ifToolsMenu.prototype.open = function(itemname)
{
	if (this.settings.openAuto)
		this.closeAll(itemname);
	//alert('open:'+this.name+'.'+itemname);
	Html.setDisplay(itemname, 1, 'block');
}
ifToolsMenu.prototype.close = function(itemname)
{
	Html.setDisplay(itemname, 0);
}

ifToolsMenu.prototype.closeAll = function(itemname)
{
	if (this.activeItem)
		Html.setClass(this.activeItem, this.settings.classNormal);
		
	for(sm in this.submenus)
	{
		//alert(this.name+':'+sm);
		if (sm != odef(itemname, ''))
			Html.setDisplay(sm, 0);
		this.submenus[sm].closeAll();
	}
}
ifToolsMenu.prototype.onOver = function(e, itemname, item)
{
	if (itemname)
		this.open(itemname);
	else
	{
		alert('ups');
		this.closeAll();
	}
	if (this.settings.keepActive && itemname)
	{
		Html.setClass(item, this.settings.classActive);
		this.activeItem = item;
	}
	e.cancelBubble = true;
}
ifToolsMenu.prototype.addSubMenu = function(itemname, name, settings)
{
	this.submenus[name] = new ifToolsMenu(name, settings);
	
	var mname = this.name;
	if (this.settings.openAuto)
		Html.addEvent(window, 'load', function () { Html.addEvent(itemname, 'mouseover', function(event) { ifTools[mname].onOver(event, name, itemname); } ); });
	else
	{
		this.submenus[name].parentMenu = this;
		this.submenus[name].parentItem = itemname;
		this.submenus[name].state = odef(settings.initialState, 0);
		Html.addEvent(window, 'load', function () { 
			Html.addEvent(itemname, 'click', function(event) { 
				var to = MyEvent.getTarget(event);
				var o = ifTools[name]; 
				
				while (o.parentItem != to.id && to.parentNode)
				{
					to = to.parentNode;
					
					if (name == to.id)
						return true;
				}

				if (o.parentItem != to.id)
					return false;


				o.state = 1 - o.state; 
				if (o.state) 
					o.open(name); 
				else 
					o.close(name); 

				return true;
			}); 
		});
	}
}

