//
var isLocalBrowse = (!location.protocol.match("^http"));
var isFirstVisit = (location.search == "" && location.pathname.match("index\.html|^/$"));
var currentPath = "";
var relativePath = "";
{
	var sep = "/";
	var path = (isLocalBrowse ? location.pathname.substring(location.pathname.lastIndexOf(sep + "Falcon" + sep) + 7) : location.pathname);
	var depth = path.split(sep).length - 2;
	relativePath = "";
	for (var i = 0; i < depth; i++) relativePath += (".." + sep);
	currentPath = location.href.substring(0, location.href.lastIndexOf("/") + 1);
}
function correctPath(path) {
//	return (path.match("^http") ? path : relativePath + path.replace(currentPath, ""));
	return relativePath + path.replace(currentPath, "");
}

// for IE only
window.onbeforeprint = function() {
//	isAnimation = false;
};
window.onafterprint = function() {
//	isAnimation = true;
};

//
// var isAnimation = true;
var isAnimation = false;
function updateContents(callback) {
	if (typeof callback == "undefined") callback = null;

	// animate contents
	if (isAnimation) {
		if (isFirstVisit) {
			var h = $("#header");
			h.find("img").show("scale", {direction: "both", origin: ['top', 'left']}, "slow", function() {
				$("#contents img").show("drop", {direction: "down"}, 3000, function() {
					h.find("h1").show("drop", {direction: "right"}, "slow");
					$("#menu").show("fast");
					$("#about_area").show("fast");
					$("#news_area").show("fast");
					// FIXME!!: Actually, callback function should be called, but may be not currently used.
				});
			});
		}
		var c = $("#contents");
		var i = c.find(".inner");
		if (i.length) {
//			i.hide();
			var h2 = c.find("h2");
			var en = h2.find(".en").hide();
			h2.find(".ja").show("drop", {direction: "left"}, "fast");
			i.show("drop", {direction: "down"}, function() {
				en.show("drop", {direction: "up"}, "slow", function() {
					isAnimation = true;
					if (callback) callback();
				});
			});
		}
//		inner.fadeIn("slow");
//		inner.show("drop", {direction: "down"}, "def");
//		inner.show("puff", {mode: "show"}, "slow");
	}

	// correct paths validly
	{
		$("#header, #footer, #menu").
			find("a[base-href]").each(function() {
				$(this).attr({href: correctPath($(this).attr("base-href"))});
			}).end().
			find("img[base-src]").each(function() {
				$(this).attr({src: correctPath($(this).attr("base-src"))});
			});
		$("#menu .node a[base-href]").each(function() {
			var a = correctPath($(this).attr("base-href"));
			$(this).attr({href: a});
			$(this).parent().click(function() {
				location.href = a;
				return false;
			});
		});
	}

	// form contents
	{
		$("#contents ul.use li").
			filter(":not(:last)").
				append(", ").end().
			filter(":last").
				append(", ...");
	
		$("a.mailTo").click(function() {
			var enc = $(this).attr("enc");
			var off = $(this).attr("off") - 0;	// make sure value is numeric but not string
			var s = "";
			for (var i = 0; i < enc.length; i++) s += String.fromCharCode((enc.charCodeAt(i) + off) % 93 + 33);
			eval(s);
		});
	}

	// set title when hovered
	{
		// クリックすると拡大します
		$("a[title=][href=#][onclick*=dialog]").filter(":has(img)").attr({title: "\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u62e1\u5927\u3057\u307e\u3059"});
		// クリックすると別ページに移動します
		$("a[title=][target!=_blank][href!=#]").attr({title: "\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u5225\u30da\u30fc\u30b8\u306b\u79fb\u52d5\u3057\u307e\u3059"});
		// クリックすると外部サイトが別ウィンドウで開きます
		$("a[title=][target=_blank]").attr({title: "\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u5916\u90e8\u30b5\u30a4\u30c8\u304c\u5225\u30a6\u30a3\u30f3\u30c9\u30a6\u3067\u958b\u304d\u307e\u3059"});
	}

	// set icon of Iconize to specific link which could not identify with CSS3
	{
		$("a[href=#][onclick*=dialog]:not(:has(img))").addClass("icon_popup");
		$("a[target=_blank]:not(:has(img))").addClass("icon_external");
	}
}
var nMaxLoad, nCurLoad;
function onLoad(nTimes, callback) {
	if (typeof nTimes == "undefined") nTimes = 0;

	if (nTimes == 0) {
		if (++nCurLoad == nMaxLoad) {
			updateContents(callback);
		}
	} else {
		nMaxLoad = nTimes;
		nCurLoad = 0;
	}
}
function loadParts(activeMenuIndex, callback) {
	if (typeof activeMenuIndex == "undefined") activeMenuIndex = 0;
	if (typeof callback == "undefined") callback = null;

	if (isAnimation && isFirstVisit) {
		$("#about_area").hide();
		$("#contents img").hide();
	}
	if (isAnimation) {
		var c = $("#contents");
		var i = c.find(".inner");
		if (i.length) {
			i.hide();
			c.find("h2").
				find("ja").hide().end().
				find("en").hide();
		}
	}

	var nTimes = 3;
	$("#contents .getar").length && nTimes++;
	$("#news_area").length && nTimes++;
	onLoad(nTimes, callback);
	
	var e = $("#header");
	e.load(correctPath("parts/header.html"), function() {
		if (isAnimation && isFirstVisit) $(this).find("h1").hide().end().find("img").hide();
		onLoad(0, callback);
	});
	e = $("#footer");
	e.load(correctPath("parts/footer.html"), function() {
		onLoad(0, callback);
	});
	e = $("#menu");
	e.load(correctPath("parts/menu.html"), function() {
		$(this).accordion({header: "h3", active: activeMenuIndex/*, navigation: true, event: "mouseover"*/});
		if (isAnimation && isFirstVisit) $(this).hide();
		onLoad(0, callback);
	});
	e = $("#contents .getar");
	e.length && e.load(correctPath("parts/getar.html"), function() {
		onLoad(0, callback);
	});
	e = $("#news_area");
	e.length && e.load(correctPath("parts/news.html"), function() {
		if (isAnimation && isFirstVisit) $(this).hide();
		onLoad(0, callback);
	});
}

