var DevxPopup = function (settings) {
    if (!settings) {
        settings = {};
    }
	if (settings.popupStatus){
		this.popupStatus = settings.popupStatus;
	}else{
		this.popupStatus = 0;
	}
	
	if (settings.id){
		this.id = settings.id;
	}else{
		this.id = 'Popup';
	}
	
	if (settings.resizable){
		this.resizable = settings.resizable;
	}else{
		this.resizable = false;
	}
	if (settings.height){
		this.height = parseInt(settings.height);
	}else{
		this.height = null;
	}
	if (settings.width){
		this.width = parseInt(settings.width);
	}else{
		this.width = null;
	}
	
	if (settings.button){
		this.button = settings.button;
	}else{
		this.button = 'button';
	}
	if ( typeof(settings.modal) != 'undefined'){
		this.modal = settings.modal;
	}else{
		this.modal = true;
	}
	if ( typeof(settings.method) != 'undefined'){
		this.method = settings.method;
	}else{
		this.method = 'GET';
	}
  	if ( typeof(settings.data) != 'undefined'){
    	this.data = settings.data;
    }else{
    	this.data = null;
    }
	this.url = settings.url;
    this.win = null;
  	if ( typeof(settings.loadingText) != 'undefined'){
        this.loadMask = new Ext.DevxBodyMask({msg:settings.loadingText,id:settings.id+"loadMask"});
    }else{
        this.loadMask = new Ext.DevxBodyMask({msg:"Loading...",id:settings.id+"loadMask"});
    }
};
DevxPopup.prototype.loadPopup = function () {
	var c=this;
	if(this.popupStatus==0){
/*		$("#background"+this.id).css({
			"opacity": "0.7"
		});*/
		if (this.url){
			$.ajax({url:this.url, success:function (data, textStatus) {c.onLoad(c,data)}, type:this.method, data:this.data});
            this.loadMask.show();
		}else{
			//$("#background"+this.id).show();
			$("#"+this.id).show();
		}
	}
}

DevxPopup.prototype.load = function () {
    this.loadPopup();
    this.centerPopup();
}
DevxPopup.prototype.onError = function (c, data) {
    this.loadMask.hide();
}
DevxPopup.prototype.onLoad = function (c, data) {
    this.loadMask.hide();
    if (data != null && typeof(data)!='object') {
    	var token = "<!-- JS_to_eval -->";
    	var parts = data.split(token);
        document.getElementById(this.id+'inner').innerHTML = parts[0];
//        if (init) init.initElements(this.id+'inner');
        
        var winSettings = {
            applyTo: this.id,
            resizable: this.resizable,
            items: new Ext.Panel({
                applyTo: this.id+'inner',
                border: false,
                autoScroll: true
            }),
            shadow: false,
            closeAction :'close',
            modal: this.modal
        };
        if (this.height) {
            winSettings.height = this.height;
        } else {
            winSettings.autoHeight = true;
        }
        
        if (this.width) {
            winSettings.width = this.width;
        }
        if(Ext.isGecko2) {
            var div = document.getElementById(this.id+'inner').parentNode;
            $(div).css('display', '');
            var varFloat;
            varFloat = $(div).css('float');
            $(div).css('float', 'left');
            winSettings.width = $(div).width()+ 13;
            $(div).css('float', varFloat);
            $(div).css('display', 'none');
        }
    	if (parts[1] != null) eval(parts[1]);
        this.win = new Ext.Window(winSettings);
        this.win.show();
    	if (parts[2]) {
            eval(parts[2]);
        }
        if (this.onClose) this.win.addListener('close', this.onClose, this);
    }

	$("#"+c.id+"CloseButton").click(function(){
		c.disablePopup();
	});
    if (c.afterLoad){
        c.afterLoad(c,data);
    }
}

DevxPopup.prototype.addListener = function (e, f) {
    if (this.win)
        this.win.addListener(e, f, this);
}

DevxPopup.prototype.removeListener = function (e, f) {
    if (this.win)
        this.win.removeListener(e, f, this);
}
DevxPopup.prototype.disablePopup = function () {
    this.win.close();
}

DevxPopup.prototype.setBeforeClose = function (form_id) {
/*
    if (this.win && form_id) {
        this.win.addListener('beforeclose', function() {
            if (init) {
                var answ = init.checkNonSaved(null, form_id);
                if (answ) init.cleanModified(form_id);
                return answ;
            }
            return true;}, 
        this);
    }
*/
}

DevxPopup.prototype.centerPopup = function () {
    if (this.win) {
        try {
            this.win.center();
            if(this.win.getPosition()[1]<0) {
                this.win.setPosition(this.win.getPosition()[0],1);
            }
        } catch (e) {

        }
    }
}

DevxPopup.prototype.init = function () {
	if ( !document.getElementById(this.id) ) {
		var div = document.createElement('DIV');
		div.id = this.id;
		$(div).css('width','658');
		//$(div).css('overflow','auto');
        $(div).css('height','auto');
		$(div).css('display','none');
		//$(div).css('position','fixed');
		$(div).css('padding','6');
		document.body.appendChild(div);
		var divInner = document.createElement('DIV');
		divInner.id = this.id+'inner';
		div.appendChild(divInner);
		$(divInner).addClass('popup_inner');
	}
}
