var ForwardToolbox = {

	create: function(module) {
		for (var property in module) {
			if (typeof module[property] != "function") continue;

			module[property] = this.bindMethod(module[property], module);
		}

		if (module.initialize)
			this.registerLoadEvent(module.initialize);

		return module;
	},

	bindMethod: function(method, owner) {
		return function() {
			return method.apply(owner, arguments);
		}
	},

	registerLoadEvent: function(event) {
		var previous = window.onload;

		window.onload = (previous) ? function() { event(); previous(); } : event;
	}
}


var HeadlinesTicker = ForwardToolbox.create({

	initialize: function() {
		var container = document.getElementById && document.getElementById("ticker");

		if (!container) return;

		container.onmouseover = this.pause;
		container.onmouseout = this.resume;

		this.guide = 100;
		this.destination = 0;

		this.items = container.getElementsByTagName("li");
		this.currentItem = this.items[0];

		for (var i = 1; i < this.items.length; i++)
			this.items[i].style.opacity = 0;

		window.onblur = this.pause;
		window.onfocus = this.resume;

		this.timer = window.setInterval(this.increment, 5000);
	},

	increment: function() {
		if (this.paused) return;

		for (var i = 0; i < this.items.length; i++) {
			if (this.items[i] == this.currentItem) break;
		}

		this.nextItem = this.items[i + 1] || this.items[0];

		window.setTimeout(this.animate, 32);
	},

	animate: function() {
		this.guide += (this.destination - this.guide) / 3;

		this.currentItem.style.opacity = Math.round(this.guide) / 100;

		if (this.currentItem.style.opacity * 100 != this.destination) {
			window.setTimeout(this.animate, 32);

		} else if (this.nextItem != this.currentItem) {
			this.currentItem.className = "";
			this.currentItem = this.nextItem;
			this.currentItem.className = "active";

			this.destination = 100 - this.destination;

			window.setTimeout(this.animate, 32);

		} else {
			this.guide = this.destination;
			this.destination = 100 - this.guide;
		}
	},

	pause: function() {
		this.paused = true;
	},

	resume: function() {
		this.paused = false;
	}

});


var ArticleTools = ForwardToolbox.create({

	initialize: function() {
		var articleTools = document.getElementById("articletools");

		// Email Article
		this.trigger = document.getElementById("email-to-friend");

		if (this.trigger)
			this.trigger.onclick = this.open;

		this.done = document.getElementById("return-to-article");

		if (this.done) {
			this.done.innerHTML = "Close this window";
			this.done.onclick = this.close;
		}

		if (!articleTools) return;

		// Text Sizing
		this.container = document.getElementById("content");

		var states = ["text-tiny", "text-smaller", "text-small", "text-medium", "text-large",
			"text-larger", "text-huge"];

		this.increaseReference = {};
		this.decreaseReference = {};

		for (var i = 0; i < states.length; i++) {
			this.increaseReference[states[i]] = states[i + 1] || states[i];
			this.decreaseReference[states[i]] = states[i - 1] || states[i];
		}

		document.getElementById("text-larger").onclick = this.increase;
		document.getElementById("text-smaller").onclick = this.decrease;

		// Printing
		var printButton = document.getElementById("print");

		printButton.onclick = this.print;
	},

	open: function() {
		var x = window.screenX || window.screenLeft;
		var y = window.screenY || window.screenTop;

		var width = window.outerWidth || window.screen.width;
		var height = window.outerHeight || window.screen.height;

		var left = x - 180 + (width - width % 2) / 2;
		var top = y - 250 + (height - height % 2) / 2;

		window.open(this.trigger.href, null, "left=" + left + ",top=" + top +
			",height=575,width=360,toolbar=no,location=no,menubar=no");

		return false;
	},

	close: function() {
		return window.close() && false;
	},

	increase: function() {
		var currentState = this.container.className || "text-medium";
		this.container.className = this.increaseReference[currentState];

		return false;
	},

	decrease: function() {
		var currentState = this.container.className || "text-medium";
		this.container.className = this.decreaseReference[currentState];

		return false;
	},

	print: function() {
		window.print();
		return false;
	}

});

