
/**
 * @author Samsung
 */
/*add*/
var DateType = {
	DAY : 1,
	MONTH : 2,
	YEAR : 3,
	HOUR : 4,
	MINUTE : 5,
	SEC : 6
};
var minYear=1900;
var maxYear=2000;
function DatePickerBox(ButtonId, InputTextId, caller_obj) {
	this.objType = "DatePickerBox";

	this.ButtonObj = null;
	this.InputBoxObj = null;

	this.m_BufferText = "";
	this.m_posOfCaret = 0;
	this.m_NewText = "";

	this.onLeftFunc = null;
	this.onRightFunc = null;
	this.onDownFunc = null;
	this.onUpFunc = null;
	this.onReturnFunc = null;
	this.onExitFunc = null;
	this.onInfolinkFunc = null;

	this.flagFocus = false;

	this.anyKeyFunc = null;
	this.enterFunc = null;

	this.keyFuncArr = new Array();

	this.initString = "";
	this.currString = "";

	this.ButtonObjId = "";
	this.InputBoxObjId = "";

	this.maxlength = 4;

	/*add*/
	this.flagDateType = false;
	this.DateType = null;

	this.onClickCallbackFunc = null;
	this.inputBoxClicked = false;
	this.inputBoxMouseOver = false;

}

DatePickerBox.prototype.SetButtonObjId = function(id, OptionDivide) {

	if(id) {
		this.ButtonObj = new Button();
		this.ButtonObj.SetImageObjId(id, OptionDivide);
		this.ButtonObjId = id;
	} else {
		this.ButtonObj = null;
	}
}

DatePickerBox.prototype.SetButtonTextObjId = function(id) {

	if(id) {
		this.ButtonObj.SetTextObjId(id);
	}
}

DatePickerBox.prototype.SetBgImage = function(imgIndex, bgImage) {
	this.ButtonObj.SetBgImage(imgIndex, bgImage);
}

DatePickerBox.prototype.SetInputBoxObjId = function(id) {
	this.InputBoxObjId = id;
}
DatePickerBox.prototype.registHelpBar = function(obj) {
	this.ButtonObj.registHelpBar(obj);
}

DatePickerBox.prototype.changeHelpBar = function(obj) {
	this.ButtonObj.changeHelpBar(obj);
}

DatePickerBox.prototype.SetFlagDim = function(b_dim) {
	this.ButtonObj.SetFlagDim(b_dim);
}

DatePickerBox.prototype.SetTextStyle = function(styleIndex, textStyle) {
	this.ButtonObj.SetTextStyle(styleIndex, textStyle);
}

DatePickerBox.prototype.GetBtnText = function() {
	return this.ButtonObj.GetText();
}

DatePickerBox.prototype.SetBtnText = function(text_string) {
	this.ButtonObj.SetText(text_string);
}
DatePickerBox.prototype.setMaxLength = function(max_length) {
	alert(" DatePickerBox.prototype.setMaxLength()");
	this.maxlength = max_length;

}
/*add*/
DatePickerBox.prototype.setDateType = function(type) {
	alert(" DatePickerBox.prototype.setDateType()");
	this.DateType = type;
}

DatePickerBox.prototype.GetPosOfCaret = function() {
	return this.m_posOfCaret;
}

