/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2008 Aimluck,Inc.
 * http://aipostyle.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

dojo.provide("aipo.calendar.weekly");

dojo.require("aimluck.dnd.Draggable");
dojo.require("aipo.widget.ToolTip");
dojo.require("aipo.widget.MemberNormalSelectList");
dojo.require("aipo.widget.GroupNormalSelectList");

aipo.calendar.objectlist = Array();

aipo.calendar.populateWeeklyOfficecalendar = function(_portletId, params) {
    var _params;
    var member_to = dojo.byId('member_to-' + _portletId);
    if (typeof params == "undefined" || typeof ptConfig[_portletId].jsonData == "undefined") {
       _params = "";
    } else {
       _params = params;
    }

    /* セキュリティIDを追加 */
    var secid = dojo.byId('secid-' + _portletId);
    if(secid){
       _params += "&secid=" + secid.value;
    }

    /*施設重複時はパラメータを追加しない*/
    if( _params.match(/ign_dup_f/) == null){
	     if(member_to) {
	       var t_o = member_to.options;
	       to_size = t_o.length;
	       if(to_size == 0){
	           _params += "&m_id=" + aipo.calendar.login_id;
	       }
	       for(i = 0 ; i < to_size; i++ ) {
	           t_o[i].selected = true;
	           _params += "&m_id=" + t_o[i].value;
	       }
	    }
	    var chk_all = dojo.byId('showAll-' + _portletId);
	    if(chk_all) { _params += "&s_all=" + chk_all.value;}
    }

    djConfig.usePlainJson=true;
    ptConfig[_portletId].reloadFunction = aipo.calendar.populateWeeklyOfficecalendar;

    ptConfig[_portletId].isTooltipEnable = false;
    if(aipo.calendar.dummyDivObj){
         aipo.calendar.dummyDivObj.destroy();
         aipo.calendar.dummyDivObj = null;
    }


    dojo.xhrGet({
        portletId: _portletId,
        url: ptConfig[_portletId].jsonUrl + _params,
        encoding: "utf-8",
        handleAs: "json-comment-filtered",
        load: function(data, event) {
            //権限チェック
            obj_error = dojo.byId('error-'+_portletId);
            dojo.style(obj_error, "display" , "none");
            if("PermissionError" == data[0]){
               dojo.style(obj_error, "display" , "block");
               obj_error.innerHTML = data[1];
               obj_content = dojo.byId('content-'+_portletId);
               dojo.style(obj_content,  "display" , "none");
               obj_indicator = dojo.byId('indicator-'+_portletId);
               dojo.style(obj_indicator, "display" , "none")
               return;
            } else if(data["errList"]){
               if("duplicate_facility" == data.errList[0]){
                    if(confirm('既に同じ時間帯に施設が予約されています。スケジュールを登録しますか？')) {
                        var new_param = _params + '&ign_dup_f=true'
                        aipo.calendar.populateWeeklyOfficecalendar(_portletId, new_param);
                        aipo.portletReload('officecalendar', _portletId);
                        return;
                    }
               }

               if("UpdateError" == data.errList[0]){
                     dojo.style(obj_error, "display" , "block");
                     obj_error.innerHTML = "<ul><li><span class=\"caution\">"+data.errList[1]+"</span></li></ul>";
                     obj_content = dojo.byId('content-'+_portletId);
                     dojo.style(obj_content, "visibility" , "visible");
                     obj_indicator = dojo.byId('indicator-'+_portletId);
                     dojo.style(obj_indicator, "display" , "none")
                }
            }
            //オブジェクト削除
            var i;
            if(!!aipo.calendar.objectlist){
                var o_size = aipo.calendar.objectlist.length;
                for(i = 0; i < o_size; i++){
                    var obj = aipo.calendar.objectlist[i];
                    if(obj.portletId == _portletId){
                        obj.destroy();
                    }
                }
            }

            if (!aipo.errorTreatment(data, ptConfig[_portletId].thisUrl)) {
                return;
            }
            ptConfig[_portletId].jsonData = data;
            var officecalendarDiv = Array(ptConfig[_portletId].officecalendarDivDaySum);
            for(var i=0;i<ptConfig[_portletId].officecalendarDivDaySum;i++) {
                officecalendarDiv[i] = Array();
            }
            var count = 0;
            var l_count = 0;
            var html = '';
            var termHtml = '';
            var termTableHtml = '';
            var tmpHeight = [];
            var tmpNode1, tmpNode2, tmpNode3, tmpNode4;
            var startEnd = data.startDate.substring(0,4) + " 年 " + data.startDate.substring(5,7) + " 月 " + data.startDate.substring(8,10) + " 日 " + data.dayOfWeek[0] + "～ " + data.endDate.substring(5,7) + " 月 " + data.endDate.substring(8,10) + " 日 " + data.dayOfWeek[6];
            dojo.byId('viewWeekly-' + _portletId).innerHTML = startEnd;

            termTableHtml += "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"><tbody>"
            dojo.forEach(data.termOfficecalendar, function(itemList) {
                var item = null;
                termTableHtml += '<tr><td width="50"><div class="weeklyTermLeft" id="weeklyTermLeft"><div class="weeklyTermLeftTop">&nbsp;</div></div></td><td nowrap="nowrap" width="100%" valign="top"><div class="weeklyTermRights">';
                termTableHtml += '<div class="weeklyTermRight weeklyTermRightL" id="termDay0-'+ l_count + '-' +_portletId+'" style="left: 0%;"><div class="weeklyTermRightTop">&nbsp;</div></div>';
                termTableHtml += '<div class="weeklyTermRight" id="termDay1-'+ l_count + '-' +_portletId+'" style="left: 14.2857%;"><div class="weeklyTermRightTop">&nbsp;</div></div>';
                termTableHtml += '<div class="weeklyTermRight" id="termDay2-'+ l_count + '-' +_portletId+'" style="left: 28.5714%;"><div class="weeklyTermRightTop">&nbsp;</div></div>';
                termTableHtml += '<div class="weeklyTermRight" id="termDay3-'+ l_count + '-' +_portletId+'" style="left: 42.8571%;"><div class="weeklyTermRightTop">&nbsp;</div></div>';
                termTableHtml += '<div class="weeklyTermRight" id="termDay4-'+ l_count + '-' +_portletId+'" style="left: 57.1429%;"><div class="weeklyTermRightTop">&nbsp;</div></div>';
                termTableHtml += '<div class="weeklyTermRight" id="termDay5-'+ l_count + '-' +_portletId+'" style="left: 71.4286%;"><div class="weeklyTermRightTop">&nbsp;</div></div>';
                termTableHtml += '<div class="weeklyTermRight weeklyTermRightR" id="termDay6-'+ l_count + '-' +_portletId+'" style="left: 85.7143%;"><div class="weeklyTermRightTop">&nbsp;</div></div>';
                termTableHtml += '<div id="termOfficecalendarItemGarage-' + l_count + '-' + _portletId + '" class="termOfficecalendarGarage"> </div>'
                termTableHtml += "</div></td><td width=\"18\"><div class=\"weeklyTermTail\">&nbsp;</div></td></tr>"
                l_count++;
            });
           termTableHtml += "</tbody></table>"
           dojo.byId('termOfficecalendarGarage-'+_portletId).innerHTML=termTableHtml;
           dojo.byId('termOfficecalendarDivAdd_' + _portletId).style.height = (18 * (l_count + 1)) + "px";

            for(var i = 0;i < ptConfig[_portletId].officecalendarDivDaySum; i++) {
               tmpNode1 = dojo.byId('weeklyDay' + i + '-' + _portletId);
               tmpNode2 = dojo.byId('weeklyHoliday' + i + '-' + _portletId);
               tmpNode3 = dojo.byId('weeklyRight' + i + '-' + _portletId);
               tmpNode4 = dojo.byId('termDay' + i + '-' + _portletId);
               tmpNode1.innerHTML = parseInt(data.date[i].substring(8,10),10) + data.dayOfWeek[i];
               tmpNode2.innerHTML = data.holiday[i];
               if (data.dayOfWeek[i] == "（土）") {
                   dojo.addClass(tmpNode1, "saturday");
                   dojo.addClass(tmpNode2, "saturday");
                   dojo.addClass(tmpNode3, "saturday");
                   dojo.addClass(tmpNode4, "saturday");
               } else {
                   dojo.removeClass(tmpNode1, "saturday");
                   dojo.removeClass(tmpNode2, "saturday");
                   dojo.removeClass(tmpNode3, "saturday");
                   dojo.removeClass(tmpNode4, "saturday");
               }
               if (data.dayOfWeek[i] == "（日）") {
                   dojo.addClass(tmpNode1, "sunday");
                   dojo.addClass(tmpNode2, "sunday");
                   dojo.addClass(tmpNode3, "sunday");
                   dojo.addClass(tmpNode4, "sunday");
               } else {
                   dojo.removeClass(tmpNode1, "sunday");
                   dojo.removeClass(tmpNode2, "sunday");
                   dojo.removeClass(tmpNode3, "sunday");
                   dojo.removeClass(tmpNode4, "sunday");
               }
               if (data.holiday[i]) {
                   dojo.addClass(tmpNode1, "holiday");
                   dojo.addClass(tmpNode2, "holiday");
                   dojo.addClass(tmpNode3, "holiday");
                   dojo.addClass(tmpNode4, "holiday");
               } else {
                   dojo.removeClass(tmpNode1, "holiday");
                   dojo.removeClass(tmpNode2, "holiday");
                   dojo.removeClass(tmpNode3, "holiday");
                   dojo.removeClass(tmpNode4, "holiday");
               }
            }

            dojo.forEach(data.officecalendar, function(item) {
                var rowHeight = ptConfig[_portletId].rowHeight;
                var top = item.startDateHour * rowHeight * 2 + item.startDateMinute * rowHeight / 30;
                var height = item.endDateHour * rowHeight * 2 + item.endDateMinute * rowHeight / 30 - top;
                if(height <= rowHeight) {
                    tmpHeight[count] = height;
                    height = rowHeight;
                } else {
                    tmpHeight[count] = -1;
                }
                var left = 100 /  ptConfig[_portletId].officecalendarDivDaySum * item.index;
                var width = 100 / ptConfig[_portletId].officecalendarDivDaySum * 0.99;
                var name = item.name;
                var startDate = tmpHeight[count] == -1 ? ((item.startDateHour > 9) ? item.startDate : "0" +item.startDate) : item.name;
                var endDate = tmpHeight[count] == -1 ? ((item.endDateHour > 9) ? item.endDate : "0" +item.endDate) : '';
                var sepalater = tmpHeight[count] == -1 ? '-' : '';
                var officecalendarId = item.officecalendarId;

                var str_tmp = "0";
                var str_tmpflgmb = "";
                var member_to = dojo.byId('member_to-' + _portletId);
                if(member_to) {
                     var t_o = member_to.options;
                     for(i = 0 ; i < t_o.length; i++ ) {
                       if(((item.type == "U") && (item.ownerId == t_o[i].value)) || ((item.type == "F") && ("f" + item.ownerId == t_o[i].value))){
                           str_tmp = i %  aipo.calendar.maximum_to;
                       }
                       if(item.memberList){
                           var ucount = 0;
                           var fcount = 0;
                           for (j = 0 ; j < item.memberList.length ; j ++){
                               if(item.memberList[j].charAt(0) == 'f'){
                                  fcount++;
                               }else{
                                  ucount++;
                               }
                           }
                       }
                    }
                    var str_tmpflgmb;
                    if(ucount > 1){
                     str_tmpflgmb   =  "[共有]";
                    }
                    if(fcount > 0){
                     str_tmpflgmb   += "[施設]";
                    }

                }

                if(!item['public']) {
                    name += '<img src="images/officecalendar/officecalendar_secret.gif" border="0" width="16" height="16" alt="非公開" title="非公開" align="top" class="icon" />';
                }
                if(item.duplicate) {
                    name += '<img src="images/officecalendar/officecalendar_duplicate.gif" border="0" width="16" height="16" alt="重複スケジュール" title="重複スケジュール" align="top" class="icon" />';
                }
                if(item.repeat) {
                    name += '<img src="images/officecalendar/officecalendar_repeat.gif" border="0" width="16" height="16" alt="繰り返し" title="繰り返し" align="top" class="icon" />';
                }
                if(item.tmpreserve) {
                    name += '<img src="images/officecalendar/officecalendar_tmpreserve.gif" border="0" width="16" height="16" alt="仮スケジュール" title="仮スケジュール" align="top" class="icon" />';
                }
                html += '<div id="officecalendar-' + count + '-' + _portletId+'" class="officecalendarDiv color'+str_tmp+'" style="top: '+ top +'px; left: ' + left + '%; height: '+ height + 'px; width: '+ width + '%; z-index: 0; visibility: hidden;"><div class="officecalendarDivFirstLine color'+str_tmp+'"><span id="officecalendarDivStartTime-'+ count + '-' + _portletId + '" class="officecalendarDivTime color'+str_tmp+'">' + str_tmpflgmb + startDate + '</span><span id="officecalendarDivSepalater-'+ count + '-' + _portletId + '"  class="officecalendarDivSepalater color'+str_tmp+'">' + sepalater + '</span><span id="officecalendarDivEndTime-'+ count + '-' + _portletId + '" class="officecalendarDivTime color'+str_tmp+'">' + endDate + '</span></div><div class="officecalendarDivName color'+str_tmp+'">'  + name  + '</div><div class="officecalendarDivLastLine color'+str_tmp+'"><center><div class="handleDiv color'+str_tmp+'" align="center">&nbsp;</div></center></div></div>';
                count++;
            });
            html += "<div id=\"dummy_div_" +  _portletId + "\" class=\"officecalendarDivAdd dummy_div\" style=\" position:absolute; width: 0px; height : 0px; left: 0px; top: -10000px; Filter: Alpha(Opacity=10);opacity:.10; background-color:#FFFFFF; \">&nbsp;</div>"
            dojo.byId('officecalendarGarage-' + _portletId).innerHTML = html;

            var tmpDraggable = null;
            var draggable, draggable2;
            var objs = [];
            count = 0;
            dojo.forEach(data.officecalendar, function(item) {
                draggable = dojo.byId('officecalendar-' + count + '-' + _portletId);
                var officecalendarId = item.officecalendarId;
                tmpDraggable = new aipo.calendar.WeeklyOfficecalendarDraggable(draggable, {pid:_portletId , sid:'"officecalendar-' + count + '-' + _portletId +'"' , handle: '"dummy_div_-' + _portletId +'"' });
                aipo.calendar.objectlist.push(tmpDraggable);

                if(item.member || item.loginuser || item.owner || item.public){
                    tmpDraggable.setDraggable(true);
                } else {
                    tmpDraggable.setDraggable(false);
                }

                tmpDraggable.officecalendar = item;
                tmpDraggable.tmpIndex = item.index;
                tmpDraggable.count = count;
                tmpDraggable.tmpHeight = tmpHeight[count];
                tmpDraggable.position = 0;
                tmpDraggable.division = 1;
                tmpDraggable.portletId = _portletId;

                officecalendarDiv[item.index].push(draggable);
                if(item.public || item.member){
                    dojo.connect(draggable,"onclick", tmpDraggable, "onOfficecalendarClick");
                }

                dojo.connect(draggable,"onmouseover", tmpDraggable, "onOfficecalendarOver");

                var mbhtml = "";
                var mbfhtml = "";
                if(item.public || item.member){
                    dojo.forEach(data.memberAllList, function(mitem) {
                        for (j = 0 ; j < item.memberList.length ; j ++){
                            if(mitem.name == item.memberList[j]){
                                if(item.memberList[j].charAt(0) == 'f'){
                                    mbfhtml += "<li>" + mitem.aliasName +"</li>";
                                } else {
                                    mbhtml += "<li>" + mitem.aliasName +"</li>" ;
                                }
                            }
                        }
                    });
                    if(mbhtml != ""){
                       mbhtml = "<span style=\"font-size: 0.90em;\">参加者</span><br/><ul>" + mbhtml + "</ul>";
                    }

                    if(mbfhtml != ""){
                       mbfhtml = "<span style=\"font-size: 0.90em;\">施設</span><br/><ul>" + mbfhtml + "</ul>";
                    }
                }
                tmpDraggable._mbhtml = mbhtml;
                tmpDraggable._mbfhtml = mbfhtml;
                count++;
            });


            for(var i=0;i<ptConfig[_portletId].officecalendarDivDaySum;i++) {
                aipo.calendar.relocation(officecalendarDiv[i].length, officecalendarDiv[i], 100 /  ptConfig[_portletId].officecalendarDivDaySum * i);
                officecalendarDiv[i] = Array();
            }
            count = 0;
             l_count = 0;
            dojo.forEach(data.termOfficecalendar, function(itemList) {
                var item = null;
                termHtml = "";

                for(var i = 0;i < ptConfig[_portletId].officecalendarDivDaySum; i++) {
                   tmpNode5 = dojo.byId('termDay' + i + '-' + l_count + '-' + _portletId);
                   if (data.dayOfWeek[i] == "（土）") {
                       dojo.addClass(tmpNode5, "saturday");
                   } else {
                       dojo.removeClass(tmpNode5, "saturday");
                   }
                   if (data.dayOfWeek[i] == "（日）") {
                       dojo.addClass(tmpNode5, "sunday");
                   } else {
                       dojo.removeClass(tmpNode5, "sunday");
                   }
                   if (data.holiday[i]) {
                       dojo.addClass(tmpNode5, "holiday");
                   } else {
                       dojo.removeClass(tmpNode5, "holiday");
                   }
                }

                for (k = 0; k < itemList.length ; k++){
                    item = itemList[k];
                    var width = 100 / ptConfig[_portletId].officecalendarDivDaySum * item.rowspan;
                    var left = 100 / ptConfig[_portletId].officecalendarDivDaySum * item.index;
                    var name = item.name;
                    var officecalendarId = item.officecalendarId;

                    var str_tmp = "0";
                    var str_tmpflgmb = "";
                    var member_to = dojo.byId('member_to-' + _portletId);
                    if(member_to) {
                         var t_o = member_to.options;
                         for(i = 0 ; i < t_o.length; i++ ) {
                           if(((item.type == "U") && (item.ownerId == t_o[i].value)) || ((item.type == "F") && ("f" + item.ownerId == t_o[i].value))){
                               str_tmp = i %  aipo.calendar.maximum_to;
                           }
                           if(item.memberList){
                               var ucount = 0;
                               var fcount = 0;
                               for (j = 0 ; j < item.memberList.length ; j ++){
                                   if(item.memberList[j].charAt(0) == 'f'){
                                      fcount++;
                                   }else{
                                      ucount++;
                                   }
                               }
                           }
                        }
                        var str_tmpflgmb;
                        if(ucount > 1){
                         str_tmpflgmb   =  "[共有]";
                        }
                        if(fcount > 0){
                         str_tmpflgmb   += "[施設]";
                        }
                    }

                    if(!item['public']) {
                        name += '<img src="images/officecalendar/officecalendar_secret.gif" border="0" width="16" height="16" alt="非公開" title="非公開" align="top" class="icon" />';
                    }
                    if(item.duplicate) {
                        name += '<img src="images/officecalendar/officecalendar_duplicate.gif" border="0" width="16" height="16" alt="重複スケジュール" title="重複スケジュール" align="top" class="icon" />';
                    }
                    if(item.repeat) {
                        name += '<img src="images/officecalendar/officecalendar_repeat.gif" border="0" width="16" height="16" alt="繰り返し" title="繰り返し" align="top" class="icon" />';
                    }
                    if(item.tmpreserve) {
                        name += '<img src="images/officecalendar/officecalendar_tmpreserve.gif" border="0" width="16" height="16" alt="仮スケジュール" title="仮スケジュール" align="top" class="icon" />';
                    }
                    termHtml += '<div id="termOfficecalendar-' + count + '-' + _portletId +'" class="termOfficecalendarDiv termColor'+str_tmp+'" style="left: ' + left + '%; width: '+ width + '%;"><div class="termOfficecalendarDivHandleLeft" id="termOfficecalendarDivHandleLeft-' + count + '-' + _portletId +'">&nbsp;</div><div class="termOfficecalendarDivNameDiv">' + str_tmpflgmb + name + '</div><div class="termOfficecalendarDivHandleRight" id="termOfficecalendarDivHandleRight-' + count + '-' + _portletId +'">&nbsp;</div></div>';
                    count++;
                }
                dojo.byId('termOfficecalendarItemGarage-' + l_count + '-' + _portletId).innerHTML = termHtml;
                l_count++;
            });

            tableLeft = dojo.byId('weeklyTermLeft_'+_portletId);

            tmpDraggable = null;
            count = 0;
            l_count = 0;
            dojo.forEach(data.termOfficecalendar, function(itemList) {
                var item = null;
                    for (k = 0; k < itemList.length ; k++){
                        item = itemList[k];
                        var officecalendarId = item.officecalendarId;
                        draggable = dojo.byId('termOfficecalendar-' + count + '-' + _portletId);
                        draggable2 = dojo.byId('termOfficecalendarDivHandleLeft-' + count + '-' + _portletId);
                        draggable3 = dojo.byId('termOfficecalendarDivHandleRight-' + count + '-' + _portletId);
                        tmpDraggable = new aipo.calendar.WeeklyTermOfficecalendarDraggable(draggable,{pid:_portletId , sid:  'termOfficecalendar-' + count + '-' + _portletId});

                        aipo.calendar.objectlist.push(tmpDraggable);
                        tmpDraggable.officecalendar = item;
                        tmpDraggable.officecalendarNode = draggable;
                        tmpDraggable.portletId = _portletId;
                        tmpDraggable.termType = 'center';

                        dojo.connect(draggable,"onclick", tmpDraggable, "onOfficecalendarClick");
                        draggable.style.zIndex = 1;

                        if (item.indexReal >= 0) {
                            tmpDraggable2 = new aipo.calendar.WeeklyTermOfficecalendarDraggable(draggable2, {pid:_portletId , sid: 'termOfficecalendarDivHandleLeft-' + count + '-' + _portletId});
                            aipo.calendar.objectlist.push(tmpDraggable2);
                            tmpDraggable2.officecalendar = item;
                            tmpDraggable2.officecalendarNode = draggable;
                            tmpDraggable2.portletId = _portletId;
                            tmpDraggable2.termType = 'left';
                            if(item.member || item.loginuser || item.owner || item.public){
                                tmpDraggable2.setDraggable(true);
                            } else {
                                tmpDraggable2.setDraggable(false);
                            }
                        } else {
                            dojo.style(draggable2, "cursor", "pointer");
                            draggable2.style.zIndex = 1;
                        }
                        dojo.connect(draggable2,"onclick", tmpDraggable, "onOfficecalendarClick");
                        if (item.indexReal + item.colspanReal <= ptConfig[_portletId].officecalendarDivDaySum ) {
                            tmpDraggable3 = new aipo.calendar.WeeklyTermOfficecalendarDraggable(draggable3, {pid:_portletId , sid: 'termOfficecalendarDivHandleRight-' + count + '-' + _portletId});
                            aipo.calendar.objectlist.push(tmpDraggable3);
                            tmpDraggable3.officecalendar = item;
                            tmpDraggable3.officecalendarNode = draggable;
                            tmpDraggable3.portletId = _portletId;
                            tmpDraggable3.termType = 'right';
                            if(item.member || item.loginuser || item.owner || item.public){
                                tmpDraggable3.setDraggable(true);
                            } else {
                                tmpDraggable3.setDraggable(false);
                            }
                        } else {
                            dojo.style(draggable3, "cursor", "pointer");
                            draggable3.style.zIndex = 1;
                        }
                        dojo.connect(draggable3,"onclick", tmpDraggable, "onOfficecalendarClick");
                        dojo.connect(draggable,"onmouseover", tmpDraggable, "onOfficecalendarOver");

                        if(item.member || item.loginuser || item.owner || item.public){
                            tmpDraggable.setDraggable(true);
                        } else {
                            tmpDraggable.setDraggable(false);
                        }

                        var mbhtml = "";
                        var mbfhtml = "";
                        if(item.public || item.member){
                            dojo.forEach(data.memberAllList, function(mitem) {
                                for (j = 0 ; j < item.memberList.length ; j ++){
                                    if(mitem.name == item.memberList[j]){
                                        if(item.memberList[j].charAt(0) == 'f'){
                                            mbfhtml += "<li>" + mitem.aliasName +"</li>";
                                        } else {
                                            mbhtml += "<li>" + mitem.aliasName +"</li>" ;
                                        }
                                    }
                                }
                            });
                        }
                        if(mbhtml != ""){
                            mbhtml = "<span style=\"font-size: 0.90em;\">参加者</span><br/><ul>" + mbhtml + "</ul>";
                        }

                        if(mbfhtml != ""){
                            mbfhtml = "<span style=\"font-size: 0.90em;\">施設</span><br/><ul>" + mbfhtml + "</ul>";
                        }

                        tmpDraggable._mbhtml = mbhtml;
                        tmpDraggable._mbfhtml = mbfhtml;
                        count++;
                    }
                l_count++;
            });

            obj_content = dojo.byId('content-'+_portletId);
            dojo.style(obj_content, "visibility" , "visible");
            obj_indicator = dojo.byId('indicator-'+_portletId);
            dojo.style(obj_indicator, "display" , "none")

            if (!ptConfig[_portletId].isScroll) {
                dojo.byId('weeklyScrollPane_'+_portletId).scrollTop = ptConfig[_portletId].contentScrollTop;
                ptConfig[_portletId].isScroll = true;
            }
            ptConfig[_portletId].isTooltipEnable = true;
        }
    });
};

