var list = {};
YAHOO.util.Event.addListener(window, "load", function() {
    list.researcher = new function() {
        this.connectionCallback = {
            success: function(o) {
                var xmlDoc = o.responseXML;

                var myColumnDefs = [
                    {key:"id", label:"ID", sortable:false},
                    {key:"name", label:"名前", sortable:true,width:'100px',sortOptions:{sortFunction:kanaSortFunction}},
                    {key:"kana", label:"フリガナ", sortable:true},
                    {key:"shozoku", label:"所属", sortable:false},
                    {key:"researchtheme", label:"研究テーマ", sortable:false}
                ];

                this.myDataSource = new YAHOO.util.DataSource(xmlDoc);
                this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_XML;
                this.myDataSource.responseSchema = {
                    resultNode: "researcher",
                    fields: ["id","name","kana","shozoku","researchtheme"]
                };

                this.myDataTable = new YAHOO.widget.DataTable("localxml", myColumnDefs, this.myDataSource,{
                selectionMode:"single"});
                
                // Enables row highlighting 
				this.myDataTable.subscribe("rowMouseoverEvent", this.myDataTable.onEventHighlightRow); 
				this.myDataTable.subscribe("rowMouseoutEvent", this.myDataTable.onEventUnhighlightRow); 
                
                this.myDataTable.subscribe("rowClickEvent",this.myDataTable.onEventSelectRow);
                
			    //行選択時のイベント処理を実装
			    this.myDataTable.subscribe("rowSelectEvent", function(e) {

			      //選択行（レコード）の ID が入った配列を取得
			      var selected = this.getSelectedRows();
			      var rs = this.getRecordSet();

		          window.location.href='show.php?id=' + rs.getRecord(selected[0]).getData("id");
			    });

            },
            failure: function(o) {

            }
        };

        this.getXML = YAHOO.util.Connect.asyncRequest("GET",
                "ajax.php?act=getList",
                this.connectionCallback);
    };
    
    // Custom function to sort by Column2 then by Column1
var kanaSortFunction = function(a, b, desc) {
    // Deal with empty values
    if(!YAHOO.lang.isValue(a)) {
        return (!YAHOO.lang.isValue(b)) ? 0 : 1;
    }
    else if(!YAHOO.lang.isValue(b)) {
        return -1;
    }

    // compare by kana 
    var comp = YAHOO.util.Sort.compare;
    return compState = comp(a.getData("kana"), b.getData("kana"), desc);

};


    
    
    
	list.detailPanel = new YAHOO.widget.Panel("detailPanel", { 
	    width:"400px",  
	    fixedcenter: true,  
	    constraintoviewport: true,  
	    underlay:"shadow",  
	    close:true,  
	    visible:false,  
	    draggable:true} ); 
	    
	list.detailPanel.render();
	
	list.showDetail = function(id){
		list.detailPanel.setBody(id);
		list.detailPanel.show();
	}
});

