function RM_Button_Submit(id, type, buttonType, actions)
{
	var obSelf = this;
	this.button = document.getElementById(id);
	this.titleSpan = document.getElementById(id+'_title');
	this.input = document.getElementById(id+'_input');
	this.type = type;
	this.buttonType = buttonType;
	this.form = null;
	this.state = 'normal';
	this.reseted = false;
	
	this.actions = {
		'click'	: function(){
			$(obSelf.button).removeClass().addClass(obSelf.getClass('hover'));
			obSelf.state = 'hovered';
		}
	};
	$.extend(this.actions, actions);
	this.init();
};

RM_Button_Submit.prototype.preload = function(imgPath)
{
	(new Image).src='/'+imgPath+'/main/form/button_next_r.gif';
	(new Image).src='/'+imgPath+'/main/form/button_next_l.gif';
	(new Image).src='/'+imgPath+'/main/form/button_next_loader.gif';
	(new Image).src='/'+imgPath+'/main/form/button_l.gif';
	(new Image).src='/'+imgPath+'/main/form/button_r.gif';
	(new Image).src='/'+imgPath+'/main/form/button_small_loader.gif';
};

RM_Button_Submit.prototype.init = function()
{
	var obSelf = this;
	
	if(this.buttonType == 'submit')
	{
		$(this.getForm()).submit(function(){
			if(obSelf.reseted)
			{
				obSelf.reset();
				obSelf.reseted = false;
			}
			else
			{
				obSelf.push();
			}
		});
		$(window).keyup(function(event){
			if(obSelf.state == 'waited' && event.which == 27)
				obSelf.reset();
		});
	}
	
	$(this.button)
		.mouseover(function(){
			if(obSelf.state == 'normal')
			{
				obSelf.clearClasses();
				$(this).addClass(obSelf.getClass('hover'));
				obSelf.state = 'hovered';
			}
		})
		.mouseout(function(){
			if(obSelf.state == 'hovered' || obSelf.state == 'pushed')
			{
				obSelf.clearClasses();
				$(this).addClass(obSelf.getClass(''));
				obSelf.state = 'normal';
			}
		})
		.mousedown(function(event){
			if (obSelf.isRightClick(event))
				return;
			if(obSelf.state == 'hovered')
			{
				obSelf.clearClasses();
				$(this).addClass(obSelf.getClass('click'));
				obSelf.state = 'pushed';
			}
		})
		.mouseup(function(event){
			if (obSelf.isRightClick(event))
				return;
			if(obSelf.state == 'pushed')
			{
				if(obSelf.buttonType != 'submit')
					obSelf.actions.click( obSelf );
				// obSelf.buttonType == 'submit' ? $(obSelf.getForm()).submit() : obSelf.actions.click();
			}
		})
	;
};

RM_Button_Submit.prototype.isRightClick = function (event)
{
	return (event.button == 2);
};

RM_Button_Submit.prototype.clearClasses = function()
{
	var obSelf = this;
	var classes = ['','hover','click','wait','disabled'];
	for(var i in classes)
		$(this.button).removeClass(obSelf.getClass(classes[i]));
};

RM_Button_Submit.prototype.push = function()
{
	var obSelf = this;
	if(obSelf.state != 'waited' && obSelf.state != 'disabled')
	{
		obSelf.clearClasses();
		$(this.button).addClass(this.getClass('wait'));
		this.state = 'waited';
		this.runTimer();
	}
};

RM_Button_Submit.prototype.disable = function(suppressClassRemove)
{
	var obSelf = this;
	if (!suppressClassRemove)
		this.clearClasses();
	$(this.button).addClass(obSelf.getClass('disabled'));
	this.state = 'disabled';
};

RM_Button_Submit.prototype.enable = function()
{
	var obSelf = this;
	if(this.state == 'disabled')
	{
		this.clearClasses();
		$(this.button).addClass(this.getClass(''));
		this.state = 'normal';
	}
};

RM_Button_Submit.prototype.reset = function()
{
	if(this.state == 'waited')
	{
		this.clearClasses();
		$(this.button).addClass(this.getClass(''));
		this.state = 'normal';
		$(document).stopTime('submitForm');
		$(this.button).find('.loader').text('00');
	}
	else
	{
		this.reseted = true;
	}
};

RM_Button_Submit.prototype.runTimer = function(type)
{
	var obSelf = this;
	var startTime = new Date();
	$(document).everyTime(500, 'submitForm', function(){
		var currentTime = new Date();
		var seconds = parseInt((currentTime.getTime()-startTime.getTime()) / 1000)+1;
		seconds = seconds < 10 ? '0'+seconds : seconds;
		$(obSelf.button).find('.loader').text(seconds);
	});
};

RM_Button_Submit.prototype.getClass = function(type)
{
	if(!type)
		return 'button_'+this.type;
	else
		return 'button_'+this.type+'_'+type;
};

RM_Button_Submit.prototype.getForm = function()
{
	if ( this.form === null )
		this.form = $(this.button).parents('form');

	return this.form;
};

RM_Button_Submit.prototype.rename = function(textVal)
{
	this.titleSpan.innerHTML = textVal;
	this.input.value = textVal;
};
