var lastPoistion = 0;
var msgIntval = null;
var parentDiv = null;
var notiDiv = null;
var moveD = 1;
var msgLength = 0;
var msgCnt = 0;
var isNextMsg = false;
var paerntTmp = "";
var msgBoxTmp = "";
var arrMsg = new Array();
var speedTmp = 0;
var moveTmp = 0;
/*var msgOrder = 0 ; //메세지 우선순위
var ipnutOrder = 0;*/
var currentLeft = 0;
var currentTop = 0;
var rollStopCount = 20;
var splitMsgLength = 0;
var splitMsgLengthCount = 0;
var splitMsgTmp;
var isTrainInfoRunning = true;
var msgColor;
var msgFontSize;
var msgDir;
var symbolTimer = null;
var msgIntervalTime = 3000;
var isOcc = false;

function getIntervalTime(){
	var msgPath = "sdcard/metro/main/msgTime.conf";
	if(AndroidFunction.FileExist(msgPath)){
		var val = AndroidFunction.Cat(msgPath);
		if("0" == val || "" == val || "null" == val){
			return 3000;
		}else{
			return val;
		}
	}else{
		return 3000;
	}
}

function setMsgConf(){
	
	var sizePath = "/sdcard/metro/main/fontSize.conf";
	var colorPath = "/sdcard/metro/main/fontColor.conf";
	var effectPath = "/sdcard/metro/main/fontEffect.conf";
	
	if(this.isOcc) {
		if(typeof(arrMsg[0].get("disp")) != "undefined") msgIntervalTime = arrMsg[0].get("disp") * 1000;
		msgColor = arrMsg[0].get("color");
	}else{
		
		if(AndroidFunction.FileExist(colorPath)){
			var val = AndroidFunction.Cat(colorPath);
			if("0" == val || "" == val || "null" == val){
				msgColor = "#ffffff";
			}else{
				msgColor = val;
			}
		}else{
			msgColor = "#ffffff";
		}
	}
	
	if(AndroidFunction.FileExist(effectPath)){
		var val = AndroidFunction.Cat(effectPath);
		if("0" == val || "" == val || "null" == val){
			msgDir = "4";
		}else{
			if(4 > parseInt(msgDir)) msgDir = "4";
			else msgDir = val;
		}
	}else{
		msgDir = "4";
	}
	
	if(AndroidFunction.FileExist(sizePath)){
		var val = AndroidFunction.Cat(sizePath);
		if("0" == val || "" == val || "null" == val){
			msgFontSize = "38px";
		}else{
			msgFontSize = val+"px";
		}
	}else{
		msgFontSize = "38px";
	}
}

function msgControllMq(target , msg , speed , direction){
	AndroidFunction.DebugOut(33,"message controller start!!!");
	targetObj = document.getElementById(target);
	var tmpMarque = '<marquee behavior="scroll" hspace="10px" direction = "'+direction+'" scrollamount='+speed+'><font  color="#ffffff" size="50">'+msg+'</font></marquee>' ;
	targetObj.innerHTML = tmpMarque;
}

function setArrMsg(parent , msgbox , arrMsg , speed , moveD , upDelay , isTrain , _isOcc){
	if(null != msgIntval) clearMsgBox(0);
	
	/*if(isMsgPlay){
		if(msgOrder >=  ipnutOrder){
			return;
		}else{
			msgOrder = ipnutOrder;
		}
	}else{
		msgOrder = ipnutOrder;
		isMsgPlay = true;
	}*/
	this.paerntTmp =parent;
	this.msgBoxTmp = msgbox;
	this.arrMsg = arrMsg;
	this.speedTmp = speed;
	this.moveTmp = moveD;
	this.rollStopCount = upDelay;
	this.isTrainInfoRunning = isTrain;
	this.isOcc = _isOcc
	this.msgCnt = 0;
	msgIntervalTime = getIntervalTime();
	AndroidFunction.DebugOut(33," msg.js msgIntervalTime : " + msgIntervalTime);
	setMsgConf();
	msgControllMain();
}


function stopMsg(){
	if( symbolTimer != null ){
		clearTimeout(symbolTimer);
		symbolTimer = null;
	}
	clearMsgBox(0);
	msgCnt = 0;
	msgOrder = 0;
	isMsgPlay = false;
	//hideBottom();
	//document.location.reload();
}


