﻿
(function (f) {
    function p(a, b, c) { var h = c.relative ? a.position().top : a.offset().top, e = c.relative ? a.position().left : a.offset().left, i = c.position[0]; h -= b.outerHeight() - c.offset[0]; e += a.outerWidth() + c.offset[1]; var j = b.outerHeight() + a.outerHeight(); if (i == "center") h += j / 2; if (i == "bottom") h += j; i = c.position[1]; a = b.outerWidth() + a.outerWidth(); if (i == "center") e -= a / 2; if (i == "left") e -= a; return { top: h, left: e} } function t(a, b) {
        var c = this, h = a.add(c), e, i = 0, j = 0, m = a.attr("title"), q = n[b.effect], k, r = a.is(":input"), u = r && a.is(":checkbox, :radio, select, :button"),
s = a.attr("type"), l = b.events[s] || b.events[r ? u ? "widget" : "input" : "def"]; if (!q) throw 'Nonexistent effect "' + b.effect + '"'; l = l.split(/,\s*/); if (l.length != 2) throw "Tooltip: bad events configuration for " + s; a.bind(l[0], function (d) { if (b.predelay) { clearTimeout(i); j = setTimeout(function () { c.show(d) }, b.predelay) } else c.show(d) }).bind(l[1], function (d) { if (b.delay) { clearTimeout(j); i = setTimeout(function () { c.hide(d) }, b.delay) } else c.hide(d) }); if (m && b.cancelDefault) { a.removeAttr("title"); a.data("title", m) } f.extend(c,
{ show: function (d) {
    if (!e) { if (m) e = f(b.layout).addClass(b.tipClass).appendTo(document.body).hide().append(m); else if (b.tip) e = f(b.tip).eq(0); else { e = a.next(); e.length || (e = a.parent().next()) } if (!e.length) throw "Cannot find tooltip for " + a; } if (c.isShown()) return c; e.stop(true, true); var g = p(a, e, b); d = d || f.Event(); d.type = "onBeforeShow"; h.trigger(d, [g]); if (d.isDefaultPrevented()) return c; g = p(a, e, b); e.css({ position: "absolute", top: g.top, left: g.left }); k = true; q[0].call(c, function () { d.type = "onShow"; k = "full"; h.trigger(d) });
    g = b.events.tooltip.split(/,\s*/); e.bind(g[0], function () { clearTimeout(i); clearTimeout(j) }); g[1] && !a.is("input:not(:checkbox, :radio), textarea") && e.bind(g[1], function (o) { o.relatedTarget != a[0] && a.trigger(l[1].split(" ")[0]) }); return c
}, hide: function (d) { if (!e || !c.isShown()) return c; d = d || f.Event(); d.type = "onBeforeHide"; h.trigger(d); if (!d.isDefaultPrevented()) { k = false; n[b.effect][1].call(c, function () { d.type = "onHide"; k = false; h.trigger(d) }); return c } }, isShown: function (d) { return d ? k == "full" : k }, getConf: function () { return b },
    getTip: function () { return e }, getTrigger: function () { return a }
}); f.each("onHide,onBeforeShow,onShow,onBeforeHide".split(","), function (d, g) { f.isFunction(b[g]) && f(c).bind(g, b[g]); c[g] = function (o) { f(c).bind(g, o); return c } })
    } f.tools = f.tools || { version: "1.2.2" }; f.tools.tooltip = { conf: { effect: "toggle", fadeOutSpeed: "fast", predelay: 0, delay: 30, opacity: 1, tip: 0, position: ["top", "center"], offset: [0, 0], relative: false, cancelDefault: true, events: { def: "mouseenter,mouseleave", input: "focus,blur", widget: "focus mouseenter,blur mouseleave",
        tooltip: "mouseenter,mouseleave"
    }, layout: "<div/>", tipClass: "tooltip"
    }, addEffect: function (a, b, c) { n[a] = [b, c] }
    }; var n = { toggle: [function (a) { var b = this.getConf(), c = this.getTip(); b = b.opacity; b < 1 && c.css({ opacity: b }); c.show(); a.call() }, function (a) { this.getTip().hide(); a.call() } ], fade: [function (a) { var b = this.getConf(); this.getTip().fadeTo(b.fadeInSpeed, b.opacity, a) }, function (a) { this.getTip().fadeOut(this.getConf().fadeOutSpeed, a) } ] }; f.fn.tooltip = function (a) {
        var b = this.data("tooltip"); if (b) return b; a = f.extend(true,
{}, f.tools.tooltip.conf, a); if (typeof a.position == "string") a.position = a.position.split(/,?\s/); this.each(function () { b = new t(f(this), a); f(this).data("tooltip", b) }); return a.api ? b : this
    }
})(jQuery);