// aipo.calendar.relocation
aipo.calendar.relocation = function(sum,officecalendarDiv,officecalendarDivLeft) {
    var i,j;
    var offsetW = 0.99;
    var officecalendarDivWidth = 100 / 7;
    var endoverlapOfficecalendar=0;
    var overlapNumArrayMax=0;
    var bottomLineMax=0;
    var targetNum=0;
    var overlapNumArray = new Array(sum);
    var positionLeftArray = new Array(sum);
    var resizeWidthArray = new Array(sum);

    officecalendarDiv.sort(aipo.calendar.sortByRegion);


    for (i=0; i<sum; i++) {
        officecalendarDiv[i].style.zIndex = i+1;
    }
    for (i=0; i<sum; i=endoverlapOfficecalendar) {
        endoverlapOfficecalendar = aipo.calendar.overlapOfficecalendar(officecalendarDiv,i,i,++endoverlapOfficecalendar,sum);
        if (bottomLineMax < parseInt(dojo.getComputedStyle(officecalendarDiv[i]).top)) {
            targetNum = i;
            bottomLineMax = parseInt(dojo.getComputedStyle(officecalendarDiv[targetNum]).top);
            overlapNumArrayMax = 0;
        }

        for (j=targetNum; j<endoverlapOfficecalendar; j++)
            var divBottom = parseInt(dojo.getComputedStyle(officecalendarDiv[j]).top) + parseInt(dojo.getComputedStyle(officecalendarDiv[j]).height);
            if (bottomLineMax < divBottom)
                bottomLineMax = divBottom;

        for (j=targetNum; j<endoverlapOfficecalendar; j++) {
            positionLeftArray[j] = aipo.calendar.positionLeft(officecalendarDiv,positionLeftArray,targetNum,j,0);
            if (positionLeftArray[j] > overlapNumArrayMax) overlapNumArrayMax = positionLeftArray[j];
        }

        for (j=targetNum; j<endoverlapOfficecalendar; j++)
            resizeWidthArray[j] = aipo.calendar.positionRight(officecalendarDiv,positionLeftArray,overlapNumArrayMax,targetNum,j);

        for (j=targetNum; j<endoverlapOfficecalendar; j++)
            overlapNumArray[j] = overlapNumArrayMax;
    }


    for (i=0; i<sum; i++) {
        if (overlapNumArray[i] != 0) {
               if (positionLeftArray[i] < positionLeftArray[i+1])
                dojo.style(officecalendarDiv[i], "width", (officecalendarDivWidth * 2 / (overlapNumArray[i]+1))*0.8 * offsetW + "%");
            else if (resizeWidthArray[i]==0)
                dojo.style(officecalendarDiv[i], "width", (officecalendarDivWidth - (officecalendarDivWidth/(overlapNumArray[i]+1))*positionLeftArray[i]) * offsetW +"%");
            else
                dojo.style(officecalendarDiv[i], "width", (officecalendarDivWidth - (officecalendarDivWidth/(overlapNumArray[i]+1))*positionLeftArray[i] - (officecalendarDivWidth*2/(overlapNumArray[i]+1))*0.2 - (officecalendarDivWidth/(overlapNumArray[i]+1))*(resizeWidthArray[i]-1)) * offsetW +"%");
        }
        else
            dojo.style(officecalendarDiv[i], "width", officecalendarDivWidth * offsetW +"%");

        dojo.style(officecalendarDiv[i], "left", (officecalendarDivLeft + ((officecalendarDivWidth/(overlapNumArray[i]+1))*positionLeftArray[i]))+"%");
        dojo.style(officecalendarDiv[i], "visibility", "visible" );
     }
}

