/* Compatibility */
// @ref http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach
if(!Array.prototype.forEach)
{
	Array.prototype.forEach = function(fun /*, thisp*/)
	{
		var len = this.length;
		if(typeof fun != "function")
			throw new TypeError();
		
		var thisp = arguments[1];
		for(var i=0; i<len; i++)
		{
			if(i in this)
				fun.call(thisp, this[i], i, this);
		}
	};
}

if(!Array.prototype.some)
{
	Array.prototype.some = function(fun /*, thisp*/)
	{
		var len = this.length;
		if(typeof fun != "function")
			throw new TypeError();
		
		var thisp = arguments[1];
		for(var i = 0; i<len; i++)
		{
			if(	i in this &&
				fun.call(thisp, this[i], i, this))
				return true;
		}
		
		return false;
	};
}

if(!Array.prototype.filter)
{
	Array.prototype.filter = function(fun /*, thisp*/)
	{
		var len = this.length;
		if(typeof fun != "function")
			throw new TypeError();
		
		var res = new Array();
		var thisp = arguments[1];
		for(var i=0; i<len; i++)
		{
			if(i in this)
			{
				var val = this[i];
				if(fun.call(thisp, val, i, this))
					res.push(val);
			}
		}
		
		return res;
	};
}

/* extend */
// 任意の桁数になるように、ゼロ埋めする
// TODO: performance
Number.prototype.pad_zero = function(digit){
	var res = '';
	for(var i=0; i<digit; i++){
		res += '0';
	}
	return (res + this).slice(digit * -1);
};

/* common */
// TODO:
function now_as_second(){
	var now = new Date();
	return Math.floor(now.getTime() / 1000);
};

function format_time(second){
	
};

function parse_json(text){
	var res = null;
	try{
		res = eval("(" + text + ")");
	} catch(e){
		return null;
	}
	return res;
}

Select = {
	get_selected_option: function(id){
		var select = $(id);
		return select.options[select.selectedIndex];
	},
	// 失敗したら null を返す
	select_by_value: function(id, value){
		var select = $(id);
		for(var i=0;i<select.length;i++){
			if( select[i].value == value){
				select.selectedIndex = i;
				return select[i];
			}
		}
		return null;
	}
},

// extend
/*
Date.prototype.toString = function(){
	var year = this.getFullYear();
	var month = this.getMonth() + 1;
	var day = this.getDate();
	var hour = this.getHours();
	var minute = this.getMinutes();
	var second = this.getSeconds();

	return [
		year,"/",
		month.string_filled_zero(2),"/",
		day.string_filled_zero(2), " ",
		hour.string_filled_zero(2),":",
		minute.string_filled_zero(2),":",
		second.string_filled_zero(2)
	].join("");
};
*/
Date.prototype.getTimeBySecond = function(){
	return Math.floor(this.getTime() / 1000);
};
Date.from_second = function(second){
	// 0 は有効な値
	if(second == null) return null;
	return new Date(second * 1000);
};


Number.prototype.string_filled_zero= function(digit){
	var max = 1;
	var zeros = "";
	for(var i=0;i<digit - 1;i++){
		max *= 10;
		zeros += "0";
	}
	if(this < max){
		return zeros + this;
	}
	return this.toString();
};

// common function

function add_class(element, class_name){
	var classes = element.className.split(' ');
	var own_class = classes.some(function(v){
		return v == class_name;
	});
	if( !own_class ) element.className += ' ' + class_name;
	
	return element;
};

function remove_class(element, class_name){
	var classes = element.className.split(' ');
	var new_classes = classes.filter(function(v){
		return v != class_name;
	});
	element.className = new_classes.join(' ');

	return element;
};

function $N(name){
	return document.createElement(name);
}

function is_empty_string(value){
	return (value == null || value.length == 0) ? true : false;
}