DatePickerBox.prototype.keydown = function() {
	var keycode = event.keyCode;
	var keypadtype = event.keypadType;
	alert("DatePickerBox.keydown() -> key code :" + keycode);
	PluginAPIMgr.PlayAudio(keycode);

	switch(keycode) {
		case sf.key.N1:
		case sf.key.N2:
		case sf.key.N3:
		case sf.key.N4:
		case sf.key.N5:
		case sf.key.N6:
		case sf.key.N7:
		case sf.key.N8:
		case sf.key.N9:
		case sf.key.N0:
			PluginAPIMgr.PlayAudioByIndex(ENUM_AUDIO_TYPE.AUDIO_TYPE_KEYPAD_ENTER);
			this.processNumberKey(keycode);
			event.preventDefault();
			this.InputBoxObj.scrollLeft=99999;
			break;
		case 35:
			//plus100
			if(ProcMgr.isTTXMIX == true) {
				event.preventDefault();
				return;
			}
			PluginAPIMgr.PlayAudioByIndex(ENUM_AUDIO_TYPE.AUDIO_TYPE_KEYPAD_ENTER);

			break;
			
		case  650 :
			// TTXMIX
			if(ProcMgr.isTTXMIX == false) {
				sf.key.preventDefault();
				return;
			}

			PluginAPIMgr.PlayAudioByIndex(ENUM_AUDIO_TYPE.AUDIO_TYPE_KEYPAD_ENTER);
			break;
		case sf.key.PLAY:
			if(event instanceof KeyboardEvent){	//if press remote controller
				event.preventDefault();
				break;	
			}
		case  259 :
			//sf.key.PRECH:
			PluginAPIMgr.PlayAudioByIndex(ENUM_AUDIO_TYPE.AUDIO_TYPE_KEYPAD_DEL);
			this.processPreChKey();
			this.InputBoxObj.scrollLeft = 99999;
			break;
		case 72: //>>
			if(event instanceof KeyboardEvent){	//if press remote controller
				event.preventDefault();
			}
			else{
				this.startKeyPad();
				event.preventDefault();
			}
			break;	
		case sf.key.DOWN:
			this.processDownKey();
			break;

		case sf.key.UP:
			this.processUpKey();
			break;

		case sf.key.LEFT:
			this.processLeftKey();
			break;

		case sf.key.RIGHT:
			this.processRightKey();
			break;

		case sf.key.ENTER:
			if(keypadtype==1){
				if(PluginAPIMgr.FlagSmartControlNumber() ==1)
					this.processEnterKey();
				else
				this.startKeyPad();
			}else{
				this.processEnterKey();
			}
			break;

		case sf.key.RETURN:
			this.processReturnKey();
			break;

		case sf.key.EXIT:
			this.processExitKey();
			break;

		default:
			sf.key.preventDefault();
			break
	}
	sf.key.preventDefault();
}

DatePickerBox.prototype.processNumberKey = function(keycode) {
	var m_NewText = "";

	switch(keycode) {
		case sf.key.N1:
			m_NewText = "1";
			break;
		case sf.key.N2:
			m_NewText = "2";
			break;
		case sf.key.N3:
			m_NewText = "3";
			break;
		case sf.key.N4:
			m_NewText = "4";
			break;
		case sf.key.N5:
			m_NewText = "5";
			break;
		case sf.key.N6:
			m_NewText = "6";
			break;
		case sf.key.N7:
			m_NewText = "7";
			break;
		case sf.key.N8:
			m_NewText = "8";
			break;
		case sf.key.N9:
			m_NewText = "9";
			break;
		case sf.key.N0:
			m_NewText = "0";
			break;
	}
	alert("sf.key = " + m_NewText);
	this.addText(m_NewText);
	if(this.anyKeyFunc) {
		this.anyKeyFunc(keycode);
	}
}
DatePickerBox.prototype.processPreChKey = function() {
	alert(" DatePickerBox.prototype.processPreChKey ");

	this.deleteText();

	if(this.anyKeyFunc) {
		this.anyKeyFunc(259);
	}
}

DatePickerBox.prototype.processLeftKey = function() {
	alert(" DatePickerBox.prototype.processLeftKey ");
	if(this.m_posOfCaret == 0) {
		if(this.onLeftFunc) {
			this.onLeftFunc();
			return;
		}
	}
	this.moveLeft();
}

DatePickerBox.prototype.moveLeft = function() {
	alert(" DatePickerBox.prototype.moveLeft ");

	if(this.m_posOfCaret > 0) {
		this.m_posOfCaret--;
		this.InputBoxObj.cursorPosition = this.m_posOfCaret;
		//for webkit (webkit don't have cursorPosition property)
		this.InputBoxObj.selectionStart = this.InputBoxObj.cursorPosition;
		this.InputBoxObj.selectionEnd = this.InputBoxObj.cursorPosition;

		alert(" [processLeftKey] InputBoxObj.value : " + this.InputBoxObj.value);
		alert(" [processLeftKey] InputBoxObj.cursorPosition : " + this.InputBoxObj.cursorPosition);

		if(this.anyKeyFunc) {
			this.anyKeyFunc(sf.key.LEFT);
		}
	}
}

