var tree;
var isSelectItemMenu='';
/**
* load tree structure from editor
* @param dataTree JSON array
* @return
*/
function buildTreeMenuEdit(dataTree){
	Ext.QuickTips.init();
    tree = new Ext.tree.TreePanel({
        title: 'menu',
        height: 300,
        width: 440,
        useArrows:true,
        autoScroll:true,
        animate:true,
        enableDD:true,
        containerScroll: true,
        rootVisible: false,
        frame: true,
        cls:'meTree', // css format tree
        loader: new Ext.tree.TreeLoader(),
        root: new Ext.tree.AsyncTreeNode({
            expanded: true,
            children:dataTree
        }),
        listeners: {
            click: function(node, e){
                if(node.isLeaf()){
                    isSelectItemMenu=node.attributes.ids;
                    Ext.Msg.alert('Informacja', 'Zmieniles dzial dla dokumentu na <b>'+node.attributes.text+'</b>');
                }else{
                    isSelectItemMenu='';
                }
            }
        }

    });
   //  tree.getRootNode().expand(true);
}



function editDoc(docData) {
	FCKeditor.BasePath = 'lib/fckeditor/';
	FCKeditor.Height=620;
	var ststatus=false;
	if(docData.ststatus=='1') {ststatus=true;}
	isSelectItemMenu=docData.idtree;
	var wod;
	var wdo;
	if(docData.wazneod=='00000000'){wod='19000101';} else {wod=docData.wazneod;}
	if(docData.waznedo=='99999999'){wdo='30001231';} else {wdo=docData.waznedo;}
	buildTreeMenuEdit(docData.dataTree);
	var fDocEdit = new Ext.FormPanel( {
		renderTo : 'dataSite',
		frame : true,
		url:'index.php',
		title : 'Edit document',
		labelAlign : 'left',
		width : 640,
		waitMsgTarget : true,
		cls:'meTree',
		items : [ {
			xtype : 'textfield',
			fieldLabel : 'nazwa',
			emptyText : 'nazwa dokumentu',
			value: docData.stnazwa,
			name : 'stnazwa',
			allowBlank : false,
			width:400
		}, {
			xtype : 'checkbox',
			fieldLabel : 'aktywny',
			boxLabel : 'zaznacz jesli dokument ma byc widoczny',
			name : 'ststatus',
			checked: ststatus,
			width:400
		}, {
			xtype:'textfield',
			fieldLabel:'numer',
			name:'numer',
			value:docData.numer,
			width:50
		},{
			xtype:'textfield',
			fieldLabel:'lp',
			name:'lp',
			value:docData.lp,
			width:50
		},
		new Ext.form.DateField( {
			fieldLabel : 'widoczny od dnia',
			name : 'wazneod',
			width : 110,
			format : 'Ymd',
			value : wod,
			allowBlank : false
		}), new Ext.form.DateField( {
			fieldLabel : 'widoczny do dnia',
			name : 'waznedo',
			width : 110,
			format : 'Ymd',
			value : wdo,
			allowBlank : false
		}),{
			xtype : 'checkbox',
			fieldLabel : 'bez grupy w menu',
			boxLabel : 'zaznacz jesli dokument ma byc dostpny w menu',
			name : 'documenttomenu',
			checked: true,
			width:400
		},
		{
			fieldLabel:'tresc'
		},
		{
			xtype : 'fckeditor',
			/** FCKeditor's Config Options */
			Name : 'tresc',
			Height: 620,
			ToolbarSet : 'Default',
			BasePath:  'lib/fckeditor/', // If your fckeditor set up the root path,or fix it.
			Value: docData.tresc,
			/**	'fckConfig' will apply to fckeditor.Config	*/
			fckConfig : {
			//CustomConfigurationsPath: 'myconfig.js'
				BasePath: 'lib/fckeditor/editor/'
			}
		},
		{
			xtype:'textfield',
			fieldLabel:'id strony',
			value: docData.idtable
		},
		{
			xtype:'textfield',
			fieldLabel:'id trees',
			value: isSelectItemMenu
		},
		{
			fieldLabel:'dostpne menu (wybierz aby przypisac dokument)',
			items:[tree]
		} 
		
		],
		buttons : [
					{ // poprawic
						text : 'Zapis',
						formBind : true,
						width:160,
						// Function that fires when user clicks the
						// button
						handler : function() {
							Ext.QuickTips.init();
							Ext.form.Field.prototype.msgTarget = 'side';
							var editor = Ext.getCmp('tresc');
							fDocEdit.getForm().submit(
							{
								params : {
									snd : 'getEditsites',
									sid : docData.idtable,
									scm : 'savesite',
									idtree:isSelectItemMenu,
									idsite:docData.idsite,
									newtresc: editor.getValue()
								}, // encodeURIComponent
								method : 'POST',
								waitTitle : 'Connecting',
								waitMsg : 'Sending data...',
								success : function(msg) {
									// obsluga zapisu
									Ext.Msg.alert('Status','Dane zapisane',function(btn,text) {
										if (btn == 'ok') {
											fDocEdit.reset();
											fDocEdit.hide();
										}
									});
								}/*,
								failure : function(form, action) {
									var obj = Ext.util.JSON.decode(action.response.responseText);
									if (action.failureType == 'server') {
										Ext.Msg.alert('Conectin on server',obj.errors.reason);
									} else {
										Ext.Msg.alert('Warning!','No save this site from server');
									}
								}*/
							});
						}
					} ]
	});
	var editor = Ext.getCmp('tresc');	
	/**
	*	Set Event Listener
	*/
	editor.on('toolSetChanged',function(edtr,set){
		Ext.get('note').update('The toolset of the editor is '+ set);								
	});
	
	fDocEdit.show();
}

