
/**
 * mod_supportpanel
 *
 * @version 1.0
 * @author Creative Pulse
 * @copyright Creative Pulse 2009
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link http://www.creativepulse.eu
 */

function SupportPanel(params) {
	if (typeof params.iname == 'undefined')
		return;

	this.iname = params.iname;

	this.wdg_static = document.getElementById(this.iname + '_static');
	this.wdg_hover_up = document.getElementById(this.iname + '_hover_up');
	this.wdg_hover_down = document.getElementById(this.iname + '_hover_down');

	if (!this.wdg_static || !this.wdg_hover_up || !this.wdg_hover_down)
		return;

	this.static_width = typeof params.static_width == 'number' ? params.static_width : 0;
	this.static_height = typeof params.static_height == 'number' ? params.static_height : 0;
	this.hover_width = typeof params.hover_width == 'number' ? params.hover_width : 0;
	this.hover_height = typeof params.hover_height == 'number' ? params.hover_height : 0;

	if (this.static_width == 0)
		this.static_width = this.wdg_static.offsetWidth;

	if (this.static_height == 0)
		this.static_height = this.wdg_static.offsetHeight;

	this.interval = typeof params.interval == 'number' ? params.interval : 0;
	if (this.interval == 0)
		this.interval = 20;

	this.speed = typeof params.speed == 'number' ? params.speed : 0;
	if (this.speed <= 0)
		this.speed = 10;
	else if (this.speed > 100)
		this.speed = 100;

	this.opacity_start = typeof params.opacity_start == 'number' ? params.opacity_start : 0;
	if (this.opacity_start <= 0)
		this.opacity_start = 10;
	else if (this.opacity_start > 100)
		this.opacity_start = 100;

	this.opacity_end = typeof params.opacity_end == 'number' ? params.opacity_end : 0;
	if (this.opacity_end <= 0)
		this.opacity_end = 10;
	else if (this.opacity_end > 100)
		this.opacity_end = 100;

	this.orientation = typeof params.orientation == 'string' ? params.orientation : 0;
	switch (this.orientation) {
		case "top_left":
		case "top_center":
		case "top_right":
		case "bottom_left":
		case "bottom_center":
		case "bottom_right":
		case "left_top":
		case "left_middle":
		case "left_bottom":
		case "right_top":
		case "right_middle":
		case "right_bottom":
			break;

		default:
			this.orientation = "top_left";
	}

	this.offset_start = typeof params.offset_start == 'number' ? params.offset_start : 0;
	this.offset_end = typeof params.offset_end == 'number' ? params.offset_end : 0;
	this.shadow_offset_x = typeof params.shadow_offset_x == 'number' ? params.shadow_offset_x : 0;
	this.shadow_offset_y = typeof params.shadow_offset_y == 'number' ? params.shadow_offset_y : 0;

	this.shadow_max_opacity = typeof params.shadow_max_opacity == 'number' ? params.shadow_max_opacity : 0;
	if (this.shadow_max_opacity == 0)
		this.shadow_max_opacity = 15;

	this.leave_interval = typeof params.leave_interval == 'number' ? params.leave_interval : 0;

	this.wdg_static.setAttribute('iname', this.iname);
	this.wdg_static.onmouseover = function () { document[this.getAttribute('iname')].h_mouseover(); }
	this.wdg_static.onmouseout = function () { document[this.getAttribute('iname')].h_mouseout(); }

	if (this.shadow_offset_x > 0 || this.shadow_offset_y > 0)
		this.wdg_hover_shadow = document.createElement('div');

	this.wdg_hover = document.createElement('div');
	this.wdg_hover.setAttribute('iname', this.iname);
	this.wdg_hover.onmouseover = function () { document[this.getAttribute('iname')].h_mouseover(); }
	this.wdg_hover.onmouseout = function () { document[this.getAttribute('iname')].h_mouseout(); }

	this.wdg_hover.style.visibility = 'hidden';
	this.wdg_hover.style.display = 'block';
	this.wdg_hover.style.position = 'absolute';
	this.wdg_hover.style.left = '0px';
	this.wdg_hover.style.top = '0px';

	if (this.wdg_hover_shadow)
		document.getElementsByTagName('body')[0].appendChild(this.wdg_hover_shadow);

	document.getElementsByTagName('body')[0].appendChild(this.wdg_hover);

	if (this.hover_width > 0) {
		this.wdg_hover.style.width = this.hover_width + 'px';

		if (this.wdg_hover_shadow)
			this.wdg_hover_shadow.style.width = this.hover_width + 'px';
	}

	if (this.hover_height > 0) {
		this.wdg_hover.style.height = this.hover_height + 'px';

		if (this.wdg_hover_shadow)
			this.wdg_hover_shadow.style.height = this.hover_height + 'px';
	}

	if (this.wdg_hover_shadow) {
		this.wdg_hover_shadow.appendChild(this.wdg_hover_down.cloneNode(true));
		this.wdg_hover_shadow.firstChild.style.display = 'block';
		this.wdg_hover_shadow.style.display = 'none';
		this.wdg_hover_shadow.style.position = 'absolute';
		this.wdg_hover_shadow.style.left = '0px';
		this.wdg_hover_shadow.style.top = '0px';
	}

	this.wdg_hover.appendChild(this.wdg_hover_down);
	this.wdg_hover_down.style.display = 'block';

	if (this.hover_width == 0)
		this.hover_width = this.wdg_hover.offsetWidth;

	if (this.hover_height == 0)
		this.hover_height = this.wdg_hover.offsetHeight;

	this.hover_height = this.wdg_hover.offsetHeight;
	this.wdg_hover.style.height = this.hover_height + 'px';
	this.wdg_hover.style.overflow = 'hidden';

	this.wdg_hover.appendChild(this.wdg_hover_up);
	this.wdg_hover_up.style.display = 'block';
	this.wdg_hover_up.style.position = 'relative';
	this.wdg_hover_up.style.top = '-' + this.hover_height + 'px';

	this.wdg_hover.style.display = 'none';
	this.wdg_hover.style.visibility = 'visible';

	this.state = 0;
	this.progress = 0;
}

