/**
 * @author umkki
 * @since 2008.09.16
 * 이글루링크 팝업
 */

function openSpoonDlg(event, eid, blogurl, serial, cmtflag, menupost ) {
    if(serial == "0") return;

    var url     = 'http://www.egloos.com/spoon/popup_support.php';
    var param   = '?eid='+eid+'&serial='+serial+'&cmtflag='+cmtflag+'&menupost='+menupost;
    url         = url + param;

    popup = window.open(url, 'support', 'width=429, height=300');
    popup.focus();
}

var Spoon = Class.create();
Spoon.prototype = {
    DEFAULT_CMT: "후원하시면서 하고싶은 이야기를 적어주세요. 덧글영역에 표시됩니다. 비워두셔도 무관합니다.",

    initialize: function(options) {
        this.options = Object.extend({
            serial  : "",
            s_id    : "",
            s_nick  : "",
            b_url   : ""
        }, options || {});

        this.b_eid  = this.options.eid;
        this.b_key  = this.options.userid;
        this.b_url  = this.options.b_url;
        this.itemid = this.options.itemid;
        this.s_id   = this.options.s_id;
        this.s_nick = this.options.s_nick;

        var tmp     = this.itemid.split('_');
        this.serial = tmp[2];
        
        this.amount = 0;
        this.commented = false;
        this.scuccess = false;

    },

    trim: function(str) {
        return str.replace(/^\s+|\s+$/g,'');
    },

    setFocus: function(obj) {
        if(obj.value == this.DEFAULT_CMT) {
            obj.value = "";
        }
    },

    spoonSupport: function(return_url, loginYN) {
        this.amount= 0;
        var radio = document.getElementsByName("amount");
        for(var i = 0; i < radio.length; i++) {
            if(radio[i].checked) this.amount = radio[i].value;
        }
        if( this.amount == 0) {
            alert('금액을 선택해주세요.');
            return false;
        }

        var cmt_obj = $('comment');
        if(typeof(cmt_obj) != 'undefined' && cmt_obj != null ) {
            if( cmt_obj.value != "" && cmt_obj.value != this.DEFAULT_CMT ) {
                var cmtForm = $('spoonCmtForm');
                if(loginYN == 'N') {
                    if(cmtForm.name.value == "" || this.trim(cmtForm.name.value) == "") {
                        alert("이름을 입력해주세요.");
                        cmtForm.name.value = "";
                        cmtForm.name.focus();
                        return false;
                    }
                    if( this.s_nick == "" ) {
                        this.s_nick = cmtForm.name.value;
                    }

                    if(cmtForm.passwd && (cmtForm.passwd.value == "" || this.trim(cmtForm.passwd.value) == "") ) {
                        alert("비밀번호를 입력해주세요.\r\n덧글 수정 및 삭제시 필요합니다.");
                        cmtForm.passwd.value = "";
                        cmtForm.passwd.focus();
                        return false;
                    }

                    if(cmtForm.homepage && cmtForm.homepage.value != "" && cmtForm.homepage.value.search(/http\:\/\//gi) < 0 ) {
                        alert("올바른 홈페이지 주소가 아닙니다.\r\n다시 입력해주세요.");
                        cmtForm.homepage.focus();
                        return false;
                    }
                }
                this.commented = true;
            }
        }

        //window.open("/spoon/popup.php", "popup", "width=300, height=300");
        PTS.donate_point(this.b_key, this.b_url, this.itemid, this.amount, return_url);
    },

    saveComment: function(amount) {
        var cmt_obj = $('comment');
        if(typeof(cmt_obj) == 'undefined' || cmt_obj == null ) {
            return true;
        }

        if( cmt_obj.value == this.DEFAULT_CMT ) {
            return true;
        }
        
        //var icon_url    = "http://md.egloos.com/img/eg/spoon/icon_" + amount + ".gif";
        //var icon_html   = '<img src="'+ icon_url +'" width="43" hight="14" style="vertical-align:middle;">&nbsp;';
        var icon_tag    = "[SPOON" + amount + "]";
        cmt_obj.value   = icon_tag + cmt_obj.value;

        var cmtForm = $('spoonCmtForm');
        cmtForm.srl.value = this.serial;
        cmtForm.submit();
    },

    supportSuccess: function(b_key, itemid, transno, amount) {
        if(itemid != this.itemid ) {
            alert('기부정보가 올바르지 않습니다.\r\n관리자에게 문의해주세요.');
            return false;
        }

        if( this.amount != amount ) this.amount = amount;
        
        new Ajax.Request('http://www.egloos.com/spoon/support_save.php', {
            parameters: {
                'eid'           : this.b_eid,
                'account_key'   : this.b_key,
                'itemid'        : this.itemid,
                'transno'       : transno,
                'amount'        : this.amount,
                'supporter'     : this.s_id,
                'supporter_nick': this.s_nick,
                'type'          : 'post'
            },

            asynchronous: false,

            onSuccess: function(response) {
                var text = response.responseText;
                if(text.isJSON()) {
                    var resultArr = text.evalJSON();
                    if(resultArr.result != 1) {
                        alert(resultArr.message);
                        this.success = false;
                        //document.location.href = "http://www.egloos.com/spoon/popup_thankyou.php?eid="+this.b_eid+"&itemid="+this.itemid+"&serial="+this.serial;
                    } else {
                        this.success = true;
                        document.location.href = "http://www.egloos.com/spoon/popup_thankyou.php?eid="+this.b_eid+"&itemid="+this.itemid+"&serial="+this.serial;
                    }
                }
            }.bind(this),

            onFailure: function() {
                alert('일시적인 장애입니다. 관리자에 문의해주세요.');
            }
        });
        
        if( this.commented && this.success ) {
            this.saveComment(this.amount);
        }
    },

    supportFailed: function(b_key, itemid, transno, amount, msg) {
        alert(msg);
        return false;
    }
};

