/*
* lm 1.6
* Automatic functions for webpages
* Require:
*  jquery.js
*  frame.css
*  content_font.css
*
* Copyright: (c) 2008-2011 ludicmind.net
**************************************
*
* PNG Image
* PNG Image hover
* Add "first-child" class to all elements in #content
* Add "nolink" class to Heading without a[href]
* Mouse hover on input[type='image']
* Error Message
* Menu List Add Class "first" and "last" - use to ul
* Menu List Add Class "first" and "last" - use to div
* Subpage
* 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(){}};
}


// Initialization
$.lm = {
	init: function() {
		for (func in $.lm) {
			if ($.lm[func].init)
				$.lm[func].init();
		}
	}
};

$(document).ready(function(){
	$.lm.init();
});


// PNG Image
$.lm.pngfilter = {
	init: function() {
		$("img:not(.phover)").css("behavior", "url(/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(/^(.+)\-off(\.[a-z]+)$/, "$1\-on$2");
			$(this).css("behavior", "url(/htc/iepngfix.htc)");
		} else {
			this.preloaded = new Image;
			this.preloaded.src = this.src.replace(/^(.+)\-off(\.[a-z]+)$/, "$1\-on$2");
		}
	},

	enter: function() {
		if($.browser.msie && $.browser.version <= 6.0){
			if (this.src.match(/(blank)/)){
				this.src = this.preloaded.src;
				$(this).css("behavior", "url(/htc/iepngfix.htc)");
			}
		} else {
			if (!this.src.match(/^(.+)\-on(\.[a-z]+)$/) && !this.src.match(/^(.+)_a(\.[a-z]+)$/)){
				this.src = this.src.replace(/^(.+)\-off(\.[a-z]+)$/, "$1\-on$2");
			}
		}
	},

	exit: function() {
		if($.browser.msie && $.browser.version <= 6.0){
			if (this.src.match(/(blank)/)){
				this.src = this.initial.src;
				$(this).css("behavior", "url(/htc/iepngfix.htc)");
			}
		} else {
			if (this.src.match(/^(.+)\-on(\.[a-z]+)$/)){
				this.src = this.src.replace(/^(.+)\-on(\.[a-z]+)$/, "$1\-off$2");
			} else {
				this.src = this.src.replace(/^(.+)_a(\.[a-z]+)$/, "$1\-off$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");
		}
	}
};


// 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(/^(.+)\-off(\.[a-z]+)$/, "$1-on$2");
	},

	enter: function() {
		this.src = this.src.replace(/^(.+)\-off(\.[a-z]+)$/, "$1-on$2");
	},

	exit: function() {
		this.src = this.src.replace(/^(.+)\-on(\.[a-z]+)$/, "$1-off$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();
	});
}


// 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")
			.live("click", this.openWindow)
			.live("keypress", this.openWindow);
	},
	
	openWindow: function() {
		window.open($(this).attr("href"), "subPage", "width=900, height=791, menubar=no, toolbar=no, scrollbars=yes");
		return false;
	}
};

// 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;
}

