﻿/*extern HTMLElement, XPathResult *//*jslint browser: true, nomen: true, plusplus: true, undef: true, white: true */

if (window != top) {
    top.location.href = window.location.href;
}

window.xpath = !!(document.evaluate);
if (!window.opera) {
    if ((window.msie = /*@cc_on!@*/false)) {
        window[typeof(document.documentElement.style.maxHeight) != 'undefined' ? 'msie7' : 'msie6'] = true;
    } else if (window.ScriptEngine && (window.ScriptEngine().indexOf('InScript') != -1)) {
        window.icab = true;
    } else if (document.childNodes && (!document.all || navigator.accentColorName) && !navigator.taintEnabled) {
        if (navigator.vendor == 'KDE') {
            window.khtml = true;
        } else {
            window.webkit = window[window.xpath ? 'webkit522' : 'webkit419'] = true;
        }
    } else if (navigator.product == 'Gecko' && document.getBoxObjectFor !== null) {
        window.gecko = true;
    }
}

var analogue = (function () {
    return {
        bind: function (object, method) {
            return function () {
                return method.apply(object, arguments);
            };
        },
        create: function () {
            return function () {
                this.initialize.apply(this, arguments);
            };
        },
        extend: function (object, source) {
            if (!source) {
                source = object;
                object = this;
            }
            for (var i in source) if (source.hasOwnProperty(i)) {
                if (!object[i]) {
                    object[i] = source[i];
                }
            }
            return object;
        },
        path: '/assets/js/Analogue.',
        version: '0.9.b7 (20071110)'
    };
})();

analogue.elements = (function () {
    var booleans = /^(checked|declare|defer|disabled|ismap|multiple|noresize|readonly|selected)$/i;
    var translations = {
        'accesskey': 'accessKey',
        'cellpadding': 'cellPadding',
        'cellspacing': 'cellSpacing',
        'class': 'className',
        'colspan': 'colSpan',
        'enctype': 'encType',
        'for': 'htmlFor',
        'frameborder': 'frameBorder',
        'maxlength': 'maxLength',
        'rowspan': 'rowSpan',
        'tabindex': 'tabIndex',
        'title': 'title',
        'usemap': 'useMap',
        'value': 'value'
    };
    return {
        addClass: function (value) {
            var classes = this.className.split(' ');
            if (!this.hasClass(value)) {
                classes.push(value);
                this.className = classes.join(' ');
            }
            return this;
        },
        hasClass: function (value) {
            return new RegExp('(^|\\s)' + value + '(\\s|$)').test(this.className);
        },
        readAttribute: function (name) {
            if (name == 'style') {
                return this.style.cssText.toLowerCase();
            } else if (translations[name]) {
                return this[translations[name]];
            } else if (name.match(booleans)) {
                return this[name] ? name : null;
            } else if (window.msie && name.match(/^(href|src|type)$/i)) {
                return this.getAttribute(name, 2);
            }
            return this.getAttribute(name);
        },
        removeClass: function (value) {
            var classes = this.className.split(' '), i, ii;
            for (i = 0, ii = classes.length; i < ii; i += 1) {
                if (classes[i] == value) {
                    classes.splice(i, 1);
                }
            }
            this.className = classes.join(' ');
            return this;
        },
        toggleClass: function (value) {
            if (this.hasClass(value)) {
                this.removeClass(value);
            } else {
                this.addClass(value);
            }
            return this;
        },
        writeAttribute: function (name, value) {
            if (name == 'style') {
                this.style.cssText = value;
            } else if (translations[name]) {
                this[translations[name]] = value;
            } else if (name.match(booleans)) {
                if (value === true) {
                    this.setAttribute(name, name);
                } else {
                    this.removeAttribute(name);
                }
            } else if (name.match(/^on(\w+)$/i) && typeof(value) == 'function') {
                this.addEvent(RegExp.$1, value);
            } else {
                this.setAttribute(name, value);
            }
            return this;
        }
    };
})();

