var ipb = {};
ipb.vars = {};

ipb.vars['base_url'] = location.protocol + '//' + location.host + '/forums/index.php?';
ipb.vars['charset'] = "UTF-8"; 
ipb.vars['emoticon_url'] = "http://www.logic-sunrise.com/forums/public/style_emoticons/default";

var shoutboxLoader = {
	boot: function()
	{
		document.write("<script type='text/javascript' src='javascript/shoutbox.ajax.js'></script>");
	}
};

window.shoutbox = new Class({
	baseUrl:		ipb.vars['base_url'] + 'app=shoutbox&module=ajax&section=coreAjax&',
	
	can_use:			0,
	can_edit: 			0,
	members_refresh:	15,
	shouts_refresh:		30,
	hide_refresh:		1,
	editor_rte:			0,
	editor_height:		'125px',
	flood_limit:		0,
	bypass_flood:		0,
	my_last_shout:		0,
	total_shouts:		0,
	last_shout_id:		0,
	inactive_timeout:	5,	
	
	errors:			[],
	langs:			[],
	month_days:		[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
	langs:			[],
	
	global_on:			false,
	enable_cmds:		false,
	mod_in_action:		false,
	moderator:			false,
	_inactive:			false,
	view_archive:		false,
	events_loaded:		false,
	events_rte_loaded:	false,
	submittingShout:	false,
	inactiveWhenPopup:	false,
	animatingGlobal:	false,
	archive_filtering:	false,
	
	mod_shout_id:			0,
	time_minute:			60 * 1000,
	events_load_max_tries:	10,
	events_load_tries:		0,
	
	timeoutShouts:	null,
	timeoutMembers:	null,
	mod_command:	'',
	emoPalette:		null,
	tempShout:		null,
	
	initialize:	function()
	{
		window.addEvent('domready', function()
		{
			ipshoutbox.global_on = false;
			ipshoutbox.setupShoutbox();
		}.bind(this));
	},
	
	setupShoutbox: function()
	{
		if ( !ipshoutbox.can_use )
		{
			ipshoutbox.myMemberID   = 0;
			ipshoutbox.moderator    = 0;
			ipshoutbox.hide_refresh = 0;
			ipshoutbox.view_archive = false;
		}
		
		ipshoutbox.initEvents();
		ipshoutbox.updateLastActivity();
		ipshoutbox.rewriteShoutClasses();
		ipshoutbox.shoutsGetLastID();
		ipshoutbox.shoutsScrollThem();
		ipshoutbox.shoutAjax.reloadShouts(true);
	},
	
	rewriteShoutClasses: function(check)
	{
		var table = null,
			check = check == false ? false : true;
			skip  = 0;
		
		if ( check && ipshoutbox.in_archive )
		{
			table = $('shoutbox-archive-shouts');
		}
		else
		{
			table = $('shoutbox-shouts-table');
		}
		
		/* Let's update the rows! =D */
		$A( table.getFirst('tbody').getChildren() ).each(
			function(tr)
			{
				skip = ( skip == 0 ) ? 1 : 0;
				
				$A( tr.getChildren() ).each(
					function(td)
					{
						/* Remove inline styles added by Highlight */
						td.setStyles({
							backgroundColor: '',
							backgroundImage: ''
						});
						
						if ( skip )
						{
							if ( td.hasClass('altrow') )
							{
								td.removeClass('altrow');
							}
						}
						else
						{
							td.addClass('altrow');
						}
					}
				);
			}
		);
	},
	
	initEvents: function()
	{
		if ( !ipshoutbox.events_loaded && $('input-shoutbox'))
		{
			ipshoutbox.events_loaded = true;
			$('input-shoutbox').addEvent('keypress', ipshoutbox.keypress_handler);
		}
	},
	
	produceError: function( error )
	{
		var errorDiv  = 'app';
		
		/* Global shoutbox? */
		if ( ipshoutbox.global_on )
		{
			if ( ipshoutbox.can_edit && ipshoutbox.mod_in_action )
			{
				errorDiv = 'editShout';
			}
			else
			{
				errorDiv = 'glb';
			}
		}
		else if ( ipshoutbox.in_prefs )
		{
			errorDiv = 'myprefs';
		}
		else if ( ipshoutbox.in_mod )
		{
			errorDiv = 'moderator';
		}
		else if ( ipshoutbox.in_archive )
		{
			errorDiv = 'archive';
		}
		
		if ( ipshoutbox.errors[ error ] )
		{
			error = ipshoutbox.errors[ error ];
		}
		if ( $('shoutbox-inline-error-'+errorDiv) )
		{
			$('shoutbox-inline-error-'+errorDiv).set('html', error ).setStyle('display', '');
			setTimeout("$('shoutbox-inline-error-" + errorDiv + "').setStyle('display', 'none')", 2000);
		}
		else
		{
			alert( error );
		}
	},
	
	getShout: function()
	{
		var d = '';
		d = $('input-shoutbox').value;
		d = d.trim();
		
		while (d.match(new RegExp("^(.+?)<br>$", 'i')))
		{
			d = d.replace(new RegExp("^(.+?)<br>$", 'i'), '$1');
		}
	
		d = d.trim();
		
		if (d.toLowerCase().substring(d.length-4, d.length) == '<br>')
		{
			d = d.substring(0, d.length-4);
		}
	
		d = d.trim();
		
		return d;
	},
	
	clearShout: function()
	{
		ipshoutbox.updateLastActivity();

		ipshoutbox.tempShout = $('input-shoutbox').get('value');
		$('input-shoutbox').set('value', "");
		$('input-shoutbox').focus();
	},
	
	restoreShout: function()
	{
		if ( ipshoutbox.tempShout != null )
		{
			$('input-shoutbox').set('value', ipshoutbox.tempShout );
			$('input-shoutbox').focus();
		}
	},
	
	getTimestamp: function()
	{
		var d = new Date(),
			t = d.getTime();
	
		return Math.floor( t / 1000 );
	},
	
	shoutsGetLastID: function()
	{
		var tempLastID = 0;
		
		$A( $('shoutbox-shouts-table').getFirst('tbody').getChildren() ).each(
			function(tr)
			{
				tempLastID = parseInt(tr.id.substring(10));
				
				if ( tempLastID > ipshoutbox.last_shout_id )
				{
					ipshoutbox.last_shout_id = tempLastID;
				}
			}
		);
		
		return ipshoutbox.last_shout_id;
	},
	
	shoutsScrollThem: function()
	{
		var area = $( 'shoutbox-shouts' );
		
		if ( ipshoutbox.shout_order == 'asc' )
		{
			area.scrollTop = area.scrollHeight - parseInt( area.getHeight() ) + 500;
		}
		else
		{
			area.scrollTop = 0;
		}
	},
	
	isInactive: function()
	{
		
		if ( ipshoutbox._inactive )
		{
			return true;
		}
	
		var diff     = parseInt( ipshoutbox.getTimestamp() - ipshoutbox.last_active ),
			myMin    = ( diff / 60 ) * ipshoutbox.time_minute,
			checkMin = ipshoutbox.inactive_timeout * ipshoutbox.time_minute;
		
		if ( myMin >= checkMin )
		{
			ipshoutbox.displayInactivePrompt();
			return true;
		}
		else
		{
			return false;
		}
	},
	
	displayInactivePrompt: function()
	{
		ipshoutbox._inactive = true;
		clearTimeout( ipshoutbox.timeoutShouts );
		clearTimeout( ipshoutbox.timeoutMembers );
		
		$('shoutbox-shouts-table').setStyle('display', 'none');
		$('shoutbox-inactive-prompt').setStyle('display', '');
	},
	
	processInactivePrompt: function()
	{
		if ( !ipshoutbox._inactive )
		{
			return false;
		}
		
		/** Do some common things =O **/
		ipshoutbox._inactive = false;
		ipshoutbox.updateLastActivity();
		
		// Which shoutbox? :D
		if ( ipshoutbox.global_on )
		{
			$('shoutbox-inactive-prompt').setStyle('display', 'none');
			$('shoutbox-shouts-table').setStyle('display', '');
			
			// Refresh shout only if we are not submitting
			if ( !ipshoutbox.submittingShout )
			{
				ipshoutbox.shoutAjax.reloadShouts(true);
			}
		}
		else
		{
			ipshoutbox.inactivePrompt.setStyle('display', 'none');
			
			// App page, get also members :D
			ipshoutbox.shoutAjax.reloadMembers(true);
			ipshoutbox.shoutAjax.reloadShouts(true);
		}
	},
	
	refreshShouts: function()
	{
		if ( ipshoutbox._inactive )
		{
			return false;
		}
		
		ipshoutbox.updateLastActivity();
		ipshoutbox.shoutAjax.reloadShouts(true);
	},
	
	rect: function(x, y, w, h)
	{
		var recta = new Object;
		recta.x = x;
		recta.y = y;
		recta.w = w;
		recta.h = h;
		
		return recta;
	},
	
	shouts_fade: function(ids)
	{
	},

	keypress_handler: function(e)
	{
		if (!e || e == null || typeof(e) == 'undefined')
		{
			return false;
		}

		ipshoutbox.updateLastActivity();

		var ret = true;
		var alt   = e.altKey;
		var ctrl  = e.ctrlKey;
		var shift = e.shiftKey;
		if (e.code > 0)
		{
			var key = e.code;
		}

		var obj = (e.srcElement) ? e.srcElement : e.target;
		if ( Browser.Engine.webkit || Browser.Engine.gecko )
		{
			if (obj.nodeType == 3 && e.target.parentNode.nodeType == 1)
			{
				obj = e.target.parentNode;
			}
		}

		if (typeof(obj.id) == 'undefined')
		{
			obj.id = '';
		}

		if ( ipshoutbox.my_prefs['enter_key_shout'] == 1 && obj.id == 'input-shoutbox' && !shift && !alt && !ctrl && key == 13)
		{
			ipshoutbox.shoutAjax.submitShout();
			
			Event.stop(e);
			return false;
		}
		else if ( (alt && key == 13) || (ctrl && key == 13) )
		{
			ipshoutbox.shoutAjax.submitShout();
			
			Event.stop(e);
			return false;
		}

		return true;
	},

	checkForErrors: function( text, status )
	{
		if ( text.substring(0, 6) == 'error-' )
		{
			if ( status != null && status != '' )
			{
				ipshoutbox.popupUpdateStatus( status );
			}
			
			ipshoutbox.produceError( text.substring(6) );
			
			return true;
		}
		else
		{
			return false;
		}
	},
	
	updateLastActivity: function()
	{
		ipshoutbox.last_active = ipshoutbox.getTimestamp();
	},
	
	updateTotalShouts: function( newCount )
	{
		ipshoutbox.total_shouts = parseInt(newCount);
	}
});

