
/**
 * mod_vmrotmulti_latestprod
 *
 * @version 1.1
 * @author Creative Pulse
 * @copyright Creative Pulse 2008- Soeren Eberhardt 2004-2005 - Mr PHP 2000-2004
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link http://www.creativepulse.eu
 */

function ModVmrotmultiLatestprod(iname, wdg, item_width, interval, speed, container_wdg, autoscroll_interval) {
	this.iname = iname;
	this.wdg = wdg;
	this.container_wdg = container_wdg;
	this.target_item = 0;
	this.target_left = 0;
	this.item_width = item_width;
	this.max = 0;
	this.timer = 0;
	this.interval = interval;
	this.speed = speed;
	this.autoscroll_interval = autoscroll_interval;
	this.mouse_is_over = false;

	if (this.speed < 1)
		this.speed = 1;
	else if (this.speed > 100)
		this.speed = 100;

	this.speed /= 100;

	if (this.interval < 1)
		this.interval = 1;

	this.autoscroll(true);
}

ModVmrotmultiLatestprod.prototype.autoscroll = function (start) {
	if (this.autoscroll_timer != 0) {
		clearTimeout(this.autoscroll_timer);
		this.autoscroll_timer = 0;
	}

	if (start && this.autoscroll_timer == 0 && this.autoscroll_interval > 0)
		this.autoscroll_timer = setTimeout('document["' + this.iname + '"].h_timer_scroll()', this.autoscroll_interval);
}

ModVmrotmultiLatestprod.prototype.h_mouseover = function (e) {
	this.mouse_is_over = true;
	this.autoscroll(false);
}

ModVmrotmultiLatestprod.prototype.h_mouseout = function (e) {
	this.mouse_is_over = false;
	this.autoscroll(true);
}

ModVmrotmultiLatestprod.prototype.set_max = function (val) {
	if (val > this.max)
		this.max = val;
}

ModVmrotmultiLatestprod.prototype.finalize_max = function (val) {
	this.max -= val;
	if (this.max < 0)
		this.max = 0;
}

ModVmrotmultiLatestprod.prototype.h_timer_scroll = function () {
	if (this.timer == 0) {
		if (this.target_item >= this.max) {
			this.target_item = 0;
			this.target_left = 0;
			this.wdg.scrollLeft = 0;
		}
		else {
			this.target_item++;
			this.do_move();
		}
	}

	this.autoscroll(true);
}

ModVmrotmultiLatestprod.prototype.move_to = function (i) {
	this.target_item += i;
	if (this.target_item < 0)
		this.target_item = 0;
	else if (this.target_item > this.max)
		this.target_item = this.max;

	this.do_move();
}

ModVmrotmultiLatestprod.prototype.do_move = function () {
	this.target_left = this.target_item * this.item_width;

	if (this.timer == 0 && this.wdg.scrollLeft != this.target_left)
		this.timer = setTimeout('document["' + this.iname + '"].h_timer()', this.interval);
	else if (!this.mouse_is_over)
		this.autoscroll(true);
}

ModVmrotmultiLatestprod.prototype.h_timer = function () {
	if (this.timer > 0)
		this.timer = 0;

	if (this.wdg.scrollLeft != this.target_left) {
		var coeff = this.target_left > this.wdg.scrollLeft ? 1 : -1;
		this.wdg.scrollLeft += coeff * Math.ceil(Math.abs(this.target_left - this.wdg.scrollLeft) * this.speed);

		if (this.wdg.scrollLeft != this.target_left)
			this.timer = setTimeout('document["' + this.iname + '"].h_timer()', this.interval);
	}
	else if (!this.mouse_is_over) {
		this.autoscroll(true);
	}
}


