

/*
*	SearchField definitiion
*/
Ext.app.SearchField = Ext.extend(Ext.form.TwinTriggerField, {
    initComponent : function(){
        Ext.app.SearchField.superclass.initComponent.call(this);
        this.on('specialkey', function(f, e){
            if(e.getKey() == e.ENTER){
                this.onTrigger2Click();
            }
        }, this);
    },

    validationEvent:false,
    validateOnBlur:false,
    trigger1Class:'x-form-clear-trigger',
    trigger2Class:'x-form-search-trigger',
	hideTrigger1:true
	,paramName:'query'

    ,onTrigger1Click : function(theevent){
		this.triggers[0].hide();
		this.el.dom.value = '';
		if(this.el.dom.id == 'bm_search'){
			document.getElementById('my_bookmarks_div').innerHTML = '';
			create_my_bookmarks();		
//		}else if(this.el.dom.id == 'fileproperties_radlex_searchfield'){
		}else if(this.el.dom.id.substring(0,33) == 'fileproperties_radlex_searchfield'){
			this.store.removeAll();
			var winid = this.el.dom.id.substring(33);
			Ext.getCmp('apply_terms'+winid).disable();
		}
	}

    ,onTrigger2Click : function(theevent){
        var v = this.getRawValue();
        if(v.length < 1){
			Ext.Msg.alert('Search','Please enter a search term.');
            return;
        }
		var o = {start: 0};

		this.triggers[0].show();
		if(this.el.dom.id == 'bm_search'){
			document.getElementById('my_bookmarks_div').innerHTML = '';
			create_my_bookmarks(v);		
		}else if(this.el.dom.id.substring(0,33) == 'fileproperties_radlex_searchfield'){
			this.store.load({params:{start:0, limit:10, query:v}});
		}
	}
});

Ext.app.FavoriteSearchField = Ext.extend(Ext.form.TwinTriggerField, {
							 										 
    initComponent : function(){
        Ext.app.SearchField.superclass.initComponent.call(this);
		
        this.on('specialkey', function(f, e){
            if(e.getKey() == e.ENTER){
                this.onTrigger2Click();
            }
        }, this);
    },

    validationEvent:false,
    validateOnBlur:false,
    trigger1Class:'x-form-favorite-trigger',
    trigger2Class:'x-form-search-trigger',
	hideTrigger1:false

    ,onTrigger1Click : function(theevent){
        var v = this.getRawValue();
        if(v.length < 1){
			Ext.Msg.alert('Search','Enter a search term to add to My Favorites.');
            return;
        }
		
		handleRecentSearch('ADD','FAVORITE','0',v);
		var searchtab = myrsnatabs.find('tab_type','SEARCH')[0];
		
		var rs_id = recent_searches_store.find('searchterm',v);
		if(rs_id == -1 || (rs_id != -1 && searchtab.searchterm != v)){
			
			if (myrsnatabs.getActiveTab().id != searchtab.id){
				myrsnatabs.setActiveTab(searchtab.id);
			}
			searchtab.load({url: myRSNApath + 'yottalook/index.cfm?q=' + v, text: 'Loading...', scripts:true});				
		}
	}

    ,onTrigger2Click : function(theevent){
        var v = this.getRawValue();
        if(v.length < 1){
			Ext.Msg.alert('Search','Please enter a search term.');
            return;
        }
		var searchtab = myrsnatabs.find('tab_type','SEARCH')[0];
		if (myrsnatabs.getActiveTab().id != searchtab.id){
			myrsnatabs.setActiveTab(searchtab.id);
		}
		searchtab.load({url: myRSNApath + 'yottalook/index.cfm?q=' + v, text: 'Loading...', scripts:true});
	}
});


Ext.override(Ext.PagingToolbar, {

	onClear : function(store) {
	   this.cursor = 0;

	   this.afterTextEl.el.innerHTML = String.format(this.afterPageText, 1);
	   this.field.dom.value = 1;
	   this.first.setDisabled(true);
	   this.prev.setDisabled(true);
	   this.next.setDisabled(true);
	   this.last.setDisabled(true);
	   this.loading.enable();
	   this.updateInfo();
    },

    unbind : function(store){
        store = Ext.StoreMgr.lookup(store);
        store.un("beforeload", this.beforeLoad, this);
        store.un("load", this.onLoad, this);
        store.un("loadexception", this.onLoadError, this);
        store.un("clear", this.onClear, this);
        this.store = undefined;
    },
    
    bind : function(store){
        store = Ext.StoreMgr.lookup(store);
        store.on("beforeload", this.beforeLoad, this);
        store.on("load", this.onLoad, this);
        store.on("loadexception", this.onLoadError, this);
        store.on("clear", this.onClear, this);
        this.store = store;
    }
});




