﻿RegExp.specials = /([.*+?^${}()|[\]\/\\])/g;
RegExp.escape = function(text) {
    return text.replace(RegExp.specials, '\\$1')
};



var HotSpringsCVB = (function() {
    var Event = Aris.Event;
    var DOM = Aris.DOM;
    var onSite = new RegExp('javascript|mailto|' + RegExp.escape(location.host.replace('www', '')));

    Event.onDOMLoaded.add(function() {
        //FontSizer = new FontSizer('main', 'fsUp', 'fsDown', 'fsNormal');

        els = document.getElementsByTagName('a');
        for (var i = 0; i < els.length; i++) {
            if (els[i].rel.indexOf(':') >= 0) {
                els[i].onclick = function() {
                    args = this.rel.split(':');
                    return !popWin(this.href, args[0], args[1], args[2], false);
                }
            }
        };
    });
})();


function printWin(url) {
    popWin(url, "printWin", 700, 500, true);
}

var popWin = function(url, name, width, height, center) {
    var win,
    opt = [];
    if (width || height) {
        height = height || 570;
        width = width || 770;
        opt.push('height=' + height, 'width=' + width);
        if (center === false) {
            opt.push('top=' + ((screen.width) ? (screen.width - width) / 2 : 0));
            opt.push('left=' + ((screen.height) ? (screen.height - height) / 2 : 0));
        }
        opt.push('scrollbars=yes', 'resizable', 'menubar=1');
    }
    win = window.open(url, name, opt.join(','));
    if (win) win.focus();
    return win;
}

var Cookies = {
    add: function(name, value, days) {
        var expires = '';
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = '; expires=' + date.toGMTString(); ;
        }
        document.cookie = name + "=" + escape(value) + expires + "; path=/";
    },
    remove: function(name) {
        this.add(name, '', -1);
    },
    get: function(name) {
        var results = document.cookie.match(name + '=([^;]*?)(;|$)');
        if (results)
            return (unescape(results[1]));
        else
            return null;
    }
};

var FontSizer = Function.extend({
    constructor: function(type, upid, downid, resetid) {
        this.type = type;
        this.baseSize = 14;
        this.minSize = 9;
        this.maxSize = 20;
        this.stepSize = 2;
        this.cookieName = type + 'FontSize';
        var x = Cookies.get(this.cookieName);
        this.currentSize = x ? parseInt(x) : this.baseSize;

        if (this.currentSize != this.baseSize)
            this.update();

        Aris.Event.add(upid, 'click', this.increase, this);
        Aris.Event.add(downid, 'click', this.decrease, this);
        Aris.Event.add(resetid, 'click', this.reset, this);
        FontSizer[type] = this;
        return this;

    },
    reset: function(e) {
        Cookies.remove(this.cookieName);
        this.currentSize = this.baseSize;
        this.update();
        Aris.Event.stopEvent(e);
    },
    update: function() {
        Cookies.add(this.cookieName, this.currentSize, 7);
        var cont = document.getElementById(this.type);
        Aris.DOM.setStyle(cont, 'fontSize', (this.currentSize / this.baseSize) + 'em');

    },
    increase: function(e) {
        this.currentSize = Math.min(this.currentSize + this.stepSize, this.maxSize);
        this.update();
        Aris.Event.stopEvent(e);
    },
    decrease: function(e) {
        this.currentSize = Math.max(this.currentSize - this.stepSize, this.minSize);
        this.update();
        Aris.Event.stopEvent(e);
    }
});

Aris.Menu = Function.extend({

    constructor: function(rootEl, config) {
        this.root = Aris.DOM.get(rootEl);
        this._items = [];
        this.config = {
            active: 'active',
            selected: 'sel',
            menu: 'menu',
            closeDelay: 100,
            openDelay: 100
        };
        $merge(this.config, config);
        Aris.Menu.all.push(this);
    },

    _getActivator: function(n) {
        while (n.previousSibling) {
            n = n.previousSibling;
            if (n.nodeName == "A") return n;
        } return null;
    },

    addMenu: function(el, parent) {
        if (!el.parentNode) this.root.appendChild(el);
        var p = el.parentNode;
        Aris.DOM.addClass(p, 'parent');
        var a = this._getActivator(el);
        Aris.DOM.addClass(a, 'trigger');
        var mi = { menu: el, parent: p, activator: a, timer: null }
        this._items.push(mi);
        return mi;
    },

    closeAll: function() {
        this._items.forEach(this.closeMenu, this);
    },

    onactivate: function(mi) {
        if (mi.timer) { mi.timer = this.clearTimer(mi.timer); }
        mi.timer = setTimeout(function() { this.openMenu(mi) } .bind(this), this.config.openDelay);
    },

    ondeactivate: function(mi) {
        if (mi.timer) { mi.timer = this.clearTimer(mi.timer); }
        mi.timer = setTimeout(function() { this.closeMenu(mi) } .bind(this), this.config.closeDelay);
    },

    openMenu: function(mi) {
        Aris.DOM.addClass(mi.parent, this.config.active);
        Aris.DOM.addClass(mi.menu, this.config.menu);
        Aris.DOM.addClass(mi.activator, this.config.selected);
        if (!mi.menu.style.width) Aris.DOM.setStyle(mi.menu, 'width', mi.menu.offsetWidth + 'px');

        var count = 0 - mi.menu.offsetWidth;
        while (mi.menu.offsetLeft + mi.menu.offsetParent.offsetLeft + mi.menu.offsetWidth > document.forms[0].offsetWidth + document.forms[0].offsetLeft) {
            Aris.DOM.setStyle(mi.menu, 'left', 'auto');
            Aris.DOM.setStyle(mi.menu, 'right', count + 'px');
            count++;
        }
    },

    closeMenu: function(mi) {
        Aris.DOM.removeClass(mi.parent, this.config.active);
        Aris.DOM.removeClass(mi.menu, this.config.menu);
        Aris.DOM.removeClass(mi.activator, this.config.selected);
        Aris.DOM.setStyle(mi.menu, 'right', '');
        Aris.DOM.setStyle(mi.menu, 'left', '');
        mi.timer = this.clearTimer(mi.timer);
    },

    clearTimer: function(t) {
        clearTimeout(t);
        return t = null;
    }


}, { //begin static interface
    all: []
});


Aris.Menu.DropDown = Aris.Menu.extend({
    constructor: function(el, config) {
        this.base(el, config);
        var uls = Aris.DOM.get(el).getElementsByTagName('UL');
        forEach(uls, this.addMenu, this);
    },

    addMenu: function(el, parent) {
        var mi = this.base(el, parent);
//		var a = mi.activator.cloneNode(true);
//		a.className = '';
//		if ( a.firstChild.appendData ) a.firstChild.appendData(' Main');
//		var li = Aris.DOM.create('li',  { className: 'alt', children: [ a ] } );
//		mi.menu.insertBefore(li,mi.menu.firstChild);
        Aris.Event.add(mi.parent, 'mouseover', function(ev) { this.onactivate(mi); }, this);
        Aris.Event.add(mi.parent, 'mouseout', function(ev) { this.ondeactivate(mi); }, this);
    }

}, {
    init: function() {
        Aris.Event.onDOMLoaded.add(function() {

            Aris.DOM.getElementsByClass('dropdown').forEach(function(ddmenu) {
                return new Aris.Menu.DropDown(ddmenu);
            });
        });
    }
});