/**
 * proto-webtools core (jp.rinco): JavaScript basic lib
 * @require prototype-1.6.0.js or later
 * @licence 3-clause BSD license
 */

var jp;
if (!jp) {var jp = {};}
else if (typeof jp != 'object')
	throw new Error("jp already exists and is not an object.");

if (!jp.rinco) {
	jp.rinco = {
		url: "http://rinco.jp/#jp-rinco",
		author: "Toshio Yamashita (yamachan)",
		mail: "z@rinco.jp",
		version: "0.0.0",
		update: "2008/10/21"
	}
} else if (typeof jp.rinco != 'object')
	throw new Error("jp.rinco already exists and is not an object.");

if (true) {
	String.prototype.isNumber = function(){
		return this.match(/^[\+\-]?\d+\.?\d*$/)==null ? false : true;
	}
	String.prototype.right = function(n){
		if (typeof n=='string') {
			var p = this.indexOf(n);
			return p < 0 || n.empty() ? "" : this.substring(p + n.length);
		}
		n = Number(n);
		return isNaN(n) ? "" : n < 0 || n >= this.length ? this : this.substring(this.length - n);
	}
	String.prototype.left = function(n){
		if (typeof n=='string') {
			var p = this.indexOf(n);
			return p < 0 || n.empty() ? "" : this.substring(0,p);
		}
		n = Number(n);
		return isNaN(n) ? "" : n < 0 || n >= this.length ? this : this.substring(0,n);	
	}
	String.prototype.padding = function(l,c){
		l = Number(l);
		return isNaN(l)||l<1 ? "" : l<this.length ? this.right(l) : (c||' ').times(l-this.length).left(l-this.length)+this
	}
	String.prototype.toStringArray = function(n,a) {
		a = a||[];
		n = Number(n||1);
		if (!isNaN(n) && n>0) {
			var c = Math.floor(this.length / n);
			for (var l=0; l<c; l++)
				a.push(this.substring(n*l, n*(l+1)));
			if (n*c < this.length)
				a.push(this.substring(n*c, this.length));		
		}
		return a;
	}
	String.prototype.fromBase64 = function(){
		var s = this.replace(/=+$/,'').strip();
		if (s.empty()) return Number.NaN;
		if (s.length == 1) {
			var n = s.charCodeAt(0);
			return 64<n&&n<91 ? n-65 : 96<n&&n<123 ? n-97+26 : 47<n&&n<58 ? n-48+52 : n==43 ? 62 : n==47 ? 63 : Number.NaN;
		} else {
			var ret = 0;
			s.toArray().each(function(v){ret = ret * 64 + v.fromBase64()});
			return ret;
		}
	}	
}
if (true) {
	Number.prototype.toBase64 = function(){
		if (isNaN(this) || this<0) return "";
		var n = Math.floor(this);
		if (n < 64)
			return String.fromCharCode(n<26 ? n+65 : n<52 ? n+71 : n<62 ? n-4 : n<63 ? 43 : 47);
		else
			return (n/64).toBase64() + (n%64).toBase64();
	}
}

jp.rinco.today = new Date();
if (true) {
	Date.prototype.toSimpleDateString = function(s){
		s = s==undefined ? '/' : s;
		return this.getFullYear() + s + (this.getMonth()+1).toString().padding(2,'0') + s + this.getDate().toString().padding(2,'0');
	}
	Date.prototype.isLeapYear = function(){return this.getFullYear()%400==0 ? true : this.getFullYear()%100==0 ? false : this.getFullYear()%4==0 ? true : false};
	Date.prototype.getDaysOfMonth_List = [31,28,31,30,31,30,31,31,30,31,30,31];
	Date.prototype.getDaysOfMonth = function(){return this.getMonth()==1&&this.isLeapYear() ? 29 : this.getDaysOfMonth_List[this.getMonth()]}
	Date.prototype.addYear = function(v){this.setYear(this.getFullYear() + Number(v)); return this}
	Date.prototype.addMonth = function(v){this.setMonth(this.getMonth() + Number(v)); return this}
	Date.prototype.addDate = function(v){this.setDate(this.getDate() + Number(v)); return this}
	Date.prototype.getDaysBefore = function(v){
		var d = new Date(this.getFullYear(), this.getMonth(), 1).getDay() - v;
		return d < 0 ? d + 7 : d;
	}
	Date.prototype.getDaysBeforeMonday = function(){return this.getDaysBefore(1)}
	Date.prototype.isSameDate = function(d){ d = d||jp.rinco.today; return d instanceof Date && this.getDate()==d.getDate(); }
	Date.prototype.isSameMonth = function(d){ d = d||jp.rinco.today; return d instanceof Date && this.getMonth()==d.getMonth(); }
	Date.prototype.isSameYear = function(d){ d = d||jp.rinco.today; return d instanceof Date && this.getYear()==d.getYear(); }
	Date.prototype.isSame = function(d){ d = d||jp.rinco.today; return d instanceof Date && this.getDate()==d.getDate() && this.getMonth()==d.getMonth() && this.getYear()==d.getYear(); }
}
if (true) {
	Array.prototype.insertAt = function(e,i) {
		i = Number(i); if (isNaN(i)||i>this.length) return this;
		var ret = this.slice(0,i);
		ret.push(e);
		return ret.concat(this.slice(i));
	}
	Array.prototype.insertBefore=function(e,o){var i=this.indexOf(o);return i<0?this:this.insertAt(e,i)}
	Array.prototype.concatAt = function(e,i) {
		i = Number(i); if (isNaN(i)||i>this.length) return this;
		return this.slice(0,i).concat(e).concat(this.slice(i));
	}
	Array.prototype.concatBefore=function(e,o){var i=this.indexOf(o);return i<0?this:this.concatAt(e,i)}
}

