/*
【アプリ開発用ライブラリ】
LittleambitionJs
copyright littleambition 2020
*/
//タイマー定数
if(typeof TIMER_FUNC==undefined){
	const TIMER_FUNC = 0;
	const TIMER_TIME = 1;
	const TIMER_START_TIME = 2;
	const TIMER_FLG = 3;
	const TIMER_OPTION = 4;
}

//アプリ開発用ライブラリ
class lmObj{
	//コンストラクタ
	constructor( name ) {
		this.elementname = "";
		this.elementtype = "";
		this.timerData = [];
		this.timerlock = -1;
		if(name == undefined)return;
		if(name.indexOf("#")==0){
			return getElement(name);
		}else{
			return getElement(name,"tag");
		}
	}
	
	//要素生成
	createElement(tag,id,name,func=null){
		var elm = document.createElement(tag);
		elm.id = id;
		elm.name = name;
		if(func!=null){
			func(elm);
		}
		return elm;
	}
	
	//要素追加
	addElement(elm1,elm2){
		elm1.appendChild(elm2);
	}

	//要素取得
	getElement(elmid,type="id")
	{
		var res = null;
		
		//IDで取得
		if(type == "id"){
			res = document.getElementById(elmid);
		}
		if(type == "tag"){
			var buff_elm = document.getElementsByTagName(elmid);
			res = buff_elm[0];
		}
		
		return res;
	}
	
	//関数設定
	setElementFunction(elmid,funcObj,event,type="id"){
		var elm = this.getElement(elmid,type);
		if(event = "onclick"){
			elm.onclick = funcObj;
		}
	}
	
	//style設定
	setElementStyle(elmid,styelObj,type="id"){
		var elm = this.getElement(elmid,type);
		elm.style = styelObj;
	}

	//CSS設定
	setElementCSS(elmid,cssObj,type="id"){
		var elm = this.getElement(elmid,type);
		elm.csslist.push(cssObj);
	}
	
	//要素設定
	setElement(elmname,type="id"){
		this.elementname = elmname;
		this.elementtype = type;
	}
	
	//関数設定
	setFunction(funcObj,event){
		var elmname = this.elementname;
		var elmtype = this.elementtype;
		var elm = this.setElementFunction(elmname,funcObj,event,elmtype);
	}

	//関数設定
	setStyle(styelObj){
		var elmname = this.elementname;
		var elmtype = this.elementtype;
		var elm = this.setElementStyle(elmname,styelObj,elmtype);
	}

	//関数設定
	setCss(cssObj){
		var elmname = this.elementname;
		var elmtype = this.elementtype;
		var elm = this.setElementCSS(elmname,cssObj,elmtype);
	}
	
	//操作要素名
	get elmname(){
		return this.elementname;
	}
	
	//タイマーセット
	setTimer(funcObj,time,option){
		var date_obj = new Date();
		var setData = [funcObj,time,date_obj.getTime(),false,option];
		this.timerData.push(setData);
		if(this.timerData.length==1){
			this.Timer(this.timerData);
		}
	}
	
	//タイマー処理
	Timer(funcData){
		var timeId = setTimeout(function(){
			var func_value = 0;
			for(var loop = 0;loop<funcData.length;loop++){
				var date_obj = new Date();
				var time1 = funcData[loop][TIMER_START_TIME];
				var time2 = date_obj.getTime();
				var timeprc = time2-time1;
				if(timeprc>=funcData[loop][TIMER_TIME]){
					if(!funcData[loop][TIMER_FLG]){
						funcData[loop][TIMER_FUNC](funcData[loop][TIMER_OPTION]);
						funcData[loop][TIMER_FLG] = true;
					}
				}
				if(!funcData[loop][TIMER_FLG])func_value++;
			}
			if(func_value!=0){
				glObj.Timer(funcData);
			}else{
				funcData.length = 0;
			}
		},100);
	}
}
//LittleambitionJs本体
glObj = new lmObj();