
function SceneLicensePopup(options) {
	this.options = options;
	
	this.LicenseView=null;
	this.LicenseDim = null;
	this.LicenseViewBG=null;
	
	this.OKBtn = null;
	
	this.ReadTitleLabel = null;
	this.ReadContentsLabel = null;
	
	
	this.isReadMode = false;
	this.ReadContents = null;
	this.ContentsAnchor = null;
	
	//2013 add by minseok	
	this.ScrollBg_top=null;
	this.ScrollBg_center=null;
	this.ScrollBg_bottom=null;
	this.ScrollTop=null;
	this.ScrollCenter=null;
	this.ScrollUnder=null;
	this.FakeReturnBtn = null;
	
	this.TopArrow = null;
	this.BottomArrow = null;

	//2013 add by minseok
		
	this.KeyDown = false;
}

SceneLicensePopup.prototype.initialize = function () {
	alert("SceneLicensePopup.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();
}

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

SceneLicensePopup.prototype.handleHide = function () {
	alert("SceneLicensePopup.handleHide()");
	// this function will be called when the scene manager hide this scene  
	
}

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

SceneLicensePopup.prototype.handleBlur = function () {
	alert("SceneLicensePopup.handleBlur()");
	// this function will be called when the scene manager move focus to another scene from this scene
    this.highlight(null);	
    
    // var currentHighlight =  this.MainView.getCurrentHighlight();
    // currentHighlight.blur();

}

SceneLicensePopup.prototype.handleKeyDown = function (keyCode) {
	alert("SceneLicensePopup.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.RETURN:
            this.processReturnKey();
            //2013 modify by minseok
            sf.key.preventDefault();
            //sf.key.block(event);
			break;
        
       case sf.key.EXIT:
            this.processExitKey();
            //2013 modify by minseok
            sf.key.preventDefault();
            //sf.key.block(event);
			break;
            
       case 147:  //infolink
       case 261:  //contents
       case 115:  //wiselink
            ProcMgr.processInfolinkKey(); 
			break;
		
		default:
            ProcMgr.processBlockKey(keyCode);
            break;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

SceneLicensePopup.prototype.create = function(){
    alert("SceneLicensePopup.create()");
   	this.LicenseView = new View("LicensePopup");
	this.LicenseDim = new DimBg("LicensePopDim","LicensePopup");
	this.LicenseViewBG = new View("LicensePopupBG");
	this.LicenseViewBG.CreatePopupBG(0,"LicensePopup_popup_BG")
	// this.LicenseViewBG.SetTitleTextofPopupBG("View Terms")
	
	this.ReadPage = new View("license_read_page");
	// this.ReadPage.hiddenObj();
	
	var this_ = sf.scene.get("LicensePopup");
	var clickCF =  function(obj){this_.processClickEvent(obj);};
	var overCF =  function(obj){this_.processMouseOverEvent(obj);};
	var outCF =  function(obj){this_.processMouseOutEvent(obj);};

   	this.OKBtn = new Button();
   	this.OKBtn.SetImageObjId("licensePopup_btn", 20);
    this.OKBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_IMG, g_Image.IMAGE_POPUP_BTN_NORMAL);
    this.OKBtn.SetBgImage(BtnImgIndex.BTN_NORMAL_FOCUS_IMG, g_Image.IMAGE_POPUP_BTN_NORMALFOCUS);
	this.OKBtn.SetBgImage(BtnImgIndex.BTN_DIM_IMG, g_Image.IMAGE_POPUP_BTN_DIM);
    this.OKBtn.SetTextObjId("licensePopup_btn_text");
    this.OKBtn.SetText(SID_OK);
    this.OKBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_NORMAL_STYLE,"cPopup_btn_text");
    this.OKBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_FOCUS_STYLE,"cPopup_btn_text_high");
    this.OKBtn.SetTextStyle(BtnTextStyleIndex.BTN_TEXT_DIM_STYLE,"cPopup_btn_text_dim");
	this.OKBtn.SetOnClickCallbackFunc(clickCF);
   	
	
	this.FakeReturnBtn = new Button();
	this.FakeReturnBtn.SetImageObjId("licensePopup_fake_returnkey");
	this.FakeReturnBtn.SetOnClickCallbackFunc(clickCF);
	
	// this.ReadTitleLabel = new Label(null,"license_read_title");
    // this.ReadTitleLabel.SetTextStyle("license_popup_text_style");
	
	this.ReadContentsLabel = new Label("license_read_contents","license_read_contents");
	this.ReadContentsLabel.SetTextStyle("license_popup_text_style");
	this.ReadContentsLabel.SetOnMouseOverCallbackFunc(overCF);
	this.ReadContentsLabel.SetOnMouseOutCallbackFunc(outCF);
	
	
	
	this.ScrollBg_top= new Label("license_scroll_bg_top", null);
    this.ScrollBg_top.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_SCROLL_BG_TOP);
	this.ScrollBg_center= new Label("license_scroll_bg_center", null);
    this.ScrollBg_center.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_SCROLL_BG_CENTER);
	this.ScrollBg_bottom= new Label("license_scroll_bg_bottom", null);
    this.ScrollBg_bottom.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_SCROLL_BG_BOTTOM);
    
    this.ScrollTop= new Label("license_scroll_top", null);
    this.ScrollTop.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_SCROLL_TOP);
    
    this.ScrollUnder= new Label("license_scroll_middle", null);
    this.ScrollUnder.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_SCROLL_MIDDLE);
    
    this.ScrollCenter= new Label("license_scroll_bottom", null);
    this.ScrollCenter.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_SCROLL_BOTTOM);
    
    this.TopArrow= new Label("license_top_arrow", null);
    this.TopArrow.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_TOP_NOR);
    this.TopArrow.SetOnClickCallbackFunc(clickCF);
    this.TopArrow.SetOnMouseOverCallbackFunc(overCF);
    this.TopArrow.SetOnMouseOutCallbackFunc(outCF);
    
    // this.TopArrow.SetOnMouseOutOverImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_TOP_NOR, g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_TOP_OVER);


    this.BottomArrow= new Label("license_bottom_arrow", null);
    this.BottomArrow.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_BOTTOM_NOR);
    this.BottomArrow.SetOnClickCallbackFunc(clickCF);
    this.BottomArrow.SetOnMouseOverCallbackFunc(overCF);
    this.BottomArrow.SetOnMouseOutCallbackFunc(outCF);
    // this.BottomArrow.SetOnMouseOutOverImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_BOTTOM_NOR, g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_BOTTOM_OVER);	    
    

}
SceneLicensePopup.prototype.initPopup = function(){
	alert("SceneLicensePopup.initPopup() ");
	this.hiddenArrows();
}
SceneLicensePopup.prototype.goToReadPage = function(index){
	alert("SceneLicensePopup.goToReadPage() ");
	
	this.isReadMode = true;
	var _THIS_=sf.scene.get('LicensePopup');
	
	this.ContentsAnchor = document.getElementById("LicensePopupAgreementAnchor");	
    this.ContentsAnchor.onkeyup = function(){
            _THIS_.keyup();
    };
	this.ContentsAnchor.onkeydown = function(){
            _THIS_.keydown();
    };
    
    this.ContentsAnchor.onblur = function(){
            _THIS_.anchorblur();
    };
    
    this.ContentsDiv = document.getElementById("license_read_contents");
   	this.ContentsDiv.onmousewheel = function(){
            _THIS_.mousewheel();
    };
	
	$("div#license_read_contents p:first").html(""); //init
	
	
	this.ReadPage.visibleObj();
	
	this.ContentsAnchor.focus();
	
	setTimeout("CloseTimerCF()", 0);
	CloseTimerCF = function(){ 
		var contents ="";
		var lan_code = ResMgr.GetCurLicenseLanguageCode();
		
		var THIS_=sf.scene.get('LicensePopup');
		
		if(index==1)
		{
			 THIS_.LicenseViewBG.SetTitleTextofPopupBG(TV_SID_SKYPE_END_USER_LICENSE_AGREEMENT);
			THIS_.LicenseViewBG.CreateInnerBox("LicensePopup_popup_innerbox");
			 if(lan_code.length >0)
			 {
			 	//2013 modify by minseok
			 	var contents= sf.core.readFile("resource/eula_"+lan_code+".html");
			 }
			 else
			 {
			 	var contents= sf.core.readFile("resource/eula.html");		 	
			 }
			 
		}
		else if(index==2)
		{
			THIS_.LicenseViewBG.SetTitleTextofPopupBG(TV_SID_VIEW_TERMS_KR_CAPITAL);
			THIS_.LicenseViewBG.CreateInnerBox("LicensePopup_popup_innerbox");
			
			if (lan_code.length > 0) 
			{
				var contents= sf.core.readFile("resource/terms_"+lan_code+".html");
					
			}
			else
			{
				var contents= sf.core.readFile("resource/terms.html");
				 
			}
			
		}
		else if(index==3)
		{
			THIS_.LicenseViewBG.SetTitleTextofPopupBG(TV_VIEW_PRIVACY_STATEMENT);
			THIS_.LicenseViewBG.CreateInnerBox("LicensePopup_popup_innerbox");
			
			
			if (lan_code.length > 0)
			{
				var contents = sf.core.readFile("resource/privacy_"+lan_code+".html");
			}
			else
			{
				var contents = sf.core.readFile("resource/privacy.html");
					
			}
		}
		
		$("div#license_read_contents p:first").html(contents);
		
		THIS_.ReadContents = document.getElementById("license_read_contents");
		THIS_.ReadContents.scrollTop = 0;

		THIS_.updateScrollBar();
	}
}