//jp.rinco.styleList = [
//	"width",
//	"height",
//	"fontSize",
//	"backgroundColor"
//];

if (true) { Element.addMethods({
	getText: function(e) {return (e.innerText ? e.innerText : e.innerHTML.stripTags()).unescapeHTML();},
	replaceClassName: function(e, f, t, force) {
		e = $(e);
		if (e.hasClassName(f))
			e.className = e.className.replace(new RegExp("(^|\\s+)" + f + "(\\s+|$)"), ' '+t+' ' ).strip();
		else if (force == true) 
			e.addClassName(t);
		return e;
	},
	tryDown: function(e,s,n) {
		if (s==undefined || s=='') return e;
		var ee = e.down(s,n);
		return ee==undefined ? e : ee;
	},
	//getStyles: function(e,list) {
	//	list = list=undefined ? jp.rinco.styleList : list;
	//	var ret = {};
	//	for (var l=0; l<list.length; l++) {
	//		var v = e.getStyle(list[l]);
	//		alert(v);
	//		if (v != null)
	//			ret[list[l]] = v;
	//	}
	//	return ret;
	//},
	parentMatch: function(e,css) {
		return e.match(css) ? e : e.parentNode instanceof Element ? $(e.parentNode).parentMatch(css) : null;
	}
	//parentMyClass: function(e,c) {
	//	return e.myClass == c ? e : e.parentNode instanceof Element ? $(e.parentNode).parentMyClass(c) : null;
	//}
})}

//jp.rinco.isEmpty = function(v) {return (v==undefined||v==null||v==NaN||v=="") ? true : false;}

jp.rinco.baseObject = new Class.create({
	initialize: function(){
		this.me = this;
	},
	myClass: 'jp.rinco.baseObject',
	lastUpdate: "2008/06/04"
});

jp.rinco.alwaysTrue = function(){return true}
jp.rinco.alwaysFalse = function(){return false}

jp.rinco.getWorkElement = function (k){
	if ($(k))
		return $(k);
	else {
		var ne = document.createElement("div");
		ne.setAttribute('id',k);
		ne.setAttribute('display','none');
		//ne.setAttribute('visibility','hidden');
		return $(ne);
	}
}

jp.rinco.observeList = [];
Event.observe(window, 'load', function(){
	jp.rinco.observeList.each(function(f){
		if (f && f instanceof Function)
			f();
	});
});

//String.prototype.escHtml = function(){ var i,e={'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'},t=this; for(i in e) t=t.replace(new RegExp(i,'g'),e[i]); return t }
//String.prototype.unescHtml = function(){ var i,e={'&lt;':'<','&gt;':'>','&amp;':'&','&quot;':'"'},t=this; for(i in e) t=t.replace(new RegExp(i,'g'),e[i]); return t }
//String.prototype.isEmail = function () { var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); var matches = rx.exec(this); return (matches != null && this == matches[0]); }
//String.prototype.isURL = function () { var rx = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-\\+ ./?%:&=#\\[\\]]*)?"); var matches = rx.exec(this); return (matches != null && this == matches[0]); }

