var LoadingTypeIndex = {
	SIGNIN_WITH_CANCEL : 1,
	SIGNIN : 2,
	SEARCH : 3,
	WAIT : 4,
	LOADING : 5,
}

function SceneLoading(options) {
	this.options = options;

	this.LoadingDim = null;

	this.SuccessPopup = null;
	this.CancelBtn = null;
	this.OKBtn = null;

	this.LoadingPopup = null;

	this.FlagAnimation = false;
	this.LoadingAniImage = new Array(18);
	this.iCurrAniImage = 0;

	this.LoadingTimer = null;

	this.LoadingType = 1;

	this.popupBGFlag = true;

}

SceneLoading.prototype.initialize = function() {
	alert("SceneLoading.initialize()");
	// this function will be called only once when the scene manager show this scene first time
	// initialize the scene controls and styles, and initialize your variables here
	// scene HTML and CSS will be loaded before this function is called

	this.create();
}

SceneLoading.prototype.handleShow = function() {
	alert("SceneLoading.handleShow()");
	// this function will be called when the scene manager show this scene

	this.LoadingDim.show();
	//this.create();
	this.update();

	this.startTimer();

}

SceneLoading.prototype.handleHide = function() {
	alert("SceneLoading.handleHide()");
	// this function will be called when the scene manager hide this scene
	this.LoadingDim.hide();
	if(this.CancelBtn.GetFlagVisible()){
		this.disableCancel();
	}
	if(this.LoadingTimer != null) {
		this.stopTimer();
	}
}

SceneLoading.prototype.handleFocus = function() {
	alert("SceneLoading.handleFocus()");
	// this function will be called when the scene manager focus this scene

}

SceneLoading.prototype.handleBlur = function() {
	alert("SceneLoading.handleBlur()");
	// this function will be called when the scene manager move focus to another scene from this scene
}

