var NUM_OF_DEFAULT_AVATAR = 12;
var TOTAL_NUM_OF_SNAPSHOT = 99;

function SceneChangePicture(options) {

	this.options = options;

	this.ChangePictureView = null;
	this.ChangePictureDim = null;
	this.ChangePictureBG = null;

	this.PictureList = null;
	this.ScrollBar = null;
	this.SnapshotBtn = null;
	this.PopupHelpBar1 = null;
	this.PopupHelpBar2 = null;
	this.PageLabel = null;
	this.LeftArrow = null;
	this.RightArrow = null;

	this.CurPage = 0;
	this.PrevItemIndex = -1;
	this.NumOfSnapShot = 0;
	this.ItemNumber = 4;

	this.SavedAvatarImage = null;
	this.SavedAvatarPathArray = null;
	this.DefaultAvatarPathArray = null;


	this.DeletePhotoIndex = -1;
	this.LastIndex = -1;

	this.flag_InitData = null;

	//timer

}

SceneChangePicture.prototype.initialize = function() {
	alert("SceneChangePicture.initialize()");
	this.create();
}

SceneChangePicture.prototype.handleShow = function() {
	alert("SceneChangePicture.handleShow()");
	this.ChangePictureDim.show();

	if(this.flag_InitData) {
		this.update();
		this.highlight(null);
		this.flag_InitData = false;
		//this.PictureList.SetPrevItemIndex(0);
	}

	var CamState = PluginAPIMgr.GetCameraState();
	if(!(CamState == ENUM_CAM_STATE.CAM_STATE_DISCONNECTED || CamState == ENUM_CAM_STATE.CAM_STATE_CONNECTING)) {

		this.SnapshotBtn.SetFlagDim(false);
	} else {
		if(this.SnapshotBtn.flag_highlight == true) {
			this.highlight(this.PictureList);
		}
		this.SnapshotBtn.SetFlagDim(true);
	}

	WndMgr.startPopupHideTimer(60000);

}

SceneChangePicture.prototype.handleHide = function() {
	alert("SceneChangePicture.handleHide()");
	this.ChangePictureDim.hide();

	this.flag_InitData = true;

	WndMgr.stopPopupHideTimer();

}

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

	var currentHighlight = this.ChangePictureView.getCurrentHighlight();

	if(currentHighlight == null) {
		this.highlight(this.PictureList);
		if(this.LastIndex >= 0) {
			this.PictureList.SetHighlightForDataIndex(this.LastIndex);
		} else {
			this.PictureList.SetHighlightForDataIndex(0);
		}
		this.ChangeHelpbar(1);
	} else {
		this.highlight(currentHighlight);

	}

	this.updatePage();
	this.LastIndex = -1;
}

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

	var currentHighlight = this.ChangePictureView.getCurrentHighlight();

	if(currentHighlight != null) {
		currentHighlight.blur();
	}
}

