/*
 * Copyright (c) 2009 The Olympos Development Team.
 * 
 * http://sourceforge.net/projects/olympos/
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html. If redistributing this code, this
 * entire header must remain intact.
 */
Ext.namespace("cwe.navigation");

/**
 */
cwe.navigation.GlobalSearch = function() {
};

cwe.navigation.GlobalSearch = Ext.extend(Ext.grid.GridPanel, {
	initComponent : function() {
		var self = this;

		/**
		 * Number of objects per page.
		 * 
		 * @private
		 * @type int
		 */
		this.objectPerPage = 25;

		this.searchField = new cwe.ui.SearchField( {
			updateSearch : function(newValue) {
				self.updateSearch(newValue);
			}
		});

		this.store = new Ext.data.ArrayStore();

		/**
		 * The paging toolbar.
		 * 
		 * @private
		 * @type Ext.PagingToolbar
		 */
		this.pagingBar = new Ext.PagingToolbar( {
		    pageSize : this.objectPerPage,
		    store : this.store,
		    displayInfo : true,
		    displayMsg : chi.Dict.translate("Displaying results {0} &ndash; {1} of {2}"),
		    emptyMsg : chi.Dict.translate("No results to display")
		});

		Ext.apply(this, {
		    store : this.store,
		    loadMask : true,
		    viewConfig : {
		        forceFit : true,
		        markDirty : false
		    },
		    iconCls : "searchIcon",
		    collapsed : true,
		    height : 200,
		    title : chi.Dict.translate("Global Search"),
		    hideHeaders : true,
		    tbar : [ this.searchField ],
		    bbar : this.pagingBar,
		    columns : [ {
		        header : "Type",
		        width : 20,
		        dataIndex : "iconClass",
		        renderer : function(value, metaData, record, rowIndex, colIndex, store) {
			        return "<img src='" + Ext.BLANK_IMAGE_URL + "' style='width: 16px; height: 16px;' class='" + value + "'/>";
		        }
		    }, {
		        header : "Label",
		        width : 300,
		        dataIndex : "label"
		    }, {
		        header : "Relevance",
		        width : 100,
		        dataIndex : "cwe-relative-relevance",
		        align : "right",
		        renderer : function(value) {
			        return Math.round(value * 100) + " %";
		        }
		    } ]
		});

		cwe.navigation.GlobalSearch.superclass.initComponent.apply(this, arguments);

		this.on("rowclick", function(list, rowIndex, e) {
			self.openEditor(rowIndex);
		});

		this.on("expand", function() {
			if (self.getHeight() < 200) {
				self.setHeight(200);
			}
		});
	}
});

cwe.navigation.GlobalSearch.prototype.updateSearch = function() {
	var newStore = new cwe.navigation.GlobalSearchStore( {
		query : this.searchField.getValue()
	});

	this.store = newStore;
	this.pagingBar.bindStore(newStore);
	this.reconfigure(newStore, this.getColumnModel());

	newStore.load( {
		params : {
		    start : 0,
		    limit : this.objectPerPage
		}
	});
};

cwe.navigation.GlobalSearch.prototype.openEditor = function(rowIndex) {
	var store = this.getStore();

	var searchRecord = store.getAt(rowIndex);

	if (searchRecord) {
		var modelRecord = searchRecord.get("record");

		if (modelRecord) {
			modelRecord.openEditor();
		}
	}
};
