/*
* lm 1.6
*
* Copyright: (c) 2008-2011 ludicmind.net
**************************************/

// *** Debug ***
if (typeof window.console != 'object'){
	window.console = {log:function(){},debug:function(){},info:function(){},warn:function(){},error:function(){},assert:function(){},dir:function(){},dirxml:function(){},trace:function(){},group:function(){},groupEnd:function(){},time:function(){},timeEnd:function(){},profile:function(){},profileEnd:function(){},count:function(){}};
}

var iPhone = navigator.userAgent.indexOf("iPhone") != -1 ? 1 : 0;
var iPad = navigator.userAgent.indexOf("iPad") != -1 ? 1 : 0;
var IE = navigator.userAgent.indexOf("MSIE") != -1 ? 1 : 0;

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

$(document).ready(function(){
	$.lm.init();
	$("div#header div.col3 div a").toggle(function(){
		$("ul.foreign").slideDown("normal");
		return false;
	},
	function(){
		$("ul.foreign").slideUp("normal");
		return false;
	});
	if($("body.single").length){
		$("div.moreButton").hide();
		$("div.closeButton").hide();
	}
});


// 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");
		}
	}
};


// Image hover and active
$.lm.hover2 = {
	init: function() {
		$('.hover2')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('blur', this.exit)
			.live('mousedown', this.down)
			.live('mouseup', this.up)
			.each(this.preload);
	},

	preload: function() {
		this.preloaded = new Image;
		this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
		this.preloaded2 = new Image;
		this.preloaded2.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_a$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");
		}
	},

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

	up: function() {
		if (this.src.match(/^(.+)_a(\.[a-z]+)$/)){
			this.src = this.src.replace(/^(.+)_a(\.[a-z]+)$/, "$1_o$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);
	}
};

// Notice hover
$.lm.nHover = {
	init: function() {
		$('img.nHover')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('blur', this.exit);
		$('a.nHover')
			.live('mouseover', this.aenter)
			.live('mouseout', this.aexit)
			.live('focus', this.aenter)
			.live('blur', this.aexit);
	},

	enter: function() {
		$(this).animate({opacity: 0.5}, 0).animate({opacity: 1}, 500);
	},

	exit: function() {
	},

	aenter: function() {
		$(this).find("img").animate({opacity: 0.5}, 0).animate({opacity: 1}, 500);
	},

	aexit: function() {
	}
};

// PNG Image
/*$.lm.pngfilter = {
	init: function() {
		$("img:not(.phover)").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 && ua.appVersion.indexOf("MSIE 8.0") == -1) {
			$("#content *:first-child").addClass("first-child");
		}
	}
};


// Inner Link
$.lm.innerLink = {
	init: function() {
		$("a.innerLink").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("#") == 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='pdfIcon' src='/shared/img/icon_pdf_01.gif' alt='PDF' width='11' height='12' />");
				} else if (this.target) {
					$(this).append(" <img class='windowIcon' src='/shared/img/icon_window_01.gif' alt='別ウィンドウで開きます' width='12' height='11' />");
				}
			}
		});
	}
};


// 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.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）");
							}
						}
					}); 
				// CSV
				} else if (this.href.indexOf(".csv") != -1 || this.href.indexOf(".xls") != -1) {
					$(this).addClass("excel");
				// 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).addClass("otherWindow");
				}
			// 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");
	}
};


// Error Message
function showError(message){
	var innerWidth = $.browser.msie ? document.documentElement.clientWidth : window.innerWidth;
	var innerHeight = $.browser.msie ? document.documentElement.clientHeight : window.innerHeight;
	var winWidth = innerWidth > document.body.offsetWidth ? innerWidth : document.body.offsetWidth;
	var winHeight = innerHeight > document.body.offsetHeight ? innerHeight : document.body.offsetHeight;
	var pageOffset = $.browser.msie ? document.documentElement.scrollTop : window.pageYOffset;

	$("body").append("<div id='filter' style='position: absolute; top: 0; left: 0; width: 100%; height: " + winHeight + "px; background-color: #FFFFFF; opacity: 0.7; filter: alpha(opacity=70)'></div>");
	$("body").append("<p id='error' style='position: absolute; top: " + (pageOffset + (innerHeight - 50) / 2) + "px; left:" + (innerWidth - 495) / 2 + "px'>" + message + "</p>");
	setTimeout("hideError()", 4000);
}

function hideError(){
	$("#filter").fadeOut("slow", function(){
		$("#filter").remove();
	});
	$("#error").fadeOut("slow", function(){
		$("#error").remove();
	});
}


// Number Convert
function convertNum(from) {
    var han = ['1','2','3','4','5','6','7','8','9','0'];
    var zen = ['１','２','３','４','５','６','７','８','９','０'];
    var to = [];
    for( var i = 0 ; i < from.length ; i++ ) {
        for(var j = 0 ; j < zen.length ; j++ ) {
            if (from.charAt(i) == zen[j]) {
                to.push( han[j] );
                break;
            }
        }
        if (j == zen.length ) {
            to.push( from.charAt(i) );
        }
    }
    return to.join('');
}

// Flash Relation
function getMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}


// Navigation Submenu
$.lm.submenu = {
	init: function() {
		var toggle = function(direction, display) {
			return function() {
				var self = this;
				var ul = $(this).children("ul").eq(0);
				if( ul.css("display") == display && !self["block" + direction] ) {
					self["block" + direction] = true;
					ul["slide" + direction]("fast", function() {
						self["block" + direction] = false;
					});
				}
			};
		}
		$("li.hasSubmenu").hover(toggle("Down", "none"), toggle("Up", "block"));
		$("li.hasSubmenu ul").hide();
	}
};


// 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);


// Subpage Link
$.lm.subPage = {
	init: function() {
		$("a.subPage")
			.bind("click", this.openWindow)
			.bind("keypress", this.openWindow);
	},
	
	openWindow: function() {
		window.open($(this).attr("href"), "subPage", "width=1024, menubar=no, toolbar=no, scrollbars=yes");
		return false;
	}
};


// Price Format
function formatPrice(str) {
	var num = new String(str).replace(/,/g, "");
	while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	return num;
}


// Tweet Time
var tweetTime = (function() {
	var ints = {
			seconds: 1,
			minutes: 60,
			hours: 3600,
			days: 86400,
			weeks: 604800,
			month: 2592000,
			year: 31536000
	};

	return function(time) {
		origTime = time;
		time = new Date(time).getTime();
		if(isNaN(time)){
			var year = origTime.substring(26, 30);
			var month;
			if(origTime.indexOf("Jan") != -1) month = "01";
			if(origTime.indexOf("Feb") != -1) month = "02";
			if(origTime.indexOf("Mar") != -1) month = "03";
			if(origTime.indexOf("Apr") != -1) month = "04";
			if(origTime.indexOf("May") != -1) month = "05";
			if(origTime.indexOf("Jun") != -1) month = "06";
			if(origTime.indexOf("Jul") != -1) month = "07";
			if(origTime.indexOf("Aug") != -1) month = "08";
			if(origTime.indexOf("Sep") != -1) month = "09";
			if(origTime.indexOf("Oct") != -1) month = "10";
			if(origTime.indexOf("Nov") != -1) month = "11";
			if(origTime.indexOf("Dec") != -1) month = "12";
			var day = origTime.substring(8, 10);
			var hour = origTime.substring(11,19);
			var timezone = origTime.substring(20,25);
			var newtime = year + "/" + month + "/" + day + " " + hour + " " + timezone;
			time = new Date(newtime).getTime();
		}
		var now = new Date().getTime();
		time = Math.round(time / 1000);
		now = Math.round(now / 1000);
		
		var gap = now - time, amount, measure, dir;
		if (gap > 0) {
			dir = " ago";
		} else if (gap < 0) {
			dir = " later";
		} else {
			return "just now";
		}
		
		gap = Math.abs(gap);
		
		for (var i in ints) {
				if (gap >= ints[i]) { measure = i; }
		}
		
		amount = Math.round(gap / ints[measure]);
		amount = gap > ints.day ? (Math.round(amount * 100) / 100) : Math.round(amount);
		amount = 'about ' + Math.ceil(amount) + ' ' + measure + dir;
		
		return amount;
	};

})();


// 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){ // expiresはgetExpDate()で得る
	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;
}

