/*
 * Funkcje formularza masowych;
 * 
 * 
 */

//-- kopia funkcji z core.js
function _get(ident){ return document.getElementById(ident); } 

function wyslij( etap )
{
	document.getElementById('etap').value = etap
	document.getElementById('formularz_masowe').submit();
}

TListaZdjec	= function(param)
{
	this.obj	= _c('div',{className:'lista_zdjec'});
	//--
	this.obj.add_zdjecie	= function( param )
	{
		var zdj = new TFormZdjecie( {nr:this.nr_kolejny++,parent:this} );
		if (param != undefined)
			if (param['z']!=undefined)
			{
				zdj.z.value.value 	= param['z'];
				if (param['p'] != '') zdj.z.img.src		= param['p'];
				zdj.opis.ta.value	= param['o'];
				if (param['p'] != '') zdj.z.preview.value	= param['p'];
			} 
		this.lista.push( zdj );
		this.apC( zdj );
		//--
		this.ngl.update();
	}
	this.obj.del_zdjecie	= function( nr )
	{
		var nt = new Array();
		for( var i in this.lista)
			if (typeof(this.lista[i])!='function')
				if (this.lista[ i ].nr != nr) nt[nt.length] = this.lista[ i ];
				else this.removeChild( this.lista[ i ] );
		this.lista = nt;
		this.ngl.update();
	}
	this.obj.liczba_zdjec	= function() { return this.lista.length; }
	this.obj.load	= function( dane )
	{
		for (var i in dane)
		{
			if (typeof(dane[i])=='funciton') continue;
			this.add_zdjecie( dane[i] );
		}
	}
	this.obj.create	= function()
	{
		this.lista	= new Array();
		this.nr_kolejny	= 0;
		this.ngl	= _c('div',{parent:this,className:'ngl'});
		this.ngl.update	= function()
		{
			this.innerHTML = '';
			this.apC( _cTxt(' Obecna liczba zdjęć: '+ this.parent.liczba_zdjec() )  );
		}
		this.ngl.update();
		//--
		this.a	= _c('a',{parent:this,className:'dodaj'});
		this.a.apC( _cTxt('Dodaj nowe zdjęcie') );
		this.a.onclick	= function() { this.parent.add_zdjecie(); this.parent.ngl.update(); }
		//--
		this.apC( this.ngl );
		this.apC( this.a );
	}
	this.obj.create(param);
	return this.obj;
}

TOknoUpload	= function(param)
{
	this.obj	= _c('div');
	this.obj.create	= function( param )	
	{
		this.setParam(param);
		var link = '/masowe/upload/name_zd/zdjecie_'+this.parent.parent.nr+'/name_zd_upl/zdjecie_upl_'+this.parent.parent.nr;
		this.iframe		= _c('iframe',{src:link});
		this.icon_close	= _c('a',{parent:this,className:'zamknij'});
		this.icon_close.apC( _cTxt('[zamknij okno]') );
		this.icon_close.onclick	= function(){ this.parent.close();}
		this.apC( _cTxt( 'Wybierz i następnie dołącz zdjęcie w rozmiarze do 500kB.' ) );
		this.apC( this.iframe );
		this.apC( this.icon_close );
	}
	this.obj.close	= function(){this.parent.removeChild(this); }
	this.obj.create(param);
	return this.obj;	
}

TZdjecie	= function(param)
{
	this.obj	= _c('div');
	this.obj.create	= function( param )	
	{
		this.src	= '/images/ikony/kupon/ze_zdjeciem.jpg';
		this.setParam(param);
		var _id = 'zdjecie_'+this.parent.nr;
		var _idv = 'zdjecie_upl_'+this.parent.nr;
		var _pidv = 'pzdjecie_upl_'+this.parent.nr;
		var _name = 'zdjecie_upl['+this.parent.nr+']';
		var _pname = 'pzdjecie_upl['+this.parent.nr+']'; 
		this.value		= _c('input',{type:'hidden',name:_name,id:_idv});
		this.preview	= _c('input',{type:'hidden',name:_pname,id:_pidv});
		this.img = _c('img',{src:this.src,id:_id,parent:this});
		this.img.onclick	= function()
		{
			this.parent.okno_upload = new TOknoUpload({className:'okno_upload',parent:this.parent});
			this.parent.apC( this.parent.okno_upload );
		}
		this.o	= _c('p');
		this.a	= _c('a',{parent:this});
		this.o.apC( this.a );
		this.a.apC( _cTxt('Dodaj / zmień zdjęcie') );
		this.a.onclick	= function()
		{
			this.parent.okno_upload = new TOknoUpload({className:'okno_upload',parent:this.parent});
			this.parent.apC( this.parent.okno_upload );
		}
		this.apC( this.img );
		this.apC( this.o ); 
		this.apC( this.value );
		this.apC( this.preview );
	}
	this.obj.set	= function( dane )
	{
		this.value.value = dane['v'];
		this.img.src = dane['src'];
		this.preview.value	= dane['src'];
		this.preview.form.submit();
	}
	this.obj.create(param);
	return this.obj;
}

TOpisZdj	= function(param)
{
	this.obj	= _c('div');
	this.obj.create	= function( param )	
	{
		this.setParam(param);
		this.ta = _c('textarea');
		this.ta.name	= 'opis_zdj['+this.parent.nr+']';
		this.apC( this.ta ); 
	}
	this.obj.create(param);
	return this.obj;	
}

TFormZdjecie	= function(param)
{
	this.obj	= _c('div',{className:'form_zdjecie'});
	this.obj.create	= function( param )
	{
		this.setParam( param );
		this.z		= new TZdjecie({className:'zdjecie',parent:this});
		this.opis	= new TOpisZdj({className:'opis_zdjecia',parent:this});
		this.usun	= _c('a',{className:'usun',parent:this});
		this.usun.apC(_cTxt('usuń zdjęcie'));
		this.usun.onclick	= function(){this.parent.del();}
		//--
		this.uchwyt	= this;
		this.apC( this.z );
		this.apC( this.opis );
		this.apC( this.usun );
	}
	this.obj.del	= function() {this.parent.del_zdjecie( this.nr );}
	//--
	this.obj.create(param);
	return this.obj;
}




/* stare funkcje */
function Show( id )
	{ _get(id).style.display='inline'; }
function Hide( id )
	{ _get(id).style.display='none'; }

function load_sel(ident,tab,first){
  var l=(first==1)? 0:1;        
  var f = _get(ident);
  if(f!=null && tab!=null){
    f.length=tab.length+l;
    f[0].value='';
    f[0].text='- wybierz -';
    for (var i=0; i<tab.length; i++){
	f[i+l].value=tab[i];
	f[i+l].text =tab[i];
	}
    }
}

function ListInBack(ident,Ttab){
    var Iinput=_get(ident);
    var Iselect=_get(ident+'_sel');
    load_sel(ident+'_sel',Ttab,0);				
    if(Iinput==null) return;
    //alert(Iinput.value);
    Iselect.value=Iinput.value;
    if(Iselect.selectedIndex==0 && Iinput.value!='')
    { Show('inna_'+ident+'_layer'); Hide('wybierz_'+ident+'_layer'); }
}
