/*
 * performance.js
 * 
 * Copyright (c) 2009 MonkeyWorks Corporation
 *
 * Since:     2010-01-11
 * Modified:  2010-01-11
 *
 * jQuery 1.3.1
 * 
*/

(function($){
	$(function(){
		
		// ------------------------------------------------------------------------
		// 設定
		// ------------------------------------------------------------------------
		
		// XMLのURL
		var url = "performance.xml";
		
		// 変数
		var date = new Object();
		
		// 今年
		date["thisyear"] =  new DateFormat("yyyy").format(new Date());
		
		// 今月
		date["thismonth"] = new DateFormat("MM").format(new Date());
		
		// 来月の年
		date["nextyear"] = Number(date["thisyear"]);
		
		// 来月
		date["nextmonth"] = Number(date["thismonth"]) + 1;
		if (date["nextmonth"] > 12) {
			date["nextmonth"] = date["nextmonth"] - 12;
			date["nextyear"] = date["nextyear"] + 1;
		}
		// 頭に0をつける
		date["nextmonth"] = date["nextmonth"].toString();
		if (date["nextmonth"].length == 1) {
			date["nextmonth"] = 0 + date["nextmonth"];
		}
		
		// #messageを非表示
		$("#message").hide();
		
		
		// ------------------------------------------------------------------------
		// 処理
		// ------------------------------------------------------------------------
		
		// 変数
		var flag = new Array();
		flag[0] = false;
		flag[1] = false;
		
		// ajax
		$.ajax({
			url: url,
			type: "GET",
			dataType: "xml",
			timeout: 10000,
			cache: false,
			error: function(){
				// 読み込めなかった場合
				//alert("データの取得に失敗しました。");
				
				$("#message").text("データの読み込みに失敗しました。");
				$("#message").show();
				
			},
			success: function(xml){
				// 読め込めた場合
				//alert("データの取得に成功しました。");
				
				if ($(xml).find("entry").length > 0) {
					// データあり
					$(xml).find("entry").each(function(){
						
						// 今月のデータを取得
						if ($(this).find("execution").text() == date["thisyear"] + "年" + date["thismonth"] + "月") {
							
							// 月
							$("#first-month span").html(Number(date["thismonth"]) + "月");
							
							// 劇団名
							if ($(this).find("title").text() != "") {
								$("#first-information .title").html($(this).find("title").text());
							}
							
							// 公演期間
							if ($(this).find("period").text() != "") {
								$("#first-information .period").html($(this).find("period").text());
							}
							
							// 休演日
							if ($(this).find("cancel").text() != "") {
								$("#first-information .cancel").html($(this).find("cancel").text());
							}
							
							// 千秋楽
							if ($(this).find("last").text() != "") {
								$("#first-information .last").html($(this).find("last").text());
							}
							
							// 公演時間
							if ($(this).find("time").text() != "") {
								$("#first-information .time").html(nl2br($(this).find("time").text()));
							}
							
							// 公演案内
							if ($(this).find("guidance").text() != "") {
								$("#first-information .guidance").html(nl2br($(this).find("guidance").text()));
							}
							
							// 画像
							if ($(this).find("image").text() != "") {
								$("#first-information img").attr("src", $(this).find("image").text());
							}
							
							// flag
							flag[0] = true;
							
						}
						
						// 来月のデータを取得
						if ($(this).find("execution").text() == date["nextyear"] + "年" + date["nextmonth"] + "月") {
							
							// 月
							$("#second-month span").text(Number(date["nextmonth"]) + "月");
							
							// 劇団名
							if ($(this).find("title").text() != "") {
								$("#second-information .title").html($(this).find("title").text());
							}
							
							// 公演期間
							if ($(this).find("period").text() != "") {
								$("#second-information .period").html($(this).find("period").text());
							}
							
							// 休演日
							if ($(this).find("cancel").text() != "") {
								$("#second-information .cancel").html($(this).find("cancel").text());
							}
							
							// 千秋楽
							if ($(this).find("last").text() != "") {
								$("#second-information .last").html($(this).find("last").text());
							}
							
							// 公演時間
							if ($(this).find("time").text() != "") {
								$("#second-information .time").html(nl2br($(this).find("time").text()));
							}
							
							// 公演案内
							if ($(this).find("guidance").text() != "") {
								$("#second-information .guidance").html(nl2br($(this).find("guidance").text()));
							}
							
							// 画像
							if ($(this).find("image").text() != "") {
								$("#second-information img").attr("src", $(this).find("image").text());
							}
							
							// flag
							flag[1] = true;
							
						}
					});
					
				} else {
					
					// データなし
					$("#message").text("イベントの予定はありません。");
					$("#message").show();
					
				}
				
				// 今月、来月のデータが見つからなかった場合
				if ((flag[0] == false) && (flag[1] == false)) {
					$("#message").text("イベントの予定はありません。");
					$("#message").show();
				}
				
			}
		});
		
	});
})(jQuery);


// ------------------------------------------------------------------------
// 関数
// ------------------------------------------------------------------------

// 改行コードを<br />に変換
function nl2br(text) {
	
	text = text.replace(/(\n|\r)/g, "<br />");
	
	// 戻り値
	return text;
	
}