/*
* lm 1.6
* Automatic functions for webpages
* Require:
*  jquery.js
*  frame.css
*  content_font.css
*
* Copyright: (c) 2008-2011 ludicmind.net
**************************************
*
* Image hover
* Image hover and active
* Filter hover
* Opacity Change hover
* Notice hover
* PNG Image
* PNG Image hover
* Add "first-child" class to all elements in #content
* Inner Link
* Add "nolink" class to Heading without a[href]
* Mouse hover on input[type='image']
* Flash Relation
* Replace "abbr" with "acronym" (only IE6)
* Menu List Add Class "first" and "last" - use to ul
* Menu List Add Class "first" and "last" - use to div
* Cookie
*
**************************************/

// *** 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 INTERVAL = 175;
var DURATION = 300;

var ua = navigator.userAgent;
var Android = 0;
if(ua.indexOf("Android") != -1){
	Android = 1;
}
var iOS = 0;
if(ua.indexOf("iPhone") != -1){
	iOS = 1;
} else if(ua.indexOf("iPad") != -1){
	iOS = 1;
}
var Mac = 0;
if(ua.indexOf("like Mac OS X") == -1 && ua.indexOf("Mac OS X") != -1){
	Mac = 1;
}

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

$(document).ready(function(){
	$.lm.init();
	if(!Android && !iOS && !$("body").hasClass("top")){
		if($.browser.msie && parseInt($.browser.version) <= 6){
		} else {
			$("div#header").css("width", "100%").css("min-width", "960px").css("position", "fixed").css("top", 0).css("left", 0).css("z-index", "9999");
			$("div#body").css("padding-top", "82px");
		}
	}
	$("#left_column a.topic_block:even").addClass("first");
	$("#left_column a.topic_block:odd").addClass("last");
	$(".work_block").each(function(i){
		if (i % 4 == 0){
			$(this).addClass("first");
		}
		if (i % 4 == 3){
			$(this).addClass("last");
		}
	});
	$("#topFb").click(function(){
		_gaq.push(['_trackPageview', "/fb2013_01"]);
	});
	$("#recruitFb").click(function(){
		_gaq.push(['_trackPageview', "/fb2013_02"]);
	});
	
	if(($.browser.msie && parseInt($.browser.version) >= 8) || !$.browser.msie){
		setTimeout(function(){
			var cHeight = parseInt($("body").css("height").replace("px", ""));
			var wHeight = document.documentElement.clientHeight;
			var pad = "17px";
			if(Mac) pad = "16px";
			if(cHeight <= wHeight){
				$("body").css("padding-right", pad);
				$("#header_content").css("padding-right", pad);
			}
		}, 1000);
	}
});


// Navigation
$.lm.nav = {
	init: function() {
		var href = "";
		if(location.href.indexOf("/topic/") != -1){
			$("#gNavTopic").find("img").removeClass("hover").attr("src", $("#gNavTopic").find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_current$2"));
		}
		if(location.href.indexOf("/service/") != -1){
			$("#gNavService").find("img").removeClass("hover").attr("src", $("#gNavService").find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_current$2"));
		}
		if(location.href.indexOf("/work/") != -1){
			$("#gNavWork").find("img").removeClass("hover").attr("src", $("#gNavWork").find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_current$2"));
		}
		if(location.href.indexOf("/about/") != -1){
			$("#gNavAbout").find("img").removeClass("hover").attr("src", $("#gNavAbout").find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_current$2"));
		}
		$("#right_column dl.list01 a").each(function(){
			href = $(this).attr("href").replace("index.html", "");
			loc = location.href.replace("http://", "");
			loc = loc.substring(loc.indexOf("/")).replace("index.html", "");
			if(loc == href){
				$(this).addClass("current");
			}
		});
		$("#right_column dl.list02 a").each(function(){
			href = $(this).attr("href").replace("index.html", "");
			loc = location.href.replace("http://", "");
			loc = loc.substring(loc.indexOf("/")).replace("index.html", "");
			if(loc == href){
				$(this).parent("dd").addClass("current");
			}
		});
		$("#right_column dl.list03 a").each(function(){
			href = $(this).attr("href").replace("index.html", "");
			loc = location.href.replace("http://", "");
			loc = loc.substring(loc.indexOf("/")).replace("index.html", "");
			if(loc == href){
				if($(this).parent("dd").parent("dl").hasClass("in_navi")){
					$(this).parent("dd").parent("dl").addClass("current");
				} else {
					$(this).addClass("current");
				}
			}
		});
	}
}

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


// Filter hover
$.lm.fHover = {
	init: function() {
		$('.fHover').hover(function(){
			$(this)
				.css("display", "block")
				.css("position", "relative")
				.append("<div id='hoverFilter' style='position: absolute; margin: 0; top: 0; left: 0; width: " + $(this).find("img").attr("width") + "px; height: " + $(this).find("img").attr("height") + "px; background-color: #ffffff; opacity: 0.45; filter: alpha(opacity=45)'></div>");
			$("div#hoverFilter").delay(100).fadeOut("normal", function(){
				$("div#hoverFilter").remove();
			});
		},
		function(){
			$("div#hoverFilter").remove();
		});
	}
};


// 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[href]").each(function(){
			if($(this).attr("href").indexOf("#") == 0){
				if($(this).attr("href") == "#topic" || $(this).attr("href") == "#service" || $(this).attr("href") == "#work" || $(this).attr("href") == "#about" || $(this).attr("href") == "#access") {
					var topPos = -90;
					if(iOS){
						topPos = -40;
					}
					$(this).click(function(){
						var toPos = $(this).attr("href") + "1";
						location.hash = $(this).attr("href");
						$.scrollTo(toPos, {duration: 1400, easing: "easeOutExpo", offset: {top: topPos}});
						return false;
					});
				} else 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");
	}
};


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

	init: function() {
		$('input[type="image"]')
			.live('mouseover', this.enter)
			.live('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() {
		if(this.src.indexOf("_o.") == -1){
			this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
		}
	},

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


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


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


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

