//RaidsRUs, <http://www.raidsrus.com>. Copyright (c) 2007-2008 Bas Scholts. All rights reserved

// Site class - main active content wrapper
var Site = {
	start: function()
	{
		if ($('forumtracker'))
			Forumtracker.initialize();
		if ($('momrelease-countdown'))
			Countdown.initialize();
		
		this.constructionLinks();
	},
	constructionLinks: function()
	{
		this.constructlinks = $$('li.construction a');
		if (this.constructlinks.length >= 1)
		{
			this.constructlinks.each(function(item){
				item.addEvent('mouseenter', function(){this.set('text', 'Under construction');});
				item.addEvent('mouseleave', function(){this.set('text', this.get('title'));});
				item.addEvent('click', function(){alert('Sorry, this page is under construction.\n\nPlease try again later!');return false;});
			}, this);
		}
	}
};

// Forumtracker class - Forumtracker functionality
var Forumtracker = {
	initialize: function()
	{
		this.forumtracker = $('forumtracker');
		this.outputBox = $('forumtracker-output');
		//this.refresh();
		this.refresh.periodical(300*1000, this);
	},
	refresh: function()
	{
		if(this.request)
			this.request.cancel();
		this.request = new Ajax('/json.php?q=forumtracker', {
			method: 'post',
			onComplete: function(jsonObj) {
				this.request = null;
				this.refill(Json.evaluate(jsonObj));
			}.bind(this)
		}).request();
	},
	refill: function(data)
	{
		this.outputBox.empty();
		data.each(function(r, idx) {
			var item = new Element('li');
			item.setHTML('<a class="thread" href="/forum/showthread.php?p='+r.postId+'#post'+r.postId+'">'+r.threadTitle+'</a><br />'+
			'<a class="username" href="/forum/member.php?u='+r.userId+'">'+r.userName+'</a> - '+r.postTime);
			item.injectInside(this.outputBox);
		}.bind(this));
	}
};

var Countdown = {
	initialize: function()
	{
		if ($('momrelease-countdown'))
		{
			$('momrelease-countdown').setStyle('display','none');
			$('momrelease-countdown-loader').setStyle('display','none');
		}
		if ($('momrelease-countdown-forum'))
		{
			$('momrelease-countdown-forum').setHTML('<strong>The Gates are <span style="color:red">OPEN</span>!</strong>');
			$('momrelease-countdown').getParent('td').setStyle('text-align','center');
		}
		if ($('momrelease-countdown-site'))
		{
			$('momrelease-countdown-site').setHTML('<strong>The Gates are <span style="color:red">OPEN</span>!</strong>');
			$('momrelease-countdown').getParent('div.panel-inner').setStyle('text-align','center');
		}
	}
}

// Set vent to start running active content when DOM is loaded
window.addEvent('domready', function() { Site.start(); });