var POPUPLAYER = {};
/**
*   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);

        //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 = '600px';
            b.style.padding = '0';
            // b.style.top = (pageSize[7] + 50) + 'px';
            b.style.top = '125px';
            b.style.left = '200px';
            // b.style.left = ((pageSize[0]/2)-250)+'px';
            b.style.zIndex = 202;
            objBody.insertBefore(b, objBody.firstChild);

            //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.onclick = function(){popuplayer.hide();}
            c.style.display = 'block';
            c.style.top = '10px';
            c.style.left = '530px';
            //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 = '600px';
            c.style.border = '0px solid #d4b2b1';
            c.style.borderWidth = '0 0';
            //c.style.background = 'inherit';
            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('/static/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);

        }
    }
    
    //a felso sugo gombra nyomva megjelenik egy buble, amivel ajaxal betoltve a sugo szoveg
    POPUPLAYER.prototype.getHelp = function(){        
        try {
    		xmlhttp2 = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    	} catch (e) {}
    	xmlhttp2.onreadystatechange = this.getHelpResults;
    	xmlhttp2.open("GET", 'index.php?block=block_help' + '&param=' + this.block + '&ajax=1', true);
    	xmlhttp2.send(null);
    }
    
    //visszakapott ajax hivas utan megjeleniti a szoveget
    POPUPLAYER.prototype.getHelpResults = function(){
        if ((xmlhttp2.readyState == 4) && (xmlhttp2.status == 200)) {
            if(xmlhttp2.responseText.length>2){
                document.getElementById('buborekhelp').style.display = 'block';
                document.getElementById('buborekhelp').innerHTML = xmlhttp2.responseText;
            }
        }
    }
    
    //bezarja a buborekot
    POPUPLAYER.prototype.helpClose = function(){
        document.getElementById('buborekhelp').style.display = 'none';
    }

    //toltes effect
    POPUPLAYER.prototype.setLoading = function(){
        //ha van form akkor csak rejtsuk el
        var effect = '<p style="padding:100px;"><img src="/data/img/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(){
    	//Hirdetesek visszakerulnek.
		hideAds(0);
        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
    POPUPLAYER.prototype.post = function(formObj){
        if(formObj) this.form = formObj;

            //fajl feltoltes
            var i=0;
            var element = false;
            var regexp = /^fileupload_name_(.*)/i;
            var fileuploadName = false;
            var iframe = false;
            try {
				tinyMCE.triggerSave(false,true);
            } catch(e) {}
            for(i=0; i<this.form.elements.length; i++) {
                element = this.form.elements[i];
                if(regexp.test(element.id)) {
                    fileuploadName = element.id.replace(regexp, '$1');
                    iframe = document.getElementById('fileupload_iframe_'+fileuploadName);
                    if(iframe) {
                      try{
                        iframe.contentDocument.getElementById('uploadform').value;
                        iframe.contentDocument.getElementById('uploadform').onsubmit();
                        iframe.contentDocument.getElementById('uploadform').submit();
                        return false;
                      }catch(e) {}
                      try {
                        iframe.contentWindow.document.getElementById('uploadform').value;
                        iframe.contentWindow.document.getElementById('uploadform').onsubmit();
                        iframe.contentWindow.document.getElementById('uploadform').submit();
                        return false;
                      } catch(e) {}
                    }

                }
            }

            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;
        	}
        	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();

            //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) 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;
        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();
}
