var POPUPLAYER = {};
/**
*   NLC popup layer megjelenites es ajaxal betoltes.
*   Hasznalat: <a href="index.php?block=peti_test2" onclick="return popuplayer.getBlock('peti_test2','oldal=1')"> kuldj lapot </a>
*   2007.11.27. peter.gyorffy::netrix.hu
*/
POPUPLAYER = function () {
    //keprenyo meret
    POPUPLAYER.prototype.getPageSize = function (){
    	var xScroll, yScroll;

    	if (window.innerHeight && window.scrollMaxY) {
    		xScroll = document.body.scrollWidth;
    		yScroll = window.innerHeight + window.scrollMaxY;
    	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    		xScroll = document.body.scrollWidth;
    		yScroll = document.body.scrollHeight;
    	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    		xScroll = document.body.offsetWidth;
    		yScroll = document.body.offsetHeight;
    	}

    	var windowWidth, windowHeight;
    	if (self.innerHeight) {	// all except Explorer
    		windowWidth = self.innerWidth;
    		windowHeight = self.innerHeight;
    	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    		windowWidth = document.documentElement.clientWidth;
    		windowHeight = document.documentElement.clientHeight;
    	} else if (document.body) { // other Explorers
    		windowWidth = document.body.clientWidth;
    		windowHeight = document.body.clientHeight;
    	}

    	// for small pages with total height less then height of the viewport
    	if(yScroll < windowHeight){
    		pageHeight = windowHeight;
    	} else {
    		pageHeight = yScroll;
    	}

    	// for small pages with total width less then width of the viewport
    	if(xScroll < windowWidth){
    		pageWidth = windowWidth;
    	} else {
    		pageWidth = xScroll;
    	}

        var scrOfX = 0, scrOfY = 0;
        if( typeof( window.pageYOffset ) == 'number' ) {
            //Netscape compliant
            scrOfY = window.pageYOffset;
            scrOfX = window.pageXOffset;
        } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
            //DOM compliant
            scrOfY = document.body.scrollTop;
            scrOfX = document.body.scrollLeft;
        } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
            //IE6 standards compliant mode
            scrOfY = document.documentElement.scrollTop;
            scrOfX = document.documentElement.scrollLeft;
        }

    	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,xScroll,yScroll,scrOfX,scrOfY);
    	return arrayPageSize;
    }

    //egy div atteszore allitas. param: div nev, opacity %
    POPUPLAYER.prototype.setOpacity = function(a, b){
        if(b<0){b=0;} if(b>100){b=100;}
        var c=b/100;
        var d=document.getElementById(a);
        if(typeof(d.style.filter)=='string'){d.style.filter='alpha(opacity:'+b+')';}
        if(typeof(d.style.KHTMLOpacity)=='string'){d.style.KHTMLOpacity=c;}
        if(typeof(d.style.MozOpacity)=='string'){d.style.MozOpacity=c;}
        if(typeof(d.style.opacity)=='string'){d.style.opacity=c;}
    }

    //block amit be kell tolteni es parameterek
    POPUPLAYER.prototype.block = '';
    POPUPLAYER.prototype.param = '';

    //layerek body ala szurasa, ha mar bent vannak akkor megmutatas
    POPUPLAYER.prototype.init = function(){
        var pageSize = this.getPageSize();

        //iFrames hirdetesek eltuntetese
       // hideAds(1); jani

        //probaljuk meg megmutatni a layereket. Ha nem sikerul akkor hozzuk letre
        try{
            document.getElementById('popuplayerback').style.display = 'block';
            document.getElementById('popuplayer').style.display = 'block';
            document.getElementById('popuplayer').style.top = (pageSize[7] + 50) + 'px';
            document.getElementById('popuplayerback').style.height = pageSize[1] + 'px';
        }catch(e){
            var objBody = document.getElementsByTagName("body").item(0);

            //fekete hater
            var a = document.createElement('div');
            a.setAttribute('id','popuplayerback');
            a.style.display = 'block';
            a.style.position ='absolute';
            a.style.zIndex = 201;
            a.style.top = 0;
            a.style.left = 0;
            a.style.height = pageSize[1] + 'px';
            a.style.width = pageSize[4] + 'px';
            a.style.background = '#000';

            //Andris
            //a.onclick = function(){popuplayer.hide();}

            objBody.insertBefore(a, objBody.firstChild);
            this.setOpacity('popuplayerback',60);

            //popup lila box
            var b = document.createElement('div');
            b.setAttribute('id','popuplayer');
            b.style.display = 'block';
            b.style.position='absolute';
            b.style.color='#000';
            b.style.width = '500px';
            //b.style.background = "url('/data/img/popup_back_top.gif') top no-repeat";
            b.style.padding = '0';
            b.style.top = (pageSize[7] + 50) + 'px';
            //b.style.left = '100px';
            b.style.left = ((pageSize[0]/2)-250)+'px';
            b.style.zIndex = 202;
            b.setAttribute('onclick', 'clicker(event, 0)');
            objBody.insertBefore(b, objBody.firstChild);

            //Felso lekerekites
            var topFrame = document.createElement('div');
            topFrame.style.height = '13px';
            topFrame.className = 'popuplayer_top';
            topFrame.style.background = 'url(/kepek/bg_popuplayer_top_new.png) no-repeat';
			b.appendChild( topFrame );

            //bezar gomb
            var c = document.createElement('a');
            c.href = 'javascript:void(0)';
            c.setAttribute('id','popuplayerclose');
            c.style.display = 'block';
            c.style.position='absolute';
            c.style.color='#000';
            c.style.zIndex = 102;
            c.style.width = '60px';
            c.style.height = '24px';
            c.style.background = "url('/kepek/popup_bt_close_new.gif') top no-repeat";

        	c.onclick = function(){
        		if ( document.getElementById('forumhozza_tinymce') ) {
  					tinyMCE.execCommand('mceRemoveControl', false, 'forumhozza_tinymce');
        		}
        		popuplayer.hide();
        	}

            c.style.display = 'block';
            c.style.top = '10px';
            c.style.left = '430px';
            //objBody.insertBefore(b, objBody.firstChild);

            b.appendChild(c);

            //sugo,súgó gomb
            /* 
            var c = document.createElement('a');
            c.href = 'javascript:popuplayer.getHelp()';
            c.setAttribute('id','popuplayerhelp');
            c.style.display = 'block';
            c.style.position='absolute';
            c.style.color='#000';
            c.style.zIndex = 102;
            c.style.width = '55px';
            c.style.height = '24px';
            c.style.background = "url('/data/sitebuild/imgs/bg_popuplayer_sugo_icon.gif') no-repeat";
            c.onclick = function(){}
            c.style.top = '10px';
            c.style.left = '365px';
            //objBody.insertBefore(b, objBody.firstChild);
            b.appendChild(c);
			*/
            //tartalmi layer
            var c = document.createElement('div');
            c.setAttribute('id','popuplayercontent');
            c.className = 'clr';
            c.style.minHeight = '100px';
            c.style.width = '420px';
            c.style.border = '1px solid #000000';
            c.style.borderWidth = '0 1px';
            c.style.background = '#b6def8 url(/kepek/bg_popuplayer_bg_new.gif) no-repeat 100% 0';
            b.appendChild(c);

            //doboz alja
            var c = document.createElement('div');
            c.style.background = "url('/kepek/bg_popuplayer_bottom_new.png') no-repeat";
            c.style.height = '13px';
            c.className = 'popuplayer_bottom';
            b.appendChild(c);

            //sugo buborek
            /*
            var c = document.createElement('a');
            c.href = 'javascript:popuplayer.helpClose()';
            c.setAttribute('id','buborekhelp');
            c.style.display = 'none';
            c.style.position='absolute';
            c.style.color='#000';
            c.style.zIndex = 104;
            c.style.width = '275px';
            c.style.height = '122px';
            c.style.background = "url('/imgs/help_buborek.png') no-repeat";
            c.style.top = '10px';
            c.style.left = '165px';
            c.style.textDecoration = 'none';
            c.style.fontWeight = 'normal';
            c.style.padding = '10px;';
            //objBody.insertBefore(b, objBody.firstChild);
            b.appendChild(c);
			*/
        }
    }


    //toltes effect
    POPUPLAYER.prototype.setLoading = function(){
        //ha van form akkor csak rejtsuk el
        var effect = '<p style="padding:100px;"><img src="../kepek/popup_loading.gif"></p>';
        if(this.form){
             
			document.getElementById('popuplayercontent').innerHTML += effect;
           //document.getElementById('popuplayercontent').style.display = 'none';
            //this.form.style.display = 'none'; 
		
        }else{
            document.getElementById('popuplayercontent').innerHTML = effect;
        }

    }

    //popup bezar
    POPUPLAYER.prototype.hide = function(){
    	//Bezárja a buborák helpet.
    	//document.getElementById('buborekhelp').style.display = 'none';
		if (this.parentbezar == 1) {
			try {
				window.opener.location.reload();
				self.close();
			} 
			catch (e) {
				document.location.reload();
			}

		} else {
		//Hirdetesek visszakerulnek.
			//hideAds(0); jani
		    document.getElementById('popuplayerback').style.display = 'none';
		    document.getElementById('popuplayer').style.display = 'none';
       	}
    }

    //ajax init, ha bongeszo nem tamogatja az ajaxot akkor atdobjuk a nem layeres verziora
    POPUPLAYER.prototype.ajax = function(){
        var url = 'index.php?block=' + this.block + '&param=' + this.param + '&' + this.param;
        try {
    			
			xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    	} catch (e) {
    		//Ha nincs ajax akkor atdobjuk a usert a block oldalra
           
		   window.location = url;
    	}
    	xmlhttp.onreadystatechange = this.showResults;
    	xmlhttp.open("GET", url + '&ajax=1', true);
    	xmlhttp.send(null);

    }

    POPUPLAYER.prototype.form = '';

    //ajax post form. Elkuldi a megadott form-ot postba. Ha van fajlfeltolto iframe akkor eloszor azt kuldi majd postolja az adatokat
    // a checker paraméter ha 1 akkor ellenőrzi hogy ki vannak-e töltve a mezők a layeres cikk küldés oldalon, csak itt használjuk
    POPUPLAYER.prototype.post = function(formObj, checker){
        if(formObj) this.form = formObj;
        
        	// cikk küldés mező validátor

            var url = '/index.php?block=' + this.block + '&param=' + this.param;
          
		   var parameters = '';
            for(i=0;i<this.form.length;i++){
                if(this.form.elements[i].type == 'text' || this.form.elements[i].type == 'select-one' || this.form.elements[i].type == 'hidden' || this.form.elements[i].type == 'textarea' || this.form.elements[i].type == 'password' ){
                	parameters += '&' + this.form.elements[i].name + '=' + encodeURIComponent(this.form.elements[i].value);
                }
                if(this.form.elements[i].type == 'radio'){
                    if(this.form.elements[i].checked) parameters += '&' + this.form.elements[i].name + '=' + encodeURIComponent(this.form.elements[i].value);
                }
                if(this.form.elements[i].type == 'checkbox'){
                    if(this.form.elements[i].checked) parameters += '&' + this.form.elements[i].name + '=' + encodeURIComponent(this.form.elements[i].value);
                }
            }

            try {
        		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
        	} catch (e) {
        		//Ha nincs ajax akkor posotolja az adatokat
               return true;
        	}
        	//alert(parameters);
			xmlhttp.onreadystatechange = this.showResults;
        	xmlhttp.open("POST", url + '&ajax=1', true);
        	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.setRequestHeader("Content-length", parameters.length);
            xmlhttp.setRequestHeader("Connection", "close");
            xmlhttp.send(parameters);
            this.setLoading();

            //Andris: ha ezt nem tettem ide, akkor az onsubmit="return popuplayer.post(this)"
            //siman elpostolta a formot ajax hivas nelkul.. :(
            return false;
    }

    //ajax callback. Megmutajuk az eredmenyt
	POPUPLAYER.prototype.showResults = function(){
		if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
            document.getElementById('popuplayercontent').innerHTML = xmlhttp.responseText;
            document.getElementById('popuplayercontent').style.display = 'block';
            popuplayer.resizeBack();
			//initCaptcha();
			//ha van scrollbox
            try{
                document.getElementById('scrollbox_popup').innerHTML;
                setTimeout(function() {var scroll1 = new scrollbox( 'scrollbox_popup', 2 );},  100);
            }catch (e) {}

            //bug:#174. Ha hiba van akkor a form tetejere scrollozunk
            window.scrollTo(0,parseInt(document.getElementById('popuplayer').style.top)-50);

            // JS futtatasa a responsebol
            var resp = xmlhttp.responseText;
			var ra = resp.split('\n');
			var inJs = false;
			var js = '';
			var i=0;
			for(i=0; i<ra.length; i++) {
				if(ra[i].match(/<\/script/i)) inJs = false;	
                if(inJs==true) js += ra[i]+"\n";
				if(ra[i].match(/<script/i)) inJs = true;
            }
			try {
				eval(js);
			} catch(e) {
				alert(e);
			}
        }
    }

    //ajax get form. Elkuldi a megadott form-ot es megjeleniti popupban az eredmenyt
    POPUPLAYER.prototype.getForm = function(block,form){
        var parameters = '';
        if(form) this.form = form;
        if(block) this.block = block;
        for(i=0;i<this.form.length;i++){
            if(this.form.elements[i].type == 'text' || this.form.elements[i].type == 'select-one' || this.form.elements[i].type == 'hidden' || this.form.elements[i].type == 'textarea' || this.form.elements[i].type == 'password' ){
              	parameters += '&' + this.form.elements[i].name + '=' + encodeURIComponent(this.form.elements[i].value);
            }
        }
		
        this.getBlock(block,parameters);
        return false;
    }

    //ezt a metodust hivjuk meg ha be akkorunk tolteni egy popupot
    POPUPLAYER.prototype.getBlock = function(block,param){
        this.init();
        this.setLoading();
      
		this.block = block;
        this.param = param;
        if (this.param.indexOf('parentbezar=1') != -1) {
        	this.parentbezar = true;
        	this.param = this.param.replace('/parentbezar=1/', '');
        } else {
        	this.parentbezar = false;
        }
        	
        this.ajax();
        return false;
    }

    //popuplayer.init();

    //bug: atmeretezi a fekete hateret, ablak atmeretezesnel, meg az ajax hivas utan hivjuk meg
    POPUPLAYER.prototype.resizeBack = function(){
        var pageSize = popuplayer.getPageSize();
        try{
            document.getElementById('popuplayer').style.top = (pageSize[7] + 50) + 'px';
            document.getElementById('popuplayer').style.left = ((pageSize[0]/2)-250)+'px';
            document.getElementById('popuplayerback').style.height = pageSize[1] + 'px';
            document.getElementById('popuplayerback').style.width = pageSize[4] + 'px';
        }catch (e) {}
    }

}

var popuplayer = new POPUPLAYER();
//bug: atmeretezesnel kovesse a hater az ablak merete
window.onresize = function(){
    popuplayer.resizeBack();
}

