﻿/// <reference path="jquery-1.4.4.js" />

(function ($) {
    $.fn.checkBoxToggle = function (options) {
        var defaults = {
            animated: true,
            hideOnStart: true,
            toggleElement: false
        };
        return this.each(function () {
            var opts = $.extend(defaults, options);

            var checkboxElement = $(this);
            var toggleElement = $(opts.toggleElement);

            if (opts.hideOnStart)
                toggleElement.hide();

            checkboxElement.click(function () {
                toggleElement.toggle(opts.animated ? "fast" : this.checked);
            });
        });
    };

    $.fn.radioButtonToggle = function (options) {
        var defaults = {
            animated: true,
            hideOnStart: true,
            idPrefix: false,
            callback: false
        };

        var opts = $.extend(defaults, options);
        var numButtons = this.length;
        var buttons = this;

        buttons.each(function (i) {
            thisButton = $(this);
            thisButtonID = thisButton.attr("id");

            humanIndex = i + 1;
            toggleElement = $("#" + opts.idPrefix + humanIndex);
            thisButton.data("toggleElement", toggleElement)

            if (opts.hideOnStart)
                toggleElement.hide();

            thisButton.click(function () {
                buttons.each(function (j) {
                    if (i == j)
                        $(this).data("toggleElement").show(opts.animated ? "fast" : false);
                    else
                        $(this).data("toggleElement").hide(opts.animated ? "fast" : false);
                    if (opts.callback)
                        opts.callback(i == j, j);
                });
            });
        });

        return this;
    };

    //Popover
    $.fn.popOver = function (options) {
        var defaults = {
            verticalOffset: 0,
            horizontalOffset: 0,
            popElement: false,
            displayOnStart: false,
            triggerElement: false,
            triggerEvent: "mouseenter",
            closeElement: false,
            closeEvent: "mouseleave"
        };

        return this.each(function () {
            var opts = $.extend(defaults, options);

            var targetElement = $(this);
            var popElement = $(opts.popElement);

            var targetPosition = targetElement.position();
            popElement.css({
                "left": (targetPosition.left + opts.horizontalOffset) + "px",
                "top": (targetPosition.top + opts.verticalOffset) + "px",
                "display": opts.displayOnStart ? "block" : "none"
            });

            var triggerElement = opts.triggerElement ? $(opts.triggerElement) : targetElement;
            triggerElement.bind(opts.triggerEvent, function () {
                popElement.fadeIn("slow", function () {
                    popElement.css("display", "block");
                });

                if (opts.triggerEvent == "click")
                    return false;
            });

            var closeElement = opts.closeElement ? $(opts.closeElement) : popElement;
            closeElement.bind(opts.closeEvent, function () {
                popElement.stop(true, true).fadeOut("slow", function () {
                    popElement.css("display", "none");
                });

                if (opts.triggerEvent == "click")
                    return false;
            });
        });
    };
})(jQuery);
