/**
 * js - ican - utility.js
 * 
 * 便利メソッドを詰め込む。
 * 
 * @package    Ican
 * @subpackage javascript
 * @copyright  Befool,Inc.
 * @author     Satoshi Kiuchi <satoshi.kiuchi@befool.co.jp>
 */
/*====================
  初期化
====================*/
    ICAN.namespace('Utility');
    
    
/*====================
  関数定義
====================*/
    /**
     * include的なもの
     * @access     public
     * @param      string  path
     */
    ICAN.Utility.require = function(path)
    {
        for (var i = 0; i < JSAN.includePath.length; i++) {
            var source;
            try {
                var url = JSAN._convertPathToUrl(path, JSAN.includePath[i]);
                var dat = new Date();
                var time = parseInt(dat.getTime() / 1000);
                source = JSAN._loadJSFromUrl(url + '?' + time);
            } catch (e) {
                if (i == JSAN.includePath.length - 1) throw e;
            }
            if (source != null) {
                JSAN.globalScope.eval(source);
                break;
            }
        }
    };
    
    
    
    /**
     * print_rのような事をする
     * @access     public
     */
    ICAN.Utility.print_r = function(data, inline)
    {
        var result = new Array();
        var _type  = typeof(data);
        if(_type == 'array' || _type == 'object'){
            for(var i in data){
                try {
                    var _type = typeof(data[i]);
                    var _str  = i + ' = ';
                    if(_type == 'array' || _type == 'object' || _type == 'function'){
                        _str += '['+_type+']';
                    } else {
                        _str += ICAN.Utility.print_r(data[i], true);
                    }
                    result.push(_str);
                } catch(E) {
                    result.push(i+' = [エラー]');
                }
            }
        } else {
            result.push('['+_type+']'+data);
        }
        if(!inline){
            alert(result.join(",\r\n"));
        } else {
            return result.join(",\r\n");
        }
    }
    var print_r = ICAN.Utility.print_r;
    var var_dump = ICAN.Utility.print_r;
    
    
    /**
     * メッセージを指定の場所に、指定時間だけ表示
     * @access     public
     * @param      string  placeholder
     * @param      string  message
     * @param      int     sec
     */
    ICAN.Utility.flushMessage = function(placeholder, message, sec)
    {
        var init = { opacity : { from:1, to:0 } };
        var Anim = new YAHOO.util.Anim(placeholder, init, sec, YAHOO.util.Easing.backIn);
        $(placeholder).innerHTML = message;
        $(placeholder).style.display = '';
        Anim.onComplete.subscribe( function(){
            $(placeholder).removeNode(true);
        } );
        Anim.animate();
        //STYLE
        $(placeholder).className = 'flush_message';
        $(placeholder).style.margin  = '10px';
        $(placeholder).style.padding = '10px';
        $(placeholder).style.border  = '1px solid #5C87A2';
        $(placeholder).style.backgroundColor = '#E4EBEF';
        $(placeholder).style.textAlign = 'center';
    }
    
    
    
    
    
    /**
     * セレクションクラス
     * 選択された文字列などの置き換えを担当
     * @param      object  input
     */
    ICAN.Utility.textSelection = function(input)
    {
        this.input = input;
        if(this.input.selectionStart == undefined){
            this.selection = parent.document.selection.createRange();
        }
    }
    ICAN.Utility.textSelection.prototype = {
        /**
         * 選択された文字列を取得する
         */
        getSelectedText : function()
        {
            if(this.selection){
                return this.selection.text;
            } else {
                return this.input.value.substr(this.input.selectionStart, this.input.selectionEnd - this.input.selectionStart);
            }
        },
        
        /**
         * 選択中の文字列を別の文字で置き換える
         */
        replace : function(text)
        {
            if(this.selection){
                this.selection.text = text;
            } else {
                var pre  = this.input.value.substr(0, this.input.selectionStart);
                var post = this.input.value.substr(this.input.selectionEnd);
                this.input.value = pre + text + post;
            }
        },
        
        /**
         * 選択された状態かどうか
         * @access     public
         */
        isSelected : function()
        {
            return this.getSelectedText().length > 0;
        }
    }
    
    
    
    
    
    /**
     * フィールドの表示/非表示を切り替える
     * @access     public
     * @param      string  id
     */
    ICAN.Utility.swapZone = function(id)
    {
        if($(id).style.display == 'none' || !$(id).style.display){
            $(id).style.display = 'block';
        } else {
            $(id).style.display = 'none';
        }
        return false;
    }
    
    
    
    
    
    /**
     * TABLEの中のTBODYを抜き出す
     * @access     public
     */
    ICAN.Utility.TBODY = function(TABLE)
    {
        for(var i = 0; i < TABLE.childNodes.length; i++){
            var node = TABLE.childNodes.item(i);
            if(node.tagName && node.tagName.toUpperCase() == 'TBODY'){
                return node;
            }
        }
    }
    
    
    /**
     * Eventの伝播を止める
     * @access     public
     */
    ICAN.Utility.preventDefault = function(event)
    {
        if(event.preventDefault){
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    }
    
    
    /**
     * removeNode代用
     */
    ICAN.Utility.removeNode = function(node, with_child)
    {
        if(with_child){
            for(var i = 0; i < node.childNodes.length; i++){
                ICAN.Utility.removeNode(node.childNodes.item(i), with_child);
            }
        }
        node.parentNode.removeChild(node);
    }
