//copyright, BENE ÁDÁM © - all rights reserved
//BPortal JavaScript cuccok

function $(p){// $({ string url:'', int cid:1, {} method: , {} params: , string dst: 'hsz24' , bool jump})
if(!(t.n(p.cid)||t.s(p.cid))||!t.s(p.url))return false;//hiba
    var a=new TAjax();
    var f=null;
    if((t.u(p.dst)&&(p.cid==1000)||(p.dst==1000))){
		a.onReady=function(t){
			portal.modal.setContent(t);
			with(portal.modal.getWindowElement().style){}
			portal.modal.show();
		}
		a.onSend=function(){
			portal.modal.setContent(portal.loading_img);
			with(portal.modal.getWindowElement().style){}
			portal.modal.show();
		}
    } else {
		var dst='c'+p.cid;
		if(t.s(p.dst))dst=p.dst;
		a.onError=function(){
			//f.error(p.cid); <<<<<<------ kiegészítendő a TFixer.error !!!!!!!!!!!!!!!!!!!!
			alert('Hiba!');
		}
		a.onReady=function(t){
			document.getElementById(dst).innerHTML=t;
			f.unfix();
			f.destruct();
			f=null;
		}
		a.onSend=function(){
			var d=document.getElementById(dst);
			if(p.jump===true){
				try{
					window.scrollTo(getElementLeft(d),getElementTop(d));
				}catch(e){}
			}
			f=new TFixer(d);
			f.fix();
		}
    }
    var _url=p.url;
	if(_url.indexOf('://')==-1&&_url.charAt(0)!='/')_url='/'+_url;
    var i=_url.lastIndexOf('#');
    if(i>-1)_url=_url.substring(0,i);
    var ar={};
    if(t.o(p.params))ar=p.params;
    ar['mode']='ajax';
    ar['cid']=p.cid;
    if(t.s(p.method))ar['method']=p.method;
    a.send(_url,{},ar);
    return false;
}

function getContent(url,cid,method,params){
    var a=new TAjax();
    var f=null;

    if(cid==1000){
	a.onReady=function(t){
	    portal.popup.setContent(t);
	    portal.popup.show();
		portal.popup.setPos({top:getPageTop()+'px',left:'50%'});
			
	/*var o=document.getElementById('c'+cid);
	    o.innerHTML=t;
	    o.style.display='block';*/
	}
	a.onSend=function(){
	    portal.popup.setContent(portal.loading_img);
	    portal.popup.show();
		portal.popup.setPos({top:getPageTop()+'px',left:'50%'});
	}
    } else {
	a.onError=function(){
	    f.error(cid);
	}
	a.onReady=function(t){
	    var o=document.getElementById('c'+cid);
	    o.innerHTML=t;
	    f.unfix();
	    f.destruct();
	    f=null;
	}
	a.onSend=function(){
	    var o=document.getElementById('c'+cid);
	    f=new TFixer(o);
	    f.fix();
	//if(o)o.innerHTML=portal.loading_img;
	}
    }
    var _url=url;
	if(_url.indexOf('://')==-1&&_url.charAt(0)!='/'){
		_url='/'+_url;
	}
    var i=_url.lastIndexOf('#');
    if(i>-1){
		_url=_url.substring(0,i);
    }
    var ar={};
    if(params)ar=params;
    ar['mode']='ajax';
    ar['cid']=cid;
    if(method)ar['method']=method;
    a.send(_url,{},ar);
    return false;
}

function setContent(form,url,cid){
    var a=new TAjax();

    if(cid==1000){
	a.onReady=function(t){
	    portal.popup.setContent(t);
	    portal.popup.show();
	/*var o=document.getElementById('c'+cid);
	    o.innerHTML=t;
	    o.style.display='block';*/
	}
	a.onSend=function(){
	    portal.popup.setContent(portal.loading_img);
	    portal.popup.show();
	}
    } else {
	a.onReady=function(t){
	    var o=document.getElementById('c'+cid);
	    o.innerHTML=t;
	}
	a.onSend=function(){
	    var o=document.getElementById('c'+cid);
	    if(o)o.innerHTML=portal.loading_img;
	}
    }
    var arr=new Array();
    arr=formToArray(form);
    arr['mode']='ajax';
    arr['cid']=cid;
    a.send(url,{},arr);
    return false;
}

