

window.onload = function() {

	/*a.maplinkにポップアップ機能追加-------------------------------------------------------------------------------------------------*/
	var a = document.getElementsByTagName("a");
	for(var i=0; i<a.length; i++) {
		if(a[i].className == "maplink") {
			WindowManager.setPopup(a[i].getAttribute("id"), "HANAMAP", 514, 500, {resizable:"yes"});
		}
	}


	/*a#staffrecruitmentにポップアップ機能追加-------------------------------------------------------------------------------------------------*/
	for(var i=0; i<a.length; i++) {
		if(a[i].getAttribute("id") == "staffrecruitment") {
			WindowManager.setPopup(a[i].getAttribute("id"), "STAFFRECRUITMENT", 514, 290, {resizable:"yes", scrollbars:"yes"});
		}
	}

	/*a#harenohiplanにポップアップ機能追加-------------------------------------------------------------------------------------------------*/
	for(var i=0; i<a.length; i++) {
		if(a[i].getAttribute("id") == "harenohiplan") {
			WindowManager.setPopup(a[i].getAttribute("id"), "HARENOHIPLAN", 510, 275, {resizable:"yes"});
		}
	}

	/*a#hanacafeにポップアップ機能追加-------------------------------------------------------------------------------------------------*/
	for(var i=0; i<a.length; i++) {
		if(a[i].getAttribute("id") == "hanacafe") {
			WindowManager.setPopup(a[i].getAttribute("id"), "HANACAFE", 590, 388, {resizable:"yes"});
		}
	}

	/*a#couponにポップアップ機能追加-------------------------------------------------------------------------------------------------*/
	for(var i=0; i<a.length; i++) {
		if(a[i].getAttribute("id") == "coupon") {
			WindowManager.setPopup(a[i].getAttribute("id"), "COUPON", 510, 270);
		}
	}

	/*a#kitsukeにポップアップ機能追加-------------------------------------------------------------------------------------------------*/
	for(var i=0; i<a.length; i++) {
		if(a[i].getAttribute("id") == "kitsuke") {
			WindowManager.setPopup(a[i].getAttribute("id"), "KITSUKE", 514, 290);
		}
	}

	/*a#HCSにポップアップ機能追加-------------------------------------------------------------------------------------------------*/
	for(var i=0; i<a.length; i++) {
		if(a[i].getAttribute("id") == "HCS") {
			WindowManager.setPopup(a[i].getAttribute("id"), "HCS", 514, 275, {resizable:"yes"});
		}
	}

	/*a#mmにポップアップ機能追加-------------------------------------------------------------------------------------------------*/
	for(var i=0; i<a.length; i++) {
		if(a[i].getAttribute("id") == "m_m") {
			WindowManager.setPopup(a[i].getAttribute("id"), "MM", 500, 350, {resizable:"yes"});
		}
	}

	/*div.staffinfoを隠す-----------------------------------------------------------------------------------------------------------*/
	var div = document.getElementsByTagName("div");
	for(var j=0; j<div.length; j++) {
		if(div[j].className == "staffinfo") {
			div[j].style.display = "none";
		}
	}

	/*a.staffpicにイベントをセット--------------------------------------------------------------------------------------------------*/
	for(var k=0; k<a.length; k++) {
		if(a[k].className == "staffpic") {
			a[k].onmouseover = function() {
				var myName = this.getAttribute("id");
				var trg = document.getElementById(myName + "_info");
				this.onmousemove = function(e) {
					if(document.all) e = event;//for ie
				
					var mouseX = e.pageX || (e.x ? e.x + document.body.scrollLeft : 0);
					var mouseY = e.pageY || (e.y ? e.y + document.body.scrollTop : 0);

					trg.style.display = "block";

					//mouseX -= trg.offsetWidth / 2;
					mouseY -= trg.offsetHeight + 20;

					trg.style.left = mouseX + "px";
					trg.style.top = mouseY + "px";
				}
			}
			a[k].onmouseout = function() {
				var myName = this.getAttribute("id");
				var trg = document.getElementById(myName + "_info");
				trg.style.display = "none";
			}
		}
	}

	/*各店舗画像のポップアップをセット--------------------------------------------------------------------------------------------*/
	for(var l=0; l<a.length; l++) {
		if(a[l].getAttribute("id") == "hana_img1") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 765, 535, {resizable:"yes"});
		}
		if(a[l].getAttribute("id") == "hana_img2") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 767, 535, {resizable:"yes"});
		}
		if(a[l].getAttribute("id") == "hana_img3") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 767, 535, {resizable:"yes"});
		}

		if(a[l].getAttribute("id") == "sista_img1") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 767, 535, {resizable:"yes"});
		}
		if(a[l].getAttribute("id") == "sista_img2") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 767, 535, {resizable:"yes"});
		}
		if(a[l].getAttribute("id") == "sista_img3") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 355, 535, {resizable:"yes"});
		}

		if(a[l].getAttribute("id") == "botany_img1") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 355, 535, {resizable:"yes"});
		}
		if(a[l].getAttribute("id") == "botany_img2") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 355, 535, {resizable:"yes"});
		}
		if(a[l].getAttribute("id") == "botany_img3") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 355, 535, {resizable:"yes"});
		}

		if(a[l].getAttribute("id") == "brilliant_img1") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 355, 535, {resizable:"yes"});
		}
		if(a[l].getAttribute("id") == "brilliant_img2") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 520, 368, {resizable:"yes"});
		}
		if(a[l].getAttribute("id") == "brilliant_img3") {
			WindowManager.setPopup(a[l].getAttribute("id"), "HANAIMAGE", 395, 535, {resizable:"yes"});
		}
	}

	/*hana picturesをセット------------------------------------------------------------------------------------------------------*/
	for(var m=0; m<a.length; m++) {
		if(a[m].className == "hanapic") {
			WindowManager.setPopup(a[m].getAttribute("id"), "HANAPICTURES", 50, 50, {resizable:"yes"});
		}
	}

	/*home.htmlのnewsをセット----------------------------------------------------------------------------------------------------*/
	if(document.getElementById("newsWrapper") != null) {
		_load("news.xml", callBackFunc);
	}

	/*STAFF BLOGのMonthly Archiveをセット*/
	if(document.getElementById("blognavi") != null) {
		_load("http://www.ha-na.org/staffblog/navi.xml", callBackFunc);
	}

}//window.onload[END]


