﻿String.format = function(text) {
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return text.replace(pattern, function(capture) { return args[parseInt(capture.match(/\d+/)) + 1]; });
}
var CookieManager = {
    getCookie: function(name) {
        if (!name) { return null };
        var start = document.cookie.indexOf(name + '=');
        var len = start + name.length + 1;
        if ((!start) && (name != document.cookie.substring(0, name.length))) { return null; }
        if (start == -1) { return null; }
        var end = document.cookie.indexOf(";", len);
        if (end == -1) { end = document.cookie.length; }
        return unescape(document.cookie.substring(len, end));
    },
    setCookie: function(name, value, path, domain, expiredays) {
        var cookieArray = new Array();
        cookieArray.push(String.format('{0}={1}', name, escape(value)));
        cookieArray.push(String.format('path={0}', path ? path : '/'));
        if (domain) { cookieArray.push(String.format('domain={0}', domain)); }
        if (expiredays) {
            var expireDate = new Date();
            expireDate.setDate(expireDate.getDate() + expiredays);
            cookieArray.push(String.format('expires={0}', expireDate.toGMTString()));
        }
        document.cookie = cookieArray.join(';');
    },
    deleteCookie: function(name, path) {
        if (this.getCookie(name)) {
            var cookieArray = new Array();
            cookieArray.push(String.format('{0}=', name));
            cookieArray.push(String.format('path={0}', path ? path : '/'));
            var expireDate = new     Date();
            expireDate.setDate(expireDate.getDate() - 365);
            cookieArray.push(String.format('expires={0}', expireDate.toGMTString()));
            document.cookie = cookieArray.join(';');
        }
    }
};
var BizElement = {
    initState: false,
    Element: { content: null, leftborder: null, rightborder: null, contentLine: null, main1: null, main2: null },
    Size: { bodyHeight: null, contentHeight: 0, mainHeight: 0, lineHeight: 0,
        HEADERHEIGHT: 36, panelHeight: 0, FOOTERHEIGHT: 32, BORDER_TYPE1: 8, BORDER_TYPE2: 2
    }, // const
    resizable: { width: null, height: null },
    viewport: { height: 0, width: 0 },
    prevTargetBtn: null, // private
    initialize: function() {
        if (this.initState) return;
        this.initState = true;

        this.Element.content = $('content');
        this.Element.leftborder = $('leftborder');
        this.Element.rightborder = $('rightborder');
        this.Element.contentLine = $('content_line');
        this.Element.main1 = $('main1');
        this.Element.main2 = $('main2');

        this.Size.bodyHeight = document.body.offsetHeight;
        this.resizable.width = new Hash();
        this.resizable.height = new Hash();
    },
    initializeForce: function() {
        this.Size.bodyHeight = Math.max(document.body.offsetHeight, this.Element.main2.offsetHeight + this.Size.panelHeight + this.Size.FOOTERHEIGHT);
        this.Size.contentHeight = Math.max(this.viewport.height - (this.Size.HEADERHEIGHT + this.Size.FOOTERHEIGHT), this.Size.bodyHeight);
        this.Size.lineHeight = this.Size.contentHeight - this.Size.panelHeight - this.Size.BORDER_TYPE1;
        this.Size.mainHeight = this.Size.lineHeight - this.Size.BORDER_TYPE2;
    },
    resizeElement: function(e, h) {
        if (e.style.height != h + 'px') {
            e.style.height = h + 'px';
        }
    },
    getViewPort: function() {
        var viewportWidth;
        var viewportHeight;

        if (typeof window.innerWidth != 'undefined') {
            viewportWidth = window.innerWidth;
            viewportHeight = window.innerHeight;
        }
        else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
            viewportWidth = document.documentElement.clientWidth;
            viewportHeight = document.documentElement.clientHeight;
        }
        else {
            viewportWidth = document.getElementsByTagName('body')[0].clientWidth;
            viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
        }
        return { width: viewportWidth, height: viewportHeight };
    },
    checkPageSize: function() {
        var height = this.getViewPort().height;
        if (this.viewport.height == height) return false;
        this.viewport.height = height;

        this.Size.contentHeight = Math.max(this.viewport.height - (this.Size.HEADERHEIGHT + this.Size.FOOTERHEIGHT), this.Size.bodyHeight);
        if (this.Size.contentHeight <= 0) return false;

        this.Size.lineHeight = this.Size.contentHeight - this.Size.panelHeight - this.Size.BORDER_TYPE1;
        this.Size.mainHeight = this.Size.lineHeight - this.Size.BORDER_TYPE2;
        return true;
    },
    checkTargetBtn: function(A) { return this.prevTargetBtn != A; }
};
var StereoBtn = {
    BtnObjDown: null,
    BtnOptInfo: { src: null, width: null, height: null },
    Type: { normal: 0, down: 1, option: 2, optionSub: 3, optionCon: 4 },
    init: function(_optSrc, _optWidth, _optHeight) {
        if (_optSrc && _optWidth && _optHeight) {
            this.setBtnOptInfo(_optSrc, _optWidth, _optHeight);
        }
        document.observe('click', function(e) { StereoBtn.reset(e); });
    },
    setBtnOptInfo: function(_optSrc, _optWidth, _optHeight) {
        this.BtnOptInfo.src = _optSrc;
        this.BtnOptInfo.width = _optWidth;
        this.BtnOptInfo.height = _optHeight;
    },
    create: function(_containerId, _id, _type, _width, _height, _src, _action, _actionSub) {
        var isOption = _type == this.Type.option;

        var idPostpix = isOption ? '_main' : '';

        var btnObj = new Element('div', { 'id': _id + idPostpix, 'type': _type, 'action': _action, 'usable': '0' });

        btnObj.setStyle({ width: _width + 'px', height: _height + 'px', backgroundImage: 'url(' + _src + ')' });
        btnObj.observe('mouseover', function() { StereoBtn.mouseevent(this, 'over'); });
        btnObj.observe('mouseout', function() { StereoBtn.mouseevent(this, 'out'); });
        btnObj.observe('mousedown', function() { StereoBtn.mouseevent(this, 'down'); });
        btnObj.observe('mouseup', function() { StereoBtn.mouseevent(this, 'up'); });

        var containerObj;

        if (isOption) {
            $(_containerId).insert(new Element('div', { 'id': _id, 'type': this.Type.optionCon }));
            containerObj = $(_id);
        } else {
            containerObj = $(_containerId);
        }

        containerObj.insert(btnObj);

        this.eventSet(btnObj);

        if (isOption) {
            this.create(_id, _id + '_sub', this.Type.optionSub, this.BtnOptInfo.width, this.BtnOptInfo.height, this.BtnOptInfo.src, _actionSub);
        }
    },
    getType: function(btnObj) {
        return btnObj.readAttribute('type');
    },
    eventSet: function(btnObj) {
        btnObj.className = 'out';
        btnObj.setAttribute('usable', '1');
    },
    eventUnset: function(btnObj, disable) {
        if (disable) { btnObj.className = 'disable'; }
        btnObj.setAttribute('usable', '0');
    },
    reset: function(e) {
        if (!this.BtnObjDown) { return; }
        var eventTarget = Event.element(e);

        if (eventTarget == this.BtnObjDown) { return; }

        var btnObjDownType = this.BtnObjDown.getAttribute('type');

        if (btnObjDownType == this.Type.optionSub) {
            if (eventTarget == this.BtnObjDown.previousSibling) { return; }
        }

        this.eventSet(this.BtnObjDown);
        if (btnObjDownType == this.Type.optionSub) {
            this.eventSet(this.BtnObjDown.previousSibling);
        }
        this.BtnObjDown = null;
    },
    mouseevent: function(btnObj, eventType) {
        if (btnObj.getAttribute('usable') == '0') { return; }
        var btnObjType = btnObj.getAttribute('type');

        var btnObjDownType = this.BtnObjDown ? this.BtnObjDown.getAttribute('type') : null;
        var isTypeDown = (btnObjType == this.Type.down || btnObjType == this.Type.optionSub);

        switch (eventType) {
            case 'over':
                btnObj.className = 'over1';
                if (btnObjType == this.Type.option) { btnObj.nextSibling.className = 'over2'; }
                if (btnObjType == this.Type.optionSub) { btnObj.previousSibling.className = 'over2'; }
                break;
            case 'out':
                btnObj.className = 'out';
                if (btnObjType == this.Type.option) { btnObj.nextSibling.className = 'out'; }
                if (btnObjType == this.Type.optionSub) { btnObj.previousSibling.className = 'out'; }
                break;
            case 'down':
                btnObj.className = 'down';
                if (isTypeDown) {
                    if (this.BtnObjDown) {
                        this.eventSet(this.BtnObjDown);
                        if (btnObjDownType == this.Type.optionSub) {
                            this.eventSet(this.BtnObjDown.previousSibling);
                        }
                    }
                    this.eventUnset(btnObj);
                    if (btnObjType == this.Type.optionSub) {
                        this.eventUnset(btnObj.previousSibling);
                    }
                    this.BtnObjDown = btnObj;
                    eval(btnObj.getAttribute('action'));
                }
                break;
            case 'up':
                if (!isTypeDown) {
                    btnObj.className = 'over1';
                    eval(btnObj.getAttribute('action'));
                }
                break;
            default:
                break;
        }
    },
    enable: function(btnId) {
        var btnObj = $(btnId);
        if (this.getType(btnObj) == this.Type.optionCon) {
            this.eventSet(btnObj.childNodes[0], true);
            this.eventSet(btnObj.childNodes[1], true);
        } else {
            this.eventSet(btnObj, true);
        }
    },
    disable: function(btnId) {
        var btnObj = $(btnId);
        if (this.getType(btnObj) == this.Type.optionCon) {
            this.eventUnset(btnObj.childNodes[0], true);
            this.eventUnset(btnObj.childNodes[1], true);
        } else {
            this.eventUnset(btnObj, true);
        }
    }
};
var SlideMenu = {
    MenuObj: null,
    MenuId: null,
    Dir: { down: 0, up: 1 },
    Style: { normal: 0, radiobtn: 1 },
    BtnWidth: 100,
    BtnHeight: 20,
    MarginHeight: 2,
    MarginWidth: 4,
    TargetObjSlide: null,
    init: function(menuId) {
        this.MenuId = menuId;
        this.MenuObj = $(this.MenuId);
        document.observe('click', function(e) { SlideMenu.reset(e); });
    },
    create: function(_targetId, _param, _activeId) {
        var activeObj = (_activeId) ? $(_activeId) : $(_targetId);
        activeObj.setAttribute('menuparam', _param);
        activeObj.setAttribute('targetid', _targetId);
    },
    toggle: function(activeId, force) {
        var activeObj = $(activeId);
        var targetId = activeObj.getAttribute('targetid');
        var targetObj = (activeObj.id == targetId) ? activeObj : $(targetId);
        if ((targetObj == this.TargetObjSlide) && (!force)) { return; }

        if (!this.TargetObjSlide) {
            var menuParam = eval(activeObj.getAttribute('menuparam'));
            if (menuParam.preexec) { eval(menuParam.preexec); }

            var positionParam = {
                width: null, height: null, top: null, right: null, bottom: null, left: null
            };

            var btnWidth = (menuParam.width) ? menuParam.width : this.BtnWidth;
            var btnHeight = (menuParam.height) ? menuParam.height : this.BtnHeight;
            positionParam.width = btnWidth + this.MarginWidth + 'px';
            positionParam.height = (btnHeight + this.MarginHeight) * menuParam.btnset.length + 'px';

            if ((!menuParam.dir) || (menuParam.dir == this.Dir.down)) {
                positionParam.top = targetObj.positionedOffset()[1] + targetObj.getHeight() + 'px';
                positionParam.left = targetObj.positionedOffset()[0] + 'px';
            } else if (menuParam.dir == this.Dir.up) {
                positionParam.bottom = document.viewport.getHeight() - targetObj.positionedOffset()[1] + 'px';
                positionParam.left = targetObj.positionedOffset()[0] + 'px';
            }

            this.MenuObj.setStyle(positionParam);

            if ((!menuParam.style) || (menuParam.style == this.Style.normal)) {
                for (var i = 0; i < menuParam.btnset.length; i++) {
                    var element = new Element('div', { 'class': 'item out', 'action': menuParam.btnset[i][0], 'title': menuParam.btnset[i][2] });
                    element.update(menuParam.btnset[i][1]);
                    element.observe('click', function() { SlideMenu.mouseevent(this, 'click') });
                    element.observe('mouseover', function() { SlideMenu.mouseevent(this, 'over') });
                    element.observe('mouseout', function() { SlideMenu.mouseevent(this, 'out') });
                    element.setStyle({
                        width: btnWidth + 'px',
                        height: btnHeight + 'px',
                        lineHeight: btnHeight + 'px'
                    });
                    this.MenuObj.insert(element);
                }
            } else if (menuParam.style == this.Style.radiobtn) {
                for (var i = 0; i < menuParam.btnset.length; i++) {
                    var element = new Element('div', { 'class': 'item out', 'title': menuParam.btnset[i][4] });
                    var elementRbtn = new Element('input', { 'id': menuParam.btnset[i][0], 'action': menuParam.btnset[i][1], 'type': 'radio', 'name': 'rbtn' });
                    var elementLabel = new Element('label', { 'for': menuParam.btnset[i][0] });
                    element.observe('mouseover', function() { SlideMenu.mouseevent(this, 'over') });
                    element.observe('mouseout', function() { SlideMenu.mouseevent(this, 'out') });
                    element.setStyle({
                        width: btnWidth + 'px',
                        height: btnHeight + 'px',
                        lineHeight: btnHeight + 'px'
                    });
                    elementRbtn.observe('mousedown', function() { this.checked = 'checked'; SlideMenu.mouseevent(this, 'click') });
                    elementLabel.update(menuParam.btnset[i][2]);
                    elementLabel.observe('mousedown', function() { this.previousSibling.checked = 'checked'; SlideMenu.mouseevent(this.previousSibling, 'click') });
                    element.insert(elementRbtn);
                    element.insert(elementLabel);
                    if (menuParam.btnset[i][3]) {
                        elementRbtn.writeAttribute({ 'checked': 'checked' });
                    }
                    this.MenuObj.insert(element);
                }
            }
            this.TargetObjSlide = targetObj;
            Effect.Appear(this.MenuObj, { duration: 0.15 });
        } else {
            var effectParam;
            if ((targetObj == this.TargetObjSlide) || (force)) {
                effectParam = { duration: 0.15 };
            } else {
                effectParam = {
                    duration: 0,
                    afterFinish: function() {
                        SlideMenu.toggle(activeObj);
                    }
                };
            }
            this.TargetObjSlide = null;
            this.MenuObj.update();
            Effect.Fade(this.MenuObj, effectParam);
        }
    },
    reset: function(e) {
        if (!this.TargetObjSlide) { return; }

        var eventTarget = Event.element(e);

        while (true) {
            if (!eventTarget) { break; }
            if (eventTarget == this.TargetObjSlide) { return; }
            eventTarget = eventTarget.parentNode;
        }

        this.toggle(this.TargetObjSlide, true);
        this.TargetObjSlide = null;
    },
    mouseevent: function(btnObj, type) {
        if (type == 'click') {
            eval(btnObj.getAttribute('action'));
        } else {
            btnObj.toggleClassName('out');
            btnObj.toggleClassName('over');
        }
    }
};