// aipo.calendar.overlapOfficecalendar
aipo.calendar.overlapOfficecalendar = function(officecalendarDiv,mostLeftDiv,startNum,endNum,sum) {
    var mostLeftDivBottom = parseInt(dojo.getComputedStyle(officecalendarDiv[mostLeftDiv]).top) + parseInt(dojo.getComputedStyle(officecalendarDiv[mostLeftDiv]).height);
    var startNumBottom = parseInt(dojo.getComputedStyle(officecalendarDiv[startNum]).top) + parseInt(dojo.getComputedStyle(officecalendarDiv[startNum]).height);

    var endNumTop;
    if(officecalendarDiv[endNum]){
        endNumTop = parseInt(dojo.getComputedStyle(officecalendarDiv[endNum]).top);
    }else {
        endNumTop = 'NaN';
    }

    if ((endNum > sum-1) ||
        (mostLeftDivBottom < endNumTop) ||
            (startNumBottom < endNumTop))
        return endNum;
    else endNum = aipo.calendar.overlapOfficecalendar(officecalendarDiv,mostLeftDiv,endNum,++endNum,sum);
    endNum = aipo.calendar.overlapOfficecalendar(officecalendarDiv,mostLeftDiv,startNum,endNum,sum);
    return endNum;
}


// aipo.calendar.positionLeft
aipo.calendar.positionLeft = function(officecalendarDiv,positionLeftArray,startNum,endNum,positionLeft) {
    var endNumTop = parseInt(dojo.getComputedStyle(officecalendarDiv[endNum]).top);
    for (i=startNum; i<endNum; i++) {
        var tmpDivTop = parseInt(dojo.getComputedStyle(officecalendarDiv[i]).top);
        var tmpDivBottom = tmpDivTop + parseInt(dojo.getComputedStyle(officecalendarDiv[i]).height);
        if ((tmpDivTop <= endNumTop) &&
                (tmpDivBottom > endNumTop) &&
                (positionLeftArray[i]==positionLeft)) {
                positionLeft = aipo.calendar.positionLeft(officecalendarDiv,positionLeftArray,startNum,endNum,++positionLeft);
        }
    }
    return positionLeft;
}

