/**
 * Funkcje podstawowe
 */
function _c( ident, param_in )
{
	//-- domyślne
	var param = { not_valid:'',valid_on_blur:'',AJAXsrc:'',AJAXsrc_set:'',
		min:undefined, max:undefined,
		automat:'',def_value:'',jquery:false,
		CammelCase:false,UpperCase:false,
		clone_obj:undefined };
		
	//-- nadpisanie parametrów;
	if (typeof(param_in) == 'object')
		for (var i in param_in) if (typeof(param_in[i]!= 'function')) param[i]=param_in[i];
		
	//-- czy nowy obiekt ma być klonem istniejącego elementu?
	var clone_obj = param.clone_obj;
	//--
	if (typeof(clone_obj)=='object') //-- kopia;
		var obj	= clone_obj.cloneNode(true);
	else //-- nowy;
		var obj	= document.createElement( ident );

	//-- funkcja iterująca elementy i przetwarzająca f( dane[i] )
	//-- gdzie f jest nazwą funkcji będącą w obiekcie this;
	obj.each	= function( dane, f )
	{
		for (var i in dane)
		{
			if (typeof(dane[i]) == 'function') continue;
			this[f]( dane[i] );		
		}
	}

	obj.setParam	= function( param )
	{
		if (typeof(param) == 'object')
			{ for (var i in param) this[i]=param[i]; }
	}

    //-- własności z parametrów;
    if (typeof(param_object) == 'string')
	{
		param += ';';
		var t = param.split(';');
		for (var i in t)
		{
		    if (t[i] == '') continue;
		    var t2 = t[i].split('=');
		    if (t2.length>0)
			obj[t2[0]]=t2[1];
		}
	}
	obj.setParam(param);
    //--
    obj.apC 	= function(c)
	{
		if (c!=undefined) this.appendChild(c)
		else alert('brak obiektu do appendChild');
	}
    if (!obj.jquery)
    {
	    obj.hide 	= function() {obj.style.display = 'none';};
	    obj.show 	= function() {obj.style.display = '';};
    }
    obj.change 	= function() {(obj.style.display == '')?this.hide():this.show();};
    obj.onkeyup	= function() {if (this.not_valid != '') this.value = this.value.replace(this.not_valid,'');}
    obj.exec	= function() {};
    obj.exe_automat = function()
    {
	    var t = this.automat.split('|');
	    this.value = this.value.replace(t[0],t[1]);    	
    }
	obj.getByAjax	= function(){ getByAjax( this.AJAXsrc, this, this.param ); }
	obj.aktywny	= function(l) { this.style.opacity = (l==1) ? 1 : 0.3 ; }
	
	//-- jeżeli to 'select' to dodaj funkcję wypełniającą opcje;
	if (ident == 'select')
	{
		if (obj.ustaw_pierwszy==undefined) obj.ustaw_piwerszy = false;
		obj.fillOptions	= function( d, v, n )
		{
			//this.length	= 0;
			for ( var i in d )
			{
				if ( typeof(d[i]) == 'function' ) continue;
				//--
				var opt = _c('option',{value:d[i][v]});
				opt.apC( _cTxt( d[i][n]) );
				opt._name	= d[i][n];
				this.apC( opt );
				//--
				if (this.value == '')
					if (this.ustaw_pierwszy) this.value = d[i][v]; 
			}	
		}			
	}
	obj.onGetByAjaxEnd	= function(){}

    //-- spec funkcje;
    //- serial TD dla TR
    obj._c_td = function (count)
	{
		var td = new Array();
		td[0] = _c('td');
		obj.apC(td[0]);
	}
    return obj;
}

function _cTxt(tekst)
    { return document.createTextNode(tekst); }

/**
 * Celem funkcji jest pobranie informacji do obiektu;
 * @version	1.1;
 * @required advajax.js;
 */    
