/*

Kinetics ajax/dhtml framework funcitons
CopyrightŪ Kinetics Multimedia (www.kinetics.com.br)

*/


/* basic browse components */

//gets up in the DOM hierachy and retrieve the component object
function getme( o, comptype )
{
	while( o != null && o != document.body )
	{
		if( isdefined( o.component ) && ( !isdefined( comptype ) || o.component.type == comptype ) )
		{
			return o.component;
		}
		o = o.parentNode;
	}
	return null;
}


//gets up in the DOM hierachy and retrieve the component object
// (with debug...)
function dgetme( o, comptype )
{
	debug( 'type', comptype );
	while( o != null && o != document.body )
	{
		if( isdefined( o.component ) && ( !isdefined( comptype ) || o.component.type == comptype ) )
		{
			debug( 'found', o, o.tagName, o.id, o.className, 'component', o.component, 'type', o.component.type );
			return o.component;
		}
		o = o.parentNode;
		debug( 'up', o, o.tagName, o.id, o.className, 'component', o.component );
	}
	return null
}

var componentvars = Array();

function browsecomponent( hang, title, script )
{
	this.type = 'browser';
	this.title = title;
	this.searchHTML = '';
	this.footerHTML = '';
	this.script = script;
	this.hang = hang;
	this.hang.className = 'browse';
	this.recs = Array();
	this.arecs = Array();
	this.firstrec = 0;
	this.totrec = 0;
	this.rows = 5;
	this.cols = 1;
	this.npages = 6;
	this.pagesize = this.rows*this.cols;
	this.numrec = this.pagesize*this.npages;
	this.txt_loading = TXT_LOADING;
	this.txt_notfound = TXT_NO_ITEMS_FOUND;
	this.drawn = false;
	this.debug_mode = false;
	this.parameters = new Object;

	//process the ajax json output
	function result( txt )
	{
		var data = json( txt );
		if (data.status==0)
		{
			var tmp = this.txt_notfound;
			this.txt_notfound=data.errmess;
			this.notfound();
			this.txt_notfound = tmp;
			return;
		}
		if( data.totrec == 0 || data.lastrec < 0 )
		{
			this.notfound();
			return;
		}
		data.firstrec *= 1;
		data.lastrec *= 1;
		data.totrec *= 1;
		this.labels = data.labels;
		for( var i = data.firstrec ; i <= data.lastrec ; i++ )
		{
			this.recs[i] = data.records[i-data.firstrec];
			this.arecs[i] = new Object;
			for( var r = 0 ; r < this.recs[i].length ; r++ )
				this.arecs[i][this.labels[r]] = this.recs[i][r];
		}
		this.totrec = data.totrec;
		if( this.firstrec >= this.totrec ) this.firstrec = this.totrec-1;
		if( data.firstrec >= this.firstrec && data.firstrec <= (this.firstrec+this.numrec) )
			this.show( data.firstrec );
			
	}

	function savevars()
	{
		if( !isdefined( this.s_ident ) ) return;
		var save = new Object;
		save.firstrec = this.firstrec;
		save.parameters = this.parameters;
		savevar( 'browse_' + this.s_ident, jsonout( save ) );
	}

	function loadvars( s_ident ) 
	{
		this.s_ident = s_ident;
		try
		{
			var savedtxt = eval( 'browse_' + this.s_ident );
			save = json( savedtxt );
			for (var v in save)	
				this[v] = save[v];
		}
		catch (e)
		{
		}
	}

	function ident( s_ident )
	{
		this.s_ident = s_ident;
	}

	function reset()
	{
		this.firstrec = 0;
		this.totrec = -1;
		this.recs = Array();
		this.arecs = Array();
	}

	//requests for a new set of records
	function get( idx )
	{
		this.pagesize = this.rows*this.cols;
		this.numrec = this.pagesize*this.npages;
		params = [];
		for (var p in this.parameters)
			params.push( p + '=' + this.parameters[p] );
		params = params.length ? '&' + params.join( '&' ) : '';
		var jpar = this.script.match( /\?/ ) ? '&' : '?';
		var url = this.script + jpar + 'numrec=' + this.numrec + '&firstrec=' + this.firstrec + params;
		if( this.debug_mode ) debug( '<a href="' + url + '" target="_blank">AJAX get</a>' );
		ajaxrequest( nocache(url), function( txt, browse ) { browse.result( txt ) }, this, 1 );
	}

	function nav( idx )
	{
		if( !isdefined( this.totrec ) || this.totrec <= 0 ) return '';
		var prev = idx > 0 ? '<div class="b_prev" onclick="getme(this).show( ' + (idx-this.pagesize) +  ' )"></div>' : '<div class="b_prev_off"></div>';
		var next = idx+this.pagesize < this.totrec ? '<div class="b_next" onclick="getme(this).show( ' + (idx+this.pagesize) +  ' )"></div>' : '<div class="b_next_off"></div>';
		var idx2 = idx+this.pagesize;
		if( idx2 > this.totrec ) idx2 = this.totrec;
		var pos = (idx+1) + ' - ' + idx2 + ' ' + TXT_NAV_OF + ' ' +(this.totrec);
		var output = '<div class="b_nav"><table class="b_table" cellpadding="0" cellspacing="0" border="0"><tr><td>' + prev + '</td><td><div class="b_txt">' + pos + '</div></td><td>' + next + '</td></tr></table></div>';
		return output;
	}

	function nav1( idx )
	{
		return this.nav( idx );
	}

	function nav2( idx )
	{
		return '';
	}

	//display records in component content
	function show( idx )
	{
		if( !this.drawn ) this.draw();
		if( !isdefined( idx ) )
		{
			this.loading();
			this.get( 0 );
			return;
		}
		if( idx < 0 ) idx = 0;
		if( idx >= this.totrec && this.totrec > 0 ) idx = this.totrec - 1;
		this.firstrec = idx;

		this.savevars();

		var nav1 = this.nav1( idx );
		var nav2 = this.nav2( idx );
		var output = '<table cellpadding="0" cellspacing="0" border="0">';
		var i = idx;
		for( var row = 0 ; row < this.rows ; row++ )
		{
			output += row > 0 ? '</tr><tr>' : '<tr>';
			for( var col = 0 ; col < this.cols ; col++ )
			{
				if( i >= this.totrec ) { row = col = 1000; break; }
				var rec = this.recs[i];
				var arec = this.arecs[i];
				if( isdefined( rec ) )
				{
					output += '<td><div class="item" ' 
								 + 'onmouseover="getme(this).over( this, ' + i + ')" '
								 + 'onmouseout="getme(this).out( this, ' + i + ')" ' 
								 + 'onmousedown="getme(this).click( this, ' + i + ')">' 
								 +  this.cell( rec, arec ) + '</div></td>';
				}
				else
				{
					this.get( this.firstrec );
					this.loading();
					return;
				}
				i++;
			}
		}
		output += '</tr></table>';
		this.content.innerHTML = nav1 + output + nav2 + this.footerHTML;
	}

	//returns the the record HTML output
	function cell( rec )
	{
		return rec;
	}

	//cell mouse rollover event function
	function over( div, idx )
	{
		//debug( 'over', idx );
		div.className = 'item_sel';
	}

	//cell mouse rolloout event function
	function out( div, idx )
	{
		//debug( 'out', idx );
		div.className = 'item';
	}

	//cell mouse click event function
	function click( div, idx )
	{
		div.className = 'item_click';
		debug( 'click item', idx );
	}
	
	//loading message
	function loading()
	{
		this.content.innerHTML = '<div class="loading">' + this.txt_loading + '</div>';
	}

	//empty result message
	function notfound()
	{
		this.content.innerHTML = '<div class="notfound">' + this.txt_notfound + '</div>';
	}

	function search( frm )
	{
		var el = frm.elements;
		for( var i = 0 ; i < el.length ; i++ )
		{
			var name = el[i].name;
			var val = '';
			if( el[i].type == 'select-one' ) { val = el[i].options[el[i].selectedIndex].value }
			else if( el[i].type == 'radio' ) val = el[i].value;
			else if( el[i].type == 'checkbox' ) val = el[i].checked;
			else if( el[i].usevalue ) val = el[i].valueid;
			else val = el[i].value;

			var def = el[i].getAttribute( 'helper');
			if( val == def ) val = '';
			this.parameters[name] = val;
		}
		this.reset();
		this.show();
		return false;
	}

	function search_set_values()
	{
		frm = this.searcharea.getElementsByTagName("FORM")[0];
		var el = frm.elements;
		for( var i = 0 ; i < el.length ; i++ )
		{
			var name = el[i].name;
			var val = '';
			if( isdefined(this.parameters[name]) )
			{
				if( el[i].type == 'button' || el[i].type == 'submit' || el[i].type == 'reset' )
					;
				else if( el[i].type == 'select-one' )
				{ 
					el[i].setAttribute( 'defaultvalue', this.parameters[name] );
				}
				else if( el[i].type == 'radio' )
				{
					//need to make it work for radio
				}
				else if( el[i].type == 'checkbox' && isdefined(this.parameters[name]) ) el[i].checked = this.parameters[name];
				else if( isdefined(this.parameters[name]) )
				{
					el[i].value = this.parameters[name];
				}
			}
		}
	}


	//draw the component inside the hang div
	function draw()
	{
		this.hang.innerHTML = '';
		this.hang.component = this;
		this.head  = ce( 'DIV' );
		this.searcharea  = ce( 'DIV' );
		this.content = ce( 'DIV' );

		this.head.className = 'b_head';
		this.searcharea.className = 'b_search';
		this.content.className = 'b_content';
		this.hang.appendChild( this.head );
		this.hang.appendChild( this.searcharea );
		this.hang.appendChild( this.content );

		this.searcharea.innerHTML = this.searchHTML;
		if( this.title != '' )
			this.head.innerHTML = this.title;
		else
			this.head.style.display = 'none';

		if( this.searchHTML != '' )
		{
			this.searcharea.innerHTML = '<form onsubmit="getme(this).search(this); return false">' + this.searchHTML + '</form>';
			this.search_set_values();
			formformat( this.searcharea );
			this.searchform = this.searcharea.getElementsByTagName("FORM")[0];
		}
		else
			this.searcharea.style.display = 'none';

		this.content.innerHTML = '';
		this.drawn = true;
	}

	this._show = this.show = show;
	this._nav = this.nav = nav;
	this._nav1 = this.nav1 = nav1;
	this._nav2 = this.nav2 = nav2;
	this._cell = this.cell = cell;
	this._get = this.get = get;
	this._result = this.result = result;
	this._search = this.search = search;
	this._search_set_values = this.search_set_values = search_set_values;
	this._draw = this.draw = draw;
	this._loading = this.loading = loading;
	this._notfound = this.notfound = notfound;
	this._over = this.over = over;
	this._out = this.out = out;
	this._click = this.click = click;
	this._reset = this.reset = reset;
	this._loadvars = this.loadvars = loadvars;
	this._savevars = this.savevars = savevars;
	this._ident = this.ident = ident;
	return this;
}