function msgControllMain(){
	/*if(msgOrder >  ipnutOrder){
		return;
	} else {
		msgOrder = ipnutOrder;
	}*/
	if(msgCnt < arrMsg.length){
		//var msgTxt = arrMsg[msgCnt].get("txt");
		//var splitMsg = arrMsg[msgCnt].get("txt").split("##");
		//var splitMsg = arrMsg[msgCnt].get("txt").split("#");
		var splitMsg = arrMsg[msgCnt].get("txt");
		//var msgDir = arrMsg[msgCnt].get("dir");
		msgLength = arrMsg[msgCnt].get("txt").length;
		
		
		//AutoPA msg중  1,2,3번만 예외처리(m1 시작과 동시에 동기화 되어야함)
		if(!isOcc){
			if(typeof(arrMsg[msgCnt].get("disp")) != "undefined") msgIntervalTime = arrMsg[msgCnt].get("disp");
		}
		
		if(this.isOcc){
			if(typeof(arrMsg[msgCnt].get("dir")) != "undefined") msgDir = arrMsg[msgCnt].get("dir");
		}
		//msgFontSize = arrMsg[msgCnt].get("size");
		//msgColor = arrMsg[msgCnt].get("color");
		
		splitMsgLength = splitMsg.length;
		var msgTxt = "";
		if("1" == msgDir){
			for(var i = 0; i < splitMsg.length ; i++){
				msgTxt += splitMsg[i];
			}
			msgControllJsL(paerntTmp , msgBoxTmp , msgTxt, speedTmp , moveTmp);
			msgCnt++;
		}else if("2" == msgDir){
			for(var i = 0; i < splitMsg.length ; i++){
				 msgTxt += splitMsg[i];
			}
			msgControllJsR(paerntTmp , msgBoxTmp , msgTxt, speedTmp , moveTmp);
			msgCnt++;
		}else if("3" == msgDir){
			msgControllJsU(paerntTmp , msgBoxTmp , splitMsg, speedTmp , moveTmp);
			msgCnt++;
		}else if("4" == msgDir){
			msgControllJsInner(paerntTmp , msgBoxTmp , splitMsg, speedTmp , moveTmp);
			msgCnt++;
		}else{
			msgControllJsInner(paerntTmp , msgBoxTmp , splitMsg, speedTmp , moveTmp);
			msgCnt++;
		}
	}else {
		msgCnt = 0;
		msgOrder = 0;
		isMsgPlay = false;
		
		/*var cmd = null;
		cmd = AndroidFunction.JSONs(null, "callback.to", "window1");
		cmd = AndroidFunction.JSONs(cmd, "callback.request", "false");
		cmd = AndroidFunction.JSONs(cmd, "callback.value", "setMsgPlayStatus(false)");
		AndroidFunction.JsonRun(cmd);
		cmd = null;*/
		
		hideBottom();

		//console.log("so.MessageLOOP COMPLETE");
		so.doMessageComplete();
		
		//AndroidFunction.DebugOut(33, "bottom pageReload");
		//document.location.reload();
			
		/*if(!isTrainInfoRunning){
			var cmd = null;
 			cmd = AndroidFunction.JSONs(null, "callback.to", "window1");
			cmd = AndroidFunction.JSONs(cmd, "callback.request", "false");
 			cmd = AndroidFunction.JSONs(cmd, "callback.value", "pageReload()");
 			AndroidFunction.JsonRun(cmd);
			cmd = null;
		}else{
			var cmd = null;
			cmd = AndroidFunction.JSONs(null, "callback.to", "window1");
			cmd = AndroidFunction.JSONs(cmd, "callback.request", "false");
 			cmd = AndroidFunction.JSONs(cmd, "callback.value", "setMsgPlayStatus(false)");
 			AndroidFunction.JsonRun(cmd);
 			AndroidFunction.DebugOut(33, "bottom pageReload");
 			document.location.reload();
			cmd = null;
		}*/
	}
}

