/**
 * @author otonobungakukan
 * @since 2008/2/3
 */
// 名前空間の汚染を防ぐ
YAHOO.namespace("bungaku.util");
// onloadイベントに追加
YAHOO.util.Event.addListener(window,"load",function(){
	
	// lightboxを使用する
	$('a.lightbox').lightBox({
		imageLoading: 'http://otonobungakukan.web.fc2.com/lib/jquery/plugin/img/loading.gif',
		imageBtnClose: 'http://otonobungakukan.web.fc2.com/lib/jquery/plugin/img/close.gif',
		imageBtnPrev: 'http://otonobungakukan.web.fc2.com/lib/jquery/plugin/img/prev.gif',
		imageBtnNext: 'http://otonobungakukan.web.fc2.com/lib/jquery/plugin/img/next.gif'
	});
	
	// 省略処理を関数化
	YAHOO.bungaku.util.menuDisplay = function(list_id,listname){
		$("#" + list_id + "_title").bind("click",function(){
			if($("#" + list_id).css("display") == "none"){
				$("#" + list_id + "_title").html("▼" + listname);
				$("#" + list_id).css("display","block");
			}else{
				$("#" + list_id + "_title").html("▲" + listname);
				$("#" + list_id).css("display","none");
			}
		});		
	}
	YAHOO.bungaku.util.menuDisplay("new_release","音の文学館の新作");
	YAHOO.bungaku.util.menuDisplay("authors_list","作家一覧");
	YAHOO.bungaku.util.menuDisplay("works_list","作品一覧");
	YAHOO.bungaku.util.menuDisplay("words_list","文学用語集");
	YAHOO.bungaku.util.menuDisplay("side_link_list","リンク");
	
	YAHOO.bungaku.util.contentsDisplay = function(){
		var titles = $("h2.contents_title");
		for(i = 0;i < titles.size();i++){
			$(titles[i]).bind("click",function(){
				var title_txt = $(this).text();
				var titles_in = $("h2.contents_title");
				var contents = $("div.contents_body");
				for(j = 0;j < titles_in.size();j++){
					if(title_txt == $(titles_in[j]).text()){
						break;
					}
				}
				$(contents[j]).toggle();
			});
		}
	}
	YAHOO.bungaku.util.contentsDisplay();

	/**
	 * 補助メッセージパネルの表示
	 * @param {Object} id
	 */
	YAHOO.bungaku.util.showPanel = function(id,title,body){
		$("#panel_title").html(title);
		$("#panel_body").html(body);
		$("#panel_bottom").html("©2008 音の文学館資料室");
		var introduction_x = YAHOO.util.Dom.getX(id);
		var introduction_y = YAHOO.util.Dom.getY(id);
		YAHOO.bungaku.util.panel1 = new YAHOO.widget.Panel("panel",{
			width:"200px",
			constraintoviewport:true,
			x:introduction_x,
			y:introduction_y + 12,
			draggable:false,
			visible:false
		});
		YAHOO.bungaku.util.panel1.render();			
		YAHOO.bungaku.util.panel1.show();
	};
	/**
	 * 補助メッセージパネルを隠す
	 */
	YAHOO.bungaku.util.hidePanel = function(){
		if(YAHOO.bungaku.util.panel1){
			YAHOO.bungaku.util.panel1.hide();
		}		
	};
	// 補助メッセージパネルを指定idに登録する
	YAHOO.bungaku.util.setPanel = function(bool){
		if(bool){
//			YAHOO.util.Event.addListener("introduction","mouseover",function(){
//				YAHOO.bungaku.util.showPanel("introduction","音の文学館へようこそ！","This is a Panel that was marked up in the document.");
//			});
//			YAHOO.util.Event.addListener("introduction","mouseout",YAHOO.bungaku.util.hidePanel);
		}else{
			
		}
	};

	// 作者一覧メニューを作成する
    var author_list = $("#authors_list a");
	var author_Menu = new YAHOO.widget.Menu("author_Menu");
    author_Menu.addItems([
            { text: $(author_list[0]).text(), url: $(author_list[0]).attr("href") },
			{ text: $(author_list[1]).text(), url: $(author_list[1]).attr("href") }
        ]);
    author_Menu.render("author_menu_div");
    author_Menu.subscribe("show", author_Menu.focus);
	// 作品一覧メニューを作成する
    var works_list = $("#works_list a");
	var work_Menu = new YAHOO.widget.Menu("work_Menu");
	work_Menu.addItems([
            { text: $(works_list[0]).text(), url: $(works_list[0]).attr("href") },
			{ text: $(works_list[1]).text(), url: $(works_list[1]).attr("href") },
            { text: $(works_list[2]).text(), url: $(works_list[2]).attr("href") },
			{ text: $(works_list[3]).text(), url: $(works_list[3]).attr("href") }
        ]);
	work_Menu.render("work_menu_div");
    work_Menu.subscribe("show", work_Menu.focus);
	// 文学用語集メニューを作成する
    var words_list = $("#words_list a");
	var word_Menu = new YAHOO.widget.Menu("word_Menu");
	word_Menu.addItems([
            { text: $(words_list[0]).text(), url: $(words_list[0]).attr("href") },
			{ text: $(words_list[1]).text(), url: $(words_list[1]).attr("href") },
            { text: $(words_list[2]).text(), url: $(words_list[2]).attr("href") },
			{ text: $(words_list[3]).text(), url: $(words_list[3]).attr("href") },
            { text: $(words_list[4]).text(), url: $(words_list[4]).attr("href") },
			{ text: $(words_list[5]).text(), url: $(words_list[5]).attr("href") },
            { text: $(words_list[6]).text(), url: $(words_list[6]).attr("href") },
			{ text: $(words_list[7]).text(), url: $(words_list[7]).attr("href") },
            { text: $(words_list[8]).text(), url: $(words_list[8]).attr("href") },
			{ text: $(words_list[9]).text(), url: $(words_list[9]).attr("href") }
        ]);
    word_Menu.render("word_menu_div");
    word_Menu.subscribe("show", word_Menu.focus);
	
	// 作品一覧を表示する
	YAHOO.util.Event.addListener("works_click", "mouseover", function(){
		work_Menu.cfg.setProperty("x",YAHOO.util.Dom.getX("works_click"));
		work_Menu.cfg.setProperty("y",YAHOO.util.Dom.getY("works_click") + 12);
		work_Menu.show();
	}, null, work_Menu);
	
	// 作家一覧を表示する
	YAHOO.util.Event.addListener("author_click", "mouseover",function(){
		author_Menu.cfg.setProperty("x",YAHOO.util.Dom.getX("author_click"));
		author_Menu.cfg.setProperty("y",YAHOO.util.Dom.getY("author_click") + 12);
		author_Menu.show();
	}, null, author_Menu);
    
	// 文学用語集を表示する
	YAHOO.util.Event.addListener("words_click", "mouseover",function(){
		word_Menu.cfg.setProperty("x",YAHOO.util.Dom.getX("words_click"));
		word_Menu.cfg.setProperty("y",YAHOO.util.Dom.getY("words_click") + 12);
		word_Menu.show();
	}, null, word_Menu);
		
	// 作家一覧・作品一覧・文学用語集が同時に表示されないようにする
	YAHOO.util.Event.addListener("words_click", "mouseover", work_Menu.hide, null, work_Menu);
	YAHOO.util.Event.addListener("works_click", "mouseover", word_Menu.hide, null, word_Menu);
	YAHOO.util.Event.addListener("works_click", "mouseover", author_Menu.hide, null, author_Menu);
    YAHOO.util.Event.addListener("author_click", "mouseover", work_Menu.hide, null, work_Menu);

	// 表示を消すための処理
	YAHOO.util.Event.addListener("introduction", "mouseover", author_Menu.hide, null, author_Menu);
	YAHOO.util.Event.addListener("history_click", "mouseover", word_Menu.hide, null, word_Menu);
	YAHOO.util.Event.addListener("header", "mouseover", author_Menu.hide, null, author_Menu);
    YAHOO.util.Event.addListener("header", "mouseover", work_Menu.hide, null, work_Menu);
	YAHOO.util.Event.addListener("header", "mouseover", word_Menu.hide, null, word_Menu);

});