var	path		=	"img/nav_";
var	races		=	new Array( "", "all", "kineten", "vitali", "chaosianer", "syntheten" );
var	spelltypes	=	new Array( "", "all", "ruf", "zauber", "magie" );
var popupLayer;

function init()
{
	popupLayer	=	getElement( "popup" );
	
	if( typeof( setup ) != "undefined" )
	{
		setup();
	}
	
	if( typeof( initPrint ) != "undefined" )
	{
		initPrint();
	}

	etherPlanes.init();
	etherPlanes.buildLinks();
}

function up( nr )
{
	var el		=	getElement( "nav"+nr );
	var base	=	getImageBase( el.src );
	el.src	=	base+"_1.gif";
}

function dn( nr )
{
	var el		=	getElement( "nav"+nr );
	var base	=	getImageBase( el.src );
	el.src	=	base+"_0.gif";
}

function getImageBase( path )
{
	base	=	path.substring( 0, ( path.length-6 ) );
	return base;
}

function in_array( item, array )
{
	for( i = 0; i < array.length; i ++ )
	{
		if( array[i] == item )
			return true;
	}
	
	return false;
}

var etherPlanes	=	{

	el:				false,
	waiter:			false,
	lang: 			'en',
	dispatcher:		'',
	version:		'',
	links:			[],
	linkIdCounter:	0,
	bPos:			0,			// the bottom pixel offset of the whole page
	navEndPos:		0,			// the pixel offset of the end of the navigation
	
	init:function()
	{
		var bPosEl	=	document.getElementById( 'bottomPos' );
		this.bPos	=	getElementY( bPosEl ) + 1;
		
		var navEndPosEl	=	getElement( 'navEnd' );
		this.navEndPos	=	getElementY( navEndPosEl );

		this.positionNavEnd();
		this.positionSponsor();
		
		if( this.waiter )
		{
			var waiterElc	=	document.getElementById( 'waiterContainer' );
			waiterElc.style.height	=	this.bPos;
		}
	},
	
	positionNavEnd:function()
	{
		var navStopperEl	=	getElement( 'navStop' );
		moveElementTo( navStopperEl, 208, this.navEndPos-15 );
		showElement( navStopperEl );
	},
	
	positionSponsor:function()
	{
		if( this.lang == 'de' )
		{	
			var sponsorEl	=	getElement( 'sponsor' );
			moveElementTo( sponsorEl, 60, this.navEndPos+114 );
			showElement( sponsorEl );
		}
	},

	displayImage:function( image, width, height )
	{
		var url	=	'viewImage.php?image=' + image;
		window.open( url, '', 'width='+width+',height='+height+',scrollbars=no' );
	},
	
	showTooltip:function( content )
	{
		if( this.el === false )
			this.el	=	document.getElementById( 'tooltip' );

		var xpos	=	mouseX + 10;
		var ypos	=	mouseY + 10;
		
		var html	=	''+
			'<table width="367" cellpadding="0" cellspacing="0" border="0">'+
			'	<tr>'+
			'		<td>'+
			'			<img src="img/popup_top.gif" width="367" height="7" alt="" border="0"><br>'+
			'			<table width="367" cellpadding="0" cellspacing="0" border="0">'+
			'				<tr valign="top">'+
			'					<td width="9" background="img/popup_bgleft.gif"><img src="img/popup_bgleft.gif" width="9" height="50" alt="" border="0"></td>'+
			'					<td width="342" class="text" background="img/popup_bg.gif" valign="middle">'+
									content+
			'						<div><img src="img/px.gif" width="1" height="6" alt="" border="0"></div>'+
			'					</td>'+
			'					<td width="16" background="img/popup_bgright.gif"><img src="img/popup_bgright.gif" width="16" height="50" alt="" border="0"></td>'+
			'				</tr>'+
			'			</table>'+
			'			<img src="img/popup_bottom.gif" width="367" height="11" alt="" border="0"><br>'+
			'		</td>'+
			'	</tr>'+
			'</table>';
			
		moveElementTo( this.el, xpos, ypos );
		this.el.innerHTML	=	html;
		this.el.style.visibility	=	'visible';
	},
	
	hideTooltip:function()
	{
		if( this.el === false )
			this.el	=	document.getElementById( 'tooltip' );
			
		this.el.style.visibility	=	'hidden';
	},

	buildLinks:function()
	{
		var	tags = document.getElementsByTagName( "div" );
	
		for( var i = 0; i < tags.length; i++ )
		{
			if( !tags[i].getAttribute( "etherPlanes:link" ) )
				continue;
			
			var lid		=	this.createLinkId();
			var uri		=	tags[i].getAttribute( 'etherPlanes:link' );
			var type	=	tags[i].getAttribute( 'etherPlanes:linkType' );
			var tmpl	=	tags[i].getAttribute( 'etherPlanes:linkTmpl' );
			var title	=	tags[i].innerHTML;
			
			var container	=	new etherPlanes_LinkContainer( lid, uri, type, title, tmpl, tags[i] );
			
			this.links.push( container );
			
			this.buildLink( lid );
		}
	},
	
	getUrl:function( uri, type )
	{
		if( typeof( type ) == 'undefined' )
			type	=	'internal';
		
		var target;
		
		switch( type )
		{
			case 'external':
				window.open( uri, null, this.getWindowOptions( 'full' ) );
				break;
				
			case 'submitForm':
				document.forms.main.submit();
				break;
				
			case 'callback':
				eval( uri );
				break;
				
			case 'download':
				this.download( uri );
				break;
				
			case 'anchor':
				document.location	=	'#' + uri;
				break;
		
			case 'internal':
			default:
				if( uri.indexOf( 'PHPSESSID' ) < 1 )
				{
					target	=	this.dispatcher + uri;
				}
				else
				{
					target	=	uri;
				}
				document.location	=	target;
				break;
		}
	},
	
	buildLink:function( lid )
	{
		var link	=	this.getLink( lid );
		var html;
		
		switch( link.tmpl )
		{
			case 'normal':
				html	=	''
						+	'<a href="javascript:etherPlanes.getUrl(\'' + link.uri + '\', \'' + link.type + '\');">' + link.title + '</a>';
				break;
		
			case 'goto':
				html	=	''
						+	'<table cellpadding="0" cellspacing="0" border="0">'
						+	'	<tr valign="top">'
						+	'		<td><a href="javascript:etherPlanes.getUrl(\'' + link.uri + '\', \'' + link.type + '\');"><img src="img/b_callurl_0.gif" width="18" height="23" alt="" border="0"></a></td>'
						+	'		<td class="text" nowrap><a href="javascript:etherPlanes.getUrl(\'' + link.uri + '\', \'' + link.type + '\');">' + link.title + '</a>&nbsp;</td>'
						+	'	</tr>'
						+	'</table>';
				break;
				
			case 'hand':
				html	=	''
						+	'<table cellpadding="0" cellspacing="0" border="0">'
						+	'	<tr valign="top">'
						+	'		<td><a href="javascript:etherPlanes.getUrl(\'' + link.uri + '\', \'' + link.type + '\');"><img src="img/b_back_0.gif" width="40" height="20" alt="" border="0"></a></td>'
						+	'		<td class="text" nowrap style="padding-top:2px;padding-left:2px;"><a href="javascript:etherPlanes.getUrl(\'' + link.uri + '\', \'' + link.type + '\');" class="wht">' + link.title + '</a>&nbsp;</td>'
						+	'	</tr>'
						+	'</table>';
				break;
				
			case 'navigate':
				html	=	''
						+	'<table cellpadding="0" cellspacing="0" border="0">'
						+	'	<tr>'
						+	'		<td class="text"><a href="javascript:etherPlanes.getUrl(\'' + link.uri + '\', \'' + link.type + '\');" class="wht">' + link.title + '</a>&nbsp;</td>'
						+	'		<td><a href="javascript:etherPlanes.getUrl(\'' + link.uri + '\', \'' + link.type + '\');"><img src="img/b_go_0.gif" width="22" height="22" alt="" border="0"></a></td>'
						+	'	</tr>'
						+	'</table>';
				break;
		}

		link.el.innerHTML	=	html;
	},
	
	getLink:function( lid )
	{
		for( var i = 0; i < this.links.length; i++ )
		{
			if( this.links[i].id == lid )
				return this.links[i];
		}
	},
	
	createLinkId:function()
	{
		this.linkIdCounter++;
		
		return this.linkIdCounter;
	},
	
	getWindowOptions:function( type )
	{
		switch( type )
		{
			case 'full':
				return 'location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,dependent=no,hotkeys=yes';
		
			default:
				return '';
		}
	},
	
	download:function( dlid )
	{
		uri	=	'download.php?fileid=' + dlid;
		this.getUrl( uri, 'external' );
	},
	
	viewSpell:function( spid )
	{
		uri	=	'&section=SpellArchive&page=ShowSpell&showspell=' + spid + '&version=' + this.version;
		this.getUrl( uri, 'internal' );
	},
	
	viewReplay:function( rid )
	{
		uri	=	'&section=ReplaysDatabase&page=ShowReplay&rid=' + rid;
		this.getUrl( uri, 'internal' );
	},
	
	addComment:function()
	{
		var cel	=	document.getElementById( 'addComment' );
		var celc	=	document.getElementById( 'addCommentContainer' );

		alert( 'bPos: ' + this.bPos );
		
		celc.style.height	=	this.bPos;
		cel.style.visibility = 'visible';
	}
}

function etherPlanes_LinkContainer( lid, uri, type, title, tmpl, el )
{
	this.id		=	lid;
	this.uri	=	uri;
	this.title	=	title;
	this.type	=	type;
	this.el		=	el;
	this.tmpl	=	tmpl;
}