DatePickerBox.prototype.processRightKey = function() {
	alert(" DatePickerBox.prototype.processRightKey ");
	if(this.m_posOfCaret == this.InputBoxObj.value.length) {
		if(this.onRightFunc) {
			this.onRightFunc();
			return;
		}
	}
	this.moveRight();
}

DatePickerBox.prototype.moveRight = function() {
	alert(" DatePickerBox.prototype.moveRight ");

	if(this.m_posOfCaret < this.InputBoxObj.value.length) {
		this.m_posOfCaret++;
		this.InputBoxObj.cursorPosition = this.m_posOfCaret;
		//for webkit (webkit don't have cursorPosition property)
		this.InputBoxObj.selectionStart = this.InputBoxObj.cursorPosition;
		this.InputBoxObj.selectionEnd = this.InputBoxObj.cursorPosition;

		alert(" [processRightKey] InputBoxObj.value : " + this.InputBoxObj.value);
		alert(" [processRightKey] InputBoxObj.cursorPosition : " + this.InputBoxObj.cursorPosition);

		if(this.anyKeyFunc) {
			this.anyKeyFunc(sf.key.RIGHT);
		}
	}

}
DatePickerBox.prototype.moveDown = function() {
	alert(" DatePickerBox.prototype.moveDown ");
	var before_data = Number(this.InputBoxObj.value);
	alert("before_data = " + before_data);
	switch(this.DateType) {
		case DateType.DAY:
			if(before_data <= 1 || before_data > 31) {
				this.m_BufferText = "31";
			} else {
				before_data--;
				this.m_BufferText = String(before_data);
			}
			this.m_posOfCaret = this.m_BufferText.length;
			alert(" Current Buffer : " + this.m_BufferText);
			alert(" Current Catet : " + this.m_posOfCaret);

			break;
		case DateType.MONTH:
			if(before_data <= 1 || before_data > 12) {
				this.m_BufferText = "12";
			} else {
				before_data--;
				this.m_BufferText = String(before_data);
			}
			this.m_posOfCaret = this.m_BufferText.length;
			alert(" Current Buffer : " + this.m_BufferText);
			alert(" Current Catet : " + this.m_posOfCaret);
			break;
		case DateType.YEAR:
			if(before_data <= minYear || before_data > maxYear) {
				this.m_BufferText = String(maxYear);
			} else {
				before_data--;
				this.m_BufferText = String(before_data);
			}
			this.m_posOfCaret = this.m_BufferText.length;
			alert(" Current Buffer : " + this.m_BufferText);
			alert(" Current Catet : " + this.m_posOfCaret);
			break;
	}

	this.updateInputBox();

	if(this.anyKeyFunc) {
		this.anyKeyFunc(sf.key.RIGHT);
	}

}

DatePickerBox.prototype.processDownKey = function() {
	alert(" DatePickerBox.prototype.processDownKey ");
	this.moveDown();
	if(this.onDownFunc) {

		this.onDownFunc();
		return;
	}

}
DatePickerBox.prototype.moveUp = function() {
	alert(" DatePickerBox.prototype.moveUp ");
	var before_data = Number(this.InputBoxObj.value);
	alert("before_data = " + before_data);
	switch(this.DateType) {
		case DateType.DAY:
			if(before_data < 1 || before_data >= 31) {
				this.m_BufferText = "1";
			} else {
				before_data++;
				this.m_BufferText = String(before_data);
			}
			this.m_posOfCaret = this.m_BufferText.length;
			alert(" Current Buffer : " + this.m_BufferText);
			alert(" Current Catet : " + this.m_posOfCaret);

			break;
		case DateType.MONTH:
			if(before_data < 1 || before_data >= 12) {
				this.m_BufferText = "1";
			} else {
				before_data++;
				this.m_BufferText = String(before_data);
			}
			this.m_posOfCaret = this.m_BufferText.length;

			alert(" Current Buffer : " + this.m_BufferText);
			alert(" Current Catet : " + this.m_posOfCaret);
			break;
		case DateType.YEAR:
			if(before_data < minYear || before_data >= maxYear) {
				this.m_BufferText = String(maxYear);
			} else {
				before_data++;
				this.m_BufferText = String(before_data);
			}
			this.m_posOfCaret = this.m_BufferText.length;

			alert(" Current Buffer : " + this.m_BufferText);
			alert(" Current Catet : " + this.m_posOfCaret);
			break;
	}

	this.updateInputBox();

	if(this.anyKeyFunc) {
		this.anyKeyFunc(sf.key.RIGHT);
	}

}
DatePickerBox.prototype.processUpKey = function() {
	alert(" DatePickerBox.prototype.processUpKey ");
	this.moveUp();
	if(this.onUpFunc) {
		this.onUpFunc();
		return;
	}
}