/* basic tab object */

function tabbar( hang, opts )
{
	this.hang = hang;
	this.opts = opts;

	this.type = 'tabbar';
	var tabdiv = ce( 'DIV' );
	tabdiv.component = this;
	tabdiv.className = 'tab';
	this.tabdiv = tabdiv;
	this.tabopts = opts;
	this.drawn = false;

	function draw()
	{
		this.drawn = true;
		var items = ce( 'DIV' ); items.className = 'tab_items';
		var table = ce( 'TABLE' ); table.className = 'tab_table'; items.appendChild( table );
		table.cellPadding = table.cellSpacing = table.border = 0;
		var tb = ce( 'TBODY' ); table.appendChild( tb );
		var tr = ce( 'TR' ); tb.appendChild( tr );
		for( var i = 0 ; i < this.opts.length ; i++ )
		{
			var td = ce( 'TD' );
			tr.appendChild( td );
			var item = ce( 'DIV' ); item.className = 'tab_item'; td.appendChild( item );
			item.innerHTML = this.opts[i].label;
			item.idx = i;

			var itemdiv = ce( 'DIV' ); itemdiv.className = 'tab_content';
			this.opts[i].item = item;
			this.opts[i].drawn = false;
			this.tabdiv.appendChild( itemdiv );
			itemdiv.innerHTML = 'bababa';
			this.opts[i].itemdiv = itemdiv;
			item.onclick = function() { getme(this).select( this.idx ); };
		}

		this.tabdiv.appendChild( items );
		this.hang.appendChild( this.tabdiv );
	}

	// selects a tab
	function select( idx )
	{
		if( !this.drawn ) this.draw();

		for( var i = 0 ; i < this.tabopts.length ; i++ )
		{
			var opt = this.tabopts[i];
			if( i == idx )
			{
				opt.item.className = 'tab_item_sel';
				opt.itemdiv.style.display = 'block';
				this.currentdiv = opt.itemdiv;
				if( !opt.drawn )
				{
					if( isdefined( opt.html ) )
					{
						opt.itemdiv.innerHTML = opt.html;
					}
					if( isdefined( opt.script ) )
					{
						var ns = ce('SCRIPT');
						ns.type = 'text/javascript';
						ns.text = opt.script;
						document.body.appendChild( ns );
					}
					if( isdefined( opt.load ) )
					{
						opt.itemdiv.innerHTML = '<div class="loading">Loading...</div>';
						var url = nocache(opt.load);
						ajaxrequest( url, 'resultform', opt.itemdiv, 0 );
					}
					if( isdefined( opt.sticky ) && opt.sticky == true )
						opt.drawn = true;
				}
			}
			else
			{
				opt.item.className = 'tab_item';
				opt.itemdiv.style.display = 'none';
			}
		}
	}

	//loads the url on a "notab" div under tabbar. All tabs get unselected
	function loadurl( url )
	{
		this.currentdiv.innerHTML = '<div class="loading">Loading...</div>';
		this.currentdiv.style.display = 'block';
		ajaxrequest( nocache(url), 'resultform', this.currentdiv, 0 );
	}
	this.loadurl = loadurl;
	this.draw = draw;
	this.select = select;
	return this;
}

/* specific tab components */

function maintabbar( hang, opts )
{
	var tab = new tabbar( hang, opts );
	tab.tabdiv.className += ' maintab';
	return tab;
}


function subtabbar( hang, opts )
{
	var tab = new tabbar( hang, opts );
	tab.tabdiv.className += ' subtab';
	return tab;
}

/* ------------------------------------ end basic browse components --------------------------*/