var VIDEO_SNAPSHOT_COUNTER = {

	INIT : 0,
	NOTHING : 1,
	ONE : 2,
	TWO : 3,
	THREE : 4,

}

var VIDEO_SNAPSHOT_MODE = {

	RECORD : 1,
	SAVE : 2,
}

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

	this.VideoSnapshotView = null;

	this.Title = null;
	this.FirstCountButton = null;
	this.SecondCountButton = null;
	this.ThirdCountButton = null;
	this.ShootBtn = null;
	this.SaveBtn = null;
	this.RetryBtn = null;

	this.VideoSnapshotHelpBar = null;
	
	this.HelpBarData = null;

	this.timer = null;
	this.Counter = VIDEO_SNAPSHOT_COUNTER.INIT;

	this.magnification = 0;

	//flag
	this.IsKeyBlock = false;
	this.Is1366x768Panel = false;
	this.Mode = VIDEO_SNAPSHOT_MODE.RECORD;
}

SceneVideoSnapshot.prototype.initialize = function() {
	alert("SceneVideoSnapshot.initialize()");

	this.create();
}

SceneVideoSnapshot.prototype.handleShow = function() {
	alert("SceneVideoSnapshot.handleShow()");
	this.ChangeBG(true);
	/*if(this.Is1366x768Panel) {
		PluginAPIMgr.StartPreview(43, 24, 1280, 720);
	} else {
		PluginAPIMgr.StartPreview(422,164,1080,608);
	}*/
	
	ProcMgr.StartPreviewOfVideoSnapshot();

	this.magnification = 0;
	this.ShootBtn.visibleObj();

}

SceneVideoSnapshot.prototype.handleHide = function() {
	alert("SceneVideoSnapshot.handleHide()");
	this.ChangeBG(false);
	this.ResetState();
	this.highlight(null);

	if(ProcMgr.GetIsInCall() == false) {
		PluginAPIMgr.StopPreview();
	}
}

SceneVideoSnapshot.prototype.handleFocus = function() {
	alert("SceneVideoSnapshot.handleFocus()");

	var currentHighlight = this.VideoSnapshotView.getCurrentHighlight();

	if(currentHighlight == null) {

		if(this.Mode == VIDEO_SNAPSHOT_MODE.SAVE) {

			this.highlight(this.SaveBtn);
		} else {
			this.highlight(this.ShootBtn);
		}

	} else {

		this.highlight(currentHighlight);
	}
	this.VideoSnapshotHelpBar.setItems(this.HelpBarData);
}

SceneVideoSnapshot.prototype.handleBlur = function() {
	alert("SceneVideoSnapshot.handleBlur()");

	var currentHighlight = this.VideoSnapshotView.getCurrentHighlight();

	if(currentHighlight != null) {

		currentHighlight.blur();
	}

}