/*home.htmlのnews読み込み用-----------------------------------------------------------------------------------------------------------------------------------*/

function callBackFunc() {
	if((httpObj.readyState == 4) && (httpObj.status == 200)) {//success
		parseXML(httpObj.responseXML, "success");
	} else if((httpObj.readyState == 4) && (httpObj.status == 404)) {//指定されたファイルが存在しない
		parseXML(httpObj.responseXML, "false");
	} else {//wait
		//
	}
}

function parseXML(xml, status) {
	if(status == "success") {
		setAttr(xml);
	} else {
		if(document.getElementById("newsWrapper") != null) document.getElementById("newsWrapper").innerHTML = "ニュースの読み込みに失敗しました。ブラウザのリロードボタンを押してください。";
		if(document.getElementById("blognavi") != null) document.getElementById("blognavi").innerHTML = "ナビゲーションの読み込みに失敗しましたブラウザのリロードボタンを押してください。";
	}
}


//ニュースソート用関数
function sortByDate(a, b) {
	if(a.date < b.date) {
		return 1;
	} else if(a.date > b.date) {
		return -1;
	} else {
		return 0;
	}
}


function setAttr(xml) {

	/*news.xml*/
	var $entry = xml.getElementsByTagName("entry");//node format <entry date="yyyy.mm.dd" category="xxxxxx" link="xxx/xxx.html">XXXXXXを更新しました。</entry>
	if($entry.length != 0) {

		//"NEWS"とその他のカテゴリーでわける(news, blog, bridal_pictures, hana_pictures, hana_sista_pictures, hana_tipsnail_pictures)
		$newsNodes = new Array();
		$otherNodes = new Array();
		for(var i=0; i<$entry.length; i++) {
			//日付オブジェクトをセット
			var attrDate = $entry[i].getAttribute("date");
			var d = new Date();
			d.setFullYear(new Number(attrDate.substr(0, 4)));
			d.setMonth(new Number(attrDate.substr(5, 2))-1);
			d.setDate(new Number(attrDate.substr(8, 2)));
			//d.setHours(0);
			//d.setMinutes(0);
			//d.setSeconds(0);
			//d.setMilliseconds(0);
			if($entry[i].getAttribute("category") == "news") {
				$newsNodes.push({node:$entry[i], date:d});
			} else {
				$otherNodes.push({node:$entry[i], date:d});
			}
		}//[END FOR]

		//"NEWS"が10件以上ある場合は、すべてのニュースを表示する
		$nodes = new Array();
		if($newsNodes.length >= 10) {
			for(var i=0; i<$newsNodes.length; i++) {
				$newsNodes[i].node.setAttribute("visible", "true");
				$nodes.push($newsNodes[i].node);
			}
			writeNews($nodes);
		} else {//"NEWS"が10件以下の場合は、ニュースとその他更新情報があわせて10件表示されるようにする。
			var temp1 = new Array();
			for(var i=0; i<$newsNodes.length; i++) {
				$newsNodes[i].node.setAttribute("visible", "true");
				temp1.push($newsNodes[i]);
			}
			$otherNodes[0].node.setAttribute("visible", "true");
			for(var i=0; i<$otherNodes.length; i++) {
				var d = $otherNodes[i].node.getAttribute("date");
				var c = $otherNodes[i].node.getAttribute("category");
				for(var j=i+1; j<$otherNodes.length; j++) {
					var _d = $otherNodes[j].node.getAttribute("date");
					var _c = $otherNodes[j].node.getAttribute("category");
					if($otherNodes[j].node.getAttribute("visible") != "false" || $otherNodes[j].node.getAttribute("visible") == undefined || $otherNodes[j].node.getAttribute("visible") == null) {
						if(d == _d && c == _c && c != "news") {
							$otherNodes[j].node.setAttribute("visible", "false");
						} else {
							$otherNodes[j].node.setAttribute("visible", "true");
						}
					}
				}
			}
			var temp2 = new Array();
			for(var i=0; i<$otherNodes.length; i++) {
				if($otherNodes[i].node.getAttribute("visible") == "true") {
					if(temp2.length < (10 - temp1.length)) temp2.push($otherNodes[i]);//ニュースの表示件数指定
				}
			}
			$nodes = temp2.concat(temp1);
			$nodes.sort(sortByDate);
			/*
			for(var i=0; i<$nodes.length; i++) {
				alert($nodes[i].date);
			}
			*/
			var _nodes = new Array();
			for(var i=0; i<$nodes.length; i++) {
				_nodes.push($nodes[i].node);
			}
			writeNews(_nodes);
		}
	}//[END IF]

	/*staffblog/navi.xml*/
	var $navi = xml.getElementsByTagName("navi");
	if($navi.length != 0) {
		$navi[0].setAttribute("visible", "true");
		for(var i=0; i<$navi.length; i++) {
			var a = $navi[i].getAttribute("href");
			for(var j=i+1; j<$navi.length; j++) {
				var _a = $navi[j].getAttribute("href");
				if($navi[j].getAttribute("visible") != "false" || $navi[j].getAttribute("visible") == undefined || $navi[j].getAttribute("visible") == null) {
					if(a == _a) {
						$navi[j].setAttribute("visible", "false");
					} else {
						$navi[j].setAttribute("visible", "true");
					}
				}
			}//end for
		}//end for
		writeNavi($navi);
	}
}


