// vim: fenc=utf8 :

function cookie_opts() {
    return {
        domain: '.quantv.com',
        path: '/',
        secure: true,
        expires: 30
    };
}


$(function(){
    // autologin
    var s = $.cookie('autologin');
    try {
        if (s && s.length > 1) {
            var a = s.split('|');
            if (a.length < 2) return;
            var username = a[0].replace(/'|"/g, '');
            var autologin_key = a[1].replace(/'|"/g, '');
            var href = location.href;
            var url_ = '/members/autologin/?';
            url_ += 'username='+username;
            url_ += '&key='+autologin_key;
            url_ += '&next='+href;
            location.href = url_;
        }
    } catch (exc) {
        ;
    }
});



/**
 * autologin, save_username등을 적용할 로그인폼에 대해서 이 함수를 적용해주삼.
 *
 * opts = {
 *  form: ...,          // html form to submit when autologin triggered.
 *  username: ...,      // html element of 'username' entry.
 *  password: ...,      // html element of 'password' entry.
 *  check_save_username: ...,       // html element of checkbox for 'save username' option.
 *  check_autologin: ...            // html element of checkbox for 'autologin' option.
 * }
 */
$.autologin = function(opts) {
    var e_form = opts['form'];
    var e_un = opts['username'];
    var e_su = opts['check_save_username'];
    var e_al = opts['check_autologin'];

    // 저장된 save_username 상태가 있다면 이를 적용
    var saved_username = $.cookie('saved_username');
    if (saved_username) {
        e_un.val(saved_username);
        e_su.attr('checked', true);
    } else {
        e_su.attr('checked', false);
    }

    // autologin checker에 대해서 경고 콜백
    e_al.click(function(){
        var msg = "";
        msg += "**경고**: \n";
        msg += "자동로그인 기능을 사용하시겠습니까?\n";
        msg += "이는 보안 문제를 야기할 수 있습니다.\n";
        msg += "다른 사람과 함께 컴퓨터를 사용하는 경우에 위험할 수 있습니다.\n";
        msg += "정말 사용하시겠습니까?\n";
        if (this.checked) {
            if (confirm(msg)) {
                ;
            } else {
                this.checked = false;
            }
        }
    });
    
    // 폼 submit 콜백에 대해서 save_username 콜백
    e_form.submit(function(){
        if (e_su.attr('checked')) {
            v = e_un.val();
            $.cookie('saved_username', v, cookie_opts());
        } else {
            $.cookie('saved_username', null, cookie_opts());
        }
    });


};



// EOF