SceneChangePicture.prototype.handleKeyDown = function(keyCode) {

	alert("SceneChangePicture.handleKeyDown(" + keyCode + ")");
	PluginAPIMgr.PlayAudio(keyCode);
	if(ProcMgr.checkWidgetHighlight()==false){
		return;
	}

	var ridgeDirection = Math.round((keyCode - (5*10000000)) / 100000);
	if(ridgeDirection == 3) { // Ridge flick : Left
		if(ProcMgr.GetRidgeFlick() == false) {
			this.PrevScroll();
			ProcMgr.StartRidgeTimer();
		}
	}
	else if(ridgeDirection == 4) { // Ridge flick : Right
		if(ProcMgr.GetRidgeFlick() == false) {
			this.NextScroll();
			ProcMgr.StartRidgeTimer();
		}
	}
	else {
	switch (keyCode) {
		case sf.key.LEFT:
			WndMgr.resetPopupHideTimer();
			this.processLeftKey();
			break;

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

		case sf.key.UP:
			WndMgr.resetPopupHideTimer();
			this.processUpKey();
			break;
		case sf.key.DOWN:
			WndMgr.resetPopupHideTimer();
			this.processDownKey();
			break;

		case sf.key.BLUE:
			this.processToolBlueKey();
			break;

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

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

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

SceneChangePicture.prototype.create = function() {
	alert("SceneChangePicture.create() ");
	this_ = this;
	this.ChangePictureView = new View("ChangePicture");
	this.ChangePictureBG = new View("ChangePictureBG");
	this.ChangePictureBG.CreatePopupBG(0, "ChangePicture_popup_BG");
	this.ChangePictureBG.SetTitleTextofPopupBG(TV_SID_CHANGE_PICTURE);
	this.ChangePictureDim = new DimBg("ChangePictureDim", "ChangePicture");

	this.PageLabel = new TextLayer("pictureListPageText");

	this.PictureList = new ScrollList();
	this.PictureList.CreateBG("pictureList");
	this.PictureList.CreateScrBar("PictureScrollBar");
	this.PictureList.SetNumOfVisibleItem(this.ItemNumber);
	this.PictureList.SetStartDataIndex(0);
	this.PictureList.SetItemClass(PictureItem);
	this.PictureList.SetDataType(ITEM_DATA_TYPE.IMAGE_ONLY);

	this.PictureList.SetScrBarShapeStyle(LIST_SHAPE_STYLE.HORIZONTAL);
	this.PictureList.SetScrBarPosition(509, 0, 11);
	this.PictureList.SetScrollBarImage(g_Image.IMAGE_CHANGE_PICTURE_SLIDERBAR_LEFT, g_Image.IMAGE_CHANGE_PICTURE_SLIDERBAR_CENTER, g_Image.IMAGE_CHANGE_PICTURE_SLIDERBAR_RIGHT);
	this.PictureList.SetScrBGImage(g_Image.IMAGE_CHANGE_PICTURE_SLIDERBAR_BG_LEFT, g_Image.IMAGE_CHANGE_PICTURE_SLIDERBAR_BG_CENTER, g_Image.IMAGE_CHANGE_PICTURE_SLIDERBAR_BG_RIGHT);

	this.PreLoadingDafaultImage();
	this.ResetData();

	this.PictureList.SetOnClickCallbackFunc(0, function(){this_.ProcessClick(0);});
	this.PictureList.SetOnClickCallbackFunc(1, function(){this_.ProcessClick(1);});
	this.PictureList.SetOnClickCallbackFunc(2, function(){this_.ProcessClick(2);});
	this.PictureList.SetOnClickCallbackFunc(3, function(){this_.ProcessClick(3);});
	
	this.PictureList.SetOnMouseOverCallbackFunc(0, function(){this_.mouseOver(0);});
	this.PictureList.SetOnMouseOverCallbackFunc(1, function(){this_.mouseOver(1);});
	this.PictureList.SetOnMouseOverCallbackFunc(2, function(){this_.mouseOver(2);});
	this.PictureList.SetOnMouseOverCallbackFunc(3, function(){this_.mouseOver(3);});
	this.PictureList.SetClickEventAvailable(WndMgr.MouseClickEnable);
	

	this.PictureList.ShowItem();

	this.PictureList.Show();

	this.SnapshotBtn = new Button();
	this.SnapshotBtn.SetImageObjId("snapShotButton", 20);
	this.SnapshotBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_POPUP_BTN_NORMAL);
	this.SnapshotBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_POPUP_BTN_NORMALFOCUS);
	this.SnapshotBtn.SetBgImage(BtnImgIndex.BTN_DIM_IMG, g_Image.IMAGE_POPUP_BTN_DIM);	this.SnapshotBtn.SetTextObjId("snapShotButtonText");
	this.SnapshotBtn.SetText(IDWS_MOIP_SKYPE_TAKE_VIDEO_SNAPSHOT);
	this.SnapshotBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_NORMAL_STYLE, "popup_btn_text_normal_style");
	this.SnapshotBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_FOCUS_STYLE, "popup_btn_text_focus_style");
	this.SnapshotBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_DIM_STYLE, "popup_btn_text_dimmed_style");
	this.SnapshotBtn.ResizeButton("C");
	this.SnapshotBtn.SetOnClickCallbackFunc(this.ClickSnapshotBtn);
	this.SnapshotBtn.SetOnMouseOverCallbackFunc(function(){this_.highlight(this_.SnapshotBtn);});

	//Imae Lable
	this.LeftArrow = new Label("pictureListLeftArrow", null);
	this.LeftArrow.SetBgImage(g_Image.IMAGE_CHANGE_PICTURE_ARROW_LEFT_NORMAL);
	this.LeftArrow.SetOnMouseOutOverImage(g_Image.IMAGE_CHANGE_PICTURE_ARROW_LEFT_NORMAL, g_Image.IMAGE_CHANGE_PICTURE_ARROW_LEFT_CURSOR);
	this.LeftArrow.SetOnClickCallbackFunc(this.PrevScroll);

	this.RightArrow = new Label("pictureListRightArrow", null);
	this.RightArrow.SetBgImage(g_Image.IMAGE_CHANGE_PICTURE_ARROW_RIGHT_NORMAL);
	this.RightArrow.SetOnMouseOutOverImage(g_Image.IMAGE_CHANGE_PICTURE_ARROW_RIGHT_NORMAL, g_Image.IMAGE_CHANGE_PICTURE_ARROW_RIGHT_CURSOR);
	this.RightArrow.SetOnClickCallbackFunc(this.NextScroll);

	this.flag_InitData = false;
	
	var CurLangID = ResMgr.GetCurLanguage();
 	
	if(CurLangID == ENUM_TV_LANGUAGE.VIE ||CurLangID == ENUM_TV_LANGUAGE.JPN)
	{
		$("#snapShotButtonText").css('font-size', "21px" );
	}
	else{
		$("#snapShotButtonText").css('font-size', "24px" );
	}
	 

}

