var POPUPLAYER = {};

POPUPLAYER = function () {
    
    POPUPLAYER.prototype.block = '';
    POPUPLAYER.prototype.param = '';
    POPUPLAYER.prototype.paramhistory = '';
    POPUPLAYER.prototype.blockhistory = '';
    POPUPLAYER.prototype.xmlhttp = '';
    POPUPLAYER.prototype.reload = 0;
    
    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;
    }

    
    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;}
    }



    
    POPUPLAYER.prototype.init = function(){
            
        try{
            document.getElementById('popup').style.display = 'block';
            
        }catch(e){
            var objBody = document.getElementsByTagName("body").item(0);
            var a = document.createElement('div');
            a.innerHTML = '<div class="container"><div class="content"><div class="title" id="popuptitleframe"><span class="left" id="popuptitle">&nbsp;</span><a href="javascript:popuplayer.hide()" title="Close"><img src="img/popup_close.gif" alt="x" class="right"/></a><br /></div><div id="popupcontent">&nbsp;</div></div></div><div class="foot"></div>';
            a.setAttribute('id','popup');
            objBody.insertBefore(a, objBody.firstChild);
            DragHandler.attach(document.getElementById('popup'), document.getElementById('popuptitleframe'));
            this.resizeBack();
        }
    }

    
    POPUPLAYER.prototype.setLoading = function(){
       
        if(this.form){
            this.setTitle('<img src="img/ajax_load.gif" /> <span style="font-weight:normal">Sending data ...</span>');
        }else{
            this.setTitle('<img src="img/ajax_load.gif" /> <span style="font-weight:normal">Loading ...</span>');
        }
    }

    
    POPUPLAYER.prototype.hide = function(){
        //document.getElementById('popuplayerback').style.display = 'none';
        document.getElementById('popup').style.display = 'none';
        document.getElementById('popupcontent').innerHTML = '';
        if(this.reload == 1) window.location.reload();
    }
    
    //title
    POPUPLAYER.prototype.setTitle = function(title){
        document.getElementById('popuptitle').innerHTML = title;
    }

    
    POPUPLAYER.prototype.ajax = function(){
        var url = '' + this.block + '.php?' + this.param;
        try {
    		this.xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    	} catch (e) {    		
            window.location = url;
    	}
        
    	this.xmlhttp.onreadystatechange = this.showResults;
    	this.xmlhttp.open("GET", url, true);
    	this.xmlhttp.send(null);

    }

    POPUPLAYER.prototype.form = '';

    
	POPUPLAYER.prototype.showResults = function(){
		if ((popuplayer.xmlhttp.readyState == 4) && (popuplayer.xmlhttp.status == 200)) {
            document.getElementById('popupcontent').innerHTML = popuplayer.xmlhttp.responseText;
            document.getElementById('popupcontent').style.display = 'block';
           
    
            window.scrollTo(0,parseInt(document.getElementById('popup').style.top)-90);

    
            var resp = popuplayer.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) {
				console.log(e);
			}
        }
    }

    
    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.loadBlock(block,parameters);
        return false;
    }
    
    POPUPLAYER.prototype.post = function(formObj){
        if(formObj) this.form = formObj;
            var url = '' + this.block + '.php?' + 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);
                }
                if(formObj.elements[i].alt == 'req' && formObj.elements[i].value.length == 0) {
                    formObj.elements[i].style.border = '1px solid #c00';
                    alert('Error: Field can\'t be empty!');
                    return false;
                }
            }
            
            try {
                var pass1 = formObj.password;
                var pass2 = formObj.password2;
                if(pass1.value != pass2.value){
                    pass2.style.border = pass1.style.border = '1px solid #c00';
                    alert('Error: Password dosen\'t match!');
                    return false;            
                }            
            }catch (e) {}

            try {
        		this.xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
        	} catch (e) {
               return true;
        	}
        	this.xmlhttp.onreadystatechange = this.showResults;
        	this.xmlhttp.open("POST", url + '&ajax=1', true);
        	this.xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            this.xmlhttp.setRequestHeader("Content-length", parameters.length);
            this.xmlhttp.setRequestHeader("Connection", "close");
            this.xmlhttp.send(parameters);
            this.setLoading();

            return false;
    }

    
    POPUPLAYER.prototype.loadBlock = function(block,param){
        if(this.block){
            this.paramhistory = this.param;
            this.blockhistory = this.block;
        }
        this.reload = 0;
        this.init();
        this.setLoading();
        this.block = block;
        this.param = param;
        this.ajax();        
        //return false;
    }

    //popuplayer.init();

    
    POPUPLAYER.prototype.resizeBack = function(){
        var pageSize = popuplayer.getPageSize();
        try{
            document.getElementById('popup').style.top = '30px';//(pageSize[7] + 90) + 'px';
            document.getElementById('popup').style.left = ((pageSize[0]/2)-310)+'px';            
        }catch (e) {}
    }
    
    POPUPLAYER.prototype.back = function(){
        if(this.blockhistory){
            this.loadBlock(this.blockhistory,this.paramhistory);
        }else{
            this.hide();
        }
    }

}

var DragHandler = {


	// private property.
	_oElem : null,


	// public method. Attach drag handler to an element.
	attach : function(oElem,dragSpot) {
		//console.log(dragSpot);
        dragSpot.onmousedown = function(){ DragHandler._addDrag(oElem);}
        dragSpot.onmouseup = function(){ DragHandler._removeDrag(oElem);};
		
	},
    
    _removeDrag : function (obj){    
        obj.onmousedown = '';        
    },
    
    _addDrag : function (obj){
    
        obj.onmousedown = DragHandler._dragBegin;
        // callbacks
		obj.dragBegin = new Function();
		obj.drag = new Function();
		obj.dragEnd = new Function();

		return obj;
    
    },


	// private method. Begin drag process.
	_dragBegin : function(e) {
       
		var oElem = DragHandler._oElem = this;

		if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
		if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

		oElem.dragBegin(oElem, x, y);

		document.onmousemove = DragHandler._drag;
		document.onmouseup = DragHandler._dragEnd;
		return false;
	},


	// private method. Drag (move) element.
	_drag : function(e) {
		var oElem = DragHandler._oElem;

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
		oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';

		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

		oElem.drag(oElem, x, y);

		return false;
	},


	// private method. Stop drag process.
	_dragEnd : function() {
		var oElem = DragHandler._oElem;

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		oElem.dragEnd(oElem, x, y);

		document.onmousemove = null;
		document.onmouseup = null;
		DragHandler._oElem = null;
	}

};

var popuplayer = new POPUPLAYER();


