
/**
 * mod_stabs
 *
 * @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 SlidingTabs(params) {
	if (typeof params.iname == 'undefined')
		return;
	this.iname = params.iname;

	this.sel_id = 0;
	this.target_x = 0;

	var node = document.getElementById(params.source_id);
	var tags_tr = document.getElementById(params.tags_tr_id);
	this.data_container = document.getElementById(params.data_container_id);
	var data_tr = document.getElementById(params.data_tr_id);

	if (!node || !tags_tr || !this.data_container || !data_tr)
		return;

	this.cl_gap = typeof params.class_gap == 'string' ? params.class_gap : '';
	this.cl_norm_l = typeof params.class_norm_l == 'string' ? params.class_norm_l : '';
	this.cl_norm_le = typeof params.class_norm_le == 'string' ? params.class_norm_le : '';
	this.cl_norm_c = typeof params.class_norm_c == 'string' ? params.class_norm_c : '';
	this.cl_norm_r = typeof params.class_norm_r == 'string' ? params.class_norm_r : '';
	this.cl_norm_re = typeof params.class_norm_re == 'string' ? params.class_norm_re : '';
	this.cl_hover_l = typeof params.class_hover_l == 'string' ? params.class_hover_l : '';
	this.cl_hover_le = typeof params.class_hover_le == 'string' ? params.class_hover_le : '';
	this.cl_hover_c = typeof params.class_hover_c == 'string' ? params.class_hover_c : '';
	this.cl_hover_r = typeof params.class_hover_r == 'string' ? params.class_hover_r : '';
	this.cl_hover_re = typeof params.class_hover_re == 'string' ? params.class_hover_re : '';
	this.cl_sel_l = typeof params.class_sel_l == 'string' ? params.class_sel_l : '';
	this.cl_sel_le = typeof params.class_sel_le == 'string' ? params.class_sel_le : '';
	this.cl_sel_c = typeof params.class_sel_c == 'string' ? params.class_sel_c : '';
	this.cl_sel_r = typeof params.class_sel_r == 'string' ? params.class_sel_r : '';
	this.cl_sel_re = typeof params.class_sel_re == 'string' ? params.class_sel_re : '';

	var image_prefix = typeof params.image_prefix == 'string' ? params.image_prefix : '';

	var clear_tags_tr = typeof params.clear_tags_tr == 'boolean' ? params.clear_tags_tr : true;
	if (clear_tags_tr)
		while (tags_tr.firstChild)
			tags_tr.removeChild(tags_tr.firstChild);

	while (data_tr.firstChild)
		data_tr.removeChild(data_tr.firstChild);

	var max_height = typeof params.max_height == 'number' ? params.max_height : 0;

	var padding_left = typeof params.padding_left == 'number' ? params.padding_left : 0;
	var padding_right = typeof params.padding_right == 'number' ? params.padding_right : 0;

	var init_selection = typeof params.init_selection == 'number' ? params.init_selection : 1;
	if (init_selection < 1)
		init_selection = 1;

	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.tag_id_prefix = typeof params.tag_id_prefix == 'string' ? params.tag_id_prefix : '';
	if (this.tag_id_prefix == '')
		this.tag_id_prefix = this.iname + '_';

	this.data_container.style.width = this.data_container.offsetWidth + 'px';
	this.data_container.style.overflow = 'hidden';

	var w = this.data_container.offsetWidth - padding_left - padding_right;

	this.last_id = 0;
	node = node.firstChild;
	var first = true;
	while (node) {
		if (node.nodeType == 1) {
			// if the node is a regular DOM element, handle it as a data source
			this.last_id++;


			// try to find the header

			var header = node.firstChild;
			while (header && header.nodeType != 1)
				header = header.nextSibling;

			var header_node = null;
			if (header && header.tagName == 'H3') {
				header_node = document.createElement('span');

				if (typeof params['image' + this.last_id] != 'undefined') {
					var img = document.createElement('img');
					img.src = image_prefix + params['image' + this.last_id];
					header_node.appendChild(img);
				}

				header_node.appendChild(header.firstChild);
			}


			// build tag

			if (first)
				first = false;
			else if (this.cl_gap != '')
				this.mktd(tags_tr, this.cl_gap, this.iname, this.last_id, '', null);

			if (this.cl_norm_l != '')
				this.mktd(tags_tr, this.cl_norm_l, this.iname, this.last_id, this.tag_id_prefix + 'l_', null);

			this.mktd(tags_tr, this.cl_norm_c, this.iname, this.last_id, this.tag_id_prefix + 'c_', header_node);

			if (this.cl_norm_r != '')
				this.mktd(tags_tr, this.cl_norm_r, this.iname, this.last_id, this.tag_id_prefix + 'r_', null);


			// build data

			// cache next data block
			var content_node = node;
			node = node.nextSibling;

			var td = document.createElement('td');
			data_tr.appendChild(td);
			td.setAttribute('vAlign', 'top');

			var div = document.createElement('div');
			td.appendChild(div);
			div.style.overflow = 'hidden';
			div.style.width = w + 'px';

			if (max_height > 0)
				div.style.height = max_height + 'px';

			if (padding_left > 0)
				div.style.marginLeft = padding_left + 'px';

			if (padding_right > 0)
				div.style.marginRight = padding_right + 'px';

			div.appendChild(content_node);
		}
		else {
			// move to the next data block
			node = node.nextSibling;
		}
	}

	this.h_click(init_selection);
}

SlidingTabs.prototype.mktd = function (tr, clas, iname, click_id, prefix, caption_node) {
	var td = document.createElement('td');
	tr.appendChild(td);
	td.className = clas;
	td.setAttribute('iname', iname);
	td.setAttribute('click_id', click_id);
	td.setAttribute('nowrap', 'nowrap');
	td.onclick = function () { document[this.getAttribute('iname')].h_click(this.getAttribute('click_id')); }
	td.onmouseover = function () { document[this.getAttribute('iname')].h_mouseover(this.getAttribute('click_id')); }
	td.onmouseout = function () { document[this.getAttribute('iname')].h_mouseout(this.getAttribute('click_id')); }
	td.id = prefix + click_id;

	if (caption_node != null)
		td.appendChild(caption_node);

	return td;
}

SlidingTabs.prototype.h_timer = function() {
	if (this.target_x != this.data_container.scrollLeft) {
		this.data_container.scrollLeft = this.data_container.scrollLeft + (this.target_x < this.data_container.scrollLeft ? -1 : 1) * Math.ceil(Math.abs(this.target_x - this.data_container.scrollLeft) * this.speed / 100);

		if (this.target_x != this.data_container.scrollLeft)
			setTimeout('document["' + this.iname + '"].h_timer()', this.interval);
	}
}

SlidingTabs.prototype.get_cl = function (id) {
	var result = { }

	result.norm_l = id == 0 && this.cl_norm_le != '' ? this.cl_norm_le : this.cl_norm_l;
	result.norm_c = this.cl_norm_c;
	result.norm_r = id == this.last_id && this.cl_norm_re != '' ? this.cl_norm_re : this.cl_norm_r;

	result.hover_l = id == 0 && this.cl_hover_le != '' ? this.cl_hover_le : this.cl_hover_l;
	result.hover_c = this.cl_hover_c;
	result.hover_r = id == this.last_id && this.cl_hover_re != '' ? this.cl_hover_re : this.cl_hover_r;

	result.sel_l = id == 0 && this.cl_sel_le != '' ? this.cl_sel_le : this.cl_sel_l;
	result.sel_c = this.cl_sel_c;
	result.sel_r = id == this.last_id && this.cl_sel_re != '' ? this.cl_sel_re : this.cl_sel_r;

	return result;
}

SlidingTabs.prototype.h_click = function (click_id) {
	if (this.sel_id != click_id) {
		if (this.sel_id > 0) {
			var cl = this.get_cl(this.sel_id);

			if (cl.norm_l != '')
				document.getElementById(this.tag_id_prefix + 'l_' + this.sel_id).className = cl.norm_l;

			document.getElementById(this.tag_id_prefix + 'c_' + this.sel_id).className = cl.norm_c;

			if (cl.norm_r != '')
				document.getElementById(this.tag_id_prefix + 'r_' + this.sel_id).className = cl.norm_r;
		}

		this.sel_id = click_id;
		this.target_x = (this.sel_id - 1) * this.data_container.offsetWidth;
		var cl = this.get_cl(this.sel_id);

		if (cl.norm_l != '')
			document.getElementById(this.tag_id_prefix + 'l_' + this.sel_id).className = cl.sel_l;

		document.getElementById(this.tag_id_prefix + 'c_' + this.sel_id).className = cl.sel_c;

		if (cl.norm_r != '')
			document.getElementById(this.tag_id_prefix + 'r_' + this.sel_id).className = cl.sel_r;

		this.h_timer();
	}
}

SlidingTabs.prototype.h_mouseover = function (click_id) {
	if (click_id != this.sel_id && (this.cl_hover_l != '' || this.cl_hover_c != '' || this.cl_hover_r != '')) {
		var cl = this.get_cl(click_id);

		if (cl.norm_l != '')
			document.getElementById(this.tag_id_prefix + 'l_' + click_id).className = cl.hover_l;

		document.getElementById(this.tag_id_prefix + 'c_' + click_id).className = cl.hover_c;

		if (cl.norm_r != '')
			document.getElementById(this.tag_id_prefix + 'r_' + click_id).className = cl.hover_r;
	}
}

SlidingTabs.prototype.h_mouseout = function (click_id) {
	if (click_id != this.sel_id && (this.cl_hover_l != '' || this.cl_hover_c != '' || this.cl_hover_r != '')) {
		var cl = this.get_cl(click_id);

		if (cl.norm_l != '')
			document.getElementById(this.tag_id_prefix + 'l_' + click_id).className = cl.norm_l;

		document.getElementById(this.tag_id_prefix + 'c_' + click_id).className = cl.norm_c;

		if (cl.norm_r != '')
			document.getElementById(this.tag_id_prefix + 'r_' + click_id).className = cl.norm_r;
	}
}