SceneChangePicture.prototype.highlight = function(obj) {
	var currentHighlight = this.ChangePictureView.getCurrentHighlight();
	if(currentHighlight == obj) {
		return ;
	}
	this.ChangePictureView.setCurrenHighlight(obj);
	if(obj == this.SnapshotBtn) {
		this.ChangeHelpbar(0);
	} else if(obj == this.PictureList) {
		this.ChangeHelpbar(1);
	}
}

SceneChangePicture.prototype.PreLoadingDafaultImage = function() {

	var prePath = "file:///mtd_down/widgets/user/Skype/avatar/";
	//var prePath = "file:///mtd_down/widgets/normal/user/" + curWidget.id + "/avatar/";

	alert("prePath = " + prePath);

	this.defaultAvatarImage = new Array(NUM_OF_DEFAULT_AVATAR);
	this.DefaultAvatarPathArray = new Array(NUM_OF_DEFAULT_AVATAR);

	for(var i = 0; i < NUM_OF_DEFAULT_AVATAR; i++) {

		this.defaultAvatarImage[i] = new Image();
	}

	this.defaultAvatarImage[0].src = prePath + "default_avatar_Angel.jpg";
	this.defaultAvatarImage[1].src = prePath + "default_avatar_Architect.jpg";
	this.defaultAvatarImage[2].src = prePath + "default_avatar_Behind.jpg";
	this.defaultAvatarImage[3].src = prePath + "default_avatar_CallMe.jpg";
	this.defaultAvatarImage[4].src = prePath + "default_avatar_Chic.jpg";
	this.defaultAvatarImage[5].src = prePath + "default_avatar_Earbud.jpg";
	this.defaultAvatarImage[6].src = prePath + "default_avatar_Behind.jpg";
	this.defaultAvatarImage[7].src = prePath + "default_avatar_Headset.jpg";
	this.defaultAvatarImage[8].src = prePath + "default_avatar_Party.jpg";
	this.defaultAvatarImage[9].src = prePath + "default_avatar_Skypeness.jpg";
	this.defaultAvatarImage[10].src = prePath + "default_avatar_Star.jpg";
	this.defaultAvatarImage[11].src = prePath + "default_avatar_Travel.jpg";

	this.DefaultAvatarPathArray[0] = prePath + "default_avatar_Angel.jpg";
	this.DefaultAvatarPathArray[1] = prePath + "default_avatar_Architect.jpg";
	this.DefaultAvatarPathArray[2] = prePath + "default_avatar_Behind.jpg";
	this.DefaultAvatarPathArray[3] = prePath + "default_avatar_CallMe.jpg";
	this.DefaultAvatarPathArray[4] = prePath + "default_avatar_Chic.jpg";
	this.DefaultAvatarPathArray[5] = prePath + "default_avatar_Earbud.jpg";
	this.DefaultAvatarPathArray[6] = prePath + "default_avatar_Behind.jpg";
	this.DefaultAvatarPathArray[7] = prePath + "default_avatar_Headset.jpg";
	this.DefaultAvatarPathArray[8] = prePath + "default_avatar_Party.jpg";
	this.DefaultAvatarPathArray[9] = prePath + "default_avatar_Skypeness.jpg";
	this.DefaultAvatarPathArray[10] = prePath + "default_avatar_Star.jpg";
	this.DefaultAvatarPathArray[11] = prePath + "default_avatar_Travel.jpg";

	this.SavedAvatarImage = new Array(TOTAL_NUM_OF_SNAPSHOT);
	this.SavedAvatarPathArray = new Array(TOTAL_NUM_OF_SNAPSHOT);
}