SceneLicensePopup.prototype.anchorblur = function(){
	alert("SceneLicensePopup.anchorblur() ");
	
	if(this.isReadMode == true)
	{
		this.ContentsAnchor = document.getElementById("LicensePopupAgreementAnchor");
		this.ContentsAnchor.focus();	
	}
}

SceneLicensePopup.prototype.scrollUp = function(offset){
	alert("SceneLicensePopup.scrollUp() ");
	var scrollTop = this.ReadContents.scrollTop;
	var scrollHeight = this.ReadContents.scrollHeight;
	var offsetHeight = this.ReadContents.offsetHeight;
	if(offset == undefined){
		alert("offset is undefined!! ");
		offset = 40;
	}
	alert("offset : "+offset)
	if(scrollTop<=0)
	{
		alert("scrollTop==0");
		return;
	}
	else
	{
		if(scrollTop>=offset)
		{
			this.ReadContents.scrollTop-=offset;
		}
		else
		{
			this.ReadContents.scrollTop-=scrollTop;
		}
	}
	
	this.updateScrollBar();
}

SceneLicensePopup.prototype.scrollDown = function(offset){
	alert("SceneLicensePopup.scrollDown() ");
	
	var scrollTop = this.ReadContents.scrollTop;
	var scrollHeight = this.ReadContents.scrollHeight;
	var offsetHeight = this.ReadContents.offsetHeight;
	alert("offset : "+offset)
	if(offset == undefined){
		alert("offset is undefined!! ");
		offset = 40;
	}
	alert("offset : "+offset)
	if(scrollHeight<=scrollTop+offsetHeight)
	{
		alert("scrollHeight>=scrollTop+offsetHeight");
		return;
	}
	else
	{
		under_offset=scrollHeight-scrollTop+offsetHeight;
		if(under_offset>=offset)
		{
			this.ReadContents.scrollTop+=offset;
		}
		else
		{
			this.ReadContents.scrollTop+=under_offset;
		}
	}
	
	this.updateScrollBar();	
}