SupportPanel.prototype.h_mouseover = function () {
	if (this.leave_timer > 0) {
		clearTimeout(this.leave_timer);
		this.leave_timer = 0;
	}

	if (this.state == 1)
		return;

	if (this.state == 0) {
		this.hover_x = 0;
		this.hover_y = 0;
		var obj = this.wdg_static;
		while (obj) {
			this.hover_x += obj.offsetLeft;
			this.hover_y += obj.offsetTop;
			obj = obj.offsetParent;
		}

		switch (this.orientation) {
			case "top_left":
				this.hover_y -= this.hover_height;
				break;

			case "top_center":
				this.hover_x += Math.round(this.static_width / 2 - this.hover_width / 2);
				this.hover_y -= this.hover_height;
				break;

			case "top_right":
				this.hover_x += this.static_width - this.hover_width;
				this.hover_y -= this.hover_height;
				break;

			case "bottom_left":
				this.hover_y += this.static_height;
				break;

			case "bottom_center":
				this.hover_x += Math.round(this.static_width / 2 - this.hover_width / 2);
				this.hover_y += this.static_height;
				break;

			case "bottom_right":
				this.hover_x += this.static_width - this.hover_width;
				this.hover_y += this.static_height;
				break;

			case "left_top":
				this.hover_x -= this.hover_width;
				break;

			case "left_middle":
				this.hover_x -= this.hover_width;
				this.hover_y += Math.round(this.static_height / 2 - this.hover_height / 2);
				break;

			case "left_bottom":
				this.hover_x -= this.hover_width;
				this.hover_y += this.static_height - this.hover_height;
				break;

			case "right_top":
				this.hover_x += this.static_width;
				break;

			case "right_middle":
				this.hover_x += this.static_width;
				this.hover_y += Math.round(this.static_height / 2 - this.hover_height / 2);
				break;

			case "right_bottom":
				this.hover_x += this.static_width;
				this.hover_y += this.static_height - this.hover_height;
				break;
		}
	}

	this.state = 2;
	this.h_timer();
}

SupportPanel.prototype.h_mouseout = function () {
	this.leave_timer = setTimeout('document["' + this.iname + '"].do_hide()', this.leave_interval);
}

SupportPanel.prototype.do_hide = function () {
	this.leave_timer = 0;
	if (this.state > 0) {
		this.state = -1;
		this.h_timer();
	}
}

SupportPanel.prototype.h_timer = function () {
	if (this.state == 2) {
		this.progress += this.speed;

		if (this.progress >= 100) {
			this.progress = 100;
			this.state = 1;
		}
	}
	else if (this.state == -1) {
		this.progress -= this.speed;

		if (this.progress <= 0) {
			this.progress = 0;
			this.state = 0;
			this.wdg_hover.style.display = 'none';

			if (this.wdg_hover_shadow)
				this.wdg_hover_shadow.style.display = 'none';
		}
	}

	if (this.state != 0) {
		var opacity = Math.round(this.opacity_start + (this.opacity_end - this.opacity_start) * this.progress / 100);
		if (opacity == 0) {
			this.wdg_hover.style.display = 'none';

			if (this.wdg_hover_shadow)
				this.wdg_hover_shadow.style.display = 'none';
		}
		else {
			var x = 0, y = 0;
			var offset = this.offset_start + Math.round((this.offset_end - this.offset_start) * this.progress / 100);

			switch (this.orientation) {
				case "top_left":
				case "top_center":
				case "top_right":
					x = this.hover_x;
					y = this.hover_y - offset;
					break;

				case "bottom_left":
				case "bottom_center":
				case "bottom_right":
					x = this.hover_x;
					y = this.hover_y + offset;
					break;

				case "left_top":
				case "left_middle":
				case "left_bottom":
					x = this.hover_x - offset;
					y = this.hover_y;
					break;

				case "right_top":
				case "right_middle":
				case "right_bottom":
					x = this.hover_x + offset;
					y = this.hover_y;
					break;
			}

			this.wdg_hover.style.left = x + 'px';
			this.wdg_hover.style.top = y + 'px';

			if (this.wdg_hover_shadow) {
				var shadow_opacity = Math.round(this.shadow_max_opacity * this.progress / 100);
				if (shadow_opacity == 0) {
					this.wdg_hover_shadow.style.display = 'none';
				}
				else {
					this.wdg_hover_shadow.style.display = 'block';
					this.wdg_hover_shadow.style.opacity = shadow_opacity / 100;
					this.wdg_hover_shadow.style.filter = 'filter: alpha(opacity=' + shadow_opacity + ')';
					this.wdg_hover_shadow.style.left = (x + this.shadow_offset_x) + 'px';
					this.wdg_hover_shadow.style.top = (y + this.shadow_offset_y) + 'px';
				}
			}

			this.wdg_hover.style.display = 'block';
			this.wdg_hover.style.opacity = opacity / 100;
			this.wdg_hover.style.filter = 'filter: alpha(opacity=' + opacity + ')';
		}
	}

	if (this.state == 2 || this.state == -1)
		setTimeout('document["' + this.iname + '"].h_timer()', this.interval);
}
