﻿(function($) {
    $.fn.togglebutton = function(options) {
        var $wrapper = $(this);

        var settings = jQuery.extend({
            play: function() { playanim($wrapper) },
            pause: function() { pauseanim($wrapper) },
            updater: function() { Silder() },
            state: "off",
            interval: 1000
        }, options || {});

        var timerid = "";

        $wrapper.children(".offstate").hide();
        // returns the jQuery object to allow for chainability.  

        $wrapper.toggle(
            function() {
                $wrapper.children(".onstate").hide();
                $wrapper.children(".offstate").show();
                timerid = setInterval(settings.updater, settings.interval);
            },
            function() {
                $wrapper.children(".offstate").hide();
                $wrapper.children(".onstate").show();
                clearTimeout(timerid);
                timerid = "";
            });

        if (settings.state == "on") {
            $wrapper.toggle();
        };

        this.stop = function() {
            if (timerid != "") {
                clearTimeout(timerid);
                timerid = "";
            }
        };

        return this;
    };



})(jQuery);