SceneLicensePopup.prototype.updateScrollBar = function(){
	alert("SceneLicensePopup.updateScrollBar() ");
	
	var scrollTop = this.ReadContents.scrollTop;
	var scrollHeight = this.ReadContents.scrollHeight;
	var offsetHeight = this.ReadContents.offsetHeight;
	
	alert("scrollTop " + scrollTop);
	alert("scrollHeight " + scrollHeight);
	alert("offsetHeight " + offsetHeight);
	
	if (scrollHeight <= offsetHeight) 
	{
		this.hiddenScrollBar();
	}
	else 
	{
		var total_item = scrollHeight;
		var displayed_item = offsetHeight;
		var max_scroll_length = 163;
		var init_scroll_start_position = 113;
		var curr_scroll_start_position = init_scroll_start_position;
		var curr_scroll_length=0;  
		
		var upper_hidden_item = scrollTop;
		var under_hidden_item = scrollHeight-(scrollTop+offsetHeight);
		
		var scroll_length = Math.floor((max_scroll_length *displayed_item)/total_item);
		var upper_hidden_length = Math.floor((max_scroll_length *upper_hidden_item)/total_item);
		var under_hidden_length = Math.floor((max_scroll_length *under_hidden_item)/total_item);
     
		if(scroll_length < 1)
		{
			alert("scroll_length < 2");
			//2013 modify by minseok
			upper_hidden_length =  Math.floor(( 2 *upper_hidden_length)/scroll_length);
			under_hidden_length =  Math.floor(( 2 *under_hidden_length)/scroll_length);
			scroll_length =2;
			 
		}
     	
     
     	var extra_length =max_scroll_length - (scroll_length+upper_hidden_length+under_hidden_length);
      
		if(upper_hidden_length >0)
		{
			curr_scroll_start_position =  init_scroll_start_position + upper_hidden_length+ extra_length;
		}
		else
		{
			curr_scroll_start_position =  init_scroll_start_position + upper_hidden_length;
		}
      
		curr_scroll_length  =  scroll_length;
		
		var center_top = String(curr_scroll_start_position);
		var center_height = String(curr_scroll_length);
		
		//2013 modify by minseok
		var scroll_top = String(curr_scroll_start_position-4);
		var scroll_under = String(curr_scroll_start_position+curr_scroll_length);
		
		$("#license_scroll_middle").css("top",  center_top+"px");
		$("#license_scroll_middle").height(center_height+"px");
		
		$("#license_scroll_top").css("top",  scroll_top+"px");
		$("#license_scroll_bottom").css("top",  scroll_under+"px");
		
		alert("SceneLicensePopup.updateScrollBar() top: "+ scroll_top+" length : "+center_height+" bottom : " + scroll_under);
		alert("SceneLicensePopup.updateScrollBar() extra_length: "+ extra_length+" upper_hidden_length : "+upper_hidden_length+" under_hidden_length : " + under_hidden_length);
		this.visibleScrollBar();
		
		// this.toggleAllow();
	}	
}