DatePickerBox.prototype.processReturnKey = function() {
	alert(" DatePickerBox.prototype.processReturnKey ");
	if(this.onReturnFunc) {
		this.onReturnFunc();
		return;
	}
}

DatePickerBox.prototype.processEnterKey = function() {
	alert(" DatePickerBox.prototype.processEnterKey ");
	if(this.enterFunc) {
		this.enterFunc();
		return;
	}
}

DatePickerBox.prototype.processExitKey = function() {
	alert(" DatePickerBox.prototype.processEixtKey ");
	if(this.onExitFunc) {
		this.onExitFunc();
		return;
	}
}

DatePickerBox.prototype.isMaxLength = function() {
	if(this.InputBoxObj.value.length == this.InputBoxObj.maxLength) {
		return true;
	} else {
		return false;
	}
}

DatePickerBox.prototype.addText = function(m_NewText) {
	alert(" DatePickerBox.prototype.addText()");
	if(m_NewText == "") {
		return;
	}
	var befor = "";
	var after = "";
	if(this.InputBoxObj.selectionStart != this.InputBoxObj.selectionEnd) {
		alert("block area delete")
		this.deleteText();
	}
	if(this.isMaxLength()) {
		alert(" isMaxLength() == true");
		
		this.m_BufferText="";
		
		 this.m_posOfCaret=this.m_BufferText.length;
		
		//return;
	}
	befor = this.m_BufferText.substr(0, this.m_posOfCaret);
	after = this.m_BufferText.substr(this.m_posOfCaret, this.m_BufferText.length);

	this.m_BufferText = befor + m_NewText + after;

	alert(" Current Buffer : " + this.m_BufferText);

	this.m_posOfCaret++;
	alert(" Current Catet : " + this.m_posOfCaret);

	this.updateInputBox();
}

DatePickerBox.prototype.deleteText = function() {
	alert(" DatePickerBox.prototype.deleteText()");

	if(this.flagFocus == false) {
		this.m_posOfCaret = this.m_BufferText.length;
	}
	if(this.m_posOfCaret<=0){
		return ;
	}

	var before = "";
	var after = "";

	if(this.m_BufferText.length > 0) {
		if(this.InputBoxObj.selectionStart ==this.InputBoxObj.selectionEnd){
			this.m_posOfCaret--;
			before = this.m_BufferText.substr(0, this.m_posOfCaret);
			after = this.m_BufferText.substr(this.m_posOfCaret + 1, this.InputBoxObj.value.length);
		}else{
			before = this.m_BufferText.substr(0, this.InputBoxObj.selectionStart);
			after = this.m_BufferText.substr(this.InputBoxObj.selectionEnd, this.InputBoxObj.value.length);
			this.m_posOfCaret =this.InputBoxObj.selectionStart;
		}
		this.m_BufferText = before + after;
		//this.m_posOfCaret = this.m_BufferText.length;
		alert(" Current Buffer : " + this.m_BufferText);
		alert(" Current Catet : " + this.m_posOfCaret);

		this.updateInputBox();
	}
}

DatePickerBox.prototype.deleteAllText = function() {
	alert(" DatePickerBox.prototype.deleteAllText()");

	if(this.m_posOfCaret >= 0) {
		this.m_BufferText = "";
		this.m_posOfCaret = this.m_BufferText.length;

		alert(" Current Buffer : " + this.m_BufferText);
		alert(" Current Catet : " + this.m_posOfCaret);

		this.updateInputBox();
	}
}

