
/**
 * Exchange - Joomla Template
 *
 * @package bt_exchange
 * @version 1.0
 * @copyright BonusThemes.com 2010
 * @link http://www.bonusthemes.com/goto/bt_exchange
 */

function ExtDDPanel(params) {
	for (var k in params)
		if (params.hasOwnProperty(k))
			this[k] = params[k];

	this.wdg_tag1 = null;
	this.wdg_tag2 = null;
	this.wdg_cnt = null;

	this.wdg_tag = document.getElementById(this.wdg_tag_id);
	this.wdg_tag.onmouseover = function () { document.ext_dd_panel.h_mouseover(); }
	this.wdg_tag.onmouseout = function () { document.ext_dd_panel.h_mouseout(); }

	var r = this.setup_wdg(this.wdg_tag1_id);
	if (r != null) {
		this.wdg_tag1 = r.wdg;
		var wdg_tag1_w = r.w;
		this.wdg_tag1_h = r.h;
		this.wdg_tag1.onmouseover = function () { document.ext_dd_panel.h_mouseover(); }
		this.wdg_tag1.onmouseout = function () { document.ext_dd_panel.h_mouseout(); }
	}

	var r = this.setup_wdg(this.wdg_tag2_id);
	if (r != null) {
		this.wdg_tag2 = r.wdg;
		this.wdg_tag2.style.width = wdg_tag1_w + "px";
		this.wdg_tag2_h = r.h;
		this.wdg_tag2.onmouseover = function () { document.ext_dd_panel.h_mouseover(); }
		this.wdg_tag2.onmouseout = function () { document.ext_dd_panel.h_mouseout(); }
	}

	var r = this.setup_wdg(this.wdg_cnt_id);
	if (r != null) {
		this.wdg_cnt = r.wdg;
		this.wdg_w = r.w;
		this.wdg_h = r.h;
		this.wdg_cnt.onmouseover = function () { document.ext_dd_panel.h_mouseover(); }
		this.wdg_cnt.onmouseout = function () { document.ext_dd_panel.h_mouseout(); }
	}

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

	this.leave_timer = 0;
}

ExtDDPanel.prototype.setup_wdg = function (wdg_id) {
	var wdg = document.getElementById(wdg_id);
	if (wdg) {
		document.getElementsByTagName("body")[0].appendChild(wdg);
		wdg.style.position = "absolute";
		wdg.style.left = "0px";
		wdg.style.top = "0px";
		wdg.style.visibility = "hidden";
		wdg.style.display = "block";
		var w = wdg.offsetWidth;
		var h = wdg.offsetHeight;
		wdg.style.display = "none";
		wdg.style.visibility = "visible";

		if (this.z_index != null)
			wdg.style.zIndex = this.z_index;
		
		return {wdg: wdg, w: w, h: h};
	}
	else {
		return null;
	}
}

ExtDDPanel.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.position();

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

ExtDDPanel.prototype.h_mouseout = function () {
	this.leave_timer = setTimeout("document.ext_dd_panel.do_hide()", this.leave_interval);
}

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

ExtDDPanel.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_cnt.style.display = "none";

			if (this.wdg_tag1)
				this.wdg_tag1.style.display = "none";

			if (this.wdg_tag2)
				this.wdg_tag2.style.display = "none";
		}
	}

	if (this.state != 0) {
		var opacity = Math.round(this.start_opacity + (this.end_opacity - this.start_opacity) * this.progress / 100);
		if (opacity == 0) {
			this.wdg_cnt.style.display = "none";

			if (this.wdg_tag1)
				this.wdg_tag1.style.display = "none";

			if (this.wdg_tag2)
				this.wdg_tag2.style.display = "none";
		}
		else {
			this.wdg_cnt.style.top = this.wdg_cnt_y + this.start_y_offset + Math.round((this.end_y_offset - this.start_y_offset) * this.progress / 100) + "px";

			this.wdg_cnt.style.display = "block";
			this.wdg_cnt.style.opacity = opacity / 100;
			this.wdg_cnt.style.filter = "filter: alpha(opacity=" + opacity + ")";

			if (this.wdg_tag1)
				this.wdg_tag1.style.display = "block";

			if (this.wdg_tag2)
				this.wdg_tag2.style.display = "block";
		}
	}

	if (this.state == 2 || this.state == -1)
		setTimeout("document.ext_dd_panel.h_timer()", this.interval);
}

ExtDDPanel.prototype.position = function () {
	this.wdg_cnt_x = -19; // this.wdg_tag1.offsetWidth - this.wdg_w;
	this.wdg_cnt_y = -16; // this.wdg_tag.offsetHeight + 20;
	var obj = this.wdg_tag;
	while (obj) {
		this.wdg_cnt_x += obj.offsetLeft;
		this.wdg_cnt_y += obj.offsetTop;
		obj = obj.offsetParent;
	}

	if (this.wdg_tag1) {
		this.wdg_tag1.style.left = this.wdg_cnt_x + "px";
		this.wdg_tag1.style.top = this.wdg_cnt_y + "px";
		this.wdg_cnt_y += this.wdg_tag1_h;
	}

	if (this.wdg_tag2) {
		this.wdg_tag2.style.left = this.wdg_cnt_x + "px";
		this.wdg_tag2.style.top = this.wdg_cnt_y + "px";
		this.wdg_cnt_y += this.wdg_tag2_h;
	}

	this.wdg_cnt.style.left = this.wdg_cnt_x + "px";
	this.wdg_cnt.style.top = this.wdg_cnt_y + "px";
}

function init_ext_dd_panel() {
	document.ext_dd_panel = new ExtDDPanel({
		wdg_tag_id: "tpl_login_tag_login",
		wdg_tag1_id: "tpl_login_tag1",
		wdg_tag2_id: "tpl_login_tag2",
		wdg_cnt_id: "tpl_login_cnt",
		z_index: null,
		leave_interval: 500,
		interval: 20,
		speed: 10,
		start_y_offset: 0,
		end_y_offset: 0,
		start_opacity: 50,
		end_opacity: 100
	});
}

if (window.addEventListener)
	window.addEventListener("load", init_ext_dd_panel, false);
else if (window.attachEvent)
	window.attachEvent("onload", init_ext_dd_panel);