analogue.events = (function () {
    return {
        add: function (type, listener) {
            if (this.addEventListener) {
                this.addEventListener(type, listener, false);
            } else {
                if (!listener.guid) {
                    listener.guid = analogue.events.guid += 1;
                }
                if (!this.queue) {
                    this.queue = {};
                }
                var listeners = this.queue[type];
                if (!listeners) {
                    listeners = this.queue[type] = {};
                    if (this['on' + type]) {
                        listeners[0] = this['on' + type];
                    }
                }
                listeners[listener.guid] = listener;
                this['on' + type] = analogue.events.handle;
            }
            return this;
        },
        fix: function (event) {
            event.relatedTarget = event.fromElement == event.srcElement ? event.toElement : event.fromElement;
            event.pageX = event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft || 0);
            event.pageY = event.clientY + (document.documentElement.scrollTop || document.body.scrollTop || 0);
            event.preventDefault = function () {
                this.returnValue = false;
            };
            event.stopPropagation = function () {
                this.cancelBubble = true;
            };
            event.target = event.srcElement;
            event.timeStamp = new Date().getTime();
            event.which = event.charCode = event.keyCode;
            if (!event.which && event.button) {
                event.which = (event.button == 1 ? 1 : (event.button == 2 ? 3 : (event.button == 4 ? 2 : 0)));
            }
            return event;
        },
        guid: 1,
        handle: function (event) {
            event = event || analogue.events.fix(window.event);
            var listeners = this.queue[event.type];
            for (var i in listeners) if (listeners.hasOwnProperty(i)) {
                if (listeners[i].call(this, event) === false) {
                    return false;
                }
            }
            return true;
        },
        remove: function (type, listener) {
            if (this.removeEventListener) {
                this.removeEventListener(type, listener, false);
            } else {
                if (this.queue && this.queue[type] && listener.guid) {
                    delete this.queue[type][listener.guid];
                }
            }
            return this;
        }
    };
})();

analogue.methods = (function () {
    var method = window.xpath ? 'xpath' : 'css';
    return {
        addEvent: analogue.events.add,
        delegateEvent: function (type, listener, tag) {
            return this.addEvent(type, function (event) {
                var target = event.target;
                if (window.webkit && target.nodeType == 3) {
                    target = target.parentNode;
                }
                if (!tag || target.nodeName.toLowerCase() == tag) {
                    listener.apply(target, arguments);
                }
            });
        },
        getElementsByAttribute: function (name, value, tag) {
            return analogue.selectors[method](this, (tag ? tag : '') + '[' + name + (value ? '~="' + value + '"' : '') + ']');
        },
        getElementsByClassName: function (value, tag) {
            return analogue.selectors[method](this, (tag ? tag : '') + '.' + value.replace(' ', '.'));
        },
        getElementsBySelector: function (selector) {
            return analogue.selectors[method](this, selector);
        },
        querySelector: function (selector) {
            return analogue.selectors[method](this, selector)[0];
        },
        querySelectorAll: function (selector) {
            return analogue.selectors[method](this, selector);
        },
        removeEvent: analogue.events.remove
    };
})();