function clearMsgBox(cnt){
	rollStopCount = 20;
	splitMsgLength = 0;
	splitMsgLengthCount = 0;
	clearInterval(msgIntval);
	msgIntval = null;
	try {
		if(notiDiv != null && notiDiv != ""){
			notiDiv.innerHTML = "";
			parentDiv.removeChild(notiDiv);
			parentDiv.innerHTML = "";
		}else{
			parentDiv.innerHTML = "";
		}
	} catch (e) {
		// TODO: handle exception
		AndroidFunction.DebugOut(31,"## Function clearMsgBox : " + e);
		//테스트 코드 catch 문이 동작하면 bottom을 다시 열어주어서 자막을 재활성화 한다.
		hideBottom();
	}
	msgCnt = cnt;
}

function msgControllJsL(parent , msgbox , msg , speed , moveD){
	if(null != msgIntval){
		clearMsgBox(msgCnt);
	}
	
	this.moveD = moveD;
	parentDiv = document.getElementById(parent);
	parentDiv.style.backgroundColor = '#000000';
	/*if(!isTrainInfoRunning){
		parentDiv.style.backgroundColor = '#000000';
	}*/
	notiDiv = document.createElement("div");
	notiDiv.id = msgbox;
	notiDiv.style.whiteSpace = "nowrap";
	notiDiv.style.position = "relative";
	notiDiv.style.width = "auto";
	notiDiv.style.height = "auto";
	notiDiv.style.fontSize=msgFontSize;
	notiDiv.style.color=msgColor;
	notiDiv.innerHTML = msg;
	parentDiv.appendChild(notiDiv);
	//notiDiv.style.display = "inline-block";
	//notiDiv.style.backgroundColor = 'red';
	//currentLeft = parentDiv.offsetWidth + parentDiv.offsetLeft;
	currentLeft = parentDiv.offsetWidth;
	//lastPoistion = parentDiv.offsetLeft -  (msgLength*22);
	lastPoistion = 0 - notiDiv.offsetWidth;
	notiDiv.style.left = currentLeft + "px";
	msgIntval = setInterval("doMoveL()",speed);
	AndroidFunction.DebugOut(33,"msgControllJsL : !!!set caption!!!");
}

function doMoveL(){
	currentLeft = currentLeft - moveD;
	notiDiv.style.left = currentLeft + "px";
	if(notiDiv.offsetLeft <= lastPoistion ){
		clearMsgBox(msgCnt);
		 msgControllMain();
	}
}

function msgControllJsR(parent , msgbox , msg , speed , moveD){
	if(null != msgIntval){
		clearMsgBox(msgCnt);
	}
	
	this.moveD = moveD;
	parentDiv = document.getElementById(parent);
	parentDiv.style.backgroundColor = '#000000';
	/*if(!isTrainInfoRunning){
		parentDiv.style.backgroundColor = '#000000';
	}*/
	notiDiv = document.createElement("div");
	notiDiv.id = msgbox;
	notiDiv.style.whiteSpace = "nowrap";
	notiDiv.style.position = "relative";
	notiDiv.style.width = "auto";
	notiDiv.style.height = "auto";
	notiDiv.style.fontSize=msgFontSize;
	notiDiv.style.color=msgColor;
	notiDiv.innerHTML = msg;
	parentDiv.appendChild(notiDiv); 
	//notiDiv.style.display = "inline-block";
	//notiDiv.style.backgroundColor = 'green';
	//currentLeft = parentDiv.offsetLeft-(msgLength*22);
	currentLeft = 0 - notiDiv.offsetWidth ;
	//lastPoistion = parentDiv.offsetWidth +  notiDiv.offsetWidth ;
	lastPoistion = parentDiv.offsetWidth;
	notiDiv.style.left = currentLeft + "px";
	msgIntval = setInterval("doMoveR()",speed);
	AndroidFunction.DebugOut(33,"msgControllJsR : !!!set caption!!!");
}

function doMoveR(){
	currentLeft = currentLeft + moveD;
	notiDiv.style.left = currentLeft + "px";
	if(notiDiv.offsetLeft >= lastPoistion ){
		clearMsgBox(msgCnt);
		msgControllMain();
	}
}


