/*
 * ImageWindow - preconfigured class
 * http://extjs.com/learn/Tutorial:Writing_a_Big_Application_in_Ext#Writing_a_pre-configured_class
 * 
 * > Window > Panel > Container > BoxComponent > Component
 */
 
function openPopupWithUrl(url) {

	var win = new Ext.ux.AjaxPopupWindow({
		width: 602,
		height:452
	});
	win.show();
	win.load({
		url: url,
		params: {}, // or a URL encoded string
		callback: function() {
			//win.resizeWindow();
		},
		discardUrl: false,
		nocache: false,
		text: i18n.L_loadingContent,
		timeout: 30,
		scripts: true
});
}

Ext.ux.AjaxPopupWindow = Ext.extend(Ext.Window, {
     border:false
	,shadow:false
	,animate: true
	,resizeSpeed: 0.8
	,fadeSpeed: 0.6
	,modal:true
	,closable:false
	,resizeable: false
	,autoHeight:false
	,bodyCssClass:'ajax-popup-window ext-kfr-popup-body'
	,cls:'ext-kfr-popup ext-kfr-ajax-popup-window'
	
    ,initComponent:function() {	
		var self = this;						
        Ext.apply(this, {							 
		     closeAction: 'close'		
			,imageIndex: 0		
			,buttonAlign:'right'
			,headerAsText:false
			,header:false
			,buttons:[	
				
				new Ext.BoxComponent({
					autoEl: {
						tag: 'img',
						src: Ext.ICON_BUTTON_CLOSE
					},
					listeners:{
						render: function(c) {
							c.getEl().on('click', function() {
								self.closeme();
							});
						}
					}
				})
			]
        });
        Ext.ux.AjaxPopupWindow.superclass.initComponent.apply(this, arguments);	
    }

	, dataLoadSuccess: function(store, records, options){
		console.log('loaded');
		this.resizeWindow();
	}

	,resizeWindow: function(){
		var wCur = this.getSize()[0];
		var hCur = this.getSize()[1];
		
		var xCur = this.getPosition()[0];
		var yCur = this.getPosition()[1];
		
		var w = wCur;
		var h = hCur;

        var wNew = 602;//(w);
        var hNew = 452;//(h);		
		var xNew = Ext.lib.Dom.getViewWidth()/2 - wNew /2;
		var yNew = Ext.lib.Dom.getViewHeight()/2- hNew /2;

        var xDiff = xCur - xNew;
        var yDiff = yCur - yNew;


		//debugMsg('w/h alt:' +wCur +',' + hCur + ', neu '+wNew + ','+ hNew + ', POS: alt:' +xCur +',' + yCur + ', neu:' +xNew +',' + yNew);
		
        if (xDiff != 0 || yDiff != 0) {
        	this.setPosition(xNew, yNew);
        	this.setSize(wNew, hNew);
        } 

    }	

	,closeme: function(args){
		this.destroy();
	}
});