// aipo.calendar.positionRight
aipo.calendar.positionRight = function(officecalendarDiv,positionLeftArray,overlapNumArrayMax,startNum,endNum) {
    var resizeWidth=0;
    var endNumTop = parseInt(dojo.getComputedStyle(officecalendarDiv[endNum]).top);
    for (i=startNum; i<endNum; i++) {
        var tmpDivTop = parseInt(dojo.getComputedStyle(officecalendarDiv[i]).top);
        var tmpDivBottom = tmpDivTop + parseInt(dojo.getComputedStyle(officecalendarDiv[i]).height);
        if ((tmpDivTop <= endNumTop) &&
                (tmpDivBottom > endNumTop) &&
                (positionLeftArray[i]>positionLeftArray[endNum]) &&
                ((overlapNumArrayMax-positionLeftArray[i]+1)>resizeWidth)) {
                resizeWidth=overlapNumArrayMax-positionLeftArray[i]+1;
        }
    }
    return resizeWidth;
}

// aipo.calendar.sortByRegion
aipo.calendar.sortByRegion = function(a,b) {
    var aTop = parseInt(dojo.getComputedStyle(a).top);
    var bTop = parseInt(dojo.getComputedStyle(b).top);
    var aBottom = aTop + parseInt(dojo.getComputedStyle(a).height);
    var bBottom = aBottom + parseInt(dojo.getComputedStyle(b).height);
    if (aTop == bTop)
        return bBottom - aBottom;
    else return aTop - bTop;
}