function mod_vmrotmulti_latestprod_init() {
	var buttons = document.getElementsByTagName('button');
	var divs = document.getElementsByTagName('div');
	for (var i = 0, len = divs.length; i < len; i++)
		if (divs[i].className == 'mod_vmrotmulti_latestprod' && divs[i].id && divs[i].id != '') {
			// get instance number from last part of the wrapper id
			var e = divs[i].id.split('_'); // example id: mod_vmrotmulti_latestprod_X
			var inst_num = parseInt(e[e.length - 1]);
			if (inst_num > 0) {
				var conf = document['mod_vmrotmulti_latestprod_conf'] && document['mod_vmrotmulti_latestprod_conf']['inst_' + inst_num] ? document['mod_vmrotmulti_latestprod_conf']['inst_' + inst_num] : null;

				var visible_items = conf && conf.visible_items ? parseInt(conf.visible_items) : 4;
				if (visible_items < 1)
					visible_items = 1;

				var autoscroll_interval = conf && conf.autoscroll_interval ? parseInt(conf.autoscroll_interval) : 4;

				var panel = document.getElementById('mod_vmrotmulti_latestprod_panel_' + inst_num);

				var total_width = panel.offsetWidth;
				var item_width = Math.ceil(total_width / visible_items);

				var interval = conf && conf.interval ? conf.interval : 20;
				var speed = conf && conf.speed ? conf.speed : 10;

				var iname = 'mod_vmrotmulti_latestprod_js_' + inst_num;
				document[iname] = new ModVmrotmultiLatestprod(iname, panel, item_width, interval, speed, divs[i], autoscroll_interval);

				// apply default item width
				for (var k = 0; k < len; k++)
					if (divs[k].id && divs[k].id.substr(0, 31) == 'mod_vmrotmulti_latestprod_item_') {
						var e = divs[k].id.split('_'); // example id: mod_vmrotmulti_latestprod_item_X_Y
						// find the items that belong to the current instance
						if (e.length >= 2 && parseInt(e[e.length - 2]) == inst_num) {
							divs[k].style.overflow = 'hidden';
							divs[k].style.width = item_width + 'px';
							document[iname].set_max(parseInt(e[e.length - 1]) - 1);

							divs[k].setAttribute('iname', iname);
							divs[k].onmouseover = function (e) {
								document[this.getAttribute('iname')].h_mouseover(e);
							}
							divs[k].onmouseout = function (e) {
								document[this.getAttribute('iname')].h_mouseout(e);
							}
						}
					}
				document[iname].finalize_max(visible_items - 1);

				// bind buttons
				var obj = document.getElementById('mod_vmrotmulti_latestprod_btnprev_' + inst_num);
				if (obj) {
					obj.setAttribute('iname', iname);
					obj.onmouseover = function (e) {
						document[this.getAttribute('iname')].h_mouseover(e);
					}
					obj.onmouseout = function (e) {
						document[this.getAttribute('iname')].h_mouseout(e);
					}
					obj.onfocus = function () {
						this.blur();
					}
					obj.onclick = function () {
						var e = this.id.split('_');
						document['mod_vmrotmulti_latestprod_js_' + e[e.length - 1]].move_to(-1);
					}
				}

				var obj = document.getElementById('mod_vmrotmulti_latestprod_btnnext_' + inst_num);
				if (obj) {
					obj.setAttribute('iname', iname);
					obj.onmouseover = function (e) {
						document[this.getAttribute('iname')].h_mouseover(e);
					}
					obj.onmouseout = function (e) {
						document[this.getAttribute('iname')].h_mouseout(e);
					}
					obj.onfocus = function () {
						this.blur();
					}
					obj.onclick = function () {
						var e = this.id.split('_');
						document['mod_vmrotmulti_latestprod_js_' + e[e.length - 1]].move_to(1);
					}
				}

				// clean up container
				while (panel.firstChild)
					panel.removeChild(panel.firstChild);

				// load container
				panel.style.width = total_width + 'px';

				var data = document.getElementById('mod_vmrotmulti_latestprod_data_' + inst_num);
				if (data) {
					panel.appendChild(data);
					data.style.display = 'block';
				}
			}
		}
}

if (window.addEventListener)
	window.addEventListener('load', mod_vmrotmulti_latestprod_init, false);
else if (window.attachEvent)
	window.attachEvent('onload', mod_vmrotmulti_latestprod_init);