SceneVideoSnapshot.prototype.handleKeyDown = function(keyCode) {
	alert("SceneVideoSnapshot.handleKeyDown(" + keyCode + ")");
	PluginAPIMgr.PlayAudio(keyCode);
	if(ProcMgr.checkWidgetHighlight()==false){
		return;
	}
	if(this.IsKeyBlock == false) {

		switch (keyCode) {

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

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

			case sf.key.LEFT:
			case sf.key.RIGHT:
				this.processLeftRightKey();
				break;

			case 147:
			//infolink
			case 261:
			//contents
			case 115:
				//wiselink
				PluginAPIMgr.StopPreview();
				ProcMgr.processInfolinkKey();
				break;

			default:
				ProcMgr.processBlockKey(keyCode);
				break;

		}
	} else {
		if(keyCode == sf.key.RETURN && this.Counter > VIDEO_SNAPSHOT_COUNTER.INIT) {
			this.ResetState();
			this.highlight(this.ShootBtn);
		} 
		sf.key.preventDefault();
	}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
SceneVideoSnapshot.prototype.ChangeBG = function(flag) {
	alert("SceneViewProfileProfile.chageBG()");
	// if(WndMgr.Is1366x768Panel()) {
		// alert("**************** this panel is 1366 x 768");
		// this.Is1366x768Panel = true;
		// this.VideoSnapshotView.SetBgImage(g_Image.IMAGE_SNAPSHOT_SHOOT_BG);
	// } else {
		// alert("**************** this panel is not 1366 x 768");
		// this.VideoSnapshotView.SetBgImage(g_Image.IMAGE_SNAPSHOT_SHOOT_BG);
	// }
	if(flag){
		$("#skype_main_bg").css('background-image','url('+g_Image.IMAGE_SNAPSHOT_SHOOT_BG+')');
	}else{
		$("#skype_main_bg").css('background-image','url('+g_Image.IMAGE_SKYPE_BG+')');
	}
}
SceneVideoSnapshot.prototype.create = function() {
	alert("SceneVideoSnapshot.create()");

	//PluginAPIMgr.GetResolution();

	this.VideoSnapshotView = new View("VideoSnapshot");

	this.Title = new TextLayer("VideoSnapshot_title");
	this.Title.SetText(IDWS_MOIP_SKYPE_TAKE_VIDEO_SNAPSHOT);

	this.FirstCountLabel = new Label("VideoSnapshot_Count1", null);
	this.FirstCountLabel.SetBgImage(g_Image.IMAGE_CALLING_ANIMATION_NORMAL);

	this.FirstCountButton = new ButtonBase();
	this.FirstCountButton.Create("VideoSnapshot_Count1", BUTTON_BASE_TYPE.ONLY_BG);
	this.FirstCountButton.SetBgImage(BUTTON_BASE_STATE.NORMAL, g_Image.IMAGE_LIST_MAIN_PROFILE_SNAPSHOT_COUNT1_NORMAL_BG);
	this.FirstCountButton.SetBgImage(BUTTON_BASE_STATE.HIGHLIGHT, g_Image.IMAGE_LIST_MAIN_PROFILE_SNAPSHOT_COUNT1_HIGHLIGHT_BG);

	this.SecondCountButton = new ButtonBase();
	this.SecondCountButton.Create("VideoSnapshot_Count2", BUTTON_BASE_TYPE.ONLY_BG);
	this.SecondCountButton.SetBgImage(BUTTON_BASE_STATE.NORMAL, g_Image.IMAGE_LIST_MAIN_PROFILE_SNAPSHOT_COUNT2_NORMAL_BG);
	this.SecondCountButton.SetBgImage(BUTTON_BASE_STATE.HIGHLIGHT, g_Image.IMAGE_LIST_MAIN_PROFILE_SNAPSHOT_COUNT2_HIGHLIGHT_BG);

	this.ThirdCountButton = new ButtonBase();
	this.ThirdCountButton.Create("VideoSnapshot_Count3", BUTTON_BASE_TYPE.ONLY_BG);
	this.ThirdCountButton.SetBgImage(BUTTON_BASE_STATE.NORMAL, g_Image.IMAGE_LIST_MAIN_PROFILE_SNAPSHOT_COUNT3_NORMAL_BG);
	this.ThirdCountButton.SetBgImage(BUTTON_BASE_STATE.HIGHLIGHT, g_Image.IMAGE_LIST_MAIN_PROFILE_SNAPSHOT_COUNT3_HIGHLIGHT_BG);

	this.ShootBtn = new Button();
	this.ShootBtn.SetImageObjId("VideoSnapshot_ShootButton", 30);
	this.ShootBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_SNAPSHOT_SHOOT_BTN_NORMAL);
	this.ShootBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_SNAPSHOT_SHOOT_NORMALFOCUS);
	this.ShootBtn.SetBgImage(BtnImgIndex.BTN_DIM_IMG, g_Image.IMAGE_SNAPSHOT_SHOOT_DIM);
	this.ShootBtn.SetTextObjId("VideoSnapshot_ShootButtonText");
	this.ShootBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_NORMAL_STYLE, "videoSnapshot_button_text_nor_style");
	this.ShootBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_FOCUS_STYLE, "videoSnapshot_button_text_focus_style");
	this.ShootBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_DIM_STYLE, "videoSnapshot_button_text_dim_style");
	this.ShootBtn.SetText(TV_SID_TAKE_A_SNAPSHOT);
	this.ShootBtn.SetOnClickCallbackFunc(this.ClickShootBtn);
	this.ShootBtn.ResizeButton("C");
	this.ShootBtn.hiddenObj();

	this.SaveBtn = new Button();
	this.SaveBtn.SetImageObjId("VideoSnapshot_SaveButton", 30);
	this.SaveBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_SNAPSHOT_SHOOT_BTN_NORMAL);
	this.SaveBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_SNAPSHOT_SHOOT_NORMALFOCUS);
	this.SaveBtn.SetBgImage(BtnImgIndex.BTN_DIM_IMG, g_Image.IMAGE_SNAPSHOT_SHOOT_DIM);
	this.SaveBtn.SetTextObjId("VideoSnapshot_SaveButtonText");
	this.SaveBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_NORMAL_STYLE, "videoSnapshot_button_text_nor_style");
	this.SaveBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_FOCUS_STYLE, "videoSnapshot_button_text_focus_style");
	this.SaveBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_DIM_STYLE, "videoSnapshot_button_text_dim_style");
	this.SaveBtn.SetText(SID_SAVE);
	this.SaveBtn.SetOnClickCallbackFunc(this.ClickSaveBtn);
	this.SaveBtn.ResizeButton("L");
	this.SaveBtn.hiddenObj();

	this.RetryBtn = new Button();
	this.RetryBtn.SetImageObjId("VideoSnapshot_RetryButton", 30);
	this.RetryBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_SNAPSHOT_SHOOT_BTN_NORMAL);
	this.RetryBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_SNAPSHOT_SHOOT_NORMALFOCUS);
	this.RetryBtn.SetBgImage(BtnImgIndex.BTN_DIM_IMG, g_Image.IMAGE_SNAPSHOT_SHOOT_DIM);
	this.RetryBtn.SetTextObjId("VideoSnapshot_RetryButtonText");
	this.RetryBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_NORMAL_STYLE, "videoSnapshot_button_text_nor_style");
	this.RetryBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_FOCUS_STYLE, "videoSnapshot_button_text_focus_style");
	this.RetryBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_DIM_STYLE, "videoSnapshot_button_text_dim_style");
	this.RetryBtn.SetText(SID_RETRY_KR_RETRY);
	this.RetryBtn.SetOnClickCallbackFunc(this.ClickRetryBtn);
	this.RetryBtn.ResizeButton("R");
	this.RetryBtn.hiddenObj();
	
	//helpbar
	this.VideoSnapshotHelpBar = new HelpBar.create("VideoSnapshot");
	this.HelpBarData = {
		'return' : SID_RETURN
	};
	var this_ = this;
	this.ShootBtn.SetOnMouseOverCallbackFunc(function(obj) {
		this_.processMouseOver(obj);
	});
	this.SaveBtn.SetOnMouseOverCallbackFunc(function(obj) {
		this_.processMouseOver(obj);
	});
	this.RetryBtn.SetOnMouseOverCallbackFunc(function(obj) {
		this_.processMouseOver(obj);
	});
	
	var CurLangID = ResMgr.GetCurLanguage();
	if(CurLangID == ENUM_TV_LANGUAGE.JPN)
	{
		$("#VideoSnapshot_ShootButtonText").css('font-size', "22px" );
	}
	else{
		$("#VideoSnapshot_ShootButtonText").css('font-size', "24px" );
	}
}