function writeNews($e) {
	var count = 0;

	var ul = document.createElement("ul");
	ul.setAttribute("id", "ul_news");
	ul.className = "x";
	
	$target = document.getElementById("newsWrapper");
	for(var i=0; i<$e.length; i++) {
		var node = $e[i];
		if(node.getAttribute("visible") == "true" && count < 10) {
			if(node.getAttribute("category") != "news") {
				var li = document.createElement("li");
				li.className = "x";
				var span = document.createElement("span");
				var date = document.createTextNode(node.getAttribute("date"));
				span.appendChild(date);
				if(node.firstChild.nodeValue.length > 50) {
					span.className = "h2em";
				}

				var a = document.createElement("a");
				a.href = node.getAttribute("link");
				var inner = document.createTextNode(node.firstChild.nodeValue);
				a.appendChild(inner);
				li.appendChild(span);
				li.appendChild(a);
				ul.appendChild(li);
			} else {
				var li = document.createElement("li");
				li.className = "x";
				var span = document.createElement("span");
				if(node.firstChild.nodeValue.length > 50) {
					span.className = "h2em";
				}
				var date = document.createTextNode(node.getAttribute("date"));
				span.appendChild(date);

				var inner = document.createTextNode(node.firstChild.nodeValue);
				li.appendChild(span);
				li.appendChild(inner);
				ul.appendChild(li);
			}
			count++;
		}
	}//end for
	$target.appendChild(ul);

}


