
rowIndex = 0;

addGrid = function() { 

	YAHOO.example.EnhanceFromMarkup = new function() { 

	// This is the handler for ratings column
	//Get the data from another field-ratingScore and sort it accordingly
	var sortRating = function(a, b, desc) {
		A = a.getData().ratingScore;
		B = b.getData().ratingScore;
		// Deal with empty values
		if(!YAHOO.lang.isValue(A)) {
			return (!YAHOO.lang.isValue(B)) ? 0 : 1;
		}
		else if(!YAHOO.lang.isValue(B)) {
			return -1;
		}
		
		return (A < B);
	};

	var onSort = function(args, e) {
		//regenerate the ratings
		//generateRatingForms();
	};

	var myFormatDate = function(elCell, oRecord, oColumn, oData) {
		rating = new Rating();
		//pass the unique row id got from server and create a star rating from the corresponding form 
		var starRatingElement = rating.init(oRecord.getData().index, oRecord.getId(), oRecord.getData()['ratingScore'], oRecord.getData()['name'] );
		//add the star rating to the table cell
		elCell.appendChild(starRatingElement);

		//reset the star to show the average rating
        rating.reset_stars();
	}
	
	var myColumnDefs = [
		{key:"rowKey",label:"Row Key",hidden:true},
		{key:"index",label:"Index",hidden:true},
		{key:"name",label:"&#2986;&#3014;&#2991;&#2992;&#3021;",sortable:true},
		{key:"meaning",label:"&#2986;&#3018;&#2992;&#3009;&#2995;&#3021;", sortable:true},
		{key:"ratingForm", width:100, label:"Rating", sortable:true, sortOptions:{sortFunction:sortRating}, formatter: myFormatDate},
		{key:"ratingScore",label:"Rating Score",hidden:true},
		{key:"ratingCount",label:"Rating Count",hidden:true}
	];

	this.myDataSource = new YAHOO.util.DataSource(YAHOO.util.Dom.get("names"));
	this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
	this.myDataSource.responseSchema = {
		fields: [
				{key:"rowKey"},
				{key:"index"},
				{key:"name"},
				{key:"meaning"},
				{key:"ratingForm"},
				{key:"ratingScore"},
				{key:"ratingCount"}
		]
	};

	this.myDataTable = new YAHOO.widget.DataTable("tableContainer", myColumnDefs, this.myDataSource,
			{sortedBy:{key:"name",dir:"asc"}}
	);
	this.myDataTable.subscribe("theadCellClickEvent", onSort); 
	
	};
};

//Generate a Datatable from a table markup. Make sure this happens after the table is loaded
YAHOO.util.Event.addListener(window, "load", addGrid);
