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

function SmartNewsSlideOne(iname, break_interval, ani_interval, ani_step, ani_side, direction) {
	this.iname = iname;
	this.break_interval = break_interval;
	this.ani_interval = ani_interval;
	this.ani_step = ani_step;
	this.ani_side = ani_side;
	this.items = [ ];
	this.i_item = -1;
	this.timer = 0;
	this.state = 0;
	this.prev_state = 0;
	this.progress = 100;
	this.cx = 0;
	this.cy = 0;

	if (direction == "up")
		this.direction = 1;
	else if (direction == "right")
		this.direction = 2;
	else if (direction == "down")
		this.direction = 3;
	else if (direction == "left")
		this.direction = 4;
	else
		this.direction = 2;

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

	this.wdg_driver = document.getElementById(this.iname + "_driver");

	for (var i = 0; ; i++) {
		var wdg = document.getElementById(this.iname + "_" + i);
		if (!wdg)
			break;

		wdg.style.position = "absolute";
		wdg.setAttribute("iname", this.iname);
		wdg.onmouseover = function () { var o = document[this.getAttribute("iname")]; o.prev_state = o.state; o.state = 0; }
		wdg.onmouseout = function () { var o = document[this.getAttribute("iname")]; o.state = o.prev_state; }

		this.items.push(wdg);
	}

	if (this.items.length > 0) {
		this.i_item = 0;
		this.position();
		this.state = 2;
		this.rhythm();
	}
}

SmartNewsSlideOne.prototype.position = function () {
	this.items[this.i_item].style.display = "none";
	this.wdg_driver.style.display = "block";

	this.cx = 0;
	this.cy = 0;
	var obj = this.wdg_driver;
	while (obj) {
		this.cx += obj.offsetLeft;
		this.cy += obj.offsetTop;
		obj = obj.offsetParent;
	}

	for (var i = 0, len = this.items.length; i < len; i++) {
		this.items[i].style.width = this.wdg_driver.offsetWidth + "px";
		this.items[i].style.left = this.cx + "px";
		this.items[i].style.top = this.cy + "px";
	}

	this.wdg_driver.style.display = "none";
	this.items[this.i_item].style.display = "block";
}

SmartNewsSlideOne.prototype.rhythm = function () {
	this.timer = 0;

	if (this.state == 0) {
		this.timer = setTimeout('document["' + this.iname + '"].rhythm()', 1000);
		return;
	}

	if (this.state == 1) {
		this.position();
		this.state = -1;
		this.progress = 0;
	}

	this.progress += this.ani_step;
	if (this.progress > 100)
		this.progress = 100;

	var wdg = this.items[this.i_item];

	if (this.direction == 1 && this.state == 2) {
		wdg.style.left = this.cx + "px";
		wdg.style.top = (this.cy + Math.round(this.ani_side * (100 - this.progress) / 100)) + "px";
	}
	else if (this.direction == 1 && this.state == -1) {
		wdg.style.left = this.cx + "px";
		wdg.style.top = (this.cy - this.ani_side + Math.round(this.ani_side * (100 - this.progress) / 100)) + "px";
	}
	else if (this.direction == 2 && this.state == 2) {
		wdg.style.left = (this.cx - this.ani_side + Math.round(this.ani_side * this.progress / 100)) + "px";
		wdg.style.top = this.cy + "px";
	}
	else if (this.direction == 2 && this.state == -1) {
		wdg.style.left = (this.cx + Math.round(this.ani_side * this.progress / 100)) + "px";
		wdg.style.top = this.cy + "px";
	}
	else if (this.direction == 3 && this.state == 2) {
		wdg.style.left = this.cx + "px";
		wdg.style.top = (this.cy - this.ani_side + Math.round(this.ani_side * this.progress / 100)) + "px";
	}
	else if (this.direction == 3 && this.state == -1) {
		wdg.style.left = this.cx + "px";
		wdg.style.top = (this.cy + Math.round(this.ani_side * this.progress / 100)) + "px";
	}
	else if (this.direction == 4 && this.state == 2) {
		wdg.style.left = (this.cx + Math.round(this.ani_side * (100 - this.progress) / 100)) + "px";
		wdg.style.top = this.cy + "px";
	}
	else if (this.direction == 4 && this.state == -1) {
		wdg.style.left = (this.cx - this.ani_side + Math.round(this.ani_side * (100 - this.progress) / 100)) + "px";
		wdg.style.top = this.cy + "px";
	}

	var opacity = this.progress / 100;
	if (opacity < 0.1)
		opacity = 0.1;

	if (this.state == -1)
		opacity = 1 - opacity;

	wdg.style.opacity = opacity;
	wdg.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';

	wdg.style.display = "block";

	if (this.progress == 100 && this.state == 2) {
		this.state = 1;
		this.timer = setTimeout('document["' + this.iname + '"].rhythm()', this.break_interval);
	}
	else {
		if (this.progress == 100 && this.state == -1) {
			this.items[this.i_item].style.display = "none";

			this.i_item++;
			if (this.i_item >= this.items.length)
				this.i_item = 0;

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

		this.timer = setTimeout('document["' + this.iname + '"].rhythm()', this.ani_interval);
	}
}

function smartnews_slideone_position() {
	for (k in document.smartnews_slideone_conf) if (document.smartnews_slideone_conf.hasOwnProperty(k)) {
		var conf = document.smartnews_slideone_conf[k];
		document[conf.iname].position();
	}
}

if (window.addEventListener)
	window.addEventListener("resize", smartnews_slideone_position, false);
else if (window.attachEvent)
	window.attachEvent("onresize", smartnews_slideone_position);

function smartnews_slideone_init() {
	for (k in document.smartnews_slideone_conf) if (document.smartnews_slideone_conf.hasOwnProperty(k)) {
		var conf = document.smartnews_slideone_conf[k];
		document[conf.iname] = new SmartNewsSlideOne(conf.iname, conf.break_interval, conf.ani_interval, conf.ani_step, conf.ani_side, conf.direction);
	}
}

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

