/*
* 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#noscriptMessage").hide();
	$("dl.newsDLA01 dd:eq(0)").addClass("first");
/*	if($("div#content.shop")){
		var bodyText = new Array();
		$("div#content.shop div.body p").each(function(i){
			bodyText[i] = $(this).text();
			if(bodyText[i].length > 70){
				var newText = bodyText[i].substring(0, 70) + "...<br /><a id='body" + i + "' href='#'>続きを読む</a>"
				$(this).html(newText);
				var anchorId = "a#body" + i;
				$(anchorId).click(function(){
					var identifier = $(this).attr("id").replace("body", "");
					$(this).parent("p").text(bodyText[identifier]);
					return false;
				});
			}
		});
	}*/
	$("div.itemBlockA01:odd").removeClass("itemBlockA01").addClass("itemBlockA02");
	$("div.itemBlockA03:odd").removeClass("itemBlockA03").addClass("itemBlockA04");
	$("dl.profileDLA01 dd:eq(0)").css("margin-top", "0");
	$("#favoriteHelp").hover(
		function(){
			$("#baloon").css("visibility", "visible").fadeIn("slow");
		},
		function(){
			$("#baloon").css("visibility", "hidden").fadeOut("fast");
		}
	);
	$("dl.formDLA01 dd:eq(0)").css("margin-top", "0");
	$("dl.formDLA02 dd:eq(0)").css("margin-top", "0");
	$("div.profileTableA01 table tr:even").addClass("odd");
	$("div#content.company").css("padding-top", "45px");
	$("div.enlargeHover").hover(
		function(){
			var img = $(this).siblings("p").children("a").children("img").attr("src");
			img = img.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
			$(this).siblings("p").children("a").children("img").attr("src", img);
		},
		function(){
			var img = $(this).siblings("p").children("a").children("img").attr("src");
			img = img.replace(/^(.+)_o(\.[a-z]+)$/, "$1$2");
			$(this).siblings("p").children("a").children("img").attr("src", img);
		}
	);
	$("body.home div#body")
		.css("padding", "0")
		.css("width", "100%")
		.css("height", "100%");
	$("div.articleBlockA01:not(:last)").css("border-style", "none none dashed");
	$("div.articleBlockA02:not(:last)").css("border-style", "none none dashed");
	$("div.recommendTableA01:not(:last)").css("border-style", "none solid dashed");
	$("div.seasonal_index > div:not(:last)").css("border-style", "none solid dashed");
	$("div.kidsBlock").each(function(){
		$(this).children("div:not(:last)").css("border-style", "none solid dashed");
	});
	if($("body.individual").length == 0){
		if($("#content").length > 0){
			if(document.getElementById("content").clientHeight < 491){
				$("#content").css("height", "491px");
			}
		}
	}
	$("img#windowClose").click(function(){
		window.close();
	});
});


// Image hover
$.lm.hover = {
	init: function() {
		$('.hover')
			.bind('mouseover', this.enter)
			.bind('mouseout', this.exit)
			.bind('focus', this.enter)
			.bind('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')
			.bind('mouseover', this.enter)
			.bind('mouseout', this.exit)
			.bind('focus', this.enter)
			.bind('blur', this.exit)
			.bind('mousedown', this.down)
			.bind('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);
	}
};


// 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");
	}
};


// 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(../images/icon_mail_01.gif) no-repeat left 0.4em");
				}
				if(this.href.indexOf(".pdf") != -1) {
//					$(this).append(" <img class='pdfIcon' src='/design/plain_site/images/icon_pdf_01.gif' alt='PDF' width='11' height='12' />");
				} else if (this.target && !$(this).hasClass("parking") && !$(this).hasClass("zoomLink")) {
					$(this).prepend("<img class='windowIcon' src='/design/plain_site/images/icon_window_01.gif' alt='別ウィンドウで開きます' width='12' height='11' /> ");
				} else if ($(this).hasClass("innerLink2")){
					$(this).prepend(" <img src='/design/plain_site/images/icon_arrow_01.gif' alt='' width='13' height='9' /> ");
				}
			}
		});
	}
};

// 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");
	}
};


// 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 {
					$(this).click(function(){
						$.scrollTo($(this).attr("href"), {duration: 700, easing: "easeOutExpo", offset: {top: -20}});
						return false;
					});
				}
			}
		});
	}
};

// 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);

// Flash Relation
function getMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}


// 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;
}
