﻿Type.registerNamespace('BWS.Web.UI');

BWS.Web.UI.DivCssButton = function(buttonId, elementId, postback, url, hoverCss, pushCss, disabledCss, autoDisable) {
	var element = $get(elementId);
	if (element.divCssButton) return;
	this.target = $get(buttonId);
	this.element = element;
	this.postback = postback;
	this.url = url;
	this.defaultCss = this.element.className;
	this.hoverCss = hoverCss;
	this.pushCss = pushCss;
	this.disabledCss = disabledCss;
	this.autoDisable = autoDisable;
	this.isHover = false;
	this.isPushed = false;
	this.isDisabled = this.target.disabled;
	this.element.divCssButton = this;
	if (this.target.disabled) this.disable();

	var clickDelegate = Function.createDelegate(this, this.click);
	$addHandler(this.element, "click", Function.createDelegate(this, clickDelegate));

	if (this.hoverCss && !this._hoverDelegate) {
		var hoverDelegate = Function.createDelegate(this, this.toggleHover);
		$addHandler(this.element, "mouseover", hoverDelegate);
		$addHandler(this.element, "mouseout", hoverDelegate);
	}
	if (this.pushCss && !this._pushDelegate) {
		var pushDelegate = Function.createDelegate(this, this.togglePush);
		$addHandler(this.element, "mousedown", pushDelegate);
		$addHandler(this.element, "mouseup", pushDelegate);
	}
}

BWS.Web.UI.DivCssButton.prototype.click = function() {
	if (!this.isDisabled) {
		if (this.postback) {
			if (this.target.click())
				if (this.autoDisable) this.disable();
		}
		else if (this.url) {
			if (this.autoDisable) this.disable();
			window.location.href = this.url;
		}
	}
}

BWS.Web.UI.DivCssButton.prototype.toggleHover = function() {
	if (!this.isDisabled) {
		this.isHover = !this.isHover;
		this.element.className = this.isHover ? this.hoverCss : this.defaultCss;
	}
	else
		this.element.className = this.defaultCss;
	window.status = "";
}

BWS.Web.UI.DivCssButton.prototype.togglePush = function() {
	this.isPushed = !this.isPushed;
	this.element.className = this.isPushed ? this.pushCss : this.defaultCss;
}

BWS.Web.UI.DivCssButton.prototype.enable = function() {
	this.isDisabled = false;
	this.target.disabled = false;
	this.element.className = this.isHover ? this.hoverCss : this.normalCss;
}

BWS.Web.UI.DivCssButton.prototype.disable = function() {
	this.isDisabled = true;
	this.target.disabled = true;
	this.element.className = this.disabledCss;
}

BWS.Web.UI.DivCssButton.prototype.setText = function(text) {
	for (var i = 0; i < this.element.childNodes.length; i++)
		if (this.element.childNodes[i].nodeType == 1) {
			this.element.childNodes[i].innerHTML = text;
			break;
		}
}

BWS.Web.UI.DivCssButton.registerClass("BWS.Web.UI.DivCssButton");
Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();