
function JSLoader(){var JSLOADER_ISLOADCHECKTIME=constObj.JSLoader.isLoadCheckTime||300;var JSLOADER_PROGRESSWATCHTIME=constObj.JSLoader.progressWatchTime||60;var JSLOADER_ISLOADCHECKINTERVAL=constObj.JSLoader.isLoadCheckInterval||5000;var JSLOADER_ISAPPENDTIME=constObj.JSLoader.isAppendTime||false;var JSLOADER_FILEVER=constObj.JSLoader.fileVer||"";var SCRIPTTAG_ID_PREFIX="jsFile_";var loadingJSFileList=new Array();var loadingJSFileListHash=new Object();var loadingCheckTimer;var isLoadCheckCnt;var onFinishCallBackObj;var beforeSameCnt=0;var beforeUnCompleteCnt=0;var loadingTotalCnt=0;var loadingFinishedCnt=0;var isLoading=false;var elmHead=document.getElementsByTagName('head')[0];this.getLoadingJSFileList=function(){return loadingJSFileList;}
this.setIsLoadFinished=function(jsFileKey,bool){if(isExistJSFileKey(jsFileKey)){loadingJSFileListHash[jsFileKey].isLoadFinished=bool;loadingFinishedCnt++;if(loadingFinishedCnt==loadingTotalCnt){writeLog("setIsLoadFinished()","check force");checkLoadFinished();}}else{if(DEBUG){writeLog("setIsLoadFinished()","not found  jsFileKey="+jsFileKey);}}}
function addLoadingJSFileList(jsFilePath,jsFileKey){if(!isExistJSFileKey(jsFileKey)){var objJS=new Object();objJS.key=jsFileKey;objJS.path=jsFilePath;objJS.isLoadFinished=false;loadingJSFileList.push(objJS);loadingJSFileListHash[loadingJSFileList[loadingJSFileList.length-1].key]=loadingJSFileList[loadingJSFileList.length-1];}else{writeLog("addLoadingJSFileList()","overwrite  jsFileKey="+jsFileKey+" jsFilePath="+jsFilePath);if(loadingJSFileListHash[jsFileKey].isLoadFinished){removeScriptTag(jsFileKey);}
loadingJSFileListHash[jsFileKey].path=jsFilePath;loadingJSFileListHash[jsFileKey].isLoadFinished=false;}}
function isExistJSFileKey(jsFileKey){if(loadingJSFileListHash[jsFileKey]==undefined){return false;}
return true;}
this.loadJSFileList=function(jsFileList,cbo){writeLog("loadJSFileList()","start..");reset();onFinishCallBackObj=cbo;for(var i=0;i<jsFileList.length;i++){addLoadingJSFileList(jsFileList[i].path,jsFileList[i].key);}
startLoad();}
function reset(){writeLog("reset()","start..");clearTimeout(loadingCheckTimer);loadingJSFileList=null;loadingJSFileList=new Array();loadingJSFileListHash=null;loadingJSFileListHash=new Object();beforeSameCnt=0;beforeUnCompleteCnt=0;loadingTotalCnt=0;loadingFinishedCnt=0;isLoadCheckCnt=0;}
function startLoad(){writeLog("startLoad()","start..");if(isLoading){alert("JSLoader:既にロード中です");return;}
isLoading=true;isLoadCheckCnt=0;beforeSameCnt=0;beforeUnCompleteCnt=loadingJSFileList.length;loadingFinishedCnt=0;loadingTotalCnt=loadingJSFileList.length;for(var i=0;i<loadingJSFileList.length;i++){addScriptTag(loadingJSFileList[i].path,loadingJSFileList[i].key);}
loadingCheckTimer=setTimeout(checkLoadByTimer,JSLOADER_ISLOADCHECKINTERVAL);}
function checkLoadByTimer(){if(!isLoading)return;checkLoadFinished();}
function checkLoadFinished(){writeLog("checkLoadFinished()","start..");clearTimeout(loadingCheckTimer);var unCompleteCnt=0;for(var i=0;i<loadingJSFileList.length;i++){if(!loadingJSFileList[i].isLoadFinished){unCompleteCnt++;}}
if(unCompleteCnt==beforeUnCompleteCnt){beforeSameCnt++;}else{beforeSameCnt=0;}
beforeUnCompleteCnt=unCompleteCnt;if(unCompleteCnt==0){reset();isLoading=false;onFinishCallBackObj.onLoadJSFile(true);}else{isLoadCheckCnt++;var ISLOADCHECK_MAXCNT=JSLOADER_ISLOADCHECKTIME/(JSLOADER_ISLOADCHECKINTERVAL/1000);var PROGRESSWATCH_MAXCNT=JSLOADER_PROGRESSWATCHTIME/(JSLOADER_ISLOADCHECKINTERVAL/1000);if(isLoadCheckCnt>ISLOADCHECK_MAXCNT){writeLog("checkLoadFinished()","faliure timeout isLoadCheckTime");isLoading=false;onFinishCallBackObj.onLoadJSFile(false,getFailureFileListText());return;}
if(beforeSameCnt>PROGRESSWATCH_MAXCNT){writeLog("checkLoadFinished()","faliure timeout progressWatchTime");isLoading=false;onFinishCallBackObj.onLoadJSFile(false,getFailureFileListText());return;}
writeLog("checkLoadFinished()","waiting..  isLoadCheckCount:"+isLoadCheckCnt+"/"+ISLOADCHECK_MAXCNT+"   progressWatchCount:"+beforeSameCnt+"/"+PROGRESSWATCH_MAXCNT);loadingCheckTimer=setTimeout(checkLoadFinished,JSLOADER_ISLOADCHECKINTERVAL);}}
function getFailureFileListText(){var ret="";for(var i=0;i<loadingJSFileList.length;i++){if(!loadingJSFileList[i].isLoadFinished){ret+=loadingJSFileList[i].key+":"+loadingJSFileList[i].path+"   ";}}
return ret;}
var tm=new Date().getTime();var elmHead=document.getElementsByTagName('head')[0];function addScriptTag(jsFilePath,jsFileKey){if(DEBUG&&DEVELOP){}
var elm=document.createElement('script');var elmSrc=jsFilePath;if(JSLOADER_ISAPPENDTIME){elmSrc+="&time="+tm;}
if(JSLOADER_FILEVER){if(JSLOADER_ISAPPENDTIME){elmSrc+="&";}else{elmSrc+="?";}
elmSrc+="ver="+JSLOADER_FILEVER;}
elm.setAttribute('src',elmSrc);elm.setAttribute('type','text/javascript');elm.setAttribute('charset','utf-8');elm.setAttribute('id',SCRIPTTAG_ID_PREFIX+jsFileKey);elmHead.appendChild(elm);}
function removeScriptTag(jsFileKey){writeLog("removeScriptTag()","jsFileKey="+jsFileKey);return true;}
function writeLog(kind,txt){if(log){log.write("JSLoader#"+kind,txt);}}}
function jsLoadingFinished(jsFilePath,jsFileKey){if(jsLoader){jsLoader.setIsLoadFinished(jsFileKey,true);}else{alert("jsLoaderがありません");}}