SceneLicensePopup.prototype.toggleAllow = function()
{
	alert("SceneLicensePopup.toggleAllow() ");
	var scrollTop = this.ReadContents.scrollTop;
	var scrollHeight = this.ReadContents.scrollHeight;
	var offsetHeight = this.ReadContents.offsetHeight;

	if(scrollTop<=0)
	{
		this.TopArrow.hiddenObj();		
	}
	else
	{
		this.TopArrow.visibleObj();
	}
	
	if(scrollHeight<=scrollTop+offsetHeight)
	{
		this.BottomArrow.hiddenObj();
	}
	else
	{
		this.BottomArrow.visibleObj();
	}	
}

SceneLicensePopup.prototype.hiddenScrollBar = function()
{
    this.ScrollTop.hiddenObj();
    this.ScrollUnder.hiddenObj();
    this.ScrollCenter.hiddenObj();
    
    this.TopArrow.hiddenObj();
    this.BottomArrow.hiddenObj();
}
SceneLicensePopup.prototype.visibleScrollBar = function()
{
    this.ScrollTop.visibleObj();
    this.ScrollUnder.visibleObj();
    this.ScrollCenter.visibleObj();
}
//2013 add by minseok
SceneLicensePopup.prototype.visibleArrows = function(obj){
	switch(obj){
		case this.TopArrow:
			alert("toparrow over!!!!!!!!!!!!!!!");
			this.TopArrow.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_TOP_OVER);
			this.BottomArrow.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_BOTTOM_NOR);	
			break;
		case this.BottomArrow:
			alert("bottomarrow over!!!!!!!!!!!!!!!");
			this.TopArrow.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_TOP_NOR);
			this.BottomArrow.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_BOTTOM_OVER);
			break;
		default:
			this.TopArrow.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_TOP_NOR);
			this.BottomArrow.SetBgImage(g_Image.IMAGE_CREATE_ACCOUNT_AGREEMENT_ALLOW_BOTTOM_NOR);
			break;
	}
	this.TopArrow.visibleObj();
    this.BottomArrow.visibleObj();
}
SceneLicensePopup.prototype.hiddenArrows = function(){
	this.TopArrow.SetBgImage(" ");
	this.BottomArrow.SetBgImage(" ");
	// this.TopArrow.hiddenObj();
    // this.BottomArrow.hiddenObj();
}