DatePickerBox.prototype.updateInputBox = function() {
	this.InputBoxObj.value = this.m_BufferText;
	if(this.flagFocus) {
		this.InputBoxObj.cursorPosition = this.m_posOfCaret;
		//for webkit (webkit don't have cursorPosition property)
		this.InputBoxObj.selectionStart = this.InputBoxObj.cursorPosition;
		this.InputBoxObj.selectionEnd = this.InputBoxObj.cursorPosition;
		//
	}

	alert(" [updateInputBox] InputBoxObj.value : " + this.InputBoxObj.value);
	alert(" [updateInputBox] InputBoxObj.cursorPosition : " + this.InputBoxObj.cursorPosition);

	this.ButtonObj.SetText(this.InputBoxObj.value);
	//for sync
}

DatePickerBox.prototype.updateCursorPosition = function() {

	if(this.flagFocus) {
		alert(" [updateCursorPosition] InputBoxObj.selectionStart : " + this.InputBoxObj.selectionStart);
		alert(" [updateCursorPosition] InputBoxObj.selectionEnd : " + this.InputBoxObj.selectionEnd);
		//this.m_posOfCaret = this.InputBoxObj.cursorPosition;

		this.m_posOfCaret = this.InputBoxObj.selectionStart;
		//this.InputBoxObj.selectionEnd ;
		this.InputBoxObj.cursorPosition = this.m_posOfCaret;

		if(this.anyKeyFunc) {
			this.anyKeyFunc(sf.key.RIGHT);
		}
	}

}

DatePickerBox.prototype.setString = function(str) {
	this.InputBoxObj.value = String(str);
	this.InputBoxObj.cursorPosition = String(str).length;

	this.m_BufferText = String(str);
	this.m_posOfCaret = String(str).length;
}

DatePickerBox.prototype.getString = function() {
	return this.InputBoxObj.value;
}

DatePickerBox.prototype.setInitString = function(initStr) {
	this.initString = initStr;
	this.ButtonObj.SetText(this.initString);
}

DatePickerBox.prototype.highlight = function() {
	if(this.flagFocus == true) {
		return;
	}

	ProcMgr.setNumericRemoteType();
	this.ButtonObj.highlight();

	this.flagFocus = true;

	$("<input  id='" + this.InputBoxObjId + "' type='text'/>").appendTo("#" + this.ButtonObjId);
	$("#" + this.InputBoxObjId).attr("maxLength", this.maxlength);

	this.InputBoxObj = document.getElementById(this.InputBoxObjId);

	var _THIS_ = this;
	this.InputBoxObj.focus();
	g_NumObj = this;
	g_AppCommon.OnMessage = BT_Input;
	g_AppCommon.SubscribeEvent(APPCOMMON_MESSAGE_QWERTY_KEY_INPUT);

	this.setString(this.ButtonObj.GetText());

	this.InputBoxObj.onkeydown = function() {
		_THIS_.keydown();
	};

	this.InputBoxObj.onblur = function() {
		if(_THIS_.flagFocus == true) {
			_THIS_.InputBoxObj.focus();
		}
	};
	if(this.onClickCallbackFunc != null) {
		this.InputBoxObj.onclick = function() {
			_THIS_.inputBoxClicked = true;
			_THIS_.updateCursorPosition();
			_THIS_.startKeyPad();
			_THIS_.onClickCallbackFunc(_THIS_);
		};
		$("#" + this.InputBoxObjId).css({
			'cursor' :  'auto'
		});
	}
	this.InputBoxObj.scrollLeft = 99999;
	alert("DatePickerBox : highlight :cursorPosition : " + this.InputBoxObj.cursorPosition);
}

DatePickerBox.prototype.blur = function() {
	alert("DatePickerBox : blur  ");
	ProcMgr.setDefaultRemoteType();
	this.ButtonObj.blur();
	
	var tmpdata = $("#" + this.InputBoxObjId).val();
	if(tmpdata) {
		this.ButtonObj.SetText(tmpdata);
	}
	g_NumObj = null;
	g_AppCommon.UnsubscribeEvent(APPCOMMON_MESSAGE_QWERTY_KEY_INPUT);

	this.InputBoxObj.onblur = null;
	this.flagFocus = false;

	$("#" + this.InputBoxObjId).blur();
	$("#" + this.InputBoxObjId).empty();
	$("#" + this.InputBoxObjId).remove();

	sf.scene.returnFocus();
	//for preventing default key event
	if(event)
	{
		event.stopPropagation();
	}
	
	// sf.key.preventDefault();
	//$.sf.returnFocus();

}