function getByAjax( url, id, param )
{
	if (typeof(advAJAX)=='undefined') return false;
	
	advAJAX.get(
	{
		url			: url,
		parameters	: param,
		onLoading : function(obj)
		{
			var o=(typeof(id)=='object')? id:_get(id);
			if ( o != undefined )
				if ( o.onload != undefined )
					o.onload(); 
		},
		onError : function(obj)
		{
			if (typeof(console)=='object') console.log( obj.status );
		},
		onSuccess : function(obj)
		{
			var o=(typeof(id)=='object')? id:_get(id);
			if (o != undefined )
				if ( o.load != undefined )
				{
					o.load(
						eval( obj.responseText.replace('<p>dane</p><!--','').replace('-->','').replace('<dane>','').replace('</dane>','') )					
						);
					if (typeof(o.onGetByAjaxEnd)=='function') o.onGetByAjaxEnd();
				}
		},
		onTimeout : function(obj)
		{
			var o=(typeof(id)=='object')? id:_get(id);
			if (o != undefined )
				if ( o.onTimeout != undefined )
				{
					o.onTimeout();
					if (typeof(o.onTimeoutEnd)=='function') o.onTimeoutEnd();
				}			
		}
	}
   	);
}


/**
 * Celem funkcji jest wysłanie danych metodą POST i ewentualne
 * odebranie informacji zwrotnej;
 * Zwrócenie informacji o obiektach koniecznych do odświeżanie
 * metodą getByAjax(); 
 * @required advajax.js;
 */
function setByAjax( url, id, param )
{
	if (typeof(advAJAX)=='undefined') return false;

	advAJAX.post(
		{
		url			: url,
		parameters	: param,
		onSuccess	: function(obj)
		{
			var o=(typeof(id)=='object')? id:_get(id);
			if ( o != undefined )
			{
				if ( o.do_zmiany != undefined )
					o.do_zmiany( eval( obj.responseText.replace('<p>dane</p><!--','').replace('-->','').replace('<dane>','').replace('</dane>','') ) );
				if (typeof(o.onSetByAjaxEnd)=='function') o.onSetByAjaxEnd();
			}
        },
		onLoading : function(obj)
		{
			var o=(typeof(id)=='object')? id:_get(id);
			if ( o != undefined )
			{
				if ( o.onsend != undefined ){o.onsend();}
			}
		},
		onError : function(obj)
			{
				if (typeof(console)=='object') console.log( obj.status );
			}
   		}
   	);   	 
}

/**
 * Funkcja nadpisuje wskazany obiekt nowym o okreslonej klasie;
 * przykład: overload( 'id_warstwy' , TKalendarium, {parametr:1,param:2} );
 * 
 * Oryginalna warstwa jest ukrywana i dołączana do nowej;
 * 
 * NIEPRAKTYCZNA! aktualna: convertToClass
 */
function overload( target , append_object, param )
{
	if (_get( target ) == undefined ) return false;
	var zastap = true;
	//--
	var obj = (param != undefined) ? new append_object( param ) : new append_object;
	if (obj == undefined) return false;
	if (param != undefined)
		if (param.zastap != undefined) zastap = param.zastap;  
	//--
	if (obj.uchwyt == undefined) obj.uchwyt = _c('div');
	//-- przepisanie stylu (class);
	obj.oryginal	= _get( target );
	obj.className	= obj.oryginal.className;
	//-- przepisanie id;
	this.oryginal_id	= obj.oryginal.id;
	obj.oryginal.id 	= '_'+this.oryginal_id;
	obj.id				= this.oryginal_id;
	//-- 
	if (!zastap) obj.oryginal.parentNode.appendChild( obj.uchwyt );
	else obj.oryginal.parentNode.replaceChild( obj.uchwyt, obj.oryginal );
	//-- ukrycie i przełączenie;
	obj.oryginal.style.display = 'none';
	obj.uchwyt.apC( obj.oryginal );
	//--
	return obj;
}
        
/**
 * Zamienia element HTML na obiekt danej klasy;
 * @param {Object} id
 * @param {Object} nazwa_klasy
 */
function convertToClass(id,nazwa_klasy,param_in)
{
	if (typeof(id)!= 'object') var w = _get(id); else w = id;
	var param = {clone_obj:w};
	if (typeof(param_in) == 'object')
		for (var i in param_in) if (typeof(param_in[i]!= 'function')) param[i]=param_in[i];	
	//--
	var a = new nazwa_klasy(param);
	w.parentNode.replaceChild(a,w);
	//-- przepisanie ID;
	a.id = w.id;
	w = undefined;	
	return a;
}