var TControl = Class({
    construct: function (par) {
	this.el=document.createElement('div');
	if(par&&par.events)this.events=par.events;
	with(this.el.style){
	    //display='block';
	    display='inline';
	    textAlign='center';
	    /*position='fixed';
	    left='100px';
	    top='100px';
	    width='200px';
	    height='300px';*/
	    padding='0px';
	    margin='0px';
	    }
	//this.hide();
	try{
	    this.parent_el=par.getElement();//Ha másik TControl a parent
	    this.parent_el.appendChild(this.el);
	}catch(e){
	    try{
		par.appendChild(this.el);//Ha par elem akkor ez nem dob hibát
		this.parent_el=par;
	    }catch(ee){
		this.parent_el=document.getElementsByTagName('body')[0];//Egyébként megy a body-ba
		this.parent_el.appendChild(this.el);
	    }
	}
    },
    destruct: function () {
	this.parent_el.removeChild(this.el);
    },
    getElement: function () {
	return this.el;
    },
    show: function () {
	this.visible=true;
	this.el.style.visibility='visible';
	try{
	    this.events.onShow();
	//.apply(this,[]);
	}catch(e){}
    },
    hide: function () {
	this.visible=false;
	this.el.style.visibility='hidden';
	try{
	    this.events.onHide();
	}catch(e){}
    },
    setContent: function (o) {
	if(t.s(o))this.el.innerHTML=o;
	else
	    try{
		this.el.appendChild(o);
	    }catch(e){
		return false;
	    }
    },
    setPos: function (p) {
	if(p=='center'){

	} else {
	    if(p.left)this.el.style.left=p.left;
	    if(p.top)this.el.style.top=p.top;
	}
    },
    setSize: function (p) {
	if(p.width)this.el.style.width=p.width;
	if(p.height)this.el.style.height=p.height;
    },
    setOver: function (el) {
	try{
	    this.el.style.width=el.offsetWidth+'px';
	    this.el.style.height=el.offsetHeight+'px';
	    this.el.style.left=getElementLeft(el)+'px';
	    this.el.style.top=getElementTop(el)+'px';
	    return true;
	}catch(e){
	    return false;
	}
    },
    setStyle: function (s,ds) {/*style, default style*/
	if(t.u(s))return false;
	var res=true;
	for(var i in s){
	    try{
		if(!t.u(s[i])){
		    if(getNav()===NAV_IE && i.toLowerCase()=='opacity'){//IE-s opacity-kezelés
			var o=parseFloat(s[i]);
			o=Math.round(o*100);
			this.el.style['filter']='alpha(opacity='+o+')'
		    } else{
			this.el.style[i]=s[i];
		    }
		}
	    }catch(e){
		if(!t.u(ds)&&!t.u(ds[i]))this.el.style[i]=ds[i];
		res=false;
	    }
	}
	return res;
    }
});

var TWindow = Class({
    construct: function (p, p2) {
	this.parent.construct.apply(this,[p]);

	this.handler=p.handler;
	this.id=p.id;

	//this.el=document.createElement('div');
	this.inner=new TControl(this);
	this.el2=this.inner.getElement();//document.createElement('div');
	//this.inner.show();
	//this.el2.id='c1000';
	with(this.el2.style){
	    display='block';
	    backgroundColor='white';
	    padding='10px';
		
	    /*width='400px';
	    height='300px';*/

	    /*borderRadius='3px';
	    MozBorderRadius='3px';
	    webkitBorderRadius='3px';*/

	    overflow='auto';
		
	    //border='1px solid gray';
	    }
	with(this.el.style){
	    display='block';
	    position='fixed';
	    /*left='100px';
	    top='100px';*/
	    width='';
	    height='';
	    //padding='7px';
		
	    /*var shadow='2px 2px 20px rgba(0,0,0,0.4)';

	    MozBoxShadow=shadow;
	    webkitBoxShadow=shadow;
	    boxShadow=shadow;

	    borderRadius='10px';
	    MozBorderRadius='10px';
	    webkitBorderRadius='10px';

	    border='1px solid gray';*/
	    /*
	    try{
		backgroundColor='rgba(150,150,150,0.8)';
	    }catch(e){
		backgroundColor='rgb(150,150,150)';
	    }*/
	    }
	this.el.appendChild(this.el2);
	document.getElementsByTagName('body')[0].appendChild(this.el);

	this.hide();
	this.inner.setStyle(p2.styleI);
	this.setStyle(p2.styleO);
    },
    /*show: function () {
	this.visible=true;
	this.el.style.visibility='visible';
    },
    hide: function () {
	this.visible=false;
	this.el.style.visibility='hidden';
    },*/
    setContent: function (t) {
	this.el2.innerHTML=t;
    },
    setPos: function (p) {
	if(p=='center'){

	} else {
	    this.setStyle({
		left:p.left,
		top:p.top
	    });
	}
    },
    setSize: function (p) {
	if(p.width)this.el2.style.width=p.width;
	if(p.height)this.el2.style.height=p.height;
    }
});
TWindow.Extends(TControl);

