//Ext.EventManager.on(window, 'load', function(){
Ext.onReady(function(){
	var cuisine_root_expanded = false;
	//var district_root_expanded = false;
	var expense_root_expanded = false;
	var occasion_root_expanded = false;
	//tree1: cuisine search*******************************************************
/*
    var cuisineStore = new Ext.data.Store({
    	proxy: new Ext.data.HttpProxy({
            url: '/RestaurantBookingServlet?action=get_restaurant_list&type=cuisine'
        }),

        reader: new Ext.data.XmlReader(
            {record: 'cuisine'},
            []
        )
    });
*/
	var cuisineStore = new Ext.data.SimpleStore({
	    fields: ['cuisine', 'id', 'lvl'],
	    data: [ 
	    	['Buffet (%E8%87%AA%E5%8A%A9%E9%A4%90)', 'cuisine-13-008', '1'], 
	    	['Crab/Seafood (%E8%9E%83%E8%9F%B9/%E6%B5%B7%E9%B2%9C)', 'cuisine-13-004', '1'], 
	    	['Chinese (%E4%B8%AD%E9%A4%90)', 'cuisine-010', '1'], ['All (%E5%85%A8%E9%83%A8%E8%8F%9C%E7%B3%BB)', 'cuisine-01', '2'], ['Anhui (%E5%BE%BD%E8%8F%9C)', 'cuisine-01-019', '2'], ['Beijing (%E4%BA%AC%E8%8F%9C)', 'cuisine-01-001', '2'], ['Cantonese (%E7%B2%A4%E8%8F%9C)', 'cuisine-01-002', '2'], ['Dong Bei (%E4%B8%9C%E5%8C%97%E8%8F%9C)', 'cuisine-01-003', '2'], ['Hainanese (%E6%B5%B7%E5%8D%97%E8%8F%9C)', 'cuisine-01-020', '2'], ['Hakka (%E5%AE%A2%E5%AE%B6%E8%8F%9C)', 'cuisine-01-004', '2'], ['Hokkien (%E7%A6%8F%E5%BB%BA%E8%8F%9C)', 'cuisine-01-005', '2'], ['Huaiyang (%E6%B7%AE%E6%89%AC%E8%8F%9C)', 'cuisine-01-006', '2'], ['Hubei (%E6%B9%96%E5%8C%97%E8%8F%9C)', 'cuisine-01-007', '2'], ['Hunanese (%E6%B9%96%E5%8D%97%E8%8F%9C)', 'cuisine-01-008', '2'], ['Mongolian (%E8%92%99%E5%8F%A4%E8%8F%9C)', 'cuisine-01-021', '2'], ['Shandong (%E9%B2%81%E8%8F%9C)', 'cuisine-01-009', '2'], ['Shanghai (%E4%B8%8A%E6%B5%B7%E6%9C%AC%E5%B8%AE%E8%8F%9C)', 'cuisine-01-010', '2'], ['Suzhou/Hangzhou (%E8%8B%8F%E6%9D%AD%E8%8F%9C)', 'cuisine-01-011', '2'], ['Szechuan (%E5%B7%9D%E8%8F%9C)', 'cuisine-01-012', '2'], ['Taiwanese (%E5%8F%B0%E6%B9%BE%E8%8F%9C)', 'cuisine-01-013', '2'], ['Teochew (%E6%BD%AE%E5%B7%9E%E8%8F%9C)', 'cuisine-01-014', '2'], ['Xinjiang (%E6%96%B0%E7%96%86%E8%8F%9C)', 'cuisine-01-015', '2'], ["Xi'an (%E8%A5%BF%E5%AE%89%E8%8F%9C)", 'cuisine-01-016', '2'], ['Yunnan/Guizhou (%E4%BA%91%E8%B4%B5%E8%8F%9C)', 'cuisine-01-017', '2'], ['Others (%E5%85%B6%E4%BB%96%E8%8F%9C%E7%B3%BB)', 'cuisine-01-018', '2'], 
	    	['Brazilian', 'cuisine-02', '1'],
	    	['Fusion', 'cuisine-03', '1'],
	    	['Indian', 'cuisine-04', '1'], 
	    	['International', 'cuisine-14', '1'],
	    	['Japanese', 'cuisine-05', '1'],
	    	['Korean', 'cuisine-06', '1'], 
	    	['Mediterranean', 'cuisine-07', '1'], 
	    	['Mexican', 'cuisine-08', '1'], 
	    	['Middle Eastern', 'cuisine-09', '1'], 
	    	['Russian', 'cuisine-10', '1'], 
	    	['South-East Asia', 'cuisine-110', '1'], ['All', 'cuisine-11', '2'], ['Burmese', 'cuisine-11-006', '2'], ['Indonesian', 'cuisine-11-001', '2'], ['Malay', 'cuisine-11-002', '2'], ['Nepalese', 'cuisine-11-007', '2'], ['Philippine', 'cuisine-11-008', '2'], ['Singaporean', 'cuisine-11-009', '2'], ['Thai', 'cuisine-11-003', '2'], ['Vietnamese', 'cuisine-11-004', '2'], ['Others', 'cuisine-11-005', '2'],  
	    	['Western', 'cuisine-120', '1'], ['All', 'cuisine-12', '2'], ['American', 'cuisine-12-001', '2'], ['Australian', 'cuisine-12-008', '2'], ['European ', 'cuisine-12-002', '2'], ['French', 'cuisine-12-003', '2'], ['German', 'cuisine-12-004', '2'], ['Italian', 'cuisine-12-005', '2'], ['Spanish', 'cuisine-12-006', '2'], ['Others', 'cuisine-12-007', '2'], 
	    	['More', 'cuisine-130', '1'], ['All', 'cuisine-13', '2'], ['Halal', 'cuisine-13-001', '2'], ['Herbal', 'cuisine-13-002', '2'], ['Local Delights', 'cuisine-13-003', '2'], ['Peranakan/Nonya', 'cuisine-13-005', '2'], ['Steamboat', 'cuisine-13-006', '2'], ['Vegetarian', 'cuisine-13-007', '2']
	    ]//end data
	});

	//get selected nodes
	var selectedCuisineNode1 = Ext.get("req-param-cuisine-id1");
	var selectedCuisineNodeId1 = '';
	if (selectedCuisineNode1) {
		selectedCuisineNodeId1 = selectedCuisineNode1.dom.innerHTML;
		cuisine_root_expanded = true;
	}
	var selectedCuisineNode = Ext.get("req-param-cuisine-id");
	var selectedCuisineNodeId = '';
	if (selectedCuisineNode) {
		selectedCuisineNodeId = selectedCuisineNode.dom.innerHTML;
		cuisine_root_expanded = true;
	}
	var searchByCuisineExpandDomNode = Ext.get("req-param-cuisine-search-expand");
	if (searchByCuisineExpandDomNode) {
		var searchByCuisineExpand = searchByCuisineExpandDomNode.dom.innerHTML;
		if (searchByCuisineExpand == 'true') {
			cuisine_root_expanded = true;
		}
	}
	
	//end get selected nodes
	
	//tree1 cuisine search*******************************************************
	var cuisineDomNode = Ext.get('cuisine-tree');
	if (cuisineDomNode) {
	    var cuisineTree = new Ext.tree.TreePanel({
	        autoHeight:true,
	        width:170,
	        layout:'fit',
	        useArrows:true,
	        //bodyStyle: 'background: transparent;',
	        ctCls: 'left-nav-ctcls',
	        autoScroll:true,
	        animate:true,
	        containerScroll: true, 
	        singleExpand: true,
	        border:false,
	        style:'padding-bottom:10px',
	        rootVisible: false
	    });
		
	    // set the root node
	    var cuisineRoot = new Ext.tree.TreeNode({
	        text: '<font class="left-nav-root">Cuisine Search</font>',
	        id:'left-nav-cuisine-search',
	        singleClickExpand: true,
	        expanded: true
	    });
	    cuisineTree.setRootNode(cuisineRoot);
		
		var cuisineTreeNode;
		cuisineStore.each(function(cuisineRec){
			var desc = Url.decode(cuisineRec.get('cuisine'));
			var id = cuisineRec.get('id');
			var lvl = cuisineRec.get('lvl');
			
			//detect whether this node should be expanded or highlighted
	    	var highlight = '';
	    	var expand = false;
	    	if (id == selectedCuisineNodeId1 || id == selectedCuisineNodeId) {
	    		highlight = 'tree-node-highlight';
	    		expand = true;
	    	}
		    if (lvl=='1') {
		    	var newNode =  new Ext.tree.TreeNode({
			        text: '<font class="left-nav-branch ' + highlight + '">' + desc + '</font>'
			       ,id: id
			       ,expanded: expand
			       ,href: '/showSummary.jsp?cuisine-id='+id
			       ,singleClickExpand: true
			    });
		    	cuisineRoot.appendChild(newNode);
		    	cuisineTreeNode = newNode;
		    }
		    if (lvl=='2') {
		    	var shortdesc = '';
	            if (desc.length > 18) {
	            	shortdesc=desc.substring(0, 17) + '..';
	            } else {
	            	shortdesc = desc;
	            }
		    	
		    	var newNode =  new Ext.tree.TreeNode({
//			        text: '<font class="left-nav-leaf ' + highlight + '">' + desc + '</font>'
			        text: shortdesc
			       ,cls: 'left-nav-leaf ' + highlight
			       ,qtip: '<font class="left-nav-leaf-qtip">'+desc+'</font>'
			       ,id: id
			       ,expanded: expand
			       ,href: '/showSummary.jsp?cuisine-id1='+cuisineTreeNode.id+'&cuisine-id='+id
			    });
			    cuisineTreeNode.attributes.href="";
			    cuisineTreeNode.appendChild(newNode);
		    }
		});
	
		var cuisinePanel = new Ext.Panel({
            title: '<span class="home-centerpanel-header">Cuisine Search <span style="font-weight:normal;">&#25353;&#33756;&#31995;&#25628;</span></span>',
            el: 'cuisine-tree',
            frame: true,
            bodyStyle: 'padding-left:5px;padding-right:5px;font-size:12px',
            layout: 'fit',
            height: 'auto',
            id: 'cuisine-panel',
            collapsible: true,
            collapsed: !cuisine_root_expanded,
            items: cuisineTree
		});
		
	    // render the tree
	    cuisinePanel.render();
	}
		
    //tree2 dish search*******************************************************
    var dishDomNode = Ext.get('dish-tree');
	if (dishDomNode) {
		var dishPanel = new Ext.Panel({
            title: '<span class="home-centerpanel-header">Dish Search <span style="font-weight:normal;">&#25353;&#33756;&#32948;&#25628;</span></span>',
            el: 'dish-tree',
            frame: true,
            bodyStyle: 'padding-left:5px;padding-right:5px;font-size:12px',
            layout: 'fit',
            height: 'auto',
            id: 'dish-panel',
            html: '<a href="/RestaurantDishSearch.jsp"><img src="/shared/sir/images/dish.png" height="100" width="170" /></a>'
		});
		
	    // render the tree
	    dishPanel.render();
	}

    //tree3 district search*******************************************************
    var districtDomNode = Ext.get('district-tree');
    if (districtDomNode) {
		var districtPanel = new Ext.Panel({
            title: '<span class="home-centerpanel-header">District Search <span style="font-weight:normal;">&#25353;&#21306;&#22495;&#25628;</span></span>',
            el: 'district-tree',
            frame: true,
            bodyStyle: 'padding-left:5px;padding-right:5px;font-size:12px',
            layout: 'fit',
            height: 'auto',
            id: 'district-panel',
            html: '<a href="/RestaurantDistrictSearch.jsp"><img src="/shared/sir/images/singapore_districts.gif" height="100" width="170" /></a>'
		});
		
	    // render the tree
	    districtPanel.render();
    }


    //tree4 expense search*******************************************************
    var expenseDomNode = Ext.get('expense-tree');
    if (expenseDomNode) {
		var expenseStore = new Ext.data.SimpleStore({
		    fields: ['expense', 'id'],
		    data: [
	    	['About S$10', 'expense-01'], 
	    	['About S$15', 'expense-02'], 
	    	['About S$20', 'expense-03'], 
	    	['About S$25', 'expense-04'],
	    	['About S$30', 'expense-05'],
	    	['About S$35', 'expense-06'],
	    	['About S$40', 'expense-07'],
	    	['About S$45', 'expense-08'],
	    	['Above S$50', 'expense-09']]
		});
	
		//get selected nodes
		var selectedExpenseNode = Ext.get("req-param-expense-id");
		var selectedExpenseNodeId = '';
		if (selectedExpenseNode) {
			selectedExpenseNodeId = selectedExpenseNode.dom.innerHTML;
			expense_root_expanded = true;
		}
		var searchByExpenseExpandDomNode = Ext.get("req-param-expense-search-expand");
		if (searchByExpenseExpandDomNode) {
			var searchByExpenseExpand = searchByExpenseExpandDomNode.dom.innerHTML;
			if (searchByExpenseExpand == 'true') {
				expense_root_expanded = true;
			}
		}
		//end get selected nodes
		
	    var expenseTree = new Ext.tree.TreePanel({
	        autoHeight:true,
	        width:170,
	        layout:'fit',
	        useArrows:true,
	        //bodyStyle: 'background: transparent;',
	        ctCls: 'left-nav-ctcls',
	        autoScroll:true,
	        animate:true,
	        containerScroll: true, 
	        singleExpand: true,
	        rootVisible: false,
	        style:'padding-bottom:10px',
	        border:false
	    });
	
	    // set the root node
	    var expenseRoot = new Ext.tree.TreeNode({
	        text: '<font class="left-nav-root">Expense Search</font><br/><font class="left-nav-root-sub">(per person)</font>',
	        id:'left-nav-expense-search',
	        singleClickExpand: true,
	        expanded: true
	    });
	    expenseTree.setRootNode(expenseRoot);
		
		expenseStore.each(function(expenseRec){
			var desc = expenseRec.get('expense');
			var id = expenseRec.get('id');
			
			//detect whether this node should be expanded or highlighted
	    	var highlight = '';
	    	var expand = false;
	    	if (id == selectedExpenseNodeId) {
	    		highlight = 'tree-node-highlight';
	    	}
	    	
	    	var newNode =  new Ext.tree.TreeNode({
		        text: '<font class="left-nav-leaf ' + highlight + '">' + desc + '</font>'
		       ,id: id
		       ,href: '/showSummary.jsp?expense-boundary=About&expense-id='+id
		    });
	    	expenseRoot.appendChild(newNode);
		});
	
		var expensePanel = new Ext.Panel({
            title: '<span class="home-centerpanel-header">Expense Search <span style="font-weight:normal;">&#25353;&#28040;&#36153;&#25628;</span></span>',
	        el:'expense-tree',
            frame: true,
            bodyStyle: 'padding-left:5px;padding-right:5px;font-size:12px',
            layout: 'fit',
            height: 'auto',
            id: 'expense-panel',
            collapsible: true,
            collapsed: !expense_root_expanded,
            items: expenseTree
		});
		
	    // render the tree
	    expensePanel.render();
    }
    
    //tree5 occasion search*******************************************************
    var occasionDomNode = Ext.get('occasion-tree');
    if (occasionDomNode) {
		var occasionStore = new Ext.data.SimpleStore({
		    fields: ['occasion', 'id'],
		    data: [
		    	['Business/Company', 'occasion-01'], 
		    	['Wedding', 'occasion-02'], 
		    	['Birthday', 'occasion-03'], 
		    	['Friends', 'occasion-04'],
		    	['Lovers', 'occasion-05']]
		});
	
		//get selected nodes
		var selectedOccasionNode = Ext.get("req-param-occasion-id");
		var selectedOccasionNodeId = '';
		if (selectedOccasionNode) {
			selectedOccasionNodeId = selectedOccasionNode.dom.innerHTML;
			occasion_root_expanded = true;
		}
		var searchByOccasionExpandDomNode = Ext.get("req-param-occasion-search-expand");
		if (searchByOccasionExpandDomNode) {
			var searchByOccasionExpand = searchByOccasionExpandDomNode.dom.innerHTML;
			if (searchByOccasionExpand == 'true') {
				occasion_root_expanded = true;
			}
		}
		//end get selected nodes
		
	    var occasionTree = new Ext.tree.TreePanel({
	        autoHeight:true,
	        width:170,
	        layout:'fit',
	        useArrows:true,
	        //bodyStyle: 'background: transparent;',
	        ctCls: 'left-nav-ctcls',
	        autoScroll:true,
	        animate:true,
	        containerScroll: true, 
	        singleExpand: true,
	        rootVisible: false,
	        style:'padding-bottom:10px',
	        border:false
	    });
	
	    // set the root node
	    var occasionRoot = new Ext.tree.TreeNode({
	        text: '<font class="left-nav-root">Occasion Search</font>',
	        id:'left-nav-occasion-search',
	        singleClickExpand: true,
	        expanded: true
	    });
	    occasionTree.setRootNode(occasionRoot);
		
		occasionStore.each(function(occasionRec){
			var desc = occasionRec.get('occasion');
			var id = occasionRec.get('id');
			
			//detect whether this node should be expanded or highlighted
	    	var highlight = '';
	    	var expand = false;
	    	if (id == selectedOccasionNodeId) {
	    		highlight = 'tree-node-highlight';
	    	}
	    	
	    	var newNode =  new Ext.tree.TreeNode({
		        text: '<font class="left-nav-leaf ' + highlight + '">' + desc + '</font>'
		       ,id: id
		       ,href: '/showSummary.jsp?occasion-id='+id
		    });
	    	occasionRoot.appendChild(newNode);
		});
	
		var occasionPanel = new Ext.Panel({
            title: '<span class="home-centerpanel-header">Occasion Search <span style="font-weight:normal;">&#25353;&#24773;&#26223;&#25628;</span></span>',
	        el:'occasion-tree',
            frame: true,
            bodyStyle: 'padding-left:5px;padding-right:5px;font-size:12px',
            layout: 'fit',
            height: 'auto',
            id: 'occasion-panel',
            collapsible: true,
            collapsed: !occasion_root_expanded,
            items: occasionTree
		});
		
	    // render the tree
	    occasionPanel.render();
    }

});

function getNodeByAttribute(tree, attrib, value){
	var root = tree.getRootNode();
	var nodeFound = null;
	root.cascade(function fnCheckAttrib() {
//		alert(this.attributes['id']);
		if(this.attributes[attrib] && this.attributes[attrib] == value){
			nodeFound = this;
			return false;
		}
	});
	return nodeFound;
}