// aipo.calendar.getDate
aipo.calendar.getDate = function (thisDate, days) {
    // ex: inputDate = "2007-01-01"
    //     days = "3"
    //     return = "2007-01-04"
    tmpYear = parseInt(thisDate.substring(0,4),10);
    tmpMonth = parseInt(thisDate.substring(5,7),10);
    tmpDay = parseInt(thisDate.substring(8,10),10);
    if (days > 0) {
        do {
            tmpMonthDays = aipo.calendar.getDay(tmpYear,tmpMonth);
            if (tmpDay + days <= tmpMonthDays) {
                tmpDay = tmpDay + days;
                if ((tmpMonth < 10) && (tmpDay < 10))
                    date = tmpYear + "-0" + tmpMonth + "-0" + tmpDay;
                else if ((tmpMonth < 10) && !(tmpDay < 10))
                    date = tmpYear + "-0" + tmpMonth + "-" + tmpDay;
                else if (!(tmpMonth < 10) && (tmpDay < 10))
                    date = tmpYear + "-" + tmpMonth + "-0" + tmpDay;
                else date = tmpYear + "-" + tmpMonth + "-" + tmpDay;
                days = -1;
            } else {
                days = days - (tmpMonthDays - tmpDay) - 1;
                if (tmpMonth == 12) {
                    tmpYear++;
                    tmpMonth=1;
                } else {
                    tmpMonth++;
                }
                tmpDay = 1;
            }
        } while (days >= 0)
    } else if (days < 0) {
        do {
            if (tmpDay + days > 0) {
                tmpDay = tmpDay + days;
                if ((tmpMonth < 10) && (tmpDay < 10))
                    date = tmpYear + "-0" + tmpMonth + "-0" + tmpDay;
                else if ((tmpMonth < 10) && !(tmpDay < 10))
                    date = tmpYear + "-0" + tmpMonth + "-" + tmpDay;
                else if (!(tmpMonth < 10) && (tmpDay < 10))
                    date = tmpYear + "-" + tmpMonth + "-0" + tmpDay;
                else date = tmpYear + "-" + tmpMonth + "-" + tmpDay;
                days = 1;
            } else {
                if (tmpMonth == 1) {
                    tmpYear--;
                    tmpMonth=12;
                } else {
                    tmpMonth--;
                }
                tmpMonthDays = aipo.calendar.getDay(tmpYear,tmpMonth);
                days = days + tmpDay;
                tmpDay = tmpMonthDays;
            }
        } while (days <= 0)
    } else date = thisDate;
    return date;
}

// aipo.calendar.getDay
aipo.calendar.getDay = function (year, month) {
    if ( month == 2 ) {
        if ( !(year % 4)  && ( (year % 100) || !(year % 400) ) ) return 29;
        else return 28;
    } else if ( month == 4 || month == 6 || month == 9 || month == 11 ) return 30;
    else return 31;
}

//aipo.calendar.setGridArray
aipo.calendar.setGridArray = function(_portletId, _colSize) {
    var tmpX = 0;
    if(aipo.calendar.gridArray) delete(aipo.calendar.gridArray);
    aipo.calendar.gridArray = new Array(_colSize);
    for(i = 0 ; i < _colSize ; i++){
        tmpX = dojo._abs(dojo.byId("weeklyDay"+ i +"-" + _portletId), true).x;
        aipo.calendar.gridArray[i]=tmpX;
    }
}

//aipo.calendar.getCurrentMouseX
aipo.calendar.getCurrentMouseX = function(e){
       if(aipo.calendar.gridArray == null) return {index: -1, x: 0};
       var startX = aipo.calendar.gridArray[0];
       var _tmpIndex= 0 ;
       var i;
       if(e.pageX > startX){
       var max = parseInt(aipo.calendar.gridArray.length) - 1;
           for(i = max; i > -1 ; i-- ) {
                 if(e.pageX > aipo.calendar.gridArray[i]){
                    _tmpIndex = i;
                    break;
                 }
           }
       }else {
           _tmpIndex = 0;
       }
       var _tmpX = aipo.calendar.gridArray[_tmpIndex] - startX;
       return {index: _tmpIndex, x: _tmpX};
}

aipo.calendar.onCloseMemberpicker = function( _portletId ){
    aipo.calendar.populateWeeklyOfficecalendar(_portletId);
}


dojo.declare("aipo.calendar.DummyDivObject", null, {
     portletId: null,
     parentnode: null,
     draggable: null,
     TooltipObject: null,
     constructor: function(node, params){
        this.portletId = params.pid;
        this.parentnode = params.node;
        this.node = dojo.byId(node);
        this.events = [
            dojo.connect(this.node, "onmousedown", this, "onMouseDown"),
            dojo.connect(this.node, "onmouseover", this, "onMouseOver"),
        ];
     },
     onMouseDown: function(e){
        this.hide();
        if(this.parentnode == null || this.parentnode == "undefined"){return;}
        if(this.draggable){this.draggable.onMouseDown(e);}
     },
     onMouseOver: function(e){
        if(this.parentnode == null || this.parentnode == "undefined"){return;}
     },
     destroy: function(){
        dojo.forEach(this.events, dojo.disconnect);
        this.events = this.node = this.handle = null;
    },
    hide: function(){
        dojo.marginBox (this.node,{ l: 0, t: -10000, w: 0, h: 0 });
    }
});

// aipo.calendar.WeeklyOfficecalendarDragMoveObject
dojo.declare("aipo.calendar.WeeklyOfficecalendarDragMoveObject", [aimluck.dnd.DragMoveObject], {
    _rowHeight_: 18,
    isResize: false,
    distance: 3,
    lastScroll: 0,
    onFirstMove: function(e){
        if(this.dragSource.TooltipObject != null){
           this.dragSource.TooltipObject.uninitialize();
        }

        aimluck.dnd.DragMoveObject.prototype.onFirstMove.apply(this, arguments);
        dojo.style(this.node, "opacity", 0.5);
        this.node.style.zIndex = 999;
        this.startY = this._pageY;
        this.startAbsoluteY = dojo._abs(dojo.byId(this.node), true).y;
        // WebKitレンダリングエンジンのブラウザではdojo._absの挙動が異なるので、AbsoluteYの座標修正
        var userAgent = window.navigator.userAgent.toLowerCase();
        if(userAgent.indexOf("webkit") > -1){
            this.startAbsoluteY += document.body.scrollTop;
        }
        this.startHeight = parseInt(dojo.getComputedStyle(this.node).height);
        this.startTop = parseInt(dojo.getComputedStyle(this.node).top);
        if(this.startHeight - 6 < this.startY-this.startAbsoluteY) {
            this.isResize = true;
        }
        aipo.calendar.setGridArray(this.portletId, parseInt(ptConfig[this.portletId].officecalendarDivDaySum));
        lastScroll = dojo.byId('weeklyScrollPane_'+this.portletId).scrollTop;
    },
    onMouseMove: function(e){

        if(this.dragSource.isDraggable == false) return;

        aimluck.dnd.DragMoveObject.prototype.onMouseMove.apply(this, arguments);
        this.dragSource.officecalendar.isDrag = true;
        if(this.dragSource.tmpHeight > 3) {
            dojo.style(this.node, "height", this.dragSource.tmpHeight + "px");
            this.dragSource.tmpHeight = 3;
        }
        var distance = ptConfig[this.portletId].distance;
        var distance_scr = dojo.byId('weeklyScrollPane_'+this.portletId).scrollTop - lastScroll;

        this.leftTop.t = Math.floor((this.leftTop.t + distance_scr )/distance)*distance;
        if (this.isResize) {
            if(- this.startTop + this.leftTop.t + this.startHeight < 0) {
                dojo.style(this.node, "height", "0px");
                this.leftTop.t += this.startHeight;
            } else {
                var rh;
                if (this.leftTop.t + this.startHeight > 864 ) {
                    rh = 864 - this.startTop - 3;
                } else {
                    rh =  - this.startTop + this.leftTop.t + this.startHeight;
                }
                this.leftTop.t = this.startTop;
                this.leftTop.h = parseInt(rh);
            }
        } else {
            if(!this.disableY) {
              if ( this.leftTop.t < 0 ) this.leftTop.t = 0;
              if ( this.leftTop.t + this.startHeight > 864)
                this.leftTop.t = 864 - this.startHeight - 6;
            }
        }
        if(!this.disableX) {
           mouseX = aipo.calendar.getCurrentMouseX(e);
           this.leftTop.l = mouseX.x;
           this.dragSource.officecalendar.index = mouseX.index;
        }
        dojo.marginBox(this.node, this.leftTop);
        var tmpTop = parseInt(dojo.getComputedStyle(this.node).top);
        var tmpHeight = parseInt(dojo.getComputedStyle(this.node).height);
        var quotient = tmpTop/distance;
        var hour = Math.floor(quotient/12);
        var minute = Math.floor(quotient%12);



        hour = (hour > 9)? hour : "0" + hour;
        minute = (minute > 1) ? minute*(60/12): "0" + minute*(60/12);
        var id = this.dragSource.count;
        dojo.byId('officecalendarDivStartTime-'+ id + '-' + this.portletId).innerHTML = hour + ':'+ minute;
        this.dragSource.officecalendar.startDateHour = hour;
        this.dragSource.officecalendar.startDateMinute = minute;
        this.dragSource.officecalendar.startDate = hour + ':'+ minute;

        quotient += tmpHeight/distance;
        hour = Math.floor(quotient/12);
        minute = Math.floor(quotient%12);

        hour = (hour > 9)? hour : "0" + hour;
        minute = (minute > 1) ? minute*(60/12): "0" + minute*(60/12);

        dojo.byId('officecalendarDivEndTime-'+ id + '-' + this.portletId).innerHTML = hour + ':'+ minute;
        this.dragSource.officecalendar.endDateHour = hour;
        this.dragSource.officecalendar.endDateMinute = minute;
        this.dragSource.officecalendar.endDate = hour + ':'+ minute;
        dojo.byId('officecalendarDivSepalater-'+ id + '-' + this.portletId).innerHTML = '-';
        return;
    },
    onMouseUp: function (e) {
        ptConfig[this.portletId].isTooltipEnable = true;
        if(this.dragSource.officecalendar.isDrag != true){
            dojo.style(this.node, "opacity", 1.0 );
            aimluck.dnd.DragMoveObject.prototype.onMouseUp.apply(this, arguments);
            if(this.dragSource){this.dragSource.onOfficecalendarClick(e);}
            return;
        }

        var tmpHeight = parseInt(dojo.getComputedStyle(this.node).height);
        if(tmpHeight < ptConfig[this.portletId].rowHeight) {
            dojo.style(this.node, "height", ptConfig[this.portletId].rowHeight+ "px");
            this.dragSource.tmpHeight = tmpHeight;
        } else {
            this.dragSource.tmpHeight = -1;
        }

        var params = "";
        params += "&mode=update";
        params += "&entityid="+ this.dragSource.officecalendar.officecalendarId;
        params += "&view_start=" + ptConfig[this.portletId].jsonData.date[0].substring(0, 10);

        if(this.dragSource.officecalendar.repeat) {
            params += "&edit_repeat_flag=1";
            params += "&view_date=" + ptConfig[this.portletId].jsonData.date[this.dragSource.tmpIndex].substring(0, 10);
        }

        params += "&start_date=" + ptConfig[this.portletId].jsonData.date[this.dragSource.officecalendar.index].substring(0, 11) + this.dragSource.officecalendar.startDateHour + '-' + this.dragSource.officecalendar.startDateMinute;
        params += "&end_date=" + ptConfig[this.portletId].jsonData.date[this.dragSource.officecalendar.index].substring(0, 11) + this.dragSource.officecalendar.endDateHour + '-' + this.dragSource.officecalendar.endDateMinute;

        aipo.calendar.populateWeeklyOfficecalendar(this.portletId, params);
        aipo.portletReload('officecalendar', this.portletId);

        aimluck.dnd.DragMoveObject.prototype.onMouseUp.apply(this, arguments);
         this.dragSource.destroy();
    }
});