SceneLoading.prototype.handleKeyDown = function(keyCode) {
	alert("SceneLoading.handleKeyDown(" + keyCode + ")");
	// TODO : write an key event handler when this scene get focued
	PluginAPIMgr.PlayAudio(keyCode);
	if(ProcMgr.checkWidgetHighlight()==false){
		return;
	}
	switch (keyCode) {
		case sf.key.LEFT:
		case sf.key.RIGHT:
			break;
		case sf.key.UP:

			break;
		case sf.key.DOWN:

			break;

		case sf.key.ENTER:
			this.processEnterKey();
			break;

		case sf.key.RETURN:
			//2013 modify by minseok
			sf.key.preventDefault();
			//sf.key.block(event);
			break;

		case sf.key.EXIT:
			//2013 modify by minseok
			sf.key.preventDefault();
			//sf.key.block(event);
			break;

		case 147:
		//infolink
		case 261:
		//contents
		case 115:
			//wiselink
			//2013 modify by minseok
			sf.key.preventDefault();
			//sf.key.block(event);
			break;
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

SceneLoading.prototype.create = function() {
	alert("SceneLoading.create()");

	// this.LoadingView.SetBgImage(g_Image.IMAGE_LOADING_BG);
	this.LoadingPopup = new View("Loading_popup");

	this.LoadingDim = new DimBg("LoadingDim", "Loading_popup");

	var a = 0;
	for( a = 0; a < this.LoadingAniImage.length; a++) {
		var b = (a + 1);
		if(b < 10) {
			this.LoadingAniImage[a] = g_Image.base_loading64_image_path + "loading_64_" + "0" + b + ".png";
		} else {
			this.LoadingAniImage[a] = g_Image.base_loading64_image_path + "loading_64_" + b + ".png";
		}
	}

	this.LoadingAni= new Label("loading_animation", null);
	this.LoadingAni.SetBgImage(this.LoadingAniImage[this.iCurrAniImage]);

	// this.LoadingMsg = new Label(null,"loading_message");
	// this.LoadingMsg.SetTextStyle("loading_message_style");

	var CurLangID = ResMgr.GetCurLanguage();
	if(CurLangID == ENUM_TV_LANGUAGE.VIE) {
		$("#Loading_popup_popup_loading_text").css('font-size', "18px");
	}

	var this_ = sf.scene.get("Loading");
	var clickCF = function(obj) {
		this_.processClickEvent(obj);
	};
	//
	this.CancelBtn = new Button();
	this.OKBtn  = new Button();
	// this.CancelBtn.SetImageObjId("loading_btn", 20);
	// this.CancelBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_POPUP_BTN_NORMAL);
	// this.CancelBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_POPUP_BTN_NORMALFOCUS);
	// this.CancelBtn.SetBgImage(BtnImgIndex.BTN_DIM_IMG, g_Image.IMAGE_POPUP_BTN_DIM);
	// this.CancelBtn.SetTextObjId("loading_btn_text");
	// this.CancelBtn.SetText(SID_CANCEL);
	// this.CancelBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_NORMAL_STYLE,"common_btn_text_f17_normal_style");
	// this.CancelBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_DIM_STYLE, "common_btn_text_f17_dim_style");
	//
	this.OKBtn.hiddenObj();
	this.CancelBtn.hiddenObj();
	// this.CancelBtn.SetOnClickCallbackFunc(clickCF);
}

SceneLoading.prototype.initLoadingType = function(typeIndex) {
	this.LoadingType = typeIndex;
}

SceneLoading.prototype.update = function() {
	this.resetLoadingView();
	this.LoadingPopup = new View("Loading_popup");
	var this_ = sf.scene.get("Loading");
	var clickCF = function(obj) {
		this_.processClickEvent(obj);
	};
	if(this.LoadingType == LoadingTypeIndex.SIGNIN_WITH_CANCEL) {
		// this.LoadingPopup.CreatePopupBG(1,494,254,300,212);
		this.LoadingPopup.CreatePopupBG(1, "Loading_popup_BG_SIGNIN_WITH_CANCEL");
		this.LoadingPopup.CreateLoadingPopup("Loading_popup_loading_icon_SIGNIN_WITH_CANCEL");
		this.LoadingPopup.CreateLoadingPopupText("Loading_popup_loading_text_SIGNIN_WITH_CANCEL");
		this.CancelBtn = this.LoadingPopup.CreateLoadingBtn("Loading_popup_loading_Btn_SIGNIN_WITH_CANCEL");
		this.CancelBtn.SetText(SID_CANCEL);
		this.CancelBtn.SetOnClickCallbackFunc(clickCF);
		// this.LoadingPopup.SetLoadingPopupText(IDWS_MOIP_SKYPE_SIGNIN);
		this.LoadingPopup.SetLoadingPopupText(IDWS_MOIP_SKYPE_SIGNING_IN);
		if(this.CancelBtn.GetFlagDim() == true) {
			this.CancelBtn.SetFlagDim(false);
		}

		this.highlight(this.CancelBtn);
		this.CancelBtn.visibleObj();
	} else if(this.LoadingType == LoadingTypeIndex.SIGNIN) {
		this.LoadingPopup.CreatePopupBG(1, "Loading_popup_BG_SIGNIN");
		this.LoadingPopup.CreateLoadingPopup("Loading_popup_loading_icon_SIGNIN");
		this.LoadingPopup.CreateLoadingPopupText("Loading_popup_loading_text_SIGNIN");
		this.LoadingPopup.SetLoadingPopupText(TV_SID_CHECKING_YOUR_SKYPE_NAME_IS_AVAILABLE_DOT);
		this.CancelBtn.hiddenObj();
		this.highlight(null);
	} else if(this.LoadingType == LoadingTypeIndex.SEARCH) {
		this.LoadingPopup.CreatePopupBG(1, "Loading_popup_BG_SIGNIN");
                this.LoadingPopup.CreateLoadingPopup("Loading_popup_loading_icon_SIGNIN");
		this.LoadingPopup.CreateLoadingPopupText("Loading_popup_loading_text_SIGNIN");
		this.LoadingPopup.SetLoadingPopupText(SID_SEARCHING);
		this.CancelBtn.hiddenObj();
		this.highlight(null);
	} else if(this.LoadingType == LoadingTypeIndex.WAIT) {
		this.LoadingPopup.CreatePopupBG(1, "Loading_popup_BG_SIGNIN");
		this.LoadingPopup.CreateLoadingPopup("Loading_popup_loading_icon_SIGNIN");
		this.LoadingPopup.CreateLoadingPopupText("Loading_popup_loading_text_SIGNIN");
		this.LoadingPopup.SetLoadingPopupText(TV_SID_WAITING_DOT);
		this.CancelBtn.hiddenObj();
		this.highlight(null);
	} else if(this.LoadingType == LoadingTypeIndex.LOADING) {
		this.popupBGFlag = false;
		/*this.LoadingPopup.CreatePopupBG(1, "Loading_popup_BG_SIGNIN");
		this.LoadingPopup.CreateLoadingPopup("Loading_popup_loading_icon_SIGNIN");
		this.LoadingPopup.CreateLoadingPopupText("Loading_popup_loading_text_SIGNIN");*/
		this.LoadingAni.visibleObj();
		this.CancelBtn.hiddenObj();
		this.highlight(null);
	} else {
		// this.LoadingPopup.SetLoadingPopupText(IDWS_MOIP_SKYPE_SIGNIN);
		this.LoadingPopup.SetLoadingPopupText(IDWS_MOIP_SKYPE_SIGNING_IN);
		this.CancelBtn.hiddenObj();
	}
}

SceneLoading.prototype.highlight = function(obj) {
	this.LoadingPopup.setCurrenHighlight(obj);
}

SceneLoading.prototype.startTimer = function() {
	// alert("SceneLoading.startTimer() ");

	var scene = sf.scene.get('Loading');
	this.LoadingTimer = setInterval("timerCF()", 100);
	timerCF = function() {
		scene.loadingTimerCallback();
	}
}

SceneLoading.prototype.stopTimer = function() {
	alert("SceneLoading.stopTimer() ");

	clearInterval(this.LoadingTimer);
	this.LoadingTimer = null;
	this.iCurrAniImage = 0;
}

SceneLoading.prototype.loadingTimerCallback = function() {
	this.iCurrAniImage++;

	if(this.iCurrAniImage >= this.LoadingAniImage.length) {
		this.iCurrAniImage = 0;
	}
	if(this.popupBGFlag){
	this.LoadingPopup.SetLoadingPopupBG(this.LoadingAniImage[this.iCurrAniImage]);
	}else{
		this.LoadingAni.SetBgImage(this.LoadingAniImage[this.iCurrAniImage]);	
	}
}

SceneLoading.prototype.processEnterKey = function() {
	alert("SceneLoading.processEnterKey() ");

	var currentHighlight = this.LoadingPopup.getCurrentHighlight();

	switch(currentHighlight) {
		case this.CancelBtn:
			this.requestSigninCancel();
			break;
		case this.OKBtn:
			ProcMgr.StartLoggedInState();
			break;
	}
}

SceneLoading.prototype.requestSigninCancel = function() {
	alert("SceneLoading.requestSigninCancel() ");

	if(this.LoadingType == LoadingTypeIndex.SIGNIN_WITH_CANCEL) {
		if(this.CancelBtn.GetFlagDim() == false) {
			this.CancelBtn.blur();
			this.CancelBtn.SetFlagDim(true);
			this.LoadingPopup.SetLoadingPopupText(TV_SID_WAITING_DOT);
			ProcMgr.CancelSignin();
		}
	}
}
/*
 SceneLoading.prototype.processCancelBtnClick = function(){
 alert("SceneLoading.processCancelBtnClick() ");

 this.requestSigninCancel();
 }*/

SceneLoading.prototype.processReturnKey = function() {
	alert("SceneLoading.processReturnKey() ");

	sf.scene.hide('Loading');
}

SceneLoading.prototype.disableCancel = function() {
	if(this.CancelBtn.GetFlagDim() == false) {
		this.CancelBtn.blur();
		this.CancelBtn.SetFlagDim(true);
		this.LoadingPopup.SetLoadingPopupText(TV_SID_WAITING_DOT);
	}
}
SceneLoading.prototype.resetLoadingView = function() {
	this.popupBGFlag = true;
	this.LoadingAni.hiddenObj();
	if(this.LoadingPopup) {
		this.LoadingPopup.RemovePopupBG();
	}
	if(this.SuccessPopup) {
		this.SuccessPopup.RemovePopupBG();
	}
}
SceneLoading.prototype.processClickEvent = function(obj) {
	alert("SceneLoading.processClickEvent() ");

	this.processEnterKey();
}