SceneLicensePopup.prototype.keydown = function(){
	var keycode = event.keyCode;
	alert("SceneLicensePopup.keydown() -> key code :" + keycode);
	var ridgeDirection = Math.round((keycode - (5*10000000)) / 100000);
	
	if(ridgeDirection == 1) { // Ridge flick : Up
		if(ProcMgr.GetRidgeFlick() == false) {
			ProcMgr.StartRidgeTimer();
			alert("keydown - ridge up!!!");
			this.scrollUp(80);
		}
	}
	else if(ridgeDirection == 2) { // Ridge flick : Down
		if(ProcMgr.GetRidgeFlick() == false) {
			ProcMgr.StartRidgeTimer();
			alert("keydown - ridge down!!!");
			this.scrollDown(80);
		}
	}
	switch (keycode) {
	case sf.key.DOWN:
		if(this.KeyDown==false)
		{
			this.KeyDown=true;
		}
		else{
			this.updateScrollBar();
		}
		
        break;
    case sf.key.UP:
		if(this.KeyDown==false)
		{
			this.KeyDown=true;
		}
		else{
			this.updateScrollBar();
		}
		
        break;
	case sf.key.ENTER:
		this.processReturnKey();
		sf.scene.returnFocus();
		break;
	case sf.key.RETURN:
    	this.processReturnKey();
		sf.scene.returnFocus();
		//2013 modify by minseok
        sf.key.preventDefault();
        //sf.key.block(event);
        break;
	}
}

SceneLicensePopup.prototype.keyup = function(){
	var keycode = event.keyCode;
	alert("SceneLicensePopup.keyup() -> key code :" + keycode);
	switch (keycode) {
	case sf.key.DOWN:
		this.KeyDown=false;
		this.updateScrollBar();
        break;
    case sf.key.UP:
		this.KeyDown=false;
		this.updateScrollBar();
        break;
	}
}

SceneLicensePopup.prototype.mousewheel = function(){
	alert("SceneLicensePopup.mousewheel()");
	
	this.updateScrollBar();	
}

SceneLicensePopup.prototype.highlight=function(obj)
{
	alert("SceneLicensePopup.highlight()");
	
    this.LicenseView.setCurrenHighlight(obj);
}


SceneLicensePopup.prototype.processReturnKey = function(){
    alert("SceneLicensePopup.processReturnKey() ");
    ProcMgr.CloseLicensePopup();
}


///////// Processing Click evnet  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////


SceneLicensePopup.prototype.processClickEvent = function(obj){
	alert("SceneLicensePopup.processClickEvent() ");
	
	if(obj == this.TopArrow)
	{
		this.scrollUp();
		return;
	}
	else if(obj == this.BottomArrow)
	{
		this.scrollDown();
		return;
	}
	
	var currentHighlight =  this.LicenseView.getCurrentHighlight();
    if(currentHighlight != obj)
    {
    	this.highlight(obj);
    }
	
	this.processReturnKey();
	//DF140108-01092 -> when popup close with mouse click, scene cannot get focus
	sf.scene.returnFocus();
}
SceneLicensePopup.prototype.processMouseOverEvent = function(obj){
	alert("SceneLicensePopup.processMouseOverEvent() ");
	this.visibleArrows(obj);
}
SceneLicensePopup.prototype.processMouseOutEvent = function(obj){
	alert("SceneLicensePopup.processMouseOutEvent() ");
	this.hiddenArrows();
}