// aipo.calendar.WeeklyOfficecalendarDraggable
dojo.declare("aipo.calendar.WeeklyOfficecalendarDraggable", [aimluck.dnd.Draggable], {
    DragMoveObject: aipo.calendar.WeeklyOfficecalendarDragMoveObject,
    isDraggable: false,
    _mbhtml: "",
    _mbfhtml: "",
    officecalendarObjId: null,
    constructor: function(node, params){
        this.officecalendarObjId = params.sid;
    },
    onMouseDown: function(e){
        ptConfig[this.portletId].isTooltipEnable = false;
        if(!!aipo.calendar.dummyDivObj && !!aipo.calendar.dummyDivObj.TooltipObject)aipo.calendar.dummyDivObj.TooltipObject.close();
        aimluck.dnd.Draggable.prototype.onMouseDown.apply(this, arguments);
    },
    onOfficecalendarClick: function(e) {
        if(this.officecalendar.isDrag || !this.isDraggable) {
            return;
        }
        var uid = this.officecalendar.ownerId;
        if(this.officecalendar.type == "F"){
            uid = "f"+ uid;
        }

        aipo.common.showDialog(ptConfig[this.portletId].detailUrl + "&entityId=" + this.officecalendar.officecalendarId + "&view_date=" + ptConfig[this.portletId].jsonData.date[this.officecalendar.index] + "&userid=" + uid, this.portletId, aipo.officecalendar.onLoadOfficecalendarDetail );
        //** FIXME IEで追加ダイアログを閉じるとスクロールバーのｙ座標が強制的に０になってしまう現象
        aipo.officecalendar.tmpScroll = parseInt(dojo.byId('weeklyScrollPane_'+this.portletId)["scrollTop"]);
        //**//
    },
    onOfficecalendarOver: function(e) {
        if(ptConfig[this.portletId].isTooltipEnable == false){return;}
        var ttdiv = dojo.byId("dummy_div_" + this.portletId);
        var left =  dojo.getComputedStyle(this.node).left ;
        var top =  dojo.getComputedStyle(this.node).top;
        var width =  dojo.getComputedStyle(this.node).width;
        var height =  dojo.getComputedStyle(this.node).height;
        dojo.marginBox (ttdiv,dojo._getMarginBox(this.node,{ l: left, t: top, w: width, h: height }));
        ttdiv.style.zIndex = this.node.style.zIndex ;
        ttdiv.style.height = (parseInt(height) - 6) + "px";
        if(!aipo.calendar.dummyDivObj){
            aipo.calendar.dummyDivObj = new aipo.calendar.DummyDivObject(ttdiv , {pid: this.officecalendarObjId , node: this.node});
        }else{
            aipo.calendar.dummyDivObj.parentnode = this.node;
        }
        aipo.calendar.dummyDivObj.draggable = this;
        this.createTooltip();
    },
    createTooltip: function(){
        var startDate = ((this.officecalendar.startDateHour > 9) ? this.officecalendar.startDate : "0" +this.officecalendar.startDate);
        var endDate = ((this.officecalendar.endDateHour > 9) ? this.officecalendar.endDate : "0" +this.officecalendar.endDate);

        if(aipo.calendar.dummyDivObj.TooltipObject){
            aipo.calendar.dummyDivObj.TooltipObject.destroyRecursive();
            aipo.calendar.dummyDivObj.TooltipObject = null;
        }
        aipo.calendar.dummyDivObj.TooltipObject = new aipo.widget.ToolTip({label:"<h4>" + this.officecalendar.name + "</h4>" + "<span style=\"font-size: 0.90em;\">" + startDate +"～" + endDate + "</span>" + "<br/>" + this._mbhtml + this._mbfhtml  , connectId:["dummy_div_" + this.portletId]} ,this.portletId );

    },
    setDraggable: function(flag){
        this.isDraggable = flag;
    }
});

/*
*/

