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(callback) {
		var previous = window.onload;

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

var HeadlinesTicker = ForwardToolbox.create({
	initialize: function() {
		var container = 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];

		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) continue;

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

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

		var k = Math.round(this.guide);

		this.currentItem.style.opacity = k / 100;

		if (k != this.destination) {
			window.setTimeout(this.animate, 40);

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

			this.destination = 100 - this.destination;

			window.setTimeout(this.animate, 40);

		} 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=720,width=390,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;
	}
});

// Update current time
ForwardToolbox.registerLoadEvent(function() {
	var ts = document.getElementById("timestamp"),
	    nw = new Date().toUTCString().replace(/(\w+)\W+(\w+)\W+(\w+)\W+(\w+).+/, "$1. $3 $2, $4");

	if (ts) ts.firstChild.data = nw;
});