SceneChangePicture.prototype.ResetData = function() {

	alert("SceneChangePicture.ResetData ");

	this.PictureList.DeleteDataAll();

	// alert("strSnapShotPathList = " + strSnapShotPathList );

	this.NumOfSnapShot = PluginAPIMgr.GetNumOfSnapShot();
	alert("NumOfSnapShot = " + this.NumOfSnapShot);

	if(this.NumOfSnapShot > 0) {
		var strSnapShotPathList = PluginAPIMgr.GetSnapShotPathList();
		var STX = String.fromCharCode(2);
		var ETX = String.fromCharCode(3);
		var splitter = ETX + STX;

		//var tempData = new Array(this.NumOfSnapShot);
		var tempData = strSnapShotPathList.split(splitter);
		var TempPARAM;
		var TempImagePath;

		//Set Data "file:///mtd_moip/moip/snapshot/"
		for(var i = 0; i < this.NumOfSnapShot; i++) {

			if(this.SavedAvatarImage[i] == null) {

				alert("Create Image Object");
				this.SavedAvatarImage[i] = new Image();
			}
			TempImagePath = this.PreLoadingSavedAvatar(i, tempData[(this.NumOfSnapShot - 1) - i]);
			TempPARAM = [null, TempImagePath];

			//alert("tempData["+ i + "]" + tempData[i]);

			this.PictureList.AddData(TempPARAM);

		}

		//to set confirm popup
		for(var j = 0; j < this.NumOfSnapShot; j++) {

			this.SavedAvatarPathArray[j] = tempData[(this.NumOfSnapShot - 1) - j];
		}
	}

	var tempDefault;
	for(var j = 0; j < NUM_OF_DEFAULT_AVATAR; j++) {
		tempDefault = [null, this.defaultAvatarImage[j].src];
		//alert("defaultAvatarImage["+ j + "]" + this.defaultAvatarImage[j].src );
		this.PictureList.AddData(tempDefault);
	}

	//Set Page Info
	this.PictureList.ResetTotalPage();
}

SceneChangePicture.prototype.PreLoadingSavedAvatar = function(Index, ImagePath) {

	this.SavedAvatarImage[Index].src = ImagePath + g_Data.GetImgSuffixPath();
	return this.SavedAvatarImage[Index].src;

}

SceneChangePicture.prototype.update = function() {
	alert("SceneChangePicture.update ");

	if(this.PictureList) {

		this.ResetData();
		this.PictureList.RefreshPage();
	}

}