// aipo.calendar.WeeklyTermOfficecalendarDragMoveObject
dojo.declare("aipo.calendar.WeeklyTermOfficecalendarDragMoveObject", [aimluck.dnd.DragMoveObject], {
    positionFrom: -1,
    positionTo: -1,
    moveIndex: 0,
    onFirstMove: function(e){
        if(this.dragSource.TooltipObject != null){
               this.dragSource.TooltipObject.uninitialize();
        }
       aimluck.dnd.DragMoveObject.prototype.onFirstMove.apply(this, arguments);
       dojo.style(this.node, "opacity", 0.5);
       aipo.calendar.setGridArray(this.portletId, parseInt(ptConfig[this.portletId].officecalendarDivDaySum));
    },
    onMouseMove: function(e){
        if(this.dragSource.isDraggable == false) return;
        aimluck.dnd.DragMoveObject.prototype.onMouseMove.apply(this, arguments);
        this.dragSource.officecalendar.isDrag = true;
        var distance = ptConfig[this.portletId].distance;
        var mouseX = aipo.calendar.getCurrentMouseX(e);
        _tmpIndex = mouseX.index;
        /*y = Math.floor(y/distance)*distance;
        if(!this.disableY) { this.node.style.top = y + "px"; }*/
        if(!this.disableX) {
            var tmpOfficecalendar = this.dragSource.officecalendar;
            var type = this.dragSource.termType;
            var officecalendarNode = this.dragSource.officecalendarNode;
            var tmpW, tmpL;
            if (type == "center") {
                if(this.positionFrom == -1 && _tmpIndex != -1) {
                    this.positionFrom = _tmpIndex;
                    this.positionTo = this.positionFrom;
                }
                if(this.positionTo != -1 && _tmpIndex != -1) {
                    this.positionTo = _tmpIndex;
                }
                this.moveIndex = - this.positionFrom + this.positionTo;
                tmpL = tmpOfficecalendar.indexReal + this.moveIndex;
                tmpW = tmpOfficecalendar.colspanReal;
                var tmpS = ptConfig[this.portletId].officecalendarDivDaySum;
                if (tmpW + tmpL > tmpS) {
                    if (tmpL < 0) {
                        tmpW = tmpS;
                    } else {
                        tmpW = tmpS - tmpL;
                    }
                } else if (tmpL < 0) {
                    tmpW = tmpW + tmpL;
                }

                if (tmpL < 0) {
                    tmpL = 0;
                }
            } else if(type == "left") {
                if(this.positionFrom == -1) {
                    this.positionFrom = tmpOfficecalendar.index;
                    this.positionTo = tmpOfficecalendar.index;
                }
                if(this.positionTo != -1 && _tmpIndex != -1) {
                    this.positionTo = _tmpIndex;
                }
                this.moveIndex = - this.positionFrom + this.positionTo;
                if (this.positionTo >= this.positionFrom + tmpOfficecalendar.colspanReal) {
                    tmpL = tmpOfficecalendar.indexReal+ tmpOfficecalendar.rowspan - 1;
                    tmpW = this.positionTo - this.positionFrom - tmpOfficecalendar.colspanReal + 2;
                } else {
                    tmpL = this.positionTo;
                    tmpW = tmpOfficecalendar.rowspan + this.positionFrom - this.positionTo;
                }

            } else {
                if(this.positionFrom == -1) {
                    this.positionFrom = tmpOfficecalendar.index;
                    this.positionTo = tmpOfficecalendar.index;
                }
                if(this.positionTo != -1 && _tmpIndex != -1 && this._tmpIndex != -1) {
                    this.positionTo = _tmpIndex;
                }
                this.moveIndex =   - tmpOfficecalendar.index - tmpOfficecalendar.rowspan + this.positionTo + 1;
                if (this.positionTo <= this.positionFrom) {
                    tmpL = this.positionTo;
                    tmpW = this.positionFrom - this.positionTo + 1;
                } else {
                    tmpL = tmpOfficecalendar.index;
                    tmpW = this.positionTo - tmpOfficecalendar.index + 1;
                }
            }
            var width = 100 / ptConfig[this.portletId].officecalendarDivDaySum * tmpW;
            var left = 100 / ptConfig[this.portletId].officecalendarDivDaySum * tmpL;
            dojo.style(officecalendarNode, "left",  left + "%");
            dojo.style(officecalendarNode, "width", width + "%");
        }
    },
    onMouseUp: function (e) {
        ptConfig[this.portletId].isTooltipEnable = true;
        if(this.dragSource.officecalendar.isDrag != true){
            dojo.style(this.node, "opacity", 1.0 );
            aimluck.dnd.DragMoveObject.prototype.onMouseUp.apply(this, arguments);
            return;
        }
        var tmpOfficecalendar = this.dragSource.officecalendar;
        var viewStart = ptConfig[this.portletId].jsonData.date[0].substring(0, 10);
        var type = this.dragSource.termType;
        var officecalendarNode = this.dragSource.officecalendarNode;
        var startDate, endDate;
        if (type == 'center') {
            startDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal + this.moveIndex) + "-00-00";
            endDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal + this.moveIndex + tmpOfficecalendar.colspanReal-1) + "-00-00";
        } else if (type == 'left') {
            if (tmpOfficecalendar.colspanReal - this.moveIndex > 0) {
                startDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal + this.moveIndex) + "-00-00";
                endDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal + tmpOfficecalendar.colspanReal - 1) + "-00-00";
            } else {
                startDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal + tmpOfficecalendar.colspanReal - 1) + "-00-00";
                endDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal + this.moveIndex) + "-00-00";
            }
        } else {
            if (tmpOfficecalendar.colspanReal + this.moveIndex > 0) {
                startDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal) + "-00-00";
                endDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal + tmpOfficecalendar.colspanReal + this.moveIndex-1) + "-00-00";
            } else {
                startDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal + tmpOfficecalendar.colspanReal + this.moveIndex-1) + "-00-00";
                endDate = aipo.calendar.getDate(viewStart, tmpOfficecalendar.indexReal) + "-00-00";

            }
        }

        this.positionFrom = -1;
        this.positionTo = -1;
        this.moveIndex = 0;
        this.tmpIndex = 0;

        var params = "";
        params += "&mode=update";
        params += "&is_span=TRUE";
        params += "&entityid="+ this.dragSource.officecalendar.officecalendarId;
        params += "&view_start=" + viewStart;
        params += "&start_date=" + startDate;
        params += "&end_date=" + endDate;

        aipo.calendar.populateWeeklyOfficecalendar(this.portletId, params);

        aipo.portletReload('officecalendar', this.portletId);
        aimluck.dnd.DragMoveObject.prototype.onMouseUp.apply(this, arguments);
    }
});

// aipo.calendar.WeeklyTermOfficecalendarDraggable
dojo.declare("aipo.calendar.WeeklyTermOfficecalendarDraggable", [aimluck.dnd.Draggable], {
    DragMoveObject: aipo.calendar.WeeklyTermOfficecalendarDragMoveObject,
    isDraggable: false,
    _mbhtml: "",
    _mbfhtml: "",
    TooltipObject: null,
    officecalendarObjId: null,
    isDraggable: false,
    constructor: function(node, params){
        this.officecalendarObjId = params.sid;
    },
    onMouseDown: function(e){
        ptConfig[this.portletId].isTooltipEnable = false;
        if(this.TooltipObject)this.TooltipObject.close();
        aimluck.dnd.Draggable.prototype.onMouseDown.apply(this, arguments);
    },
    onOfficecalendarOver: function(e) {
        if(ptConfig[this.portletId].isTooltipEnable == false){return;}
        /*
        var ttdiv = dojo.byId("dummy_div_" + this.portletId);
        var left =  dojo.getComputedStyle(this.node).left ;
        var top =  dojo.getComputedStyle(this.node).top;
        var width =   dojo.getComputedStyle(this.node).width;
        var height =  dojo.getComputedStyle(this.node).height;
        dojo.marginBox (ttdiv,dojo._getMarginBox(this.node,{ l: left, t: top, w: width, h: height }));
        ttdiv.style.zIndex = this.node.style.zIndex ;

        if(!aipo.calendar.dummyDivObj){
            aipo.calendar.dummyDivObj = new aipo.calendar.DummyDivObject(ttdiv , {pid: this.portletId , node: this.node});
        }else{
            aipo.calendar.dummyDivObj.parentnode = this.node;
        }
        aipo.calendar.dummyDivObj.draggable = this;
        */
        this.createTooltip();
    },
    onOfficecalendarClick: function(e) {
        if(this.officecalendar.isDrag || !this.isDraggable) {
            return;
        }
        var uid = this.officecalendar.ownerId;
        if(this.officecalendar.type == "F"){
            uid = "f"+ uid;
        }
        aipo.common.showDialog(ptConfig[this.portletId].detailUrl + "&entityId=" + this.officecalendar.officecalendarId + "&view_date=" + ptConfig[this.portletId].jsonData.date[this.officecalendar.index]  + "&userid=" + uid, this.portletId, aipo.officecalendar.onLoadOfficecalendarDetail);
        //** FIXME IEで追加ダイアログを閉じるとスクロールバーのｙ座標が強制的に０になってしまう現象
        aipo.officecalendar.tmpScroll = parseInt(dojo.byId('weeklyScrollPane_'+this.portletId)["scrollTop"]);
        //**//
    },
    createTooltip: function(){
    /*
        var startDate = ((this.officecalendar.startDateHour > 9) ? this.officecalendar.startDate : "0" +this.officecalendar.startDate);
        var endDate = ((this.officecalendar.endDateHour > 9) ? this.officecalendar.endDate : "0" +this.officecalendar.endDate);

        if(aipo.calendar.dummyDivObj.TooltipObject){
            aipo.calendar.dummyDivObj.TooltipObject.destroy();
            aipo.calendar.dummyDivObj.TooltipObject = null;
        }
        */
         if(!this.TooltipObject)this.TooltipObject = new aipo.widget.ToolTip({label:"<h4>" + this.officecalendar.name + "</h4>" + this._mbhtml + this._mbfhtml  , connectId:[this.node.id]} ,this.portletId );
         aipo.calendar.objectlist.push(this.TooltipObject);
    },
    setDraggable: function(flag){
        this.isDraggable = flag;
    }
});

/*
*/