function writeNavi($e) {

	var ul = document.createElement("ul");
	
	$target = document.getElementById("blognavi");
	for(var i=0; i<$e.length; i++) {
		var node = $e[i];
		if(node.getAttribute("visible") == "true") {
			var li = document.createElement("li");
			var a = document.createElement("a");
			a.href = node.getAttribute("href");
			a.appendChild(document.createTextNode(node.firstChild.nodeValue));
			li.appendChild(a);
			ul.appendChild(li);
		}
	}//end for
	$target.appendChild(ul);
}

/*-------------------------------------------------------------------------------------------------------------------------------------------*/



//E-mail設定
function setEmail(str) {
	if(str == "contact_text") {
		document.write('<a href="mailto:info@ha' + '-na.org?subject=%5bHANA%5d%e3%81%8a%e5%95%8f%e5%90%88%e3%81%9b%e3%83%a1%e3%83%bc%e3%83%ab">info@ha-na.org</a>&nbsp;(E-mailでのご予約は承っておりません)');
	} else if(str == "contact") {
		document.write('<a href="mailto:info@ha' + '-na.org?subject=%5bHANA%5d%e3%81%8a%e5%95%8f%e5%90%88%e3%81%9b%e3%83%a1%e3%83%bc%e3%83%ab">info@ha-na.org</a>');
	} else if(str == "cancel") {
		document.write('<a href="mailto:info@ha' + '-na.org?subject=%5bHANA%5d%e3%82%ad%e3%83%a3%e3%83%b3%e3%82%bb%e3%83%ab%e3%83%bb%e5%a4%89%e6%9b%b4%e3%83%a1%e3%83%bc%e3%83%ab">info@ha-na.org</a>');
	}
}




/*TOP SWF----------------------------------------------------------------------------------------------------------------------*/

var SWF = {embed:function() {
	document.write('<object width="100%" height="100%">');
	document.write('<param name="movie" value="top.swf" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="wmode" value="transparent" />');
	document.write('<param name="bgcolor" value="#cccc99" />');
	document.write('<embed src="top.swf" menu="false" quality="high" wmode="transparent" bgcolor="#cccc99" width="100%" height="100%" name="top" align="left" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
	}
}


/*Header setting----------------------------------------------------------------------------------------------------------------------*/

function setHeader() {
	var headerImageNum = 2;
	document.write('<h1 class="img' + Math.ceil(Math.random()*headerImageNum) + '"><a href="http://www.ha-na.org/">HANA</a></h1>');
}