SceneChangePicture.prototype.updatePage = function() {

	if(this.PictureList) {
		this.PageLabel.SetText(this.PictureList.CurPage + "/" + this.PictureList.TotalPage + " " + TV_SID_PAGES);
	}
}

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

	StateMgr.Stop_Change_Picture_State(true);

}

SceneChangePicture.prototype.processEnterKey = function() {

	var currentHighlight = this.ChangePictureView.getCurrentHighlight();
	if(currentHighlight == this.PictureList) {

		var confirmScene = sf.scene.get("ChangePictureConfirm");
		var curIndex = this.PictureList.GetCurrentDataIndex();
		var curImgPath;

		if(curIndex < this.NumOfSnapShot) {

			//check
			curImgPath = this.SavedAvatarPathArray[curIndex];
		} else {
			curImgPath = this.DefaultAvatarPathArray[curIndex - this.NumOfSnapShot];
		}

		this.SaveLastIndex();
		WndMgr.StartPictureConfirmStep(curImgPath);

	} else if(currentHighlight == this.SnapshotBtn) {
		if(this.NumOfSnapShot < TOTAL_NUM_OF_SNAPSHOT) {
			this.SaveLastIndex();
			StateMgr.Start_Vidoe_Snapshot_State();
		} else {
			WndMgr.StartWarningWndStep();
		}
	}
}
SceneChangePicture.prototype.processUpKey = function() {
	alert("SceneChangePicture.processUpKey() ");
	var currentHighlight = this.ChangePictureView.getCurrentHighlight();
	if(currentHighlight == this.SnapshotBtn) {
		this.highlight(this.PictureList);

	}
}
SceneChangePicture.prototype.processDownKey = function() {
	alert("SceneChangePicture.processDownKey() ");

	var currentHighlight = this.ChangePictureView.getCurrentHighlight();

	if(currentHighlight == this.PictureList) {

		if(this.SnapshotBtn.GetFlagDim() != true) {
			this.highlight(this.SnapshotBtn);

		}
	}
}

SceneChangePicture.prototype.processLeftKey = function() {
	alert("SceneChangePicture.processLeftRightKey() ");

	var currentHighlight = this.ChangePictureView.getCurrentHighlight();

	if(currentHighlight == this.PictureList) {

		this.PictureList.KeyProcess(sf.key.LEFT);

		this.updatePage();
		this.ChangeHelpbar(1);
	}

}

SceneChangePicture.prototype.processRightKey = function() {
	alert("SceneChangePicture.processLeftRightKey() ");

	var currentHighlight = this.ChangePictureView.getCurrentHighlight();

	if(currentHighlight == this.PictureList) {

		this.PictureList.KeyProcess(sf.key.RIGHT);
		this.updatePage();
		this.ChangeHelpbar(1);
	}
}

SceneChangePicture.prototype.processToolBlueKey = function() {
	alert("SceneChangePicture.processToolBlueKey() ");

	var currentHighlight = this.ChangePictureView.getCurrentHighlight();
	this.DeletePhotoIndex = this.PictureList.GetCurrentDataIndex();

	alert("Seleted photo index =  " + this.DeletePhotoIndex);

	if(currentHighlight == this.PictureList && (this.DeletePhotoIndex < this.NumOfSnapShot)) {

		this.SaveLastIndex();
		WndMgr.StartDeletePictureStep();
	}

}

SceneChangePicture.prototype.SaveLastIndex = function() {

	this.LastIndex = this.PictureList.GetCurrentDataIndex();
}

SceneChangePicture.prototype.ResetLastIndex = function() {

	this.LastIndex = -1;
}

SceneChangePicture.prototype.GetDeletePhotoIndex = function() {

	alert("DeletePhotoIndex = " + this.DeletePhotoIndex);

	var Index = (this.NumOfSnapShot - 1) - this.DeletePhotoIndex;

	alert("retrun changed  original Index = " + Index);

	return Index;
}

