/*
* lm 1.2.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#newsImage div.small:odd").addClass("even");
	$("div#newsImage div.small:lt(2)").css("margin-top","0");
	$("ul.downloadULA01 li:odd").addClass("even");
	$("ul.downloadULA01 li:even").addClass("odd");
	if(!($.browser.msie && parseInt($.browser.version) < 7)){
		$("div#topFilter")
			.fadeOut(4000, function(){
				$("div#lead").fadeIn(1500);
			});
		$("div#mainImageFilter").fadeOut(4000, function(){
			if($("div#mainImage2")){
				$("div#mainImage2").fadeIn(2500);
			}
		});
	}
	$("#searchWord").bind("focus", function(){
		this.select();
	});
	$("#searchWord").bind("click", function(){
		this.select();
	});
	$("#newsImage div.small a").click(function(){
		$("#newsImage div.large img").attr("src", $(this).attr("href"));
		return false;
	});
});


// 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);
	}
};


// 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");
		}
	}
};


// 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");
	}
};


// Mouse hover on input[type='image']
$.lm.inputHover = {
	init: function() {
		$('input[type="image"]').each(function(){
			if(!$(this).hasClass("noHover")){
				$(this).bind('mouseover', $.lm.inputHover.enter)
				.bind('mouseout', $.lm.inputHover.exit)
				.each($.lm.inputHover.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");
	}
};

// 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;
					});
				}
			}
		});
	}
};

// Navigation Submenu
$.lm.submenu = {
	init: function() {
		$("ul#gNav li.first").hover(
			function () {
				$(this).children("ul.gNavSecond").fadeIn("fast");
				$("ul#gNav li.first").css("z-index", "1");
				$(this).css("z-index", "2");
			},function(){
				$(this).children("ul.gNavSecond").fadeOut("fast");
		});

		$("ul.gNavSecond li.second").hover(
			function () {
				$(this).children("ul").fadeIn("fast");
				$("ul.gNavSecond li.second").css("z-index", "1");
				$(this).css("z-index", "2");
			},function(){
				$(this).children("ul").fadeOut("fast");
		});
	}
};

// Local Navigation
$.lm.lNav = {
	init: function() {
		$("ul#lNav > li:has('ul')").each(function(){
			if($(this).hasClass("current")) {
				$(this).append("<div><img class='lNavButton' src='/corp/shared/img/btn_lNav_close.gif' alt='閉じる' width='11' height='13' /></div>");
			} else {
				$(this).append("<div><img class='lNavButton' src='/corp/shared/img/btn_lNav_open.gif' alt='開く' width='11' height='13' /></div>");
			}
		});
		$("img.lNavButton").bind("click", this.change);
	},
	
	change: function() {
		if($(this).attr("src").indexOf("close") != -1) {
			$(this).attr("src", "/corp/shared/img/btn_lNav_open.gif").attr("alt", "開く");
			$(this).parent("div").siblings("ul").hide("fast");
		} else {
			$(this).attr("src", "/corp/shared/img/btn_lNav_close.gif").attr("alt", "閉じる");
			$(this).parent("div").siblings("ul").show("fast");
		}
	}
};

// Link Icon & PDF Size
$.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) {
					var elem = $(this);
					elem.addClass("pdf");
					$.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）");
							}
						}
					}); 
				} else if (this.href.indexOf(".csv") != -1 || this.href.indexOf(".xls") != -1) {
					$(this).addClass("excel");
				} 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).addClass("otherWindow");
				}
			} 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>");
						}
					}
				}); 
			}
		});
	}
};

// News Block
$.lm.newsBlock = {
	init: function() {
		if(document.URL.indexOf("?year") != -1){
			var showId = "#news" + document.URL.substring(document.URL.indexOf("=") + 1);
			if($(showId).length){
				$(".newsBlockA01").hide();
				$(showId).show();
			} else {
				$(".newsBlockA01:gt(0)").hide();
			}
		} else {
			$(".newsBlockA01:gt(0)").hide();
		}
		$(".newsBlockA01 .pagination a").click(function(){
			$(".newsBlockA01").hide();
			var id = $(this).attr("href");
			$(id).show();
			return false;
		});
	}
};

// 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);

// 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;
}

// Set Fontsize
$.lm.fontSize = {
	init: function(){
		var fontSize = getCookie("fontsize");
		var folder = "";
		var https = document.URL.indexOf("https");
		if(https == 0){
			folder = "/novarese";
		}
		if(fontSize){
			if(fontSize == "s") {
				$("#fontSmall")
					.attr("src", folder + "/corp/shared/img/icon_fontSize_small_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") {
				$("#fontMedium")
					.attr("src", folder + "/corp/shared/img/icon_fontSize_medium_o.gif")
					.removeClass("hover")
					.unbind("mouseover")
					.unbind("mouseout")
					.unbind("focus")
					.unbind("blur");
				$("body").css("font-size", "100%");
			}
			if(fontSize == "l") {
				$("#fontLarge")
					.attr("src", folder + "/corp/shared/img/icon_fontSize_large_o.gif")
					.removeClass("hover")
					.unbind("mouseover")
					.unbind("mouseout")
					.unbind("focus")
					.unbind("blur");
				$("body").css("font-size", "110%");
			}
		} else {
			$("#fontMedium")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_medium_o.gif")
				.removeClass("hover")
				.unbind("mouseover")
				.unbind("mouseout")
				.unbind("focus")
				.unbind("blur");
			$("body").css("font-size", "100%");
		}
		$("#fontSmall").click(function(){
			$("#fontSmall")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_small_o.gif")
				.removeClass("hover")
				.unbind("mouseover")
				.unbind("mouseout")
				.unbind("focus")
				.unbind("blur");
			$("#fontMedium")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_medium.gif")
				.addClass("hover");
			$("#fontLarge")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_large.gif")
				.addClass("hover");
			$.lm.hover.init();
			if($.browser.msie) {
				$("body").css("font-size", "76%");
			} else {
				$("body").css("font-size", "90%");
			}
			var expire = getExpDate(365, 0, 0);
			setCookie("fontsize", "s", expire, "/");
		});
		$("#fontMedium").click(function(){
			$("#fontSmall")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_small.gif")
				.addClass("hover");
			$("#fontMedium")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_medium_o.gif")
				.removeClass("hover")
				.unbind("mouseover")
				.unbind("mouseout")
				.unbind("focus")
				.unbind("blur");
			$("#fontLarge")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_large.gif")
				.addClass("hover");
			$.lm.hover.init();
			$("body").css("font-size", "100%");
			var expire = getExpDate(365, 0, 0);
			setCookie("fontsize", "m", expire, "/");
		});
		$("#fontLarge").click(function(){
			$("#fontSmall")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_small.gif")
				.addClass("hover");
			$("#fontMedium")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_medium.gif")
				.addClass("hover");
			$("#fontLarge")
				.attr("src", folder + "/corp/shared/img/icon_fontSize_large_o.gif")
				.removeClass("hover")
				.unbind("mouseover")
				.unbind("mouseout")
				.unbind("focus")
				.unbind("blur");
			$.lm.hover.init();
			$("body").css("font-size", "110%");
			var expire = getExpDate(365, 0, 0);
			setCookie("fontsize", "l", expire, "/");
		});
	}
};
