var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.itemInit();
        this.eventInit();
        this.otherInit();
    },
    itemInit:function() {
        $(".SignWrap").jSignature(); // 서명 플러그인
        //상단 표기 상태 변경
        cordova.plugins.finPlugin.getUseType("", function(res){
            if(0 < res.length){
                if('연동형' == res){
                    $("#hd_title").html("<a href='#' onclick='app.backbuttonAction();' id='btn_back' ><h3 class='btnPre'><span class='blind'>이전</span></h3></a><h3 class='title'>서명</h3><h3 id='battery' class='battery'><span></span></h3>");
                }else {

                }
                app.setSubTitle();
            }
        }, null);
    },
    eventInit:function(){

    },
    otherInit:function(){
        document.addEventListener("backbutton", app.backbuttonAction, false);
    },
    backbuttonAction:function(){
        var isPopupClose = false;

        if(!isPopupClose){
            cordova.plugins.finPlugin.backButton("", null, null);
        }
    },
    setBattery:function(battery){
        $('#battery').html(battery);
    },
    setSubTitle:function(){
        var data = {"sepa":"union_card"};
        cordova.plugins.finPlugin.getSignTitle(data,function(title){
            $('.title').html(title);
        },function(error){
            $('.title').html(title);
        });
    },
    reqConfirm:function(){
        if (isClickedButton()) {
            return;
        }
        // 서명한 캔버스 이미지로 변환
        var data = $(".SignWrap").jSignature('getData','image');
        var i = new Image();
        i.src = 'data:' + data[0] + ',' + data[1];
        $(i).appendTo(".imgArea");
        
        var jsonObject = new Object();
        jsonObject.format = data[0];
        jsonObject.value = data[1];
        var jsonData = JSON.stringify(jsonObject);
        
        
        
        cordova.plugins.finPlugin.setSignUnionData(jsonData, function(res){

        }, function(error){
            showToastMessage(error);
        });
    }
};

app.initialize();
