Simian.controllers.Controller = function(name) {
	this.type = 'json';
	this.name = name;
	this.URL = Simian.app.baseUrl;
	this.adminURL = Simian.app.baseUrl  + Simian.app.routing.admin + '/';
};

Simian.controllers.Controller.prototype  = {
	create: function() {},
	
	remove: function(id, data) {
		var action = 'delete';
		var url = this.adminURL + this.name + '/' + action + '/' + id + '.' + this.type;
		var callback = {
			success: this.onRemoveSuccess,
			scope: this,
			argument: data
		};
		
		YAHOO.util.Connect.asyncRequest('GET', url, callback, null); 
	},
	
	update: function() {},
	
	saveField: function(id, name, value, data, callback) {
		var action = 'saveField';
		var url = this.adminURL + this.name + '/' + action + '/' + id + '/' + name + '/' + value + '.' + this.type;
		
		if (callback) {
			callback.success = callback.success ? callback.success : this.onSaveFieldSuccess;
		} else {
			callback = {
				success: this.onSaveFieldSuccess
			};
		}
		
		var _callback = {
			success: callback.success,
			scope: this,
			argument: data
		};
		
		YAHOO.util.Connect.asyncRequest('GET', url, _callback, null);		
	},
	
	view: function() {},
	
	/**
	 * Callbacks
	 */
	onRemoveSuccess: function(o) {
		var message = document.getElementById('message');
		var response = YAHOO.lang.JSON.parse(o.responseText);		
				
		if (response.status == 200) {
			o.argument.container.parentNode.removeChild(o.argument.container);
			message.innerHTML = response.results.message;
			message.className = 'success';
			message.style.display = 'block';
		} else {
			message.innerHTML = response.results.message;
			message.className = 'error-message';
			message.style.display = 'block';
		}
	},
	
	onSaveFieldSuccess: function(o) {
		var message = document.getElementById('message');
		var response = YAHOO.lang.JSON.parse(o.responseText);
		
		message.innerHTML = response.results.message;
		message.className = 'success';
		message.style.display = 'block';		
	}
}; 
Simian.controllers.DepartmentsController = function() {
	// Chain the constructors 
	this.constructor.superclass.constructor.call(this, 'departments');
};

YAHOO.lang.extend(Simian.controllers.DepartmentsController, Simian.controllers.Controller);

Simian.controllers.DepartmentsController.prototype.removeMember  =	function(id, data) {
	var action = 'delete_member';
	var url = this.adminURL + this.name + '/' + action + '/' + id + '.' + this.type;
	var callback = {
		success: this.onRemoveSuccess,
		scope: this,
		argument: data
	};
	
	YAHOO.util.Connect.asyncRequest('GET', url, callback, null); 
};

Simian.controllers.DepartmentsController.prototype.removeImage  =	function(id, data) {
	var action = 'delete_member_image';
	var url = this.adminURL + this.name + '/' + action + '/' + id + '.' + this.type;
	var callback = {
		success: this.onRemoveSuccess,
		scope: this,
		argument: data
	};
	
	YAHOO.util.Connect.asyncRequest('GET', url, callback, null); 
};

Simian.controllers.DepartmentsController.prototype.view  =	function(id, data) {
	var action = 'view';
	var url = this.URL + this.name + '/' + action + '/' + id + '.' + this.type;
	var callback = {
		success: this.onViewSuccess,
		scope: this,
		argument: data
	};
	
	YAHOO.util.Connect.asyncRequest('GET', url, callback, null); 
};

/**
 * Callbacks
 */
Simian.controllers.DepartmentsController.prototype.onViewSuccess = function(o) {
	var response = YAHOO.lang.JSON.parse(o.responseText);		
			
	if (response.status == 200) {
		o.argument.container.innerHTML = response.results.elements.department;
	}
};
/** 
 * Application definition
 */
Simian.views.Departments = {
	load: function() {		
		// Instantiate and configure YUI Loader:
	    var loader = new YAHOO.util.YUILoader({ 
	    	require: ['treeview'], 
	        combine: true,
	        scope: this, 
	        onSuccess: this.onLoadSuccess
	    }); 
	 
		// Load the files using the insert() method. 
		loader.insert();		
	},
	
	init: function() {		
		//Controller
		this.controller = new Simian.controllers.DepartmentsController();
		
		// DOM		
		this.elements = {
			departments: document.getElementById('departments'),
			department: document.getElementById('department')
		}
		
		var tree = new YAHOO.widget.TreeView(this.elements.departments);		
		tree.subscribe('clickEvent', this.onNodeClick, this, true);		
		tree.render();		
	},
	/**
	 * Events Handlers
	 */
	onLoadSuccess: function() {
		YAHOO.util.Event.onDOMReady(this.init, this, true);
	},
	
	onNodeClick: function(node) {		
		var data = {
			container: this.elements.department
		};
		
		this.elements.department.innerHTML = 'Cargando...';			
		this.controller.view(node.node.data.id, data);
	}
};

Simian.views.Departments.load();