DatePickerBox.prototype.SetOnClickCallbackFunc = function(func) {
	if(WndMgr.MouseClickEnable == false) {
		return;
	}

	if(this.ButtonObj != null) {
		alert("DatePickerBox.SetOnClickCallbackFunc() set on image_layer");

		this.onClickCallbackFunc = func;
		var this_ = this;
		var btnClickCF = function(obj) {
			this_.processBtnClickEvent(obj);
		};
		this.ButtonObj.SetOnClickCallbackFunc(btnClickCF);
	}
}

DatePickerBox.prototype.processBtnClickEvent = function(obj) {
	alert("DatePickerBox.processBtnClickEvent() ");

	if(this.ButtonObj == obj) {
		var this_ = this;
		if(this_.inputBoxClicked == false) {
			this.onClickCallbackFunc(this_);
		} else {
			this_.inputBoxClicked = false;
		}
	}
}
DatePickerBox.prototype.SetOnMouseOverCallbackFunc = function(func) {
	if(WndMgr.MouseClickEnable == false) {
		return;
	}

	if(this.ButtonObj != null) {
		alert("DatePickerBox.SetOnMouseOverCallbackFunc() set on image_layer");

		this.onMouseOverCallbackFunc = func;
		var this_ = this;
		var btnOverCF = function(obj) {
			this_.processBtnMouseOverEvent(obj);
		};
		this.ButtonObj.SetOnMouseOverCallbackFunc(btnOverCF);
	}
}

DatePickerBox.prototype.processBtnMouseOverEvent = function(obj) {
	alert("DatePickerBox.processBtnMouseOverEvent() ");

	if(this.ButtonObj == obj) {
		
		var this_ = this;
		if(this_.inputBoxMouseOver == false) {
			this.onMouseOverCallbackFunc(this_);
		} else {
			this_.inputBoxMouseOver = false;
		}
	}
}
DatePickerBox.prototype.startKeyPad = function() {
	
	alert("DatePickerBox.startKeyPad() ");
	
	if(PluginAPIMgr.FlagSmartControlNumber() !=1)
	{
		PluginAPIMgr.ShowSmartControlNumber();
		WndMgr.stopPopupHideTimer();
	}
}

DatePickerBox.prototype.stopKeyPad = function() {
	
	alert("DatePickerBox.stopKeyPad() ");
	
	
	if(PluginAPIMgr.FlagSmartControlNumber() ==1)
	{
		PluginAPIMgr.HideSmartControlNumber();
	}
}

DatePickerBox.prototype.SetReverseDisable = function(flag) {
	this.ButtonObj.SetReverseDisable(flag);
}

DatePickerBox.prototype.setAnyKeyFunc = function(func) {
	this.anyKeyFunc = func;
	alert("InputBox : setAnyKeyFunc");
}

DatePickerBox.prototype.setKeyFunc = function(keyCode, func) {
	alert("InputBox : setKeyFunc keycode : " + keyCode);
	this.keyFuncArr[keyCode] = func;
}

DatePickerBox.prototype.setEnterFunc = function(func) {
	if(func) {
		this.enterFunc = func;
	}
}

DatePickerBox.prototype.setPrevEventOnLeftFunc = function(func) {
	if(func) {
		this.onLeftFunc = func;
	}
}

DatePickerBox.prototype.setPrevEventOnRightFunc = function(func) {
	if(func) {
		this.onRightFunc = func;
	}
}

DatePickerBox.prototype.setPrevEventOnDownFunc = function(func) {
	if(func) {
		this.onDownFunc = func;
	}
}

DatePickerBox.prototype.setPrevEventOnUpFunc = function(func) {
	if(func) {
		this.onUpFunc = func;
	}
}

DatePickerBox.prototype.setPrevEventOnReturnFunc = function(func) {
	if(func) {
		this.onReturnFunc = func;
	}
}

DatePickerBox.prototype.setPrevEventOnExitFunc = function(func) {
	if(func) {
		this.onExitFunc = func;
	}
}

DatePickerBox.prototype.setPrevEventOnInfolinkFunc = function(func) {
	if(func) {
		this.onInfolinkFunc = func;
	}
}