// $Id$



$.fn.extend({ 
    radioClass: function(c) {  
        return $(this).addClass(c).siblings().removeClass(c).end();  
    },

    addOnClassOver : function(klass) {
	return $(this).hover(function() { $(this).addClass(klass);    },
		             function() { $(this).removeClass(klass); });
    },

    mask : function(val, opt) {
        opt      = $.extend({ mask: 'm-mask', masked: 'm-masked' }, opt || {});
	var key  = 'mask';
	var mask = this.data(key);

	if (val) {
	    if (mask) return this;
	    var mask = $('<div/>').addClass(opt.mask);
	    this.addClass(opt.masked).append(mask);
	    this.data(key, mask);
	} else if (mask) {
	    mask.remove();
	    this.removeClass(opt.masked);
	    this.removeData(key);

	    // XXX: webkit bug: overflow vs scrollbar
	    if ($.browser.safari) {
		var top = this.scrollTop();
		this.scrollTop(top < 1 ? 1 : (top - 1)).scrollTop(top);
	    }
	}
	return this;
    }


});

$.extend({
    isUndefined: function(val) {
	return typeof(val) === 'undefined';
    },

    repeat: function(fn, data) {
	var d  = $.makeArray(data);
	var c  = 0;
	
        return {
	    id    : null,
	    count : function() { 
		return c; },
	    cancel: function() {
		if (this.id != null) {
		    clearInterval(this.id);
	            this.id = null
		} },
	    start : function(msec, count, data) {
		var me = this;
		this.cancel();
		this.id = setInterval(function() {
		    if (c++ > count) {
			this.cancel();
		    } else {
			if (typeof data === 'undefined') data = d;
			fn.apply(me, $.makeArray(data));
		    } }, msec); }
        }
    },

    task : function(fn, data, scope) {
	scope = scope || {};
	gdata = $.makeArray(data);

        return {
	    id    : null,
	    cancel: function() {
		if (this.id != null) {
		    clearTimeout(this.id);
	            this.id = null
		} },
	    delay : function(msec, data) {
		this.cancel();
		var task = function() {
		    fn.apply(scope, typeof data === 'undefined' 
			                ? gdata : $.makeArray(data));
		    this.id = null;
		};
		if (msec > 0)
		    this.id = setTimeout(task, msec);
		else if (msec == 0)
		    task();  	                                        },
	    now   : function(data) {
		this.delay(0, data);                  	                }
        };
    }
});


