//名前空間オブジェクト
var youtube = new Object();

youtube.LiveSearchController = Class.create({
    /**
     * コンストラクタ
     * A:ページがロードされた 
     * A-1: 0.5秒おきの実行タイマーを設定
     */
    initialize: function() {
	document.observe("contentloaded", function() {
	    youtube.controller.liveSearch = 
		new youtube.LiveSearch($F('keyword'));
	    youtube.controller.pe = 
		new PeriodicalExecuter(youtube.controller.checkPeriodically, 0.5);
	});
    },
    /**
     * B: 0.5秒が経過した
     * B-1:検索結果の取得を依頼
     */
    checkPeriodically: function() {
	youtube.controller.liveSearch.search($F('keyword'), 'youtube.controller.reflesh');
    },
    /**
     * C:YouTubeから検索結果が取得された
     * C-1: 検索結果の描画を依頼
     * C-2: 描画された各動画のonClickイベントへのハンドラの登録
     */
    reflesh: function(videos) {
	youtube.LiveSearchView.update(videos);
	$('search_results').getElementsByClassName('video').each(function(element, index) {
	    element.observe("click", youtube.controller.select);
	});
    },
    /**
     * D:検索結果のうちひとつが選択された
     * D-1: クリックされた動画の詳細情報の描画を依頼
     */
    select: function() {
	var video;
	if (this.className != 'video') {
	    video = this.parentNode.parentNode;
	}
	else {
	    video = this;
	}
	youtube.LiveSearchView.show(video);
	return false;
    }
});

youtube.controller = new youtube.LiveSearchController();
