/*
* lm 1.3
* Automatic functions for webpages
* Require:
*  jquery.js
*  frame.css
*  content_font.css
*
* Copyright: (c) 2008 ludicmind.net
**************************************/

// Initialization
$.lm = {
	init: function() {
		for (func in $.lm) {
			if ($.lm[func].init)
				$.lm[func].init();
		}
	}
};

$(document).ready(function(){
	$.lm.init();
	$("div.orderBlockB01 form:eq(1)").css("margin-top", "0");
	$("div.accordionHeadA01 a").each(function(){
		if(!$(this).hasClass("open")){
			$("#" + $(this).attr("id").replace("Title","")).hide();
			$(this).find("img").attr("src", "/shared/img/icon_accordion_02.gif").attr("alt", "[+]開く");
		}
	});
	$("div.accordionHeadA01 a").bind("click", function(){
		if($(this).find("img").attr("src") == "/shared/img/icon_accordion_02.gif"){
			$(this).find("img").attr("src", "/shared/img/icon_accordion_01.gif").attr("alt", "[-]閉じる");
			var elemId = "#" + $(this).attr("id").replace("Title", "");
			$(elemId).slideDown("slow");
		} else {
			$(this).find("img").attr("src", "/shared/img/icon_accordion_02.gif").attr("alt", "[+]開く");
			var elemId = "#" + $(this).attr("id").replace("Title", "");
			$(elemId).slideUp("slow");
		}
		return false;
	})
	$("ul.linkListB03").addClassFL(4);
	if($(".fundTabA01").length){
		$("div.tabBlockA01:gt(0)").hide();
		$(".fundTabA01 li:eq(0) img")
			.attr("src", $(".fundTabA01 li:eq(0) img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"))
			.removeClass("hover");
		$(".fundTabA01 li").bind("click", function(){
			$("div.tabBlockA01").hide();
			var id = "#" + $(this).find("img").attr("id") + "Body";
			$(id).show();
			$(".fundTabA01 li img").each(function(){
				$(this).attr("src", $(this).attr("src").replace(/^(.+)_o(\.[a-z]+)$/, "$1$2"))
				.addClass("hover");
			});
			$(this).find("img")
				.attr("src", $(this).find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"))
				.removeClass("hover");
		});
	}
	$("#gNavBlock a").each(function(){
		if(document.URL.indexOf($(this).attr("href")) != -1){
			$(this).find("img")
				.attr("src", $(this).find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"))
				.removeClass("hover");
		}
	});
	if(document.URL.indexOf("/recruit/") != -1){
		$("#gNav1").find("img")
			.attr("src", $("#gNav1").find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"))
			.removeClass("hover");
	}
	if(document.URL.indexOf("/supporter/") != -1){
		$("#gNav3").find("img")
			.attr("src", $("#gNav3").find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"))
			.removeClass("hover");
	}
	$("#sNavBlock a").each(function(){
		if(document.URL.indexOf($(this).attr("href")) != -1){
			$(this).find("img")
				.attr("src", $(this).find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"))
				.removeClass("phover");
		}
	});
	$(".fBlockA01").each(function(){ $(this).find("p.default:eq(0)").css("margin-top", 0); });
	$(".fBlockA02").each(function(){ $(this).find("p.default:eq(0)").css("margin-top", 0); });
	$(".fBlockA03").each(function(){ $(this).find("p.default:eq(0)").css("margin-top", 0); });
	$(".fBlockA04").each(function(){ $(this).find("p.default:eq(0)").css("margin-top", 0); });
	$(".fBlockA05").each(function(){ $(this).find("p.default:eq(0)").css("margin-top", 0); });
	$(".fBlockA06").each(function(){ $(this).find("p.default:eq(0)").css("margin-top", 0); });
	$(".fBlockA07").each(function(){ $(this).find("p.default:eq(0)").css("margin-top", 0); });
	$(".fBlockA08").each(function(){ $(this).find("p.default:eq(0)").css("margin-top", 0); });
	$(".oldTableA01 table tr").each(function(){ $(this).find("td:eq(0)").addClass("first"); });
});


// Image hover
$.lm.hover = {
	init: function() {
		$('.hover')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('blur', this.exit)
			.each(this.preload);
	},

	preload: function() {
		this.preloaded = new Image;
		this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
	},

	enter: function() {
		if (!this.src.match(/^(.+)_o(\.[a-z]+)$/) && !this.src.match(/^(.+)_a(\.[a-z]+)$/)){
			this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
		}
	},

	exit: function() {
		if (this.src.match(/^(.+)_o(\.[a-z]+)$/)){
			this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, "$1$2");
		} else {
			this.src = this.src.replace(/^(.+)_a(\.[a-z]+)$/, "$1$2");
		}
	}
};


// Opacity Change hover
$.lm.oHover = {
	init: function() {
		$('.oHover')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('blur', this.exit);
	},

	enter: function() {
		$(this).animate({opacity: 0.7}, 200);
	},

	exit: function() {
		$(this).animate({opacity: 1}, 200);
	}
};

//PNG Image
$.lm.pngfilter = {
	init: function() {
		$("img:not(.phover)").each(function(){
			$(this).css("behavior", "url(/shared/htc/iepngfix.htc)");
		});
	}
};

//PNG Image hover
$.lm.phover = {
	init: function() {
		$('.phover')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('blur', this.exit)
			.each(this.preload);
	},

	preload: function() {
		if($.browser.msie && $.browser.version <= 6.0){
			this.initial = new Image;
			this.initial.src = this.src;
			this.preloaded = new Image;
			this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
			$(this).css("behavior", "url(/shared/htc/iepngfix.htc)");
		} else {
			this.preloaded = new Image;
			this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
		}
	},

	enter: function() {
		if($.browser.msie && $.browser.version <= 6.0){
			if (this.src.match(/(blank)/)){
				this.src = this.preloaded.src;
				$(this).css("behavior", "url(/shared/htc/iepngfix.htc)");
			}
		} else {
			if (!this.src.match(/^(.+)_o(\.[a-z]+)$/) && !this.src.match(/^(.+)_a(\.[a-z]+)$/)){
				this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
			}
		}
	},

	exit: function() {
		if($.browser.msie && $.browser.version <= 6.0){
			if (this.src.match(/(blank)/)){
				this.src = this.initial.src;
				$(this).css("behavior", "url(/shared/htc/iepngfix.htc)");
			}
		} else {
			if (this.src.match(/^(.+)_o(\.[a-z]+)$/)){
				this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, "$1$2");
			} else {
				this.src = this.src.replace(/^(.+)_a(\.[a-z]+)$/, "$1$2");
			}
		}
	}
};


// Add "first-child" class to all elements in #content
$.lm.firstChild = {
	init: function() {
		var ua = window.navigator;
		if (ua.appName == "Microsoft Internet Explorer" && ua.appVersion.indexOf("MSIE 7.0") == -1) {
			$("#content *:first-child").addClass("first-child");
		}
	}
};


// Inner Link
$.lm.innerLink = {
	init: function() {
		$("a[href]").each(function(){
			if($(this).attr("href").indexOf("#") == 0){
				if($(this).attr("href").indexOf("#top") == 0){
					$(this).click(function(){
						$.scrollTo($(this).attr("href"), {duration: 700, easing: "easeOutExpo"});
						return false;
					});
				} else if($(this).attr("href").indexOf("#h2") == 0) {
					$(this).click(function(){
						$.scrollTo($(this).attr("href"), {duration: 700, easing: "easeOutExpo", offset: {top: -20}});
						return false;
					});
				}
			}
		});
	}
};


// Add "nolink" class to Heading without a[href]
$.lm.noLink = {
	init: function() {
		$("a:not([href])").parent("h2").addClass("nolink");
		$("a:not([href])").parent("h3").addClass("nolink");
		$("a:not([href])").parent("h4").addClass("nolink");
	}
};


// Link Icon
/*$.lm.linkIcon = {
	init: function() {
		$("a[href]:has(img)").addClass("noIcon");
		$("a[href]").each(function(){
			if(!$(this).hasClass("noIcon")) {
				if(this.href.indexOf("mailto:") != -1) {
					$(this).css("background", "url(/shared/img/icon_mail_01.gif) no-repeat left 0.4em");
				}
				if(this.href.indexOf(".pdf") != -1) {
					$(this).append(" <img class='inlineIcon' src='/shared/img/icon_resource_01.gif' alt='PDF' width='18' height='17' />");
				} else if(this.href.indexOf(".xls") != -1) {
					$(this).append(" <img class='inlineIcon' src='/shared/img/icon_resource_02.gif' alt='Excel' width='16' height='17' />");
				} else if (this.target) {
					$(this).append(" <img class='inlineIcon' src='/shared/img/icon_window_01.gif' alt='別ウィンドウで開きます' width='11' height='10' /> ");
				}
			}
		});
	}
};*/

//Link Icon & PDF Size
$.lm.linkIcon = {
	init: function() {
		$("a[href]:has(img)").addClass("noIcon");
		$("a[href]").each(function(){
			if(!$(this).hasClass("noIcon")) { // noIconクラスを持つものは無視
				// メールアイコン
				if(this.href.indexOf("mailto:") != -1) {
					$(this).css("background", "url(/shared/img/icon_mail_01.gif) no-repeat left 0.4em");
				}
				// PDF
				if(this.href.indexOf(".pdf") != -1) {
					var elem = $(this);
					elem.append(" <img class='inlineIcon' src='/shared/img/icon_resource_01.gif' alt='PDF' width='18' height='17' />");
					$.ajax({
						type: "HEAD",
						url: this.href,
						complete: function(data, textStatus){
							var size = Math.round(data.getResponseHeader("Content-Length") / 1024);
							if (size >= 1024) {
								size = Math.round(size / 1024);
								elem.after(" （PDF:" + size + "MB）");
							} else {
								elem.after(" （PDF:" + size + "KB）");
							}
						}
					});
				// CSV
				} else if (this.href.indexOf(".csv") != -1 || this.href.indexOf(".xls") != -1) {
					$(this).append(" <img class='inlineIcon' src='/shared/img/icon_resource_02.gif' alt='Excel' width='16' height='17' />");
				// ZIP
				} else if(this.href.indexOf(".zip") != -1) {
					var elem = $(this);
					elem.addClass("zip");
					$.ajax({
						type: "HEAD",
						url: this.href,
						complete: function(data, textStatus){
							var size = Math.round(data.getResponseHeader("Content-Length") / 1024);
							if (size >= 1024) {
								size = Math.round(size / 1024);
								elem.after(" （ZIP:" + size + "MB）");
							} else {
								elem.after(" （ZIP:" + size + "KB）");
							}
						}
					});
				// 別ウィンドウ
				} else if (this.target) {
					if(!$(this).hasClass("site"))
						$(this).append(" <img class='inlineIcon' src='/shared/img/icon_window_01.gif' alt='別ウィンドウで開きます' width='11' height='10' /> ");
				}
			// noIconクラスを持ち、zip2クラスを持つZIP
			} else if($(this).hasClass("zip2")) {
				var elem = $(this);
				$.ajax({
					type: "HEAD",
					url: this.href,
					complete: function(data, textStatus){
						var size = Math.round(data.getResponseHeader("Content-Length") / 1024);
						if (size >= 1024) {
							size = Math.round(size / 1024);
							elem.after("<p class='note2 first-child'>（ファイルサイズ ZIP:" + size + "MB）</p>");
						} else {
							elem.after("<p class='note2 first-child'>（ファイルサイズ ZIP:" + size + "KB）</p>");
						}
					}
				});
			}
		});
	}
};

// Mouse hover on input[type='image']
/*$.lm.inputHover = {

	init: function() {
		$('input[type="image"]')
			.bind('mouseover', this.enter)
			.bind('mouseout', this.exit)
			.each(this.preload);
	},

	preload: function() {
		this.preloaded = new Image;
		this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
	},

	enter: function() {
		this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
	},

	exit: function() {
		this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, "$1$2");
	}
};*/


// Local Navigation
$.lm.lNav = {
	init: function() {
		$("div#lNav ul.second li ul").hide();
		$("div#lNav ul.second a").hover(function(){
			$(this).parent("li").addClass("over");
		},
		function(){
			$(this).parent("li").removeClass("over");
		});

		var url = document.URL.substr(document.URL.indexOf(".jp") + 3);
		if(url.indexOf("index.html") != -1){
			url = url.replace("index.html", "");
		}
		if(url.indexOf("#") != -1){
			url = url.substr(0, url.indexOf("#"));
		}
		if(url.indexOf("?") != -1){
			url = url.substr(0, url.indexOf("?"));
		}

		$("ul.second a").each(function(){
			if($(this).attr("href") == url){
				$(this).addClass("stay");
				$(this).siblings("ul").show();
				$(this).parent("li").addClass("stay");
				$(this).parent("li").parent("ul").show();
				if(!$(this).parent("li").parent("ul").hasClass("second")){
					$(this).parent("li").parent("ul").parent("li").addClass("stay");
				}
			}
		});
		if(url.indexOf("/about/news/") != -1){
			this.changeStay("news");
		}
		if(url.indexOf("/activity/report/") != -1){
			this.changeStay("report");
		}
		if(url.indexOf("/activity/blog/") != -1){
			this.changeStay("blog");
		}
		if(url.indexOf("/supporter/") != -1){
			this.changeStay("supporter");
		}
		if(url.indexOf("/support/shopping/") != -1 && url.indexOf("about.html") == -1 && url.indexOf("guide.html") == -1 && url.length > 18){
			this.changeStay("shopping");
		}
		if(url.indexOf("/join/event/") != -1){
			this.changeStay("event");
		}
	},

	changeStay: function(classText){
		var id = "ul.second li a." + classText;
		$(id).addClass("stay");
		$(id).siblings("ul").show();
		$(id).parent("li").addClass("stay");
		$(id).parent("li").parent("ul").show();
		if(!$(id).parent("li").parent("ul").hasClass("second")){
			$(id).parent("li").parent("ul").parent("li").addClass("stay");
		}
	}
};


// Replace "abbr" with "acronym" (only IE6)
$.lm.changeAbbr = {
	init: function() {
		var ua = window.navigator;
		if (ua.appName == "Microsoft Internet Explorer" && ua.appVersion.indexOf("MSIE 7.0") == -1) {
			var abbrs = document.getElementsByTagName('abbr');
			for (var i = 0; i < abbrs.length; i++) {
				var oldAbbr = abbrs.item(i);
				var newAbbr = document.createElement('abbr');
				newAbbr.title = oldAbbr.title;
				oldAbbr.parentNode.insertBefore(newAbbr, oldAbbr);
				while (oldAbbr.nextSibling.nodeName != '/ABBR') {
					newAbbr.appendChild(oldAbbr.nextSibling);
				}
				oldAbbr.parentNode.removeChild(oldAbbr.nextSibling);
				oldAbbr.parentNode.removeChild(oldAbbr);
			}
		}
	}
};


// Menu List Add Class "first" and "last" - use to ul
(function($) {
	jQuery.fn.addClassFL = function(colNum) {
		return this.children("li").each(function(i){
			if (i % colNum == 0){
				$(this).addClass("first");
			}
			if (i % colNum == colNum - 1){
				$(this).addClass("last");
			}
		});
	};
})(jQuery);


// Menu List Add Class "first" and "last" - use to div
(function($) {
	jQuery.fn.addDivClassFL = function(colNum) {
		return this.children("div").each(function(i){
			if (i % colNum == 0){
				$(this).addClass("first");
			}
			if (i % colNum == colNum - 1){
				$(this).addClass("last");
			}
		});
	};
})(jQuery);

//Set Fontsize
$.lm.fontSize = {
	init: function(){
		var fontSize = getCookie("fontsize");
		var folder = "";
		if(fontSize){
			if(fontSize == "s") {
				$("#smallText img")
					.attr("src", folder + "/shared/img/header_btn_size_01_o.gif")
					.removeClass("hover")
					.unbind("mouseover")
					.unbind("mouseout")
					.unbind("focus")
					.unbind("blur");
				if($.browser.msie) {
					$("body").css("font-size", "76%");
				} else {
					$("body").css("font-size", "90%");
				}
			}
			if(fontSize == "m") {
				$("#mediumText img")
					.attr("src", folder + "/shared/img/header_btn_size_02_o.gif")
					.removeClass("hover")
					.unbind("mouseover")
					.unbind("mouseout")
					.unbind("focus")
					.unbind("blur");
				$("body").css("font-size", "100%");
			}
			if(fontSize == "l") {
				$("#largeText img")
					.attr("src", folder + "/shared/img/header_btn_size_03_o.gif")
					.removeClass("hover")
					.unbind("mouseover")
					.unbind("mouseout")
					.unbind("focus")
					.unbind("blur");
				$("body").css("font-size", "110%");
			}
		} else {
			$("#mediumText img")
				.attr("src", folder + "/shared/img/header_btn_size_02_o.gif")
				.removeClass("hover")
				.unbind("mouseover")
				.unbind("mouseout")
				.unbind("focus")
				.unbind("blur");
			$("body").css("font-size", "100%");
		}
		$("#smallText").click(function(){
			$("#smallText img")
				.attr("src", folder + "/shared/img/header_btn_size_01_o.gif")
				.removeClass("hover")
				.unbind("mouseover")
				.unbind("mouseout")
				.unbind("focus")
				.unbind("blur");
			$("#mediumText img")
				.attr("src", folder + "/shared/img/header_btn_size_02.gif")
				.addClass("hover");
			$("#largeText img")
				.attr("src", folder + "/shared/img/header_btn_size_03.gif")
				.addClass("hover");
			if($.browser.msie) {
				$("body").css("font-size", "76%");
			} else {
				$("body").css("font-size", "90%");
			}
			var expire = getExpDate(365, 0, 0);
			setCookie("fontsize", "s", expire, "/");
			return false;
		});
		$("#mediumText").click(function(){
			$("#smallText img")
				.attr("src", folder + "/shared/img/header_btn_size_01.gif")
				.addClass("hover");
			$("#mediumText img")
				.attr("src", folder + "/shared/img/header_btn_size_02_o.gif")
				.removeClass("hover")
				.unbind("mouseover")
				.unbind("mouseout")
				.unbind("focus")
				.unbind("blur");
			$("#largeText img")
				.attr("src", folder + "/shared/img/header_btn_size_03.gif")
				.addClass("hover");
			$("body").css("font-size", "100%");
			var expire = getExpDate(365, 0, 0);
			setCookie("fontsize", "m", expire, "/");
			return false;
		});
		$("#largeText").click(function(){
			$("#smallText img")
				.attr("src", folder + "/shared/img/header_btn_size_01.gif")
				.addClass("hover");
			$("#mediumText img")
				.attr("src", folder + "/shared/img/header_btn_size_02.gif")
				.addClass("hover");
			$("#largeText img")
				.attr("src", folder + "/shared/img/header_btn_size_03_o.gif")
				.removeClass("hover")
				.unbind("mouseover")
				.unbind("mouseout")
				.unbind("focus")
				.unbind("blur");
			$("body").css("font-size", "110%");
			var expire = getExpDate(365, 0, 0);
			setCookie("fontsize", "l", expire, "/");
		});
	}
};

// Cookie
function getExpDate(days, hours, minutes){
	var expDate = new Date();
	if(typeof days == "number" && typeof hours == "number" && typeof minutes == "number"){
		expDate.setDate(expDate.getDate() + parseInt(days));
		expDate.setHours(expDate.getHours() + parseInt(hours));
		expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
		return expDate.toGMTString();
	}
}

function getCookieVal(offset){
	var endstr = document.cookie.indexOf(";", offset);
	if(endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while(i < clen){
		var j = i + alen;
		if(document.cookie.substring(i, j) == arg){
			return getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0) break;
	}
	return "";
}

function setCookie(name, value, expires, path, domain, secure){
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function deleteCookie(name, path, domain) {
	if(getCookie(name)){
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-0Jan-70 00:00:01 GMT";
	}
}

function string2Array(str){
	var arr = new Array();
	if(str.charAt(0) == ","){
		str = str.substring(1);
	}
	if(str.charAt(str.length - 1) == ","){
		str = str.substring(0, str.length - 2);
	}
	str = str.replace(" ","");
	var flag = 0;
	while(flag == 0){
		if(str.indexOf(",") != -1){
			arr.push(str.substring(0, str.indexOf(",")));
			str = str.substring(str.indexOf(",") + 1);
		} else {
			arr.push(str);
			flag = 1;
		}
	}
	return arr;
}

function array2String(arr){
	var str = "";
	var flag = 0;
	for(i in arr){
		if(flag != 0){
			str += ",";
		} else {
			flag = 1;
		}
		str += arr[i];
	}
	return str;
}