// aipo.calendar.WeeklyOfficecalendarAddDragMoveObject
dojo.declare("aipo.calendar.WeeklyOfficecalendarAddDragMoveObject", [aimluck.dnd.DragMoveObject], {
   _rowHeight_: 18,
    positionFrom: 0,
    positionTo: 0,
    _isDragging: false,
    lastScroll: 0,
    onMouseDown: function(e){
       this._isDragging = false;
       aimluck.dnd.DragMoveObject.prototype.onMouseDown.apply(this, arguments);
    },
    onFirstMove: function(e){
        this.startY = this.dragSource._lastY;
        this.startAbsoluteY = dojo._abs(dojo.byId(this.node), true).y;
        this.startX =  dojo.getComputedStyle(this.node).left;

        //Google Chrome及びSafariではdojo._absの挙動が異なるので、AbsoluteYの座標修正
        var userAgent = window.navigator.userAgent.toLowerCase();
        if (userAgent.indexOf("chrome") > -1) {
            this.startAbsoluteY += document.body.scrollTop;     // ページスクロール分を修正
        } else if(userAgent.indexOf("safari") > -1) {
            this.startAbsoluteY -= dojo.byId('weeklyScrollPane_'+this.portletId).scrollTop;     // DIVタグスクロール分を修正
        }

        lastScroll = dojo.byId('weeklyScrollPane_'+this.portletId).scrollTop;
        aimluck.dnd.DragMoveObject.prototype.onFirstMove.apply(this, arguments);
    },
    onMouseMove: function(e){
        aimluck.dnd.DragMoveObject.prototype.onMouseMove.apply(this, arguments);
        this._isDragging = true;
        var distance_scr = dojo.byId('weeklyScrollPane_'+this.portletId).scrollTop - lastScroll;

        var quotient = Math.floor((this.startY-this.startAbsoluteY) / this._rowHeight_);
        var currentQuotient = Math.floor((this.startY+this.leftTop.t-this.startAbsoluteY+distance_scr) / this._rowHeight_);
        var nextTop = 0;
        var nextHeight = 0;
        if( currentQuotient < quotient) {
            nextTop = currentQuotient*this._rowHeight_  +1;
            nextHeight = (quotient - currentQuotient+1)*this._rowHeight_;
            this.positionFrom = currentQuotient;
            this.positionTo= quotient+1;
        } else {
            nextTop = quotient*this._rowHeight_  +1;
            nextHeight = (currentQuotient - quotient+1)*this._rowHeight_;
            this.positionTo = currentQuotient+1;
            this.positionFrom = quotient;
        }

        if (nextTop + nextHeight > 864 ) {
             nextHeight = 864 - nextTop - this._rowHeight_;
             this.positionTo = 47;
        }

        this.leftTop.t = nextTop;
        this.leftTop.l = this.startX;
        this.leftTop.h = nextHeight;
        dojo.marginBox(this.node, this.leftTop);

        dojo.style(this.node, "opacity", 0.5);
    },
    onMouseUp: function(e) {
        var hour = Math.floor(this.positionFrom/2);
        hour = (hour > 9) ? hour : "0" + hour;
        var minute = Math.floor(this.positionFrom%2)*30;
        var date = ptConfig[this.portletId].jsonData.date[this.dragSource.index].substring(0, 10);
        var startTime = date + '-' + hour + '-' + minute;

        hour = Math.floor(this.positionTo/2);
        hour = (hour > 9) ? hour : "0" + hour;
        minute = Math.floor(this.positionTo%2)*30;
        var endTime = date + '-' + hour + '-' + minute;

        this.node.style.top = "0px";
        this.node.style.height = "864px";
        dojo.style(this.node, "opacity", 0.0 );
        if(this._isDragging == true){
            aipo.common.showDialog(ptConfig[this.portletId].formUrl + "&entityid=new&mode=form"  + "&form_start=" + startTime + '&form_end=' + endTime , this.portletId , aipo.officecalendar.onLoadOfficecalendarDialog);
        }
        //** FIXME IEで追加ダイアログを閉じるとスクロールバーのｙ座標が強制的に０になってしまう現象
        aipo.officecalendar.tmpScroll = parseInt(dojo.byId('weeklyScrollPane_'+this.portletId)["scrollTop"]);
        //**//

       this._isDragging = false;
       aimluck.dnd.DragMoveObject.prototype.onMouseUp.apply(this, arguments);
    }
});

// aipo.calendar.WeeklyOfficecalendarAddDraggable
dojo.declare("aipo.calendar.WeeklyOfficecalendarAddDraggable", [aimluck.dnd.Draggable], {
    DragMoveObject: aipo.calendar.WeeklyOfficecalendarAddDragMoveObject,
    constructor: function(node, params){
        this.index = params.idx;
    }
});

/*
*/

// aipo.calendar.WeeklyTermOfficecalendarAddDragMoveObject
dojo.declare("aipo.calendar.WeeklyTermOfficecalendarAddDragMoveObject", [aimluck.dnd.DragMoveObject], {
    _rowHeight_: 18,
    positionFrom: -1,
    positionTo: -1,
    _isDragging: false,
    officecalendarObjId: null,
    onMouseDown: function(e){
       this._isDragging = false;
       aimluck.dnd.DragMoveObject.prototype.onMouseDown.apply(this, arguments);
    },
    onFirstMove: function(e){
           aimluck.dnd.DragMoveObject.prototype.onFirstMove.apply(this, arguments);
           aipo.calendar.setGridArray(this.portletId, parseInt(ptConfig[this.portletId].officecalendarDivDaySum));
    },
    onMouseMove: function(e){
        aimluck.dnd.DragMoveObject.prototype.onMouseMove.apply(this, arguments);
        this._isDragging = true;
        dojo.style(this.node, "opacity", 0.5);
        var mouseX = aipo.calendar.getCurrentMouseX(e);
        var tmpIndex = mouseX.index;
        if(this.positionFrom == -1 && tmpIndex != -1) {
            this.positionFrom = tmpIndex;
            this.positionTo = this.positionFrom;
        }
        if(this.positionTo != -1 && tmpIndex != -1) {
            this.positionTo = tmpIndex;
        }
        if(this.positionTo != -1 && this.positionFrom != -1) {
            var tmpW, tmpL;
            if(this.positionTo > this.positionFrom) {
                tmpL = this.positionFrom;
                tmpW = this.positionTo - this.positionFrom + 1;
            } else {
                tmpL = this.positionTo;
                tmpW = this.positionFrom - this.positionTo + 1;
            }
            var width = 100 / ptConfig[this.portletId].officecalendarDivDaySum * tmpW;
            var left = 100 / ptConfig[this.portletId].officecalendarDivDaySum * tmpL;
            dojo.style(this.node, "left",  left + "%");
            dojo.style(this.node, "width", width + "%");

        } else {
            dojo.style(this.node, "left",  0 + "%");
            dojo.style(this.node, "width", 0 + "%");
        }
    },
    onMouseUp: function (e) {

        var left1, left2;
        if (this.positionTo != -1 && this.positionFrom != -1) {
            if(this.positionTo > this.positionFrom) {
                left1 = this.positionFrom;
                left2 = this.positionTo;
            } else {
                left2 = this.positionFrom;
                left1 = this.positionTo;
            }
            var date1 = ptConfig[this.portletId].jsonData.date[left1];
            var date2 = ptConfig[this.portletId].jsonData.date[left2];
            if(this._isDragging == true){
                aipo.common.showDialog(ptConfig[this.portletId].formUrl + "&entityid=new&mode=form&is_span=TRUE"  + "&form_start=" + date1 + '&form_end=' + date2 ,this.portletId , aipo.officecalendar.onLoadOfficecalendarDialog );
            }
            //** FIXME IEで追加ダイアログを閉じるとスクロールバーのｙ座標が強制的に０になってしまう現象
            aipo.officecalendar.tmpScroll = parseInt(dojo.byId('weeklyScrollPane_'+this.portletId)["scrollTop"]);
            //**//
        }
        this.positionFrom = -1;
        this.positionTo = -1;
        dojo.style(this.node, "left",  0 + "%");
        dojo.style(this.node, "width", 100 + "%");
        dojo.style(this.node, "opacity", 0.0 );

        aimluck.dnd.DragMoveObject.prototype.onMouseUp.apply(this, arguments);
    }
});

// aipo.calendar.WeeklyTermOfficecalendarAddDraggable
dojo.declare("aipo.calendar.WeeklyTermOfficecalendarAddDraggable", [aimluck.dnd.Draggable], {
    DragMoveObject: aipo.calendar.WeeklyTermOfficecalendarAddDragMoveObject,
    constructor: function(node, params){
        this.index = params.idx;
    }
});