Ozein.Memo = new Class.create();
Ozein.Memo.prototype = {
    _defaultMsg: "記入例：利回り良好。立地条件が良く空室対策に有効。",
    _nowMemoValue: "",
    _winboxNode: null,
    initialize: function(node, options) {
        this.setOptions(options);
        this.node = node;
        this._winboxNode = this.options.winboxNode;

        var memoElement = document.getElementsByAttribute('ozein_memo_now', '*', this._winboxNode);
        if (memoElement.length == 0) {
            this._nowMemoValue = "";
        }else{
            this._nowMemoValue = memoElement[0].getAttribute('ozein_memo_now');
        }
        var memoNode = document.getElementsByAttribute('ozein_memo', '*', this._winboxNode);
        var funcName;
        for (var i=0; i<memoNode.length; ++i) {
            funcName = 'setMemo' + memoNode[i].getAttribute('ozein_memo');
            if (this[funcName]) this[funcName](memoNode[i]);
        }
    },
    setOptions: function(options) {
        this.options = {
            bid: null,
            inputNode: null,
            dispNode: null,
            winboxNode: null
        }
        Object.extend(this.options, options || {});
    },
    setMemoClose: function(node) {
        Event.observe(node,'click',this.closeMemo.bindAsEventListener(this));
    },
    setMemoSave: function(node) {
        Event.observe(node,'click',this.saveMemo.bindAsEventListener(this,node));
    },
    setMemoCount: function(node) {
        Event.observe(node, 'keyup', this.memoCheck.bindAsEventListener(this, node));
    },
    memoCheck: function(e, node) {
        var chk = this.checkMemoCountSet(node);
        if(e) Event.stop(e);
    },
    checkMemoCount: function(node, chkFlg) {
        var memolen   = node.value.length;
        var memoCount = document.getElementsByAttribute('ozein_memo_count', '*', this._winboxNode);
        if ((memolen > memoCount[0].getAttribute('ozein_memo_count')) && (chkFlg)) {
            alert("メモは"+memoCount[0].getAttribute('ozein_memo_count')+"文字まで入力できます。");
            return false;
        }
        var ret = this.checkMemoCountSet(node);
        return ret;
    },
    checkMemoCountSet: function(node) {
        var memo      = node.value;
        var memolen   = memo.length;
        var memoCount = document.getElementsByAttribute('ozein_memo_count', '*', this._winboxNode);
        if (!memo){
            memoCount[0].innerHTML = 0;
            memoCount[0].style.color = "";
        } else {
            memoCount[0].innerHTML = memolen;
            if (memoCount[0].getAttribute('ozein_memo_count') < memolen) {
                memoCount[0].style.color = "red";
            }else{
                memoCount[0].style.color = "";
            }
        }
        return true;
    },
    saveMemo: function(e, node) {
        var val = "";
        if (this.options.inputNode.value != this._defaultMsg) {
            val = this.options.inputNode.value;
        }else{
            this.options.inputNode.value = "";
        }

        var ret = this.checkMemoCount(this.options.inputNode, true);
        if (ret == false){
            return false;
        }

        var baseUri = node.getAttribute('ozein_memo_url');
        new Ajax.Request(
            baseUri,
            {
                method: 'post',
                postBody: 'b='+this.options.bid+'&memo='+val,
                requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
                onSuccess: function(httpObj) {
                    if (val == "") {
                        this.options.dispNode.innerHTML = "";
                        this._nowMemoValue = "";
                        this.options.inputNode.value = this._defaultMsg;
                    }else{
                        this.options.dispNode.innerHTML = val.htmlspecialchars();
                        this._nowMemoValue = val;
                    }
                    this._winboxNode.close();
                }.bind(this)
            }
        );
    },
    closeMemo: function(e) {
        var memoCount = document.getElementsByAttribute('ozein_memo_count', '*', this._winboxNode);
        memoCount[0].style.color = "";
        this.options.inputNode.style.color = "";
        this._winboxNode.close();
        if ((this._nowMemoValue) && (this.options.inputNode.value != this._nowMemoValue) && (this.options.inputNode.value != this._defaultMsg)) {
            this.options.inputNode.value = this._nowMemoValue;
            var ret = this.checkMemoCount(this.options.inputNode, false);
        }else if ((this._nowMemoValue) && (this.options.inputNode.value == this._defaultMsg)) {
            this.options.inputNode.value = this._nowMemoValue;
            var ret = this.checkMemoCount(this.options.inputNode, false);
        }else if (!this._nowMemoValue) {
            this.options.inputNode.value = this._defaultMsg;
            this.options.inputNode.style.color = "gray";
            var memoCount = document.getElementsByAttribute('ozein_memo_count', '*', this._winboxNode);
            memoCount[0].innerHTML = 0;
        }
    }
};
String.prototype.htmlspecialchars = function() {
    var ch = this.replace(/&/g,"&amp;");
    ch = ch.replace(/&amp;amp;/g,"&amp;");
    ch = ch.replace(/"/g,"&quot;");
    ch = ch.replace(/'/g,"&#039;");
    ch = ch.replace(/</g,"&lt;");
    ch = ch.replace(/>/g,"&gt;");
    return ch;
};
