function RateItem(item_id,type,item_type,grid,rowIndex,notGrid) {
	
    var ratingWin = new Ext.Window({
		title: 'Rate this '+item_type,
		width: 400,
		height: 200,
		modal:true,
		bodyStyle:'padding:5px;',
		closable: true,
		resizable: false,
		buttons: [{
			text: 'Rate',
			handler: function() {
				Ext.getCmp('ratingForm').getForm().submit({
					waitMsg: 'Processing Data, please wait...',
					success: function(form, action) {
						ratingWin.close();
						
						if (notGrid) {
							starsImg = document.getElementById(grid);
							votesSpan = document.getElementById(rowIndex);
							
							starsImg.src = '/images/icons/rating/amazonstar'+action.result.average+'A.gif';
							votesSpan.innerHTML = action.result.votes;
						} else {
							grid.store.data.items[rowIndex].data.rating=action.result.average;
							grid.store.data.items[rowIndex].data.votes=action.result.votes;
							grid.reBuild();
						}
					},
					failure: function(form, action) {
						ratingWin.close();
						Ext.MessageBox.alert('Error Message', action.result.errorInfo);
					}
				});
			}
		},{
			text: 'Cancel',
			handler: function() {
				ratingWin.close();
			}
		}],
		items: [new Ext.FormPanel({
				id: 'ratingForm',
				url: '/ratings/edit',
				method: 'POST',
				baseCls: 'x-plain',
				items: [
					new Ext.form.Hidden({
				        name: 'data[Rating][type]',
						value: type
				    }),
				    new Ext.form.Hidden({
				        name: 'data[Rating][item_id]',
						value: item_id
				    }),
				    new Ext.form.Hidden({
				        name: 'data[Rating][status]',
						value: 1
				    }),
					new Ext.ux.StarRating({
						fieldLabel: 'Rating',
						name: 'data[Rating][rate]',
						totalStars: 5
					}),
					new Ext.form.TextArea({
						maxLength:125,
				        maxLengthText:"125 characters maximum",
				        fieldLabel: 'Explain your rating',
						name: 'data[Rating][content]',
				        anchor: '90% 70%'
				    })
				]
			})]
	});
	ratingWin.show();
}

function Rating(_type, _itemId, _handler) {
	
	if (!_type) _type = '';
	if (!_itemId) _itemId = 0;
	if (!_handler) _handler = function(){};
	
	var deleted=false;

	function renderFrRatingLite(value,column,record) {
    	column.css = 'verticalMiddle';
	    return '<img src="/images/icons/rating/amazonstar'+value+'A.gif" />';
    }

	var ds = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '/ratings/getAll'
		}),
		reader: new Ext.data.JsonReader({
			root: 'results',
			totalProperty: 'total',
			id: 'id'
		}, [
			{name: 'id'},
			{name: 'type'},
			{name: 'item_id'},
			{name: 'user_id'},
			{name: 'user_name'},
			{name: 'user_flname'},
			{name: 'rate'},
			{name: 'content'},
			{name: 'date'}
		]),
		remoteSort: true
	});
	ds.setDefaultSort('id', 'DESC');
	ds.load({params:{start:0, limit: myPageSize, type: _type, item_id: _itemId}});
	
    var expander = new Ext.grid.RowExpander({
        tpl : new Ext.Template(
            '<div style="padding:5px;"><b>Description:</b> {content}</div>'
        )
    });
	
	// the column model has information about grid columns
	var cm = new Ext.grid.ColumnModel([
		expander,
		{
		   id: 'id',
		   header: "Identity",
		   dataIndex: 'id',
		   width: 50,
		   hidden: true
		},{
		   header: "Rate",
		   dataIndex: 'rate',
		   width: 50,
		   renderer: renderFrRatingLite,
		   sortable: true
		},{
		   header: "User",
		   dataIndex: 'user_flname',
		   width: 80,
		   sortable: false
		},{
		   header: "Description",
		   dataIndex: 'content',
		   width: 200,
		   sortable: false
		},{
		   header: "Date",
		   dataIndex: 'date',
		   width: 100,
		   sortable: true
		}]);

	// by default columns are sortable
	cm.defaultSortable = true;
	
	var menubar = [{
			text:'Delete',
			tooltip:'Remove the selected items',
			iconCls:'remove',
			handler: function(){
				deleted = true;
				Delete(grid,'/ratings/delete','rating');
			}
		}];
	
	var grid = new Ext.grid.GridPanel({
		id: 'ratingsMainGrid',
		title:'',
		iconCls: 'article',
		loadMask:{msg: 'Loading...'},
		store: ds,
		cm: cm,
		trackMouseOver:true,
		selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
		enableColLock:false,
		plugins: expander,
		viewConfig: {
			emptyText:"No records found!",
			forceFit: true
		},
		tbar: menubar,
		bbar: new Ext.PagingToolbar({
			pageSize: myPageSize,
			store: ds,
			displayInfo: true,
			displayMsg: 'Displaying items {0} - {1} of {2}',
			emptyMsg: "No items to display"
		})
	});
	
	var window = new Ext.Window({
		id: 'ratingsWindowId',
        title: 'View Rating',
		iconCls: 'gold_star',
        width: 750,
        height:500,
        minWidth: 300,
        minHeight: 250,
        layout: 'fit',
        plain:true,
		modal:true,
        buttonAlign:'center',
        items: grid,
        listeners: {
        	'close':{
        		fn: function() {
        			if (_handler && deleted) {
        				_handler();
        			}
        		}
        	}
        }
    });
	
	return window;
};
