function cellActionSub(obj){
    this.childNodesTable  = {
        'fh_diary':{
            'Center':[
                {'id':'','class':'dparts diaryDetailBox'},
                {'id':'commentList','class':'dparts commentList'},
                {'id':'','class':'parts prevNextLinkLine'},
                {'id':'commentForm','class':'dparts formTable'}
            ]
        },
        'h_diary_list_all':{
            'Center':[
                {'id':'','class':''},
                {'id':'','class':''},
                {'id':'','class':''}
            ]
        }
    };
    
    this.getPneActionName = function(){
        document.URL.match(/[&|?]a=page_([0-9A-z_]+).*$/);
        return RegExp.$1;
    };

    this.isPneOrigianlParts = function(obj,id,className){
        obj_id = obj.id;
        if(obj_id == null){
            obj_id = '';
        }
        var obj_className = new String('');
        obj_className = obj.getAttribute('className') ? obj.getAttribute('className') : obj.getAttribute('class');
        if(obj_className == null){
            obj_className = '';
        }
        retVal = false;
        if(obj_id==id && obj_className == className){
            retVal = true;
        }
        return retVal;
    }
/***********************************************
*@description TextNodeを削除して、不足しているDIVを追加
***********************************************/
    this.setDummyRemoveTextNode = function(obj){
        for(var i=obj.childNodes['length']-1 ; i>=0;i--){
            if(obj.childNodes[i].nodeType == 3){
                obj.removeChild(obj.childNodes[i]);
            }
        }
    }
    this.setDummyDivFlag = function(obj){
        obj.flagSetDummyDiv = true;
//disp_denug_info('setDummyDivFlag:'+obj.flagSetDummyDiv);
    }
    this.isSetDummyDivFlag = function(obj){
//disp_denug_info('isSetDummyDivFlag:'+obj.flagSetDummyDiv);
        return obj.flagSetDummyDiv;
    }
    this.setDummyDiv = function(obj){
        if(this.isSetDummyDivFlag(obj) == true){
//dummy inserted
        }else{
//TextElement（つまり、改行コード）を削除
            this.setDummyRemoveTextNode(obj);

            var ap = this.childNodesTable[this.getPneActionName(obj)][obj.id];
            target = obj.lastChild;
            for(var i = ap.length - 1; i>=0;i--){
                flag = false;
                while(target){
                    flag = this.isPneOrigianlParts(target,ap[i]['id'],ap[i]['class']);
                    if(flag){
                        ap[i]['nodePos']=target;
                        break;
                    }
                    target = target.previousSibling;
                }
                if(!flag){
                    ap[i]['nodePos'] = document.createElement('DIV');
                    ap[i]['nodePos'].innerHTML='';
//                    ap[i]['nodePos'].style.cssText='padding: 5px; border: 1px solid #000000; margin-bottom: 5px;';
                    if(i == (ap.length -1)){
                        obj.appendChild(ap[i]['nodePos']);
                    }else{
                        obj.insertBefore(ap[i]['nodePos'],ap[i+1]['nodePos']);
                    }
                    target=ap[i]['nodePos']
                }
                target = target.previousSibling;
            }
        }
        this.setDummyDivFlag(obj);
    }

/**********************************************
*
*
*@description 
**********************************************/
    this.setCellFramePos = function(params){
        var buf = new String('');
        var n=0;
        for(var key in params){
            if(typeof(params[key]) == 'number'){
                if(params[key] !== 0){
                    buf+= (n ? ',' : '') + key + ':' + params[key];
                    n++;
                }
            }
        }
        if(buf=='') return;
        new Ajax.Request(
            'api.php/config/cell_frame_pos/',
            {
                'method':'post',
//                'parameters':retVal,
                'postBody':encodeURI('cell_frame_move_list='+buf),
                onSuccess: function(request) { 
                    var json; 
                    eval("json="+request.responseText);
                }
            }
        );
        
    }
/*******************************************************
@name upObj
@param target 上方へ移動させるオブジェクト
@description <div></div>の中でオブジェクトを上方へ移動させる
*********************************************************/
    this.upObj = function(target){
        var retVal = new Array({});
        var rel=0;
        var parent = target.parentNode;
        var actPneName = this.getPneActionName();
        if(parent.firstChild == target){
            alert('それ以上、上には移動できません');
        }else{
            var tmp = target;
            do {
                tmp = tmp.previousSibling;
                rel++;
                if(tmp.innerHTML != ''){
                    break;
                }
            }while(parent.firstChild != tmp);
            if(tmp.innerHTML != ''){
                retVal[actPneName+'.'+parent.id+'.'+tmp.id]=-1;
            }
            parent.insertBefore(target.cloneNode(true),tmp);
            parent.removeChild(target);
        }
        retVal[actPneName+'.'+parent.id+'.'+target.id]=rel;
        return retVal;
    }
    
    this.downObj = function(target){
        var parent = target.parentNode;
        var retVal = new Array({});
        var rel=0;
        var actPneName = this.getPneActionName();
        if(target == parent.lastChild){
            alert('これ以上、下に下がりません');
        }else{
            var tmp = target;
            do{
                tmp = tmp.nextSibling;
                rel--;
                if(tmp.innerHTML !='' ){
                    break;
                }
            }while(parent.lastChild != tmp)
            if(tmp.innerHTML != ''){
                retVal[actPneName+'.'+parent.id+'.'+tmp.id]=1;
            }
            if(tmp == parent.lastChild){
                parent.appendChild(target.cloneNode(true));
            }else{
                parent.insertBefore(target.cloneNode(true),tmp.nextSibling);
            }
            parent.removeChild(target);
            retVal[actPneName+'.'+parent.id+'.'+target.id]=rel;
        }
        return retVal;
    }
    this.plusObj = function(obj){
    }
    this.minusObj = function(obj){
        var target = obj.firstChild;
        target.removeChild(target.firstChild.nextSibling);
    }
    this.setDummyDiv(obj.parentNode);
}