var TBlackBg = Class({
    construct: function () {
	this.parent.construct.apply(this,[]);

	this.setPos({
	    left:'0px',
	    top:'0px'
	});
	this.setSize({
	    width:'100%',
	    height:'100%'
	});
	this.setStyle({
	    position:'fixed',
	    backgroundColor:'black',
	    opacity:0.7
	},{});
	this.show();
    }
});
TBlackBg.Extends(TControl);

var TWindowHandler = Class({
    construct: function () {
	this.windows=[];
	this.bb=new TBlackBg();
	this.bb.hide();
    //alert(this.bb.visible);
    },
    createWindow: function (p) {
	var nw=new TWindow({
	    handler:this,
	    id:this.windows.length,
	    events:p.events
	},p);
	nw.setPos(p.metrics);
	nw.setSize(p.metrics);
	nw.el.style.zIndex='1000';
	this.windows.push(nw);
	return nw;
    }
});

var TFixer = Class({
    construct: function (el,bFix) {
	this.el=el;
	this.width='auto';
	this.height='auto';
	if(bFix)this.fix();
	this.c=new TControl();

	this.cc=new TControl(this.c);
	this.cc.setContent('');
	this.cc.setStyle({
	    background:"url('"+portal.loading_img_src+"') center center no-repeat",
	    position:'relative',
	    display:'table-cell',
	    verticalAlign:'middle',
	    left:'0px',
	    top:'0px',
	    width:'100%',
	    height:'100%'
	});
	this.cc.show();

	this.c.setStyle({
	    position:'absolute',
	    display:'table',
		border:'1px solid #eeeeee',
	    background:"url('/base/bg.png') repeat center center"
	});
    //if(!this.c.setStyle({backgroundColor:"rgba(255,255,255,0.5)"})) this.c.setStyle({backgroundColor:"rgb(255,255,255)",filter:'alpha(opacity=50)'});
    },
    destruct: function () {
	this.cc.destruct();
	this.c.destruct();
    },
    fix: function (content) {
	if(t.u(this.el))return false;
	this.c.setOver(this.el);
	this.width=this.el.style.width;
	this.height=this.el.style.height;
	/*var w=this.el.offsetWidth;
		this.el.style.width=w+'px';
		var h=this.el.offsetHeight;
		this.el.style.height=h+'px';*/
	if(content)this.c.setContent(content);
	this.c.show();
	return true;
    },
    unfix: function () {
	if(t.u(this.el))return false;
	/*this.el.style.width=this.width;
		this.el.style.height=this.height;*/
	this.c.hide();
	return true;
    },
    error: function (cid) {
	this.cc.setStyle({
	    backgroundImage:"none"
	});
	this.el_error=document.createElement('div');

	this.el_error_txt=document.createElement('div');
	this.el_error_txt.className='error';
	this.el_error_txt.innerHTML='Nem sikerült megjeleníteni a kért adatokat.';

	this.el_error_btn=document.createElement('input');
	this.el_error_btn.type='button';
	this.el_error_btn.className='button again';
	this.el_error_btn.value='Próbálja újra!';
	var _t=this;
	this.el_error_btn.onclick=function(){
	    _t.destruct();
	    getContent(location+'',cid);
	};
		
	this.el_error.appendChild(this.el_error_txt);
	this.el_error.appendChild(this.el_error_btn);

	this.cc.setContent(this.el_error);
    //this.cc.setContent('<div class="error">Nem sikerült megjeleníteni a kért adatokat.</div><input class="button again" type="button" value="Próbálja újra!" onclick="javascript:getContent(location+\'\','+cid+')"/>');
    }
});

var portal={
    loaded:false,
    wh:null,
    popup:null,
    refresh:function(cid,method,params){
	if(arguments.length==0){
	    location.href+='';
	    return false;
	}else{
	    return getContent(location.href,cid,method,params);
	}
    },
    loading_img_src:'/images/loading.gif',
    loading_img:'<img src="/images/loading.gif"/>'
}

function _onload(){
if(getNav()==NAV_IE&&getNavVer()<7){
	alert('A portál megfelelő működéséhez válassz egy XXI. századi böngészőt!');
}

    portal.loaded=true;
    portal.wh=new TWindowHandler();
    portal.popup=portal.wh.createWindow({
	metrics:{
	    top:'20px',
	    left:'50%'
	},
	styleO:{
	    position:'absolute',
	    /*marginTop:'10%',*/
	    marginLeft:'-450px'
	},
	styleI:{
	    //marginLeft:'-150px',
	    display:'block',
	    width:'900px'
	//height:'550px'
	},
	events:{
	    onShow:function(){
			portal.wh.bb.show();
	    },
	    onHide:function(){
			portal.wh.bb.hide();
	    }
	}
    });
    portal.popup.hide();
	
	portal.modal=new TCenterModalWindow();
	portal.modal.setContent('asdfg');
	portal.modal.getWindowElement().className='center_modal_window';
	//portal.modal.show()
}