Ext.tree.TabNodeUI = Ext.extend(Ext.tree.TreeNodeUI, {
    focus: Ext.emptyFn, // prevent odd scrolling behavior
	
    renderElements : function(n, a, targetNode, bulkRender){
		this.indentMarkup = n.parentNode ? n.parentNode.ui.getChildIndent() : '';
		this.emptyIcon = Ext.BLANK_IMAGE_URL;
		
		
		
        var t = n.getOwnerTree();
        var cols = t.columns;
        var bw = t.borderWidth;
        var c = cols[0];

        var buf = [
             '<li class="x-tree-node">'
			 ,'<div ext:tree-node-id="' + n.id + '" class="x-tree-node-el x-tree-node-leaf ' + a.cls + '" style="height:22px">'
			 ,'<table width="99%" align="center"><tr><td width="100%" style="overflow:hidden;" valign="top">'
			 	,'<div class="x-tree-col" style="width:97%;overflow:hidden;" id="node_' + a.id + '">'					
			 		,'<span ></span>'
			 		,'<img src="' + this.emptyIcon + '" class="">'
			 		,'<img src="'
			 			, a.icon || this.emptyIcon									
			 			, '" class="x-tree-node-icon' + (a.icon ? " x-tree-node-inline-icon" : "") + (a.iconCls ? " "+a.iconCls : "") + '" unselectable="on">'
			 		,'<a hidefocus="on" class="x-tree-node-anchor" href="'	+ (a.href ? a.href : "#") + '" tabIndex="1" '
			 			,(a.hrefTarget ? ' target="'+a.hrefTarget+'"' : "") + (a.onclick ? ' onClick=\''+a.onclick+'\'' : "") + ' style="overflow:hidden;">'
			 			,'<span unselectable="on" style="overflow:hidden;width:100%;overflow-x:hidden;overflow-y:hidden;">'
			 				, n.text || (c.renderer ? c.renderer(a[c.dataIndex], n, a) : a[c.dataIndex])
			 		,"</span></a>"
			 	,"</div></td>"];


			 var isFolder = false;


         for(var i = 1, len = cols.length; i < len; i++){
             c = cols[i];

			if(c.dataIndex == 'canEdit' && a[c.dataIndex] == 'Y'){
				 buf.push('<td><div class="x-tree-col '
						  	,(c.cls?c.cls:'')
							,'" style="width:'
							,c.width-bw
							,'px;">'
							,'<div class="x-tree-col-text" align="left"><img src="' + myRSNApath + 'images/tab_edit.png" '
							,'onclick="Javascript:handleTabEdit('+n.attributes.id+');" id="edit_tab_'+n.attributes.id+'"></div>'
						  	,"</div></td>");				
								
			}
         }
         buf.push(
            '<div class="x-clear"></div></tr></table></div>',
            '<ul class="x-tree-node-ct" style="display:none;"></ul>',
            "</li>");

        if(bulkRender !== true && n.nextSibling && n.nextSibling.ui.getEl()){
            this.wrap = Ext.DomHelper.insertHtml("beforeBegin",
                                n.nextSibling.ui.getEl(), buf.join(""));
        }else{
            this.wrap = Ext.DomHelper.insertHtml("beforeEnd", targetNode, buf.join(""));
        }
		
        this.elNode = this.wrap.childNodes[0];
        this.ctNode = this.wrap.childNodes[1];
		var cs = document.getElementById('node_' + a.id).childNodes;
//        var cs = this.elNode.firstChild.childNodes;
        this.indentNode = cs[0];
        this.ecNode = cs[1];
        this.iconNode = cs[2];
        this.anchor = cs[3];
        this.textNode = cs[3].firstChild;
    }
});

function handleTabEdit(tab_id){
		var selected_node = editTabsTree.getNodeById(tab_id);

		
		var the_menu = new Ext.menu.Menu(
	 	{						
			id:'edit-tab-menu',
			cls:'edit-bm-menu',
			selected_node: tab_id,
			width:75,
			items: [
					
			{
				id:'the_menu_rename',
				text:'Rename',
				scope:this,
				icon:myRSNApath + 'images/tab_edit.png'
				,listeners:{'click':handleTabRename}
			},					
					
			{
				id:'the_menu_delete',
				text:'Delete',
				scope:this,
				icon: myRSNApath + 'images/tab_delete.png'
				,listeners:{'click':handleTabDelete}
			},
			
						
			
			{
				id:'the_menu_cancel',
				text:'Cancel',
				scope:this,
				icon:myRSNApath + 'images/cancel.png'
			}]
		});	

		the_menu.show($('edit_tab_'+tab_id));
	}
	
	
	function handleTabDelete(theitem,theevent){
		//alert(theitem.parentMenu.selected_node);
		var ct_id = editTabsTree.getNodeById(theitem.parentMenu.selected_node).id;
		theitem.parentMenu.hide();
		//alert(ct_id);
		var tab = eval('myrsnatabs.find("ct_id",' + ct_id + ')[0]');
		//alert(tab);
		myrsnatabs.remove(tab);
	}
	
	function handleTabRename(theitem,theevent){
		
		theitem.parentMenu.hide();
		
		var selected_node = editTabsTree.getNodeById(theitem.parentMenu.selected_node);
		Ext.Msg.show({
			title: 'Rename TAb'
			,msg: 'Rename your TAB in the field below and press "OK" to commit.'
			,buttons: Ext.MessageBox.OKCANCEL
			,multiline:true
			,value:selected_node.attributes.main_text
			,fn: function(btn,text){
					if(btn=='ok'){
//						text = text.replace(/'/g, "");
						text = text.replace(/&/g, "");
						var url = myRSNApath + "update_customer_tab.cfm?action=RENAME&ct_id=" + selected_node.attributes.id + "&ct_name=" + escape(text);
						new Ajax.Request(url, {
							method: 'get',
							asynchronous: false,
							evalJS: 'force',
							onSuccess: function(transport) {
								selected_node.setText(text);
							}
						});		
				
					} // btn=='yes'
				}			
		});		
		return false;	
	}
	
	function handleTabMove(the_tree,the_node,old_parent,new_parent,new_index) {
		
		var new_order = new_index + 1;
		var ct_id = the_node.id;
		var url = myRSNApath + "update_customer_tab.cfm?action=REORDER&ct_id=" +ct_id + "&new_order=" + new_order;
		new Ajax.Request(url, {
			method: 'get',
			asynchronous: false,
			evalJS: 'force',
			onSuccess: function(transport) {
				//alert('reordered');
			}
		});
		

	}