SceneChangePicture.prototype.UpdateVideoSnapshotBtn = function() {

	var CamState = PluginAPIMgr.GetCameraState();
	if(!(CamState == ENUM_CAM_STATE.CAM_STATE_DISCONNECTED || CamState == ENUM_CAM_STATE.CAM_STATE_CONNECTING)) {
		this.SnapshotBtn.SetFlagDim(false);
	} else {
		if(this.SnapshotBtn.flag_highlight == true) {
			this.highlight(this.PictureList);
		}
		this.SnapshotBtn.SetFlagDim(true);
	}

}
//check....
SceneChangePicture.prototype.mouseOver = function(ItemIndex) {
	alert("SceneChangePicture.mouseOver()");
	WndMgr.resetPopupHideTimer();
	var currentHighlight = this.ChangePictureView.getCurrentHighlight();
	if(currentHighlight!=this.PictureList)
		this.highlight(this.PictureList);
		
	var curIndex = ((this.PictureList.CurPage - 1) * this.ItemNumber) + ItemIndex;
	this.PictureList.SetCurrentDataIndex(curIndex);
	var curImgPath;

	if(curIndex < this.NumOfSnapShot) {
		//check
		curImgPath = this.SavedAvatarPathArray[curIndex];
	} else {
		curImgPath = this.DefaultAvatarPathArray[curIndex - this.NumOfSnapShot];
	}
	this.SaveLastIndex();
	this.PictureList.SetHighlightForDataIndex(this.LastIndex);
	this.ChangeHelpbar(1);
}
SceneChangePicture.prototype.ProcessClick = function(ItemIndex) {
	alert("SceneChangePicture.onClickEventFunc() -- " + ItemIndex);
	WndMgr.resetPopupHideTimer();
	// var This_ = sf.scene.get("ChangePicture");
	var currentHighlight = this.ChangePictureView.getCurrentHighlight();
	if(currentHighlight!=this.PictureList)
		this.highlight(this.PictureList);
	var curIndex = ((this.PictureList.CurPage - 1) * this.ItemNumber) + ItemIndex;
	this.PictureList.SetCurrentDataIndex(curIndex);
	var curImgPath;

	if(curIndex < this.NumOfSnapShot) {
		//check
		curImgPath = this.SavedAvatarPathArray[curIndex];
	} else {
		curImgPath = this.DefaultAvatarPathArray[curIndex - this.NumOfSnapShot];
	}

	this.SaveLastIndex();
	this.PictureList.SetHighlightForDataIndex(this.LastIndex);
	this.ChangeHelpbar(1);
	WndMgr.StartPictureConfirmStep(curImgPath);
	
}


SceneChangePicture.prototype.PrevScroll = function() {
	var This_ = sf.scene.get("ChangePicture");
	This_.PictureList.PreviousPage();
	This_.updatePage();
	This_.ChangeHelpbar(1);
}

SceneChangePicture.prototype.NextScroll = function() {
	var This_ = sf.scene.get("ChangePicture");
	This_.PictureList.NextPage();
	This_.updatePage();
	This_.ChangeHelpbar(1);
}

SceneChangePicture.prototype.ClickSnapshotBtn = function() {
	
	var This_ = sf.scene.get('ChangePicture');
	
	if(This_.NumOfSnapShot < TOTAL_NUM_OF_SNAPSHOT) {
		This_.highlight(This_.SnapshotBtn);
		This_.SaveLastIndex();
		StateMgr.Start_Vidoe_Snapshot_State();
	} else {
		WndMgr.StartWarningWndStep();
	}
}

SceneChangePicture.prototype.ChangeHelpbar = function(type) {

	var curIndex = this.PictureList.GetCurrentDataIndex();
	var dataInfo = {
		'return' : SID_RETURN
	};

	if(type == 1) {
		if(curIndex < this.NumOfSnapShot) {
			dataInfo = {
				'blue' : SID_DELETE,
				'return' : SID_RETURN
			};
		} else {
			dataInfo = {
				'return' : SID_RETURN
			};
		}
	} else if(type == 0) {
		dataInfo = {
			'return' : SID_RETURN
		};
	}
	this.ChangePictureBG.SetHelpbarContentsOfPopupBG(dataInfo);

}