// $Id: webapp.js 1746 2008-09-26 12:26:51Z keisuke $

/** 
 * class webapp
 */
var webapp = function() {
    this.appname = "";
    this.token = "";
    this.wwwpath = "/";
    this.ajaxpath = "ajax";
    this.sidname = "";
    this.js_framework = "";

    this.username = "";
    this.userid = 0;
    this.role_mode = "";
    this.userroles = null;

    this.listfilter = "";
    this.dialog = null;

    this.pagenum = 0;
    this.curpage = 1;    
    this.allnum = 0;
}
webapp.prototype = {
    init: function() {
        Timeline.OriginalEventPainter.prototype._showBubble = function(x, y, evt) {
            //document.location.href=evt.getLink();
            return;
        }

        Timeline.OriginalEventPainter.prototype._paintEventLabel = function(evt, text, left, top, width, height, theme) {
            var doc = this._timeline.getDocument();
            
            var labelDiv = doc.createElement("a");
            labelDiv.className = 'timeline-event-label'
            
            labelDiv.style.left = left + "px";
            labelDiv.style.width = width + "px";
            labelDiv.style.top = top + "px";
            labelDiv.style.color = "#000";
            labelDiv.style.textDecoration = "none";
            labelDiv.innerHTML = text;
            labelDiv.href=evt.getLink();
            
            if(evt._title != null)
                labelDiv.title = evt._title;    
            
            var color = evt.getTextColor();
            if (color == null) {
                color = evt.getColor();
            }
            if (color != null) {
                labelDiv.style.color = color;
            }
            
            
            var classname = evt.getClassName()
            if(classname) labelDiv.className +=' ' + classname;
            
            this._eventLayer.appendChild(labelDiv);
            
            return {
                left:   left,
                    top:    top,
                    width:  width,
                    height: height,
                    elmt:   labelDiv
                    };
        }
        
        var ownobj = this;
        var d = new Date();
        var yy = d.getYear();
        var mm = d.getMonth() + 1;
        var dd = d.getDate();
        if (yy < 2000) { yy += 1900; }
        if (mm < 10) { mm = "0" + mm; }
        if (dd < 10) { dd = "0" + dd; }
        var curdate = yy + "/" + mm + "/" + dd;
        $("#timeline").css("display","block");

        var pd = new Date();
        pd.setTime(pd.getTime()-86400000);
        $('#timeline-date').datepicker({
            dateFormat: 'yy/mm/dd',
            showOn: "both",
            buttonImage: this.wwwpath+"css/common/icons/calendar.png", 
            buttonImageOnly: true,
            maxDate: pd,
            nextText: '',
            onSelect: function(date) {
                tl.loadXML(ownobj.wwwpath+"timeline/"+date+"/", function(xml, url) { eventSource.loadXML(xml, url); });
                var d = new Date(date);
                var band = tl.getBand(0);
                band.setCenterVisibleDate(d);
            }
        });
        
        var eventSource = new Timeline.DefaultEventSource();        
        var bandInfos1 = [
            Timeline.createBandInfo({
                width:          "90%", 
                intervalUnit:   Timeline.DateTime.HOUR, 
                eventSource:    eventSource,
                timeZone:       9,
                intervalPixels: 50
            }),
            Timeline.createBandInfo({
                width:          "10%", 
                intervalUnit:   Timeline.DateTime.DAY,
                eventSource:    eventSource,
                overview:	    true,
                timeZone:       9,
                intervalPixels: 70
            })
        ];
        bandInfos1[1].syncWith = 0;
        bandInfos1[1].highlight = true;
        tl = Timeline.create(document.getElementById("timelinebox"),
                              bandInfos1, 
                              Timeline.HORIZONTAL);
        tl.loadXML(this.wwwpath+"timeline/"+curdate+"/", function(xml, url) { eventSource.loadXML(xml, url); });
    },

    showhelp: function() {
        var ownobj = this;
        this.displayDialog({
            title: ownobj._("help"),
            width: 400,
            height: 300,
            modal: true,
            resizable: true,
            buttons: {
                'OK': function() {
                    ownobj.closeDialog();
                }
            }
        });
        this.setDialogContents(ownobj._("timeline help contents"));
    },
    
    getList: function(classname,page,funcname,mergedata) {
        this.resetMCE("contents");
        this.startLoading();
        var ownobj = this;

        if(page==0) {
            page=this.curpage;
        }

        var data = {
            classname: classname,
            method: "getList",
            page: page,
            filter: this.listfilter,
            token: this.token    
        };
        
        if(this.js_framework=="jquery") {
            if(mergedata != undefined) {
                $.extend(data,mergedata);
            } 
            $.ajax({url: this.wwwpath+this.ajaxpath+"/",
                    data: data,
                    type: "POST",
                    dataType: "json",
                    success: function(data, textStatus) {
                        ownobj.finishLoading();
                        ownobj.token = data.token;
                        if(data.success) {
                            $("#content").html(data.contents);
                            $.tablesorter.defaults.widgets = ['zebra'];
                            $("#list").tablesorter();
                            ownobj.curpage = page;
                            ownobj.pagenum = data.pagenum;
                            ownobj.allnum = data.allnum;
                            ownobj.displayPageNavi(funcname);
                        } else {
                            ownobj.displayAlert(data.errormsg);
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrow) {
                        ownobj.finishLoading();
                        ownobj.displayAlert(textStatus+"<br />"+webappobj._("please reload the page"));
                    }
                });
        } else if(this.js_framework=="Ext") {
            if(mergedata != undefined) {
                date = Ext.apply(data,mergedata);
            } 
            Ext.Ajax.request({
                    url: this.wwwpath+this.ajaxpath+"/",
                    params: data,
                    method: "POST",
                    success: function(response) {
                        ownobj.finishLoading();
                        var data = eval("("+response.responseText+")");
                        if(data.success) {
                            Ext.get("content").dom.innerHTML = data.contents;
                            var grid = new Ext.grid.TableGrid("list",{stripeRows: true});
                            grid.render();
                            ownobj.curpage = page;
                            ownobj.pagenum = data.pagenum;
                            ownobj.allnum = data.allnum;
                            ownobj.displayPageNavi(funcname);
                        } else {
                            ownobj.displayAlert(data.errormsg);
                        }
                    },
                    failure: function(response) {
                        ownobj.finishLoading();
                        ownobj.displayAlert(response.statusText+"<br />"+webappobj._("please reload the page"));
                    }
                });
        }
    },
    
    getForm: function(classname,id) {
        this.resetMCE("contents");

        this.startLoading();
        var ownobj = this;
        if(this.js_framework=="jquery") {
            $.ajax({
                    url: this.wwwpath+this.ajaxpath+"/",
                    data: {
                        classname: classname,
                        method: "getForm",
                        itemid: id,
                        token: this.token
                    },
                    type: "POST",
                    dataType: "json",
                    success: function(data, textStatus) {
                        ownobj.finishLoading();
                        ownobj.token = data.token;
                        if(data.success) {
                            $("#content").html(data.contents);
                            if(data.mce && tinyMCE != undefined) {
                                eval("tinyMCE.init("+data.mce+");");
                            }
                        } else {
                            ownobj.displayAlert(data.errormsg);
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrow) {
                        ownobj.finishLoading();
                        ownobj.displayAlert(textStatus+"<br />"+webappobj._("please reload the page"));
                    }
                });
        } else if(this.js_framework=="Ext") {
            Ext.Ajax.request({
                    url: this.wwwpath+this.ajaxpath+"/",
                    params: {
                        classname: classname,
                        method: "getForm",
                        itemid: id,
                        token: this.token
                    },
                    method: "POST",
                    success: function(response) {
                        ownobj.finishLoading();
                        var data = eval("("+response.responseText+")");
                        if(data.success) {
                            Ext.get("content").dom.innerHTML = data.contents;
                            if(data.mce && tinyMCE != null) {
                                eval("tinyMCE.init("+data.mce+");");
                            }
                        } else {
                            ownobj.displayAlert(data.errormsg);
                        }
                    },
                    failure: function(response) {
                        ownobj.finishLoading();
                        ownobj.displayAlert(response.statusText+"<br />"+webappobj._("please reload the page"));
                    }
                });
        }            
    },

    resetMCE: function(target) {
        if(tinyMCE == null) {
            return;
        }
        if(this.js_framework=="jquery") {
            if(tinyMCE.activeEditor!=undefined) {
                if($("#"+target).length!=0) {
                    tinyMCE.execCommand('mceRemoveControl', true, target);
                } else {
                    tinyMCE.remove(tinyMCE.activeEditor);
                }
            }
        } else if(this.js_framework=="Ext") {
            if(tinyMCE.activeEditor!=undefined) {
                if(Ext.get(target)!=null) {
                    tinyMCE.execCommand('mceRemoveControl', true, target);
                } else {
                    tinyMCE.remove(tinyMCE.activeEditor);
                }
            }
        }
    },
    

    startLoading: function(target) {
        if(this.js_framework=="jquery") {
            if($("#mask").length==0) {
                if(target == undefined) {
                    var el = "body";
                } else {
                    var el = target;
                }
                $(el).append("<div id='mask'><div id='maskmsg'>"+webappobj._("Loading...")+"</div></div>");            
            }
        } else if(this.js_framework=="Ext") {
            if(target == undefined) {
                var el = Ext.getBody();
            } else {
                var el = Ext.get(target);
            }
            if(!el.isMasked()) {
                el.mask(webappobj._("Loading..."),"mskmsg");
            }
        }
    },

    finishLoading: function(target) {
        if(this.js_framework=="jquery") {
            if($("#mask").length!=0) {
                $("#mask").remove();
            }
        } else if(this.js_framework=="Ext") {
            if(target == undefined) {
                var el = Ext.getBody();
            } else {
                var el = Ext.get(target);
            }
            if(el.isMasked()) {
                el.unmask();
            }
        }
    },

    displayDialog: function(options) {
        if(this.js_framework=="jquery") {
            if($("#dialogbox").length==0) {
                $("body").append("<div id='dialogbox' class='flora'></div>");
            }
            $("#dialogbox").dialog(options);
            this.sizingDialogContents();
        } else if(this.js_framework=="Ext") {
            if(this.dialog!=null) {
                this.dialog.destroy();
                this.dialog = null;
            }
            this.dialog = new Ext.Window(options);
            this.dialog.show(Ext.getBody());
        } 
    },

    setDialogContents: function(contents) {
        if(this.js_framework=="jquery") {
            $("#dialogbox").html(contents);
            if($("#dialogbox").css("display")=="none") {
                $("#dialogbox").css("display","block");
            }
        } else if(this.js_framework=="Ext") {
            if(this.dialog!=null) {
                this.dialog.body.dom.innerHTML = contents;
            }
        } 
    },

    sizingDialogContents: function() {
        if(this.js_framework=="jquery") {
            dh = $(".ui-dialog").height();
            th = $(".ui-dialog-titlebar").height();
            bh = $(".ui-dialog-buttonpane").height();
            $("#dialogbox").css("height",dh-th-bh-25);
        }
    },

    closeDialog: function() {
        if(this.js_framework=="jquery") {
            if($("#dialogbox").length) {
                $("#dialogbox").dialog("destroy");
            }
        } else if(this.js_framework=="Ext") {
            if(this.dialog!=null) {
                this.dialog.destroy();
                this.dialog = null;
            }
        } 
    },
    
    displayConfirm: function(okfunc,confirm) {
        if(this.js_framework=="jquery") {
            this.displayDialog({
                    title: webappobj._("confirm"),
                    width: 400,
                    height: 120,
                    modal: true,
                    resizable: false,
                    buttons: {
                        "OK": okfunc,
                        "cancel": function() {
                            webappobj.closeDialog();
                        }
                    }
                });
            this.setDialogContents(confirm);
        } else if(this.js_framework=="Ext") {
            Ext.MessageBox.confirm(webappobj._("confirm"), confirm, function(btn) {
                    if(btn=="yes") {
                        okfunc();
                    }
                },this);
        }
    },

    displayAlert: function(alert, icon) {
        if(this.js_framework=="jquery") {
            this.displayDialog({
                    title: webappobj._("alert"),
                    width: 400,
                    height: 120,
                    modal: true,
                    resizable: false,
                    buttons: {
                        "OK": function() {
                            webappobj.closeDialog();
                        }
                    }
                });
            this.setDialogContents(alert);
        } else if(this.js_framework=="Ext") {
            if(icon == undefined) {
                icon = Ext.MessageBox.WARNING;
            }
            
            Ext.MessageBox.show({
                    title: webappobj._("alert"),
                    msg: alert,
                    buttons: Ext.MessageBox.OK,
                    icon: icon
                });
        }
    },

    displayPageNavi: function(funcname) {
        if(funcname=="") {
            return;
        }
        
        if(this.js_framework=="jquery") {
            var naviobj = $(".navi");
        } else if(this.js_framework=="Ext") {
            var naviobj = Ext.query(".navi");
        }
        var navistr = "";
        navistr += "<span class='pagenaviall'>"+webappobj._("total : ")+this.allnum+webappobj._("items")+"</span>";
        if(naviobj.length != 0 && this.pagenum > 1) {
            navistr += "<span class='pagenavinum'>";
            navistr += "<a href='javascript: "+funcname+"(1)'>&lt;&lt;</a>";
            navistr += "</span>";
            for(i=1;i<=this.pagenum;i++) {
                if(i!=this.curpage) {
                    navistr += "<span class='pagenavinum'>";
                    navistr += "<a href='javascript: "+funcname+"("+i+")'>"+i+"</a>";
                } else {
                    navistr += "<span class='pagenavinum curpage'>";
                    navistr += i;
                }
                navistr += "</span>";
            }
            navistr += "<span class='pagenavinum'>";
            navistr += "<a href='javascript: "+funcname+"("+this.pagenum+")'>&gt;&gt;</a>";
            navistr += "</span>";
        }
        if(this.js_framework=="jquery") {
            naviobj.html(navistr);
        } else if(this.js_framework=="Ext") {
            for(var i=0;i<naviobj.length;i++) {
                naviobj[i].innerHTML = navistr;
            }
        }
    },

    resetPaging: function() {
        this.pagenum = 0;
        this.curpage = 1;
        this.allnum = 0;
        this.listfilter = "";
    },
    
    checkRole: function(target,type,obj) {
        if(obj == undefined) {
            obj = this;
        }
        for(var i=0;i<obj.userroles.length;i++) {
            var item = obj.userroles[i];
            if(item.target==target && item.type==type) {
                return (obj.role_mode=="blacklist")?false:true;
            }
        }
        return (obj.role_mode=="blacklist")?true:false;
    },

    _: function(str) {
        var ret = str;
        if(this.appname && this.appRes != undefined) {
            eval("var res = this.appRes."+this.appname+";");
            for(var key in res) {
                if(key==str) {
                    value = res[key];
                    if(value!="") {
                        ret = decodeURIComponent(value);
                    }
                    break;
                }
            }
        }
        return ret;
    }
}

var tinyMCE = null;
    
/** 
 * decode strings as JavaScript code.
 * see includes/Utiliti.class.php Utility::jsEncode().
 */
function jsdecode(str) {
    eval(decodeURIComponent(unescape(str)));
}

webapp.prototype.appRes = {"lhjunction":{"please reload the page":"\u30da\u30fc\u30b8\u3092\u30ea\u30ed\u30fc\u30c9\u3057\u3066\u304f\u3060\u3055\u3044","Loading...":"\u30ed\u30fc\u30c9\u4e2d\u30fb\u30fb\u30fb","confirm":"\u78ba\u8a8d","alert":"\u8b66\u544a","items":"\u4ef6","total : ":"\u30c8\u30fc\u30bf\u30eb\uff1a","help":"\u30d8\u30eb\u30d7","timeline help contents":"\u3053\u306e\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u306b\u306f\u30e9\u30a4\u30bf\u30fc\u8a18\u4e8b\u304a\u3088\u3073\u30d6\u30ed\u30b0\u8a18\u4e8b\u304c\u516c\u958b\u3055\u308c\u305f\u9806\u306b\u4e00\u89a7\u3055\u308c\u307e\u3059\u3002<br \/>\u30de\u30a6\u30b9\u3067\u5de6\u53f3\u306b\u30c9\u30e9\u30c3\u30b0\u3059\u308b\u3053\u3068\u3067\u8868\u793a\u7bc4\u56f2\u3092\u79fb\u52d5\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/><br \/>\u30da\u30fc\u30b8\u30ed\u30fc\u30c9\u6642\u306f\u300c\u672c\u65e5\u5206\u300d\u306e\u307f\u304c\u4e00\u89a7\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\u300c\u672c\u65e5\u5206\u300d\u4ee5\u5916\u306e\u8a18\u4e8b\u306e\u4e00\u89a7\u3092\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u306b\u52a0\u3048\u308b\u306b\u306f\u3001\u5de6\u4e0a\u306e\u65e5\u4ed8\u30bb\u30ec\u30af\u30bf\u30fc\u3067\u5e0c\u671b\u306e\u65e5\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u6307\u5b9a\u3057\u305f\u65e5\u306e\u8a18\u4e8b\u304c\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u306b\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002"}};

