

//-----[ TREE ]-------------------------------------------------------
var TreeMenu = function(){
    var ds,accountId;
    var root;
	var tree;
	var xmlMap;
    var Tree = Ext.tree;
	var sUrl =  urlHTTP + 'list_menuleft.php';
    return {
        reloadTree : function () {
			$('treediv').innerHTML = "";
            root.loaded = false;
            root.expand(false);
            this.init();
        },
		getTreeDS : function () {
			//alert(ds);
			return ds;
		},
        init : function(accountId){
			Ext.QuickTips.init();
            tree = new Tree.TreePanel("treediv", {
                animate:true,
                enableDD:true,
                ddGroup : 'TreeDD',
                containerScroll: true,
                rootVisible:false,
                dropConfig: {appendOnly:true}
            });

            root = new Tree.AsyncTreeNode({
                text: 'Root',
				id: "rootTreeId",
                allowDrag:false,
                allowDrop:false
            });

            tree.setRootNode(root);

            xmlMap = Ext.data.Record.create([
                   {name: 'fol_id'},
                   {name: 'fol_parentid'},
                   {name: 'fol_name'},
                   {name: 'fol_shotname'},
                   {name: 'fol_href'}
            ]);
            ds = new Ext.data.Store({
                proxy: new Ext.data.HttpProxy({url:sUrl}),
                reader: new Ext.data.XmlReader({
                       record: 'folder'
                   }, xmlMap)
            });
            ds.load();
            ds.on('load', function(){
                var count = ds.getCount();
                var tmpArr = new Array();
                for (var index=0;index<count;index++) {

                    var myDataRecord = ds.getAt(index);
                    var fol_id = myDataRecord.get('fol_id');
                    var fol_parentid = myDataRecord.get('fol_parentid');
                    var fol_name = myDataRecord.get('fol_name');
                    var fol_shotname = myDataRecord.get('fol_shotname');
                    var fol_href = myDataRecord.get('fol_href');
					isExpand = (fol_id == cmenu)?true:false;

                    if (parseInt(fol_parentid)==0) {
                    	if(fol_href == "NULL"){
	                        root.appendChild(
	                            tmpArr[fol_id] = new Tree.TreeNode({
	                            	id:"tree_"+fol_id,
									text:fol_shotname,
									qtipCfg:{text:fol_name, autoHide:true},
									cls:'icon-node-all',
									allowDrag:false,
									expanded:isExpand
								})
	                        );
                        }else{
	                        root.appendChild(
	                            tmpArr[fol_id] = new Tree.TreeNode({
	                            	id:"tree_"+fol_id,
									text:fol_shotname,
									qtipCfg:{text:fol_name, autoHide:true},
									cls:'icon-node-all',
									allowDrag:false,
									href:fol_href,
									expanded:isExpand
								})
	                        );
                        }
                    } else {
                    	if(fol_href == "NULL"){
	                        tmpArr[fol_parentid].appendChild(
								new Tree.TreeNode({
									id:"tree_"+fol_id,
									text:fol_shotname,
									qtipCfg:{text:fol_name, autoHide:true},
									cls:'icon-node-all',
									iconCls:'tree_icon',
									allowDrag:false
								})
	                        );
                    	}else{
	                        tmpArr[fol_parentid].appendChild(
								new Tree.TreeNode({
									id:"tree_"+fol_id,
									text:fol_shotname,
									qtipCfg:{text:fol_name, autoHide:true},
									cls:'icon-node-all',
									iconCls:'tree_icon',
									href:fol_href,
									allowDrag:false
								})
	                        );
                    	}
                    }
                }
                //root.expandChildNodes(true);
				//tree.getSelectionModel().select(root.childNodes[0]);
            });
			tree.render();
        }
    };
}();
Ext.EventManager.onDocumentReady(TreeMenu.init, TreeMenu, true);

// Not click and drag //
function noclick() { 
	document.oncontextmenu = new Function ("return false"); 
}
function nodrag() { 
	document.ondragstart = new Function ("return false"); 
	document.onselectstart = new Function ("return false"); 
}
function noclickdrag() { 
	document.ondragstart = new Function ("return false"); 
	document.onselectstart = new Function ("return false"); 
	document.oncontextmenu = new Function ("return false"); 
}
document.onload	= noclickdrag();

/*
*		 Class:     BasicDialogLink
*    Author:    Aaron Conran
*    Date:      Feb. 27, 2007
*    Site:      http://www.divergingpath.com
*
*    Configurable script to implement popup windows
*    by simply setting a class on anchor tags.
*/

var BasicDialogLink = function() {
	/* user configurable defaults */
	var defaultHeight = 350;
	var defaultWidth = 450;
	var defaultSrc = 'about:blank';
	var defaultTitle = '';

	/* what class in the links would you like to popup */
	var clsName = 'window';

	/* variable to hold the dialog */
	var myDialog;

	/* return the public interface */
	return {
		/* our pseudo-constructor */
		init: function() {
			/* find all links with specified classname */
			var overrideLinks = Ext.DomQuery.select('a.' + clsName);
			/* loop over all the links to be overriden */
			for (var i = 0; i < overrideLinks.length; i++)
			{
				/* attach an onclick event handler */
				Ext.fly(overrideLinks[i]).on('click',
					/* all event handlers accept an event object */
					/* this event object has been standardized cross-browser */
					function(myEvent) {
						myEvent.preventDefault();
						BasicDialogLink.popUp(myEvent.getTarget());
					}
				);
			}
		},
		/* popUp a link into a BasicDialog */
		/* the link argument is an HTMLElement anchor link */
		popUp : function(link) {
			/* pull out the title, location, height and width */
			var title = link.getAttribute('alt') || link.getAttribute('title') || defaultTitle;
			var src = link.getAttribute('href') || defaultSrc;
			var height = link.getAttribute('height') || defaultHeight;
			var width = link.getAttribute('width') || defaultWidth;


			/* if myDialog has been created simply update it */
			if (myDialog)
			{
				/* set the title of the Dialog */
				myDialog.setTitle(title);
				/* check to see if we should resize */
				if ((myDialog.width != width) || (myDialog.height != height))
				{
					/* resizing seems to move position so center afterwards */
					/* this doesn't seem to work regardless :-( */
					myDialog.resizeTo(width, height);
					myDialog.center();
				}
				/* update the src of the iframe */
				Ext.fly('my-dlg-iframe').dom.setAttribute('src', src);

			/* if myDialog has NOT been created, create it */
			} else {
    			myDialog = new Ext.BasicDialog('my-dlg', {
                autoCreate:{
                    tag:'div',
                    cls:'x-dlg',
                    children:[ {
                        tag:'div',
                        id: 'my-dlg-hd',
                        cls:'x-dlg-hd',
                        html:title
                    },{
                        tag:'iframe',
                        cls:'x-dlg-bd',
                        src:src,
                        frameborder: 0,
                        id: 'my-dlg-iframe'
                    },{
                        tag:'div',
                        id: 'my-dlg-ft',
                        cls:'x-dlg-ft'
                    }]
                },
                height:height,
                width:width,
                shadow: true,
                modal: true,
                draggable:true,
                fixedcenter:true
            });
      }
      /* show the dialog regardless, animate from the link */
			myDialog.show(link);
		}
	};
}();
Ext.EventManager.onDocumentReady(BasicDialogLink.init, BasicDialogLink, true);