function _onkeydown(e){
    if(getKey(e)==27)portal.modal.hide();
}

var TPictureDeleter = Class({
    construct: function (p) {//pictures,el,form,gallery_dir
	var _t=this;
	this.p=p;

	if(!this.p.form.picture_del){
	    var el=document.createElement('input');
	    el.type='hidden';
	    el.name='picture_del';
	    this.p.form.appendChild(el);
	}

	this.pictures=[];
	var imgs=[];
	for(var i=0;i<this.p.pictures.length;i++){
	    var el=document.createElement('div');
	    with(el.style){
		background='silver';
		display='block';
		overflow='hidden';
		border='2px solid black';
		}

	    var file_c=document.createElement('div');
	    with(file_c.style){
		fontSize='12px';
		}
	    //file_c.innerHTML='<input type="button" value="Kép cseréje"/>';

	    var file=document.createElement('input');
	    file.type='file';
	    file.name=this.p.fileInputNames[i];
	    file.onchange=function(){
		_t.onChange(this);
	    };
	    with(file.style){
		background='silver';
		display='inline';
		//width='100px';
		/*position='absolute';
		opacity='0';
		filter='alpha(opacity=0)';*/
		}

	    var change=document.createElement('div');
	    //change.type='button';
	    change.innerHTML='Kép cseréje:';
	    file_c.appendChild(change);

	    file_c.appendChild(file);

	    var img;
	    try{
		img=new Image();
	    }catch(e){
		img=document.createElement('img');
	    }
	    img.style.cursor='pointer';
	    var o={
		src:this.p.pictures[i],
		img:img,
		file:file,
		el:el,
		bDelete:false,
		onClick:function(){
		    if(this.file.value!=''&&this.bDelete){
			this.bDelete = true;
		    }else{
			this.bDelete = !this.bDelete;
		    }
		    //this.img.style.border=this.bDelete?'2px solid red':'';
		    this.img.style.opacity=this.bDelete?'.5':'1';
		    this.img.style['filter']=this.bDelete?'alpha(opacity=50)':'';
		    if(_t.p.form.picture_del)_t.p.form.picture_del.value=_t.getDeletableJSON();
		},
		onLoad:function(){
		/*var w=this.img.offsetWidth+'px';
		    this.el.style.width=w;
		    this.file.style.width=w;*/
		},
		onChange:function(){
		    if(this.file.value!=''&&!this.bDelete){
			this.onClick();
		    }
		}
	    };
	    this.pictures.push(o);
	    img.src=this.p.gallery_dir+'/'+this.p.pictures[i];
	    img.onclick=function(){
		_t.onClick(this);
	    };
	    img.onload=function(){
		_t.onLoad(this);
	    };
	    
	    var txt=document.createElement('div');
	    with(txt.style){
		background='black';
		color='white';
		fontSize='10px';
		wordWrap='break-word';
		padding='3px';
		cursor='default';
		//width='100% !important';
		}
	    txt.innerHTML=this.p.pictures[i];
	    
	    el.appendChild(img);
	    el.appendChild(txt);
	    el.appendChild(file_c);
	    this.p.el.innerHTML="<div>Képek törlése, cseréje</div>";
	    this.p.el.appendChild(el);
	}
	this.p.form.picture_del.value=this.getDeletableJSON();
    },
    onClick: function(el){
	for(var i=0;i<this.pictures.length;i++){
	    if(el===this.pictures[i].img){
		this.pictures[i].onClick();
	    }
	}
    },
    onLoad: function(el){
	for(var i=0;i<this.pictures.length;i++){
	    if(el===this.pictures[i].img){
		this.pictures[i].onLoad();
	    }
	}
    },
    onChange: function(el){
	for(var i=0;i<this.pictures.length;i++){
	    if(el===this.pictures[i].file){
		this.pictures[i].onChange();
	    }
	}
    },
    getDeletableJSON: function(){
	var a=[];
	var b=[];
	for(var i=0;i<this.pictures.length;i++){
	    if(this.pictures[i].bDelete===true){
		a.push(this.pictures[i].src);
		b.push(this.p.fileInputNames[i]);
	    }
	}
	var sa=a.join("','");
	if(sa!='')sa="'"+sa+"'";

	var sb=b.join("','");
	if(sb!='')sb="'"+sb+"'";

	var s="{pictures:["+sa+"],fileInputNames:["+sb+"]}";

	return s;
    }
});

    function goTab(block,tabno){
		 var li = document.getElementsByClassName('here');
		  for ( i=0;i<li.length;i++) {li[i].className='';}

//		 var o=document.getElementById('tab'+tabno);
		 	 //o.className='show_content';
		 var o=document.getElementById('li'+block+tabno);
			 o.className='here';
        }

