var Controller = {
	// start
	start_bell: function(e){
		Event.element(e).disable();
		$('loader').show();
		
		var bell = State.active_bell || new Bell({});
		bell.started_at = new Date();
		
		var callback = function(request){
			$('loader').hide();
			
			if(request){
				var json = parse_json(request.responseText);
				State.active_bell = new Bell(json);
			}
			else{
				// OperatableBellの場合
			}
			
			// GUI
			Controller.update_stopwatch();
		};
		bell.create( callback );
	},
	// stop
	stop_bell: function(e){
		Event.element(e).disable();
		$('loader').show();

		var bell = State.active_bell || new Bell({});
//		console.debug(bell);
		bell.stopped_at = new Date();
		
		var callback = function(request){
			$('loader').hide();
			
			if(request){
				var json = parse_json(request.responseText);
				State.active_bell = new Bell(json);
			}
			else{
				// OperatableBellの場合
			}
			
			// GUI
			Controller.update_stopwatch();
		};
		bell.create( callback );
	},
	// activate
	activate_bell: function(bell){
		// Screen
		State.timer && clearInterval(State.timer);
		State.active_bell = bell;
		
		Controller.update_stopwatch();
	},
	// update
	update_bell: function(e){
		Event.element(e).disable();
		$('loader').show();
		
		var bell = State.active_bell;
		if(bell == null) return;

		bell.started_at = _started_at.get_date();
		bell.stopped_at = _stopped_at.get_date();
		bell.tags = TagsElement.get_tags();
		bell.note = $("barbell_note").value;
		
		var callback = function(request){
			$('loader').hide();
			
			var json = parse_json(request.responseText);
			State.active_bell = new Bell(json);
			Controller.update_stopwatch();
		};
		bell.update(callback);
	},
	delete_bell: function(id){
		var bell = new Bell({id: id});
//		var barbell = Barbells.find(id);
		if( !bell ){
//			console.debug("error:"+id);
			return;
		}
		
		var callback = function(){
			new Effect.Highlight($("bell_"+id), {});
			new Effect.Fade($("bell_"+id), {});
		};
		bell.destroy(callback);	// TODO: ie
	},
	update_stopwatch: function(){
	//	console.debug("update_stopwatch");
		// screen
		// タイマー制御
		ScreenTimer.stop();
		State.active_bell && State.active_bell.is_running() && ScreenTimer.start();
		
		// screen
		ScreenElement.update();
		
		// operation
		_operation.update();
		
		// info
		BarbellInfoElement.update();
	},
	toggle_bell_info: function(){
		if($('bell_info').visible()){
			new Effect.Fade('bell_info');
		}
		else{
			new Effect.Appear('bell_info');
		}
	}
};

// TODO:
var StopWatch = {
};