analogue.selectors = (function () {
    var clean = function (string) {
        return string.replace(/^\s+|\s+$/g, '');
    };
    var getElements = function (context, tag, combinator) {
        tag = (tag ? tag : '*').toLowerCase();
        var child, children, collection = [], element, elements, i, ii, j;
        for (i = 0, ii = context.length; i < ii; i += 1) {
            switch (combinator) {
            case '>':
                children = context[i].childNodes;
                for (j = 0; (child = children[j]); j += 1) {
                    if (child.nodeType == 1 && child.nodeName != '!') {
                        if (tag != '*' && child.nodeName.toLowerCase() != tag) {
                            continue;
                        }
                        collection.push(child);
                    }
                }
                break;
            case '+':
                element = context[i].nextSibling;
                while (element) {
                    if (element.nodeType == 1 && element.nodeName != '!') {
                        if (tag != '*' && element.nodeName.toLowerCase() != tag) {
                            break;
                        }
                        collection.push(element);
                        break;
                    } else {
                        element = element.nextSibling;
                        continue;
                    }
                }
                break;
            default:
                elements = context[i].getElementsByTagName(tag);
                for (j = 0; (element = elements[j]); j += 1) {
                    collection.push(element);
                }
            }
        }
        return collection;
    };
    var parse = function (context, selector) {
        context = [context];
        selector = clean(selector.replace(/\s*(>|\+)\s*/g, ' $1'));
        var combinator, element, elements, grouping, i, id, ii, j, k, kk, match, name, operator, property, tag, token, tokens = selector.split(' '), value;
        for (i = 0, ii = tokens.length; i < ii; i += 1) {
            token = clean(tokens[i]);
            if (token.match(/^(>|\+)/)) {
                combinator = RegExp.$1;
                token = token.replace(combinator, '');
            }
            if (token.indexOf('#') > -1) {
                grouping = token.split('#');
                tag = grouping[0];
                id = grouping[1];
                element = document.getElementById(id);
                if (!element || (tag && element.nodeName.toLowerCase() != tag)) {
                    return [];
                }
                context = [element];
                continue;
            }
            if (token.indexOf('.') > -1) {
                grouping = token.split('.');
                tag = grouping[0];
                elements = getElements(context, tag, combinator);
                context = [];
                for (j = 0; (element = elements[j]); j += 1) {
                    match = true;
                    for (k = 1, kk = grouping.length; k < kk; k += 1) {
                        value = new RegExp('(^|\\s)' + grouping[k] + '(\\s|$)');
                        if (!element.className || !element.className.match(value)) {
                            match = false;
                            break;
                        }
                    }
                    if (match) {
                        context.push(element);
                    }
                }
                continue;
            }
            if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
                tag = RegExp.$1;
                name = RegExp.$2;
                operator = RegExp.$3;
                value = RegExp.$4;
                elements = getElements(context, tag, combinator);
                context = [];
                for (j = 0; (element = elements[j]); j += 1) {
                    if (!(property = element.readAttribute(name))) {
                        continue;
                    } else if (operator == '=' && property != value) {
                        continue;
                    } else if (operator == '~' && !property.match(new RegExp('(^|\\s)' + value + '(\\s|$)'))) {
                        continue;
                    } else if (operator == '^' && property.indexOf(value) !== 0) {
                        continue;
                    } else if (operator == '$' && property.lastIndexOf(value) != (property.length - value.length)) {
                        continue;
                    } else if (operator == '*' && !(property.indexOf(value) + 1)) {
                        continue;
                    } else if (operator == '|' && !property.match(new RegExp('^' + value + '-'))) {
                        continue;
                    }
                    context.push(element);
                }
                continue;
            }
            context = getElements(context, token, combinator);
        }
        return context;
    };
    var resolve = function (rule) {
        var index = 1, last = null, match, parts = ['//', '*'];
        while (rule.length && rule != last) {
            last = rule;
            rule = rule.replace(/^\s*|\s*$/g, '');
            if (!rule.length) {
                break;
            }
            if ((match = (/^([#.]?)([a-z0-9\\*_\-]*)((\|)([a-z0-9\\*_\-]*))?/i).exec(rule))) {
                if (!match[1]) {
                    if (match[5]) {
                        parts[index] = match[5];
                    } else if (match[2] !== '') {
                        parts[index] = match[2];
                        if (parts[index - 1] == '/following-sibling::') {
                            parts.push('[1]');
                        }
                    }
                } else if (match[1] == '#') {
                    parts.push('[@id="' + match[2] + '"]');
                } else if (match[1] == '.') {
                    parts.push('[contains(concat(" ", @class, " "), " ' + match[2] + ' ")]');
                }
                rule = rule.substr(match[0].length);
            }
            if ((match = (/\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"\s*\]/i).exec(rule))) {
                switch (match[2]) {
                case '~':
                    parts.push('[contains(concat(" ", @' + match[1] + ', " "), " ' + match[3] + ' ")]');
                    break;
                case '^':
                    parts.push('[starts-with(@' + match[1] + ', "' + match[3] + '")]');
                    break;
                case '$':
                    parts.push('[substring(@' + match[1] + ', (string-length(@' + match[1] + ') - string-length("' + match[3] + '") + 1))="' + match[3] + '"]');
                    break;
                case '*':
                    parts.push('[contains(@' + match[1] + ', "' + match[3] + '")]');
                    break;
                case '|':
                    parts.push('[starts-with(@' + match[1] + ', "' + match[3] + '-")]');
                    break;
                default:
                    parts.push('[@' + match[1] + '="' + match[3] + '"]');
                }
                rule = rule.substr(match[0].length);
            } else if ((match = (/^\[([^\]]*)\]/i).exec(rule))) {
                parts.push('[@' + match[1] + ']');
                rule = rule.substr(match[0].length);
            }
            while ((match = (/^:([a-z_\-])+/i).exec(rule))) {
                rule = rule.substr(match[0].length);
            }
            if ((match = (/^(\s*[>+\s])?/i).exec(rule)) && match[0].length) {
                if (match[0].indexOf('>') != -1) {
                    parts.push('/');
                } else if (match[0].indexOf('+') != -1) {
                    parts.push('/following-sibling::');
                } else {
                    parts.push('//');
                }
                index = parts.length;
                parts.push('*');
                rule = rule.substr(match[0].length);
            }
            if ((match = (/^\s*,/i).exec(rule))) {
                parts.push(' | ', '//', '*');
                index = parts.length - 1;
                rule = rule.substr(match[0].length);
            }
        }
        return parts.join('');
    };
    var unique = function (collection) {
        var element, filtered = [], i;
        for (i = 0; (element = collection[i]); i += 1) {
            if (element.included || element.nodeName == '!') {
                continue;
            }
            element.included = true;
            filtered.push(element);
        }
        for (i = 0; (element = filtered[i]); i += 1) {
            element.included = null;
        }
        return filtered;
    };
    return {
        css: function (context, selector) {
            var collection = [], i, ii, sets = selector.split(',');
            for (i = 0, ii = sets.length; i < ii; i += 1) {
                collection = collection.concat(parse(context, sets[i]));
            }
            return unique(collection);
        },
        xpath: function (context, selector) {
            var collection = [], elements = document.evaluate(resolve(selector), context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null), i, ii;
            for (i = 0, ii = elements.snapshotLength; i < ii; i += 1) {
                collection.push(elements.snapshotItem(i));
            }
            return collection;
        }
    };
})();

analogue.extend(window, {
    addEvent: function (type, listener) {
        analogue.events.add.apply(window, arguments);
    },
    removeEvent: function (type, listener) {
        analogue.events.remove.apply(window, arguments);
    }
});

analogue.extend(document, {
    addEvent: function (type, listener) {
        if (type == 'ready' || type == 'domready' || type == 'DOMContentLoaded') {
            if (!document.ready.fired) {
                document.ready.queue.push(listener);
            } else {
                listener();
            }
            return this;
        }
        analogue.events.add.apply(document, arguments);
    },
    get: function (what) {
        var attempt = 'getElementById,getElementsByTagName,getElementsByClassName,querySelectorAll'.split(','), i, ii, result;
        for (i = 0, ii = attempt.length; i < ii; i += 1) {
            try {
                result = document[attempt[i]](what);
                if (result !== null && result.length !== 0) {
                    return result;
                }
            } catch (e) {}
        }
        return null;
    },
    head: document.getElementsByTagName('head')[0],
    loadScript: function (source) {
        if (!this.ready.fired && (window.msie || window.webkit || window.icab)) {
            this.write('<script src="' + source + '" type="text\/javascript"><\/script>');
        } else {
            var script = document.createElement('script');
            script.setAttribute('type', 'text/javascript');
            script.setAttribute('src', source);
            this.head.appendChild(script);
        }
        return this;
    },
    loadStyleSheet: function (source, media) {
        var sheet = document.createElement('link');
        sheet.setAttribute('href', source);
        sheet.setAttribute('media', media || 'screen');
        sheet.setAttribute('rel', 'stylesheet');
        sheet.setAttribute('type', 'text/css');
        this.head.appendChild(sheet);
        return this;
    },
    ready: function () {
        if (document.ready.fired) {
            return;
        }
        document.ready.fired = true;
        for (var i = 0, ii = document.ready.queue.length; i < ii; i += 1) {
            document.ready.queue[i]();
        }
        document.ready.queue = [];
        if (document.removeEventListener) {
            document.removeEventListener('DOMContentLoaded', document.ready, false);
        }
        window.removeEvent('load', document.ready);
    },
    set: function (tag, attributes, content) {
        var element, i, ii;
        if (window.msie && attributes) {
            for (i in attributes) if (i.match(/^(checked|multiple|name|readonly|type)$/i)) {
                tag += ' ' + i + '="' + attributes[i] + '"';
                delete attributes[i];
            }
            tag = '<' + tag + '>';
        }
        element = document.createElement(tag);
        if (attributes) {
            for (i in attributes) if (attributes.hasOwnProperty(i)) {
                element.writeAttribute(i, attributes[i]);
            }
        }
        if (content) {
            if (typeof(content) == 'string') {
                element.appendChild(document.createTextNode(content));
            } else {
                for (i = 0, ii = content.length; i < ii; i += 1) {
                    if (typeof(content[i]) == 'string') {
                        content[i] = document.createTextNode(content[i]);
                    }
                    element.appendChild(content[i]);
                }
            }
        }
        return element;
    }
});
analogue.extend(document.ready, {
    fired: false,
    queue: []
});

analogue.extend(document, analogue.methods);
analogue.extend(analogue.elements, analogue.methods);

if (window.webkit419) {
    var HTMLElement = function () {};
    HTMLElement.prototype = window['[[DOMElement.prototype]]'];
    document.loadScript(analogue.path + 'webkit419.js');
}
if (typeof(HTMLElement) == 'undefined') {
    document.create = document.createElement;
    document.createElement = function (tag) {
        return analogue.extend(document.create(tag), analogue.elements);
    };
    document.addEvent('ready', function () {
        var element, elements = document.getElementsByTagName('*'), i;
        for (i = 0; (element = elements[i]); i += 1) {
            analogue.extend(elements[i], analogue.elements);
        }
    }); 
} else {
    analogue.extend(HTMLElement.prototype, analogue.elements);
}

if (window.msie) {
    document.loadScript(analogue.path + (window.msie6 ? 'msie6.js' : 'msie7.js'));
}
document.loadScript(analogue.path + 'forms.js');
document.loadScript(analogue.path + 'atp.js');

if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', document.ready, false);
}
window.addEvent('load', document.ready);