// Init
UI.moduleBuilder['FTE']['fte_turnoffDialog'] = function(){
    var turnoffDialogData = {
        blackBgImg: "setting/dialog_bg.png",
        bgImg: "FTE/fte_timer_dialog_glint.png",
        timerImg: "FTE/fte_ic_timer.png",
        //mainText: "[SE3072]",//We haven't received any input from you. You are about to enter Store Mode within 30 seconds.
        cancelText: "[SE3073]",//Press any button to cancel.
        barBgImg: "FTE/fte_timer_bar_bg.png"
    };
    var template = Util.readJson("module/FTE/fte_turnoffDialog.json");
    var turnoffDialog = SubModule.create(turnoffDialogData, template);

    turnoffDialog.pageData = {
        "storeModeTimer": 0,
        "barTimer": 0,
        "flag" : 0//0:选择商场模式 ；1：CEC保护关机
    };

    var cancelText = UI.getItem(turnoffDialog, "cancelText");
    var progressBar = UI.getItem(turnoffDialog, "progressBar");
    var barBgImg = UI.getItem(turnoffDialog, "barBgImg");
    var barImg = UI.getItem(turnoffDialog, "barImg");
    var mainText = UI.getItem(turnoffDialog, "mainText");
    var mainText1 = "[SE3072]";////We haven't received any input from you. You are about to enter Store Mode within 30 seconds.
    var mainText2 = "[SE3242]";//We haven’t received any input from you. The TV will shut down within 30 seconds. Press any button to cancel.

    UI.addMask(progressBar, 612, 10, 0, 0);
    UI.setView(progressBar, 612, 10);

    turnoffDialog.currFocus = cancelText;

//})();

    /*--------------------------------Actions----------------------------------------*/
//(function(){

    turnoffDialog.onPush = function() {
        /*如果软键盘打开，将软键盘关闭*/
        if (UI.isSoftKeyBoard) {
            host.keyboard.onKeyDown({which : 27});
            UI.isSoftKeyBoard = false;
        }
        UI.setText(mainText,mainText2 );
        /*turnoffDialog.pageData.flag = FTE.flag;
        if(turnoffDialog.pageData.flag == 0){
            UI.setText(mainText, mainText1);
        }else{
            UI.setText(mainText,mainText2 );
        }*/
//        UI.clearFteMenuTimeout();
        UI.registerKeyCodes(null);
        UI.setKeyGroup(7);
    };
    turnoffDialog.onPop = function() {
        clearTimeout(turnoffDialog.pageData.storeModeTimer);
        clearInterval(turnoffDialog.pageData.barTimer);
        UI.registerKeyCodes(FTE.enableKeyGroup);
        UI.setKeyGroup(7);
    };
    turnoffDialog.onShow = function() {
        //30秒定时器进入商场模式
        DBG_INFO("turnoffDialog.onShow: current page is turnoffDialog.");
        DBG_INFO("FTE will exit and enter store mode after 30 second if there is any action");
//        DBG_INFO("progressBar.x is " + progressBar.x);
        progressBar.x = 912;    //重置位置
        UI.getTween(progressBar).to({x: progressBar.x + 612}, 30000, UI.ease.quadIn);
        turnoffDialog.pageData.storeModeTimer = setTimeout(function() {
            /*if(turnoffDialog.pageData.flag == 0){
                DBG_INFO("turnoffDialog.pageData.flag == 0");
                setCurrSignalInput();   //切到有信号的通道
                setTimeout(function() {
                    FTE.isOpenLiveTV = false;
                    FTE.isResumeDtv = false;
                    FTE.fteExit(null, true);
                    //进入商场模式 4K demo
                    try{
                        model.system.setUserMode(2);
                    }catch(e){
                        DBG_ERROR(e.message);
                    }
//            Epos_play4kVideo(true, "fte_storeDemo");
                    UI.setEposMenuTimeout(startRetailmodeTimer,retailmodeTimeout);
                    startePos(true);
                }, 100);
            }else{*/
                DBG_INFO("turnoffDialog.pageData.flag == 1");
                if(tv){
                    try {
                        setTimeout(function() {
                            switchOffTV(1);
                        }, 200);
                    }
                    catch (ex) {
                        DBG_ERROR("after fte_userMode TV power off" + ex.message);
                    }
                }
            //}
        }, 1000 * 30);
    };
    turnoffDialog.onExit = function() {
        clearTimeout(turnoffDialog.pageData.storeModeTimer);
        clearInterval(turnoffDialog.pageData.barTimer);
//        FTE.setTimeout();
    };

    turnoffDialog.onFocus = function() {
        UI.base.cursor.alpha = 0;
        UI.updateStage();
    };
    turnoffDialog.onNormal = function() {
        UI.base.cursor.alpha = 1;
    };


    //任意键退出dialog
    /*turnoffDialog.onkey = function() {
     UI.popSubModule();
     };*/
    turnoffDialog.onkey = function() {
        UI.popSubModule();
    };

    function setCurrSignalInput(){
        var defSource = 'HDMI1';
        var SignalInputId = 0;
        var SignalInputItem = [];
        var SignalInputIditem = [];
        var InputSeq = ["HDMI1","HDMI2","HDMI3","HDMI4","ANT/Cable","AV","COMPONENT"];
        var j = 0;
        var sourceItem =  [
            //id, name, signal, lock, rename
            "0", "ANT/Cable", "0", "1", "", "",
            "1", "AV", "1", "1", "", "",
            "2", "COMPONENT", "0", "0", "ComponentComponent", "",
            "3", "HDMI1", "1", "0", "", "",
            "4", "HDMI2", "0", "0", "", "",
            "5", "HDMI3", "1", "0", "", "",
            "6", "HDMI4", "1", "0", "", ""
        ];
        if (tv) {
            sourceItem = model.source.getInputName();
        }
        for(var id=0;id<sourceItem.length/6;id++)
        {
            if(defSource == sourceItem[id*6+1].replace(" ", ""))
            {
                SignalInputId = id;
                break;
            }
        }
        for(var i=0;i<sourceItem.length/6;i++){
            if(sourceItem[i*6+2] == 0) {
                if (i == 0) {   //TV时的判断
                    var ret = typeof (liveTV) != "undefined" && liveTV.getHasChannelFlag();
                    var TVSignal = tv ? ret : true;
                    if (TVSignal) {
                        SignalInputItem[j] = sourceItem[i*6+1].replace(" ", "");
                        SignalInputIditem[j] = i;
                        j++;
                    }
                }
                else
                {
                    SignalInputItem[j] = sourceItem[i*6+1].replace(" ", "");
                    SignalInputIditem[j] = i;
                    j++;
                }
            }
        }
        if(SignalInputItem.length>0)
        {
            for(var k=0;k<InputSeq.length;k++)
            {
                for(var n = 0;n<SignalInputItem.length;n++)
                {
                    if(SignalInputItem[n].toLowerCase() == InputSeq[k].toLowerCase())
                    {
                        SignalInputId = SignalInputIditem[n];
                        k = InputSeq.length;
                        break;
                    }
                }

            }
        }

        DBG_INFO("setCurrSignalInput(): current signal input is " + SignalInputId);
        changeSourceTo(SignalInputId);
    }
};