function msgControllJsU(parent , msgbox , msg , speed , moveD){
	moveD = 5;
	if(null != msgIntval){
		clearMsgBox(msgCnt);
	}
	
	this.moveD = moveD;
	parentDiv = document.getElementById(parent);
	parentDiv.style.backgroundColor = '#000000';
	/*if(!isTrainInfoRunning){
		parentDiv.style.backgroundColor = '#000000';
	}*/
	notiDiv = document.createElement("div");
	notiDiv.id = msgbox;
	//notiDiv.style.whiteSpace = "nowrap";
	notiDiv.style.position = "relative";
	notiDiv.style.width = "auto";
	notiDiv.style.height = "115px";
	notiDiv.style.fontSize=msgFontSize;
	notiDiv.style.color=msgColor;
	//notiDiv.style.backgroundColor = "green";
	//notiDiv.style.lineHeight="0px";
	splitMsgTmp = msg;
	notiDiv.innerHTML = msg[splitMsgLengthCount];
	splitMsgLengthCount++;
	parentDiv.appendChild(notiDiv); 
	currentTop = (parentDiv.offsetHeight-0+15) ;
	lastPoistion = 0 - notiDiv.offsetHeight-20;
	notiDiv.style.top = currentTop + "px";
	msgIntval = setInterval("doMoveU()",50);
	AndroidFunction.DebugOut(33,"msgControllJsU : !!!set caption!!!");
}

function doMoveU(){
	currentTop = currentTop - moveD;
	if(currentTop < 0 && rollStopCount > 0){
		rollStopCount--;
		currentTop = 0;
		return;
	}
	
	notiDiv.style.top = currentTop + "px";
	
	if(notiDiv.offsetTop <= lastPoistion ){
		if((splitMsgLength-1) >= splitMsgLengthCount){
			currentTop = (parentDiv.offsetHeight-0+15) ;
			notiDiv.innerHTML = splitMsgTmp[splitMsgLengthCount];
			rollStopCount = 20;
			splitMsgLengthCount++;
		}else{
			clearMsgBox(msgCnt);
			msgControllMain();
		}
	}
}


function msgControllJsInner(parent , msgbox , msg , speed , moveD){
	if(null != msgIntval){
		clearMsgBox(msgCnt);
	}
	//this.moveD = moveD;
	parentDiv = document.getElementById(parent);
	parentDiv.style.backgroundColor = '#000000';
	/*if(!isTrainInfoRunning){
		parentDiv.style.backgroundColor = '#000000';
	}*/
	notiDiv = document.createElement("div");
	notiDiv.id = msgbox;
	//notiDiv.style.whiteSpace = "nowrap";
	//notiDiv.style.position = "relative";
	//notiDiv.style.left = "0px";
	//notiDiv.style.top = "0px";
	notiDiv.style.width = screen.availWidth+"px";
	notiDiv.style.height = "120px";
	notiDiv.style.fontSize=msgFontSize;
	notiDiv.style.color=msgColor;
	notiDiv.style.margin="0px";
	notiDiv.style.padding="0px";
	//notiDiv.style.backgroundColor = "red";
	notiDiv.style.textAlign = "center";
	parentDiv.appendChild(notiDiv);
	splitMsgTmp = msg;
	AndroidFunction.DebugOut(33,"msgControllJsInner : !!!set caption!!!");
	if( symbolTimer != null ){
		clearTimeout(symbolTimer);
		symbolTimer = null;
	}
	//symbolTimer = setTimeout("doInnerHtml()", 10);
	doInnerHtml();
}
function doInnerHtml(){
	//console.log("sigpankyo splitMsgLengthCount:("+splitMsgLengthCount+")"+" < "+" splitMsgLength: ("+splitMsgLength+")" );
	
	if(splitMsgLengthCount < splitMsgLength){
		if(0 < splitMsgTmp[splitMsgLengthCount].length ) notiDiv.innerHTML = splitMsgTmp[splitMsgLengthCount];
		setTimeout(function(){
			parentDiv.style.top = "1px";
		},10);
		setTimeout(function(){
			parentDiv.style.top = "0px";
		},10);
		
		splitMsgLengthCount++;
		if( symbolTimer != null ){
			clearTimeout(symbolTimer);
			symbolTimer = null;
		}
		symbolTimer = setTimeout("doInnerHtml()", msgIntervalTime);
		//msgIntval = setInterval("doInnerHtml()", 2500);
	}else{
		if( symbolTimer != null ){
			clearTimeout(symbolTimer);
			symbolTimer = null;
		}
		
		clearMsgBox(msgCnt);
		msgControllMain();
		
		/*
			 메세지 반복될때마다 이부분이 호출된다
		*/
	}
}