/************************************
*@name cellAction(obj)
*@params obj
*@description セルのUp/Downを扱うクラス
************************************/
function cellAction(obj){
    this.getClassName = function(obj){
        var retVal = new String();
        if(obj.className){
            retVal = obj.className;
        }else{
            retVal = obj.getAttribute('class');
        }
        return retVal;
    }
/************************************
*@name 
*@description classNameをclassにセットされている、elmを親ノード方向へ探していく
*************************************/
    this.searchParts = function(obj00,elm,className){
        var retVal = obj00;
        while( retVal.tagName != 'BODY' ){
            if(retVal.tagName==elm && this.getClassName(retVal) == className ){
                break;
            }else{
                retVal=retVal.parentNode;
            }
        }
        return retVal;
    }
    var act = obj.getAttribute('alt');
    var target = this.searchParts(obj,'DIV','dparts');
    var target_id = target.getAttribute('id');
    var ca = new cellActionSub(target);
//    var parent = target.parentNode ;
    target = document.getElementById(target_id);
    var posInfo=new Array({});
    switch(act){
        case 'up':
            posInfo = ca.upObj(target);
            ca.setCellFramePos(posInfo);
            break;
        case 'down':
            posInfo = ca.downObj(target);
            ca.setCellFramePos(posInfo);
            break;
         case 'plus':
            ca.plusObj(target);
            break;
         case 'minus':
            ca.minusObj(target);
            break;
        default:
            disp_debug_info(this.getAttribute('alt'));
            break;
    }
  
}

function cellFrame(){
    this.getTargetIdFromURL = function(str){
        var retVal=0;
        if(!str){
            str='target_c_diary_id';
        } 
        var work = new RegExp('[/?&]'+str+'=([0-9]+)');
//        if(document.URL.match(/[/?&]target_c_diary_id=([0-9]+)/)){
        if(document.URL.match(work)){
            retVal=RegExp.$1;
        }
        return retVal;
    }
    this.createElementWithClass = function (insertTo,elementName,className){
        return this.createElementWithAttribute(insertTo,elementName,{'class':className});
    }
    this.createElementWithAttribute = function (insertTo,elementName,attr){
        var work = new String('');
        if(attr){
            for(key in attr){
                if(typeof( attr[key] ) == 'string'){
                    work += ' ' + key + '="' + attr[key]+'"';
                }
            }
        }
        switch(elementName.toUpperCase()){
            case 'AREA':
            case 'INPUT':
            case 'IMG':
                insertTo.innerHTML += '<'+elementName + work +'/>';
                retVal = insertTo.lastChild;
                break;
            case 'A':
            case 'DD':
            case 'DIV':
            case 'DL':
            case 'DT':
            case 'FORM':
            case 'H1':
            case 'H2':
            case 'H3':
            case 'H4':
            case 'H5':
            case 'LI':
            case 'MAP':
            case 'OL':
            case 'P':
            case 'UL':
                insertTo.innerHTML += '<'+elementName + work +'></'+elementName+'>';
                retVal = insertTo.lastChild;
                break;                
            default:
                retVal = document.createElement(elementName);
                if(retVal){
                    if(attr){
                        for(var key in attr){
                            if(typeof( attr[key] ) == 'string'){
                            switch(key){
                                case 'class':
                                    retVal.setAttribute('className',attr[key]);
                                    retVal.setAttribute('class',attr[key]);
                                    break;
                                case 'style':
                                    retVal.style.cssText = attr[key];
                                    break;
                                default:
                                    retVal.setAttribute(key,attr[key]);
                                    break;
                            }
                        }
                    }
                }
                insertTo.appendChild(retVal);
            }
        }
        return retVal;
    }
    this.createHeader = function (insertTo,title,event_func){
        var retVal = this.createElementWithClass(insertTo,'DIV','partsHeading');
        var h3 = this.createElementWithClass(retVal,'H3','withButton');
        h3.innerHTML = title;
        var p = this.createElementWithClass(retVal,'P','withButton');
        var button = new Array();
        button=[
             {'name':'up','func':'cellAction(this)'}
            ,{'name':'down','func':'cellAction(this)'}
        ];
        if(event_func){
            for(var i=0 ; i<event_func.length;i++){
                button.push(event_func[i]);
            }
        }
        var params = new Array();
        for(var i=0;i<button.length;i++){
            params['src'] = 'plugins/lib/img/'+button[i]['name']+'.gif';
            params['alt'] = button[i]['name'];
            params['onClick'] = button[i]['func'];
            button[i]['img'] = this.createElementWithAttribute(p,'IMG',params);
        }
        return retVal;
    }
    this.createFrame = function(insertTo,id,title,button_func){
        var cas = new cellActionSub(insertTo.firstChild);
        var div = this.createElementWithAttribute(insertTo,'DIV',{'class':'dparts','id':id});
        var retVal = this.createElementWithClass(div,'DIV','parts');
        this.createHeader(retVal,title,button_func);
        return retVal;
    }
}
