kff.widgets.Button = function(element, options)
{
	this.options = $.extend({
		repeatInterval: 50,
		delay: 100,
		onDown: null,
		onUp: null,
		onPress: null
	}, options);
	this.$element = $(element);
	this.delayTimer = null;
	this.repeatTimer = null;
};

kff.widgets.Button.prototype.init = function()
{
	this.$element.bind('mousedown', $.proxy(this.mouseDown, this));
};

kff.widgets.Button.prototype.destroy = function()
{
	this.$element.unbind('mousedown', this.mouseDown);
};

kff.widgets.Button.prototype.mouseDown = function(event)
{
	this.$element.addClass('pressed');
	this.$element.bind('mouseenter', $.proxy(this.mouseEnter, this));
	this.$element.bind('mouseleave', $.proxy(this.mouseLeave, this));
	$(document).bind('mouseup', $.proxy(this.mouseUp, this));
	if(typeof this.options.onDown == 'function')  this.options.onDown();
	clearTimeout(this.delayTimer);
	this.delayTimer = setTimeout($.proxy(this.repeatStart, this), this.options.delay);
	return false;
};

kff.widgets.Button.prototype.mouseEnter = function(event)
{
	this.$element.addClass('pressed');
	this.repeatStart();
};

kff.widgets.Button.prototype.mouseLeave = function(event)
{
	this.$element.removeClass('pressed');
	this.repeatStop();
};

kff.widgets.Button.prototype.mouseUp = function(event)
{
	this.repeatStop();	
	$('body').css({ cursor: 'auto'});
	$(document).unbind('mouseup', this.mouseUp);
	this.$element.unbind('mouseenter', this.mouseEnter);
	this.$element.unbind('mouseleave', this.mouseLeave);
	if(typeof this.options.onUp == 'function' && this.$element.hasClass('pressed')) this.options.onUp();
	this.$element.removeClass('pressed');
	event.preventDefault(); 
	return false;
}

kff.widgets.Button.prototype.repeatStart = function()
{
	clearTimeout(this.delayTimer);
	clearInterval(this.timer);
	this.timer = setInterval($.proxy(function(){
		if(typeof this.options.onPress == 'function') this.options.onPress();
	},this), this.options.repeatInterval);
};

kff.widgets.Button.prototype.repeatStop = function()
{
	clearTimeout(this.delayTimer);
	clearInterval(this.timer);
};


kff.widgets.ButtonSlider = function(element, options)
{
	options = $.extend({
		step: 1
	}, options);
	kff.widgets.Slider.call(this, element, options);
};

kff.extend(kff.widgets.ButtonSlider, kff.widgets.Slider);

kff.widgets.ButtonSlider.prototype.render = function()
{
	kff.widgets.Slider.prototype.render.call(this);
	
	this.$buttonPlus = $('<div class="kff-slider-button-plus"></div>').appendTo(this.$sliderBox);
	this.$buttonMinus = $('<div class="kff-slider-button-minus"></div>').appendTo(this.$sliderBox);	
	
	this.buttonPlus = new kff.widgets.Button(this.$buttonPlus, {onDown: $.proxy(this.increase, this), onPress: $.proxy(this.increase, this) });
	this.buttonMinus = new kff.widgets.Button(this.$buttonMinus, {onDown: $.proxy(this.decrease, this), onPress: $.proxy(this.decrease, this) });

};

kff.widgets.ButtonSlider.prototype.init = function()
{
	kff.widgets.ButtonSlider._super.init.call(this);
	
	this.buttonPlus.init();
	this.buttonMinus.init();
};

kff.widgets.ButtonSlider.prototype.destroy = function()
{
	kff.widgets.ButtonSlider._super.destroy.call(this);
	
	this.buttonPlus.destroy();
	this.buttonMinus.destroy();
};

kff.widgets.ButtonSlider.prototype.increase = function()
{
	this.setPosition(this.position + this.options.step);
};

kff.widgets.ButtonSlider.prototype.decrease = function()
{
	this.setPosition(this.position - this.options.step);
};

