/**********************************************************************
* /js/component/kawasaki-category-selection.js
* Copyright (C) 2007-2008 Kyoto University
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-
* 1301  USA
***********************************************************************/
/**
 * require lib/yui-2.3.1/build/treeview/treeview.js
 * require lib/yui-2.3.1/build/yahoo-dom-event/yahoo-dom-event.js
 * require lib/yui-2.3.1/build/yahoo/yahoo.js
 * require util/yui-tasknode.js
 */

var KawasakiCategorySelection = Class.create();
var TreeViewWrapper = Class.create();

KawasakiCategorySelection.prototype = {
	initialize: function(id,treeViewWrapperId,autoSearchCheckId,messageId,viewLanguage){
		this.id = id;
		this.element = $(this.id);
		this.autoSearchCheckElement = $(autoSearchCheckId);
		this.messageElement = $(messageId);
		this.language = viewLanguage;
		this.treeViewWrapper = new TreeViewWrapper(treeViewWrapperId,this.language,this);
	},
	
	getAutoSearchCheckElement: function(){
		return this.autoSearchCheckElement;
	},
	
	setDictionaryNode: function(dictionaryStatusPair){
		if(dictionaryStatusPair.cdr()){
			this.addDictionary(dictionaryStatusPair.car());
		} else {
			this.removeDictionary(dictionaryStatusPair.car());
		}
		if(this.treeViewWrapper.hasChildren()){
			this.messageElement.show();
		} else {
			this.messageElement.hide();
		}
	},
	
	addDictionary: function(dictionaryHash){
		this.treeViewWrapper.addDictionary(dictionaryHash);
		this.treeViewWrapper.draw();
	},
	
	removeDictionary: function(dictionaryHash){
		this.treeViewWrapper.removeDictionary(dictionaryHash);
		this.treeViewWrapper.draw();
	},
	
	getChildren: function(){
		return this.treeViewWrapper.getChildren();
	}
}

TreeViewWrapper.prototype = {
	initialize: function(id,viewLanguage,kawasakiCategorySelection){
		this.id = id;
		this.element = $(this.id);
		this.language = viewLanguage;
		this.kawasakiCategorySelection = kawasakiCategorySelection;
		this.categoryTree = new YAHOO.widget.TreeView(this.id);
	},
	
	getTreeRoot: function(){
		return this.categoryTree.getRoot();
	},
	
	getTree: function(){
		return this.categoryTree;
	},
	
	getChildren: function(){
		return this.getTreeRoot().children;
	},
	
	removeDictionary: function(dictionaryHash){
		$node = this.getNodeByProperty('dictionaryId',dictionaryHash.id);
		this.getTree().removeNode($node,true);
	},
	
	addDictionary: function(dictionaryHash){
		var tmpNode = new YAHOO.widget.TaskNode(dictionaryHash.dat[this.language],
			this.getTreeRoot(), false, false,this.kawasakiCategorySelection);
		tmpNode['dictionaryId'] = dictionaryHash.id;
		this.addChildrenRecursively(dictionaryHash.dat,tmpNode);
	},
	
	addChildrenRecursively: function(dictionaryHash, parent){
		if (dictionaryHash['children'] != undefined) {
	        for (var i=0; i<dictionaryHash['children'].length; i++){
	            var tmpNode = new YAHOO.widget.TaskNode(dictionaryHash['children'][i][this.language], parent, false, false,this.kawasakiCategorySelection);
	            tmpNode['categoryName'] = dictionaryHash['children'][i]['category'];
	            this.addChildrenRecursively(dictionaryHash['children'][i], tmpNode);
	        }
	    }
	},
	
	getNodeByProperty: function(property,value){
		var tree = this.getTree();
		for (var i in tree._nodes) {
	        var n = tree._nodes[i];
	        if (value == n[property]) {
	            return n;
	        }
	    }
	    return null;
	},
	
	hasChildren: function(){
		return this.getTreeRoot().children == '';
	},
	
	draw: function(){
		this.categoryTree.draw();
	}
}

KawasakiCategorySelection.Event = {
	
}

