/**
 * global name space for all JS functions
 */
WLT={};

/*******************************************************
	Modal Dialogs
********************************************************/

/**
 * object to store all dialogs
 */
WLT.dialogs={};

/**
 * create modal dialog and load the given form-tml in an iframe
 * @param {String} dialogname: 			the unique name of the dialog
 * @param {Object} height: 				the height of the dialog
 * @param {Object} submitButtonText:	text of the submit button
 * @param {Object} formurl:				url of the form-tml
 */
WLT.createDialog=function(dialogname, height, width, submitButtonText, formurl){
	YAHOO.ext.EventManager.onDocumentReady(function(){
		//console.log("creating WLT.dialogs."+dialogname);
		var iframe;
		//WLT.wgapath = "http://kabe/WGAPublisher/ig-demo";
		var msg = "";
		if(!WLT.dialogs[dialogname]){
			WLT.dialogs[dialogname]={};
			var dialog=WLT.dialogs[dialogname].dialog = new YAHOO.ext.LayoutDialog("dialog_"+dialogname, { 
		                    modal:true,
		                    width:width,
		                    height:height,
		                    shadow:true,
		                    minWidth:300,
		                    minHeight:200,
		                 
							
							shim: false,
							x:0,
							//y:0,
							autoScroll: true,
							resizable: false,

							
							center: {
		                        autoScroll:false,
		                        tabPosition: 'top',
		                        closeOnTab: true,
		                        alwaysShowTabs: false
		                    }
			});
			
			var layout = dialog.getLayout();
        	dialog.beginUpdate();
            WLT.dialogs[dialogname].iframe = YAHOO.ext.DomHelper.append(document.body, {tag: 'iframe', width:"100%", height:"100%", frameBorder: 0, bgcolor:'#efefef'});			    		
            layout.add('center', new YAHOO.ext.ContentPanel(WLT.dialogs[dialogname].iframe, {title: dialogname, fitToFrame:true}));
            dialog.endUpdate();

		    dialog.addKeyListener(27, dialog.hide, dialog);
		    
			WLT.dialogs[dialogname].buttons=[];
			var cancelButton=WLT.dialogs[dialogname].cancelButton=dialog.addButton('Abbrechen', dialog.hide, dialog);
			dialog.setDefaultButton(cancelButton);
			
		    WLT.dialogs[dialogname].buttons.push(cancelButton);
			if(submitButtonText!=""){
			    WLT.dialogs[dialogname].buttons.push(dialog.addButton(submitButtonText, function(){
			    	WLT.dialogs[dialogname].submit();
			    }, dialog));
				dialog.addKeyListener(13, function(){WLT.dialogs[dialogname].submit()}, dialog);
			}
			
		    WLT.dialogs[dialogname].url=formurl;
		    //console.log("WLT.dialogs." + dialogname + " created");
		}
		
		WLT.dialogs[dialogname].hideSubmitButton=function(){			
				WLT.dialogs[dialogname].buttons[button].setVisible(false);			
		}
		
		WLT.dialogs[dialogname].showSubmitButton=function(){			
				WLT.dialogs[dialogname].buttons[button].setVisible(true);			
		}
		
		
		WLT.dialogs[dialogname].disableButtons=function(){
			for(button in WLT.dialogs[dialogname].buttons){
				WLT.dialogs[dialogname].buttons[button].disable();
			}
		}
		WLT.dialogs[dialogname].enableButtons=function(readonly){
			if(readonly)
				WLT.dialogs[dialogname].cancelButton.enable();
			else {
				for(button in WLT.dialogs[dialogname].buttons)
					WLT.dialogs[dialogname].buttons[button].enable();
			}
		}
		WLT.dialogs[dialogname].show=function(el, param){
			//console.log("dlg show: " + WLT.structkey);
			
	    	WLT.dialogs[dialogname].iframe.contentWindow.document.body.innerHTML="<img src='"+WLT.wgapath+"/file/grid/loading.gif'/>&nbsp;Bitte warten ...";
			//var url=WGPPath+"/html/" + WLT.dialogs[dialogname].url + "?dbkey=" + WLT.dbkey + "&structkey="+WLT.structkey + "&contentkey="+WLT.contentkey;
			var url = WLT.dialogs[dialogname].url;
			for(var p in param)
				url += "&"+p+"="+param[p];
			
		    WLT.dialogs[dialogname].disableButtons();
			WLT.dialogs[dialogname].dialog.show(el);			
			WLT.dialogs[dialogname].iframe.src=url;
		}
		WLT.dialogs[dialogname].submit=function(){
	    	//console.log("dlg submit: " + WLT.structkey);
		    WLT.dialogs[dialogname].disableButtons();
	    	var msg=WLT.dialogs[dialogname].iframe.contentWindow.submit();
	    	WLT.dialogs[dialogname].iframe.contentWindow.document.body.innerHTML="<b>"+msg+"</b><hr size='1'><img src='"+WLT.wgapath+"/file/grid/loading.gif'/>&nbsp;Bitte warten ...";
		}
	});
}