(function ($) {

    var pageTracker;

    $.trackPage = function (account_id, options) {
        var host = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        var script;

        // Use default options, if necessary
        var settings = $.extend({}, { onload: true, status_code: 200 }, options);
        var src = host + 'google-analytics.com/ga.js';

        function init_analytics() {
            if (typeof _gat != undefined) {
                debug('Google Analytics loaded');

                pageTracker = _gat._getTracker(account_id);

                if (settings.status_code == null || settings.status_code == 200) {
                    pageTracker._trackPageview();
                } else {
                    debug('Tracking error ' + settings.status_code);
                    pageTracker._trackPageview("/" + settings.status_code + ".html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);
                }
                if ($.isFunction(settings.callback)) {
                    settings.callback();
                }
            }
            else {
                throw "_gat is undefined"; // setInterval loading?
            }
        }

        load_script = function () {
            $.ajax({
                type: "GET",
                url: src,
                success: function () {
                    init_analytics();
                },
                dataType: "script",
                cache: true // We want the cached version
            });
        }

        // Enable tracking when called or on page load?
        if (settings.onload == true || settings.onload == null) {
            $(window).load(load_script);
        } else {
            load_script();
        }
    }


    $.trackEvent = function (category, action, label, value) {
        if (typeof pageTracker == 'undefined') {
            debug('FATAL: pageTracker is not defined'); // blocked by whatever
        } else {
            pageTracker._trackEvent(category, action, label, value);
        }
    };

    /**
    * Tracks a pageview using the given uri.
    *
    */
    $.trackPageview = function (uri) {
        if (typeof pageTracker == 'undefined') {
            debug('FATAL: pageTracker is not defined');
        } else {
            pageTracker._trackPageview(uri);
        }
    }

    /**
    * Adds click tracking to elements. Usage:
    *
    *  $('a').track()
    *
    */
    $.fn.track = function (options) {
        // Add event handler to all matching elements
        return this.each(function () {
            var element = $(this);

            // Prevent an element from being tracked multiple times.
            if (element.hasClass('tracked')) {
                return false;
            } else {
                element.addClass('tracked');
            }

            // Use default options, if necessary
            var settings = $.extend({}, $.fn.track.defaults, options);

            // Merge custom options with defaults.
            var category = evaluate(element, settings.category);
            var action = evaluate(element, settings.action);
            var label = evaluate(element, settings.label);
            var value = evaluate(element, settings.value);
            var event_name = evaluate(element, settings.event_name);

            var message = "category:'" + category + "' action:'" + action + "' label:'" + label + "' value:'" + value + "'";

            debug('Tracking ' + event_name + ' ' + message);

            // Bind the event to this element. 
            // TODO Use .live since jQuery 1.4 now supports it better.
            element.bind(event_name + '.track', function () {
                // Should we skip internal links? REFACTOR
                var skip = settings.skip_internal && (element[0].hostname == location.hostname);

                if (!skip) {
                    $.trackEvent(category, action, label, value);
                    debug('Tracked ' + message);
                } else {
                    debug('Skipped ' + message);
                }

                return true;
            });
        });

        /**
        * Checks whether a setting value is a string or a function.
        * 
        * If second parameter is a string: returns the value of the second parameter.
        * If the second parameter is a function: passes the element to the function and returns function's return value.
        */
        function evaluate(element, text_or_function) {
            if (typeof text_or_function == 'function') {
                text_or_function = text_or_function(element);
            }
            return text_or_function;
        };
    };

    /**
    * Prints to Firebug console, if available. To enable:
    *   $.fn.track.defaults.debug = true;
    */
    function debug(message) {
        if (typeof console != 'undefined' && typeof console.debug != 'undefined' && $.fn.track.defaults.debug) {
            console.debug(message);
        }
    };

    /**
    * Default (overridable) settings.
    */
    $.fn.track.defaults = {
        category: function (element) { return (element[0].hostname == location.hostname) ? 'internal' : 'external'; },
        action: 'click',
        label: function (element) { return element.attr('href'); },
        value: null,
        skip_internal: true,
        event_name: 'click',
        debug: false
    };
})(jQuery);

jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};