SceneVideoSnapshot.prototype.highlight = function(obj) {

	this.VideoSnapshotView.setCurrenHighlight(obj);
}

SceneVideoSnapshot.prototype.processMouseOver = function(obj) {
	var currentHighlight = this.VideoSnapshotView.getCurrentHighlight();
	if(currentHighlight != obj) {
		this.highlight(obj);
	}
}

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

	if(this.Mode == VIDEO_SNAPSHOT_MODE.RECORD) {

		StateMgr.Start_Main_State();
		StateMgr.Start_Change_Picture_State();
	} else if(this.Mode == VIDEO_SNAPSHOT_MODE.SAVE) {

		StateMgr.Start_Main_State();
		StateMgr.Start_Change_Picture_State();
	} else {
		alert("Mode Error!!!!");
	}

}

SceneVideoSnapshot.prototype.processLeftRightKey = function() {
	alert("SceneVideoSnapshot.processLeftRightKey() ");

	var currentHighlight = this.VideoSnapshotView.getCurrentHighlight();

	if(currentHighlight == this.RetryBtn) {

		this.highlight(this.SaveBtn);
	} else if(currentHighlight == this.SaveBtn) {

		this.highlight(this.RetryBtn);
	}

}

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

	var currentHighlight = this.VideoSnapshotView.getCurrentHighlight();

	switch(currentHighlight) {

		case this.RetryBtn:
			this.IsKeyBlock = true;
			PluginAPIMgr.StopPreview();
			//PluginAPIMgr.StartPreview(422,164,1080,608);
			ProcMgr.StartPreviewOfVideoSnapshot();
			this.SetMode(VIDEO_SNAPSHOT_MODE.RECORD);
			this.IsKeyBlock = false;
			break;

		case this.ShootBtn:
			this.ProcessSootBtn();
			break;

		case this.SaveBtn:
			sf.scene.get("ChangePicture").ResetLastIndex();
			this.ProcessSaveBtn();
			break;
	}
}

SceneVideoSnapshot.prototype.ProcessSootBtn = function() {

	if(PluginAPIMgr.GetNumOfSnapShot() < TOTAL_NUM_OF_SNAPSHOT) {

		this.IsKeyBlock = true;
		this.highlight(null);
		//this.ShootBtn.dim();
		this.ShootBtn.SetFlagDim(true);
		this.StartSnapshot();

	} else {

		StateMgr.Start_Main_State();
		WndMgr.StartOneBottonPopup(OneBtnPopupIndex.NO_MORE_PHOTO_AVAILABLE_IN_VIDEO_SNAPSHOT);

	}
}

SceneVideoSnapshot.prototype.ProcessSaveBtn = function() {

	this.IsKeyBlock = true;
	//Sync function !!! Do not process event from engine.
	PluginAPIMgr.SaveSnapShot();
	PluginAPIMgr.StopPreview();

	StateMgr.Start_Main_State();
	StateMgr.Start_Change_Picture_State();

}

SceneVideoSnapshot.prototype.SetMode = function(nMode) {
	alert("SceneVideoSnapshot.SetMode() ");

	if(nMode == this.Mode) {

		if(nMode == VIDEO_SNAPSHOT_MODE.SAVE) {

			this.highlight(this.SaveBtn);
		} else {

			this.highlight(this.ShootBtn);
		}
	} else {

		this.Mode = nMode;

		if(nMode == VIDEO_SNAPSHOT_MODE.SAVE) {

			this.ShootBtn.hiddenObj();

			this.SaveBtn.visibleObj();
			this.RetryBtn.visibleObj();

			this.highlight(this.SaveBtn);
		} else if(nMode == VIDEO_SNAPSHOT_MODE.RECORD) {

			this.SaveBtn.hiddenObj();
			this.RetryBtn.hiddenObj();

			this.ShootBtn.SetFlagDim(false);
			this.ShootBtn.select();
			this.ShootBtn.visibleObj();

			this.highlight(this.ShootBtn);
		}
	}
}

SceneVideoSnapshot.prototype.SetCounterImage = function(count) {

	alert("SceneVideoSnapshot.SetCounterImage(" + count + ")");

	switch(count) {

		case VIDEO_SNAPSHOT_COUNTER.INIT:
			this.FirstCountButton.Hide();
			this.SecondCountButton.Hide();
			this.ThirdCountButton.Hide();
			this.FirstCountButton.blur();
			this.SecondCountButton.blur();
			this.ThirdCountButton.blur();
			break;
		case VIDEO_SNAPSHOT_COUNTER.NOTHING:
			this.FirstCountButton.Show();
			this.SecondCountButton.Show();
			this.ThirdCountButton.Show();
			break;
		case VIDEO_SNAPSHOT_COUNTER.ONE:
			this.FirstCountButton.highlight();
			break;
		case VIDEO_SNAPSHOT_COUNTER.TWO:
			this.FirstCountButton.blur();
			this.SecondCountButton.highlight();
			break;
		case VIDEO_SNAPSHOT_COUNTER.THREE:
			this.SecondCountButton.blur();
			this.ThirdCountButton.highlight();
			break;
	}

}

SceneVideoSnapshot.prototype.StartSnapshot = function() {

	alert("SceneVideoSnapshot.StartSnapshot()");

	var this_ = sf.scene.get('VideoSnapshot');

	this_.Counter++;
	if(this_.Counter == VIDEO_SNAPSHOT_COUNTER.THREE) {

		this_.SetCounterImage(this_.Counter);
		this_.timer = setTimeout("Callbackfunction()", 300);
		Callbackfunction = function() {
			this_.StartSnapshot();
		}
	} else if(this_.Counter > VIDEO_SNAPSHOT_COUNTER.THREE) {

		//this_.IsKeyBlock = true;
		this_.Counter = VIDEO_SNAPSHOT_COUNTER.INIT;
		this_.SetCounterImage(this_.Counter);
		PluginAPIMgr.TakeSnapShot();

		this_.SetMode(VIDEO_SNAPSHOT_MODE.SAVE);

	} else {

		this_.SetCounterImage(this_.Counter);
		this_.timer = setTimeout("Callbackfunction()", 1000);
		Callbackfunction = function() {
			this_.StartSnapshot();
		}
	}
}

SceneVideoSnapshot.prototype.stopSnapshot = function() {
	alert("SceneVideoSnapshot.stopSnapshot() ");

	clearTimeout(this.timer);
	this.timer = null;
}

SceneVideoSnapshot.prototype.RemovedCamera = function() {

	alert("SceneVideoSnapshot.RemovedCamera() ");
	this.processReturnKey();

}

SceneVideoSnapshot.prototype.ResetState = function() {

	alert("SceneVideoSnapshot.ResetState() ");

	//reset countering
	clearTimeout(this.timer);
	this.timer = null;
	this.IsKeyBlock = false;
	this.Counter = VIDEO_SNAPSHOT_COUNTER.INIT;
	this.SetCounterImage(VIDEO_SNAPSHOT_COUNTER.INIT);

	//reset button mode
	this.Mode = VIDEO_SNAPSHOT_MODE.RECORD;

	this.SaveBtn.hiddenObj();
	this.RetryBtn.hiddenObj();

	this.ShootBtn.SetFlagDim(false);
	this.ShootBtn.blur();
	this.ShootBtn.visibleObj();

}

SceneVideoSnapshot.prototype.TrunOffKeyBlock = function() {
	alert("SceneVideoSnapshot.TrunOffKeyBlock() ");
	this.IsKeyBlock = false;
}

SceneVideoSnapshot.prototype.ClickRetryBtn = function() {
	alert("SceneVideoSnapshot.ClickRetryBtn");

	var This_ = sf.scene.get('VideoSnapshot');

	This_.IsKeyBlock = true;
	PluginAPIMgr.StopPreview();
	//PluginAPIMgr.StartPreview(422,164,1080,608);
	ProcMgr.StartPreviewOfVideoSnapshot();
	This_.SetMode(VIDEO_SNAPSHOT_MODE.RECORD);
	This_.IsKeyBlock = false;
}

SceneVideoSnapshot.prototype.ClickShootBtn = function() {

	var This_ = sf.scene.get('VideoSnapshot');
	This_.ProcessSootBtn();
}

SceneVideoSnapshot.prototype.ClickSaveBtn = function() {

	var This_ = sf.scene.get('VideoSnapshot');

	sf.scene.get("ChangePicture").ResetLastIndex();
	This_.ProcessSaveBtn();
}