var AeonsShips = Class.create();

AeonsShips.prototype = {
	'ships':{},
	'active':0,
	'selectorOpen': false,
	'selectorGenerated': false,
	'options':{
		'overlayDuration':0.2,
		'overlayOpacity': 0.8,
		'shipVisualsUrl':null,
		'imageFolderUrl':null,
		'nextImage':'next.png',
		'prevImage':'prev.png',
		'menuWidth':232,
		'instanceID':null,
		'zoomLink':false,
		'menuIcons':false
	},
	
	initialize:function(){},
	
	init:function()
	{
		// display the ships navigation
		$( 'ShipsNav' ).style.display = 'block';

		// create the ships selector div
		var objBody = $$('body')[0];
		objBody.appendChild(Builder.node('div',{id:'ShipsSelector'}));
		objBody.appendChild(Builder.node('div',{id:'ShipsOverlay'}));

		$('ShipsOverlay').hide().observe('click', (function() { this.closeSelector(); }).bind(this));
		$('ShipsSelector').hide();
	},
	
	setOption:function( name, value )
	{
		this.options[name] = value;
	},
	
	add:function( shipID, name, hull, role, image_url, icon_url, mini_url, wiki_url )
	{
		var ship = {
			'shipID':shipID,
			'name':name,
			'hull':hull,
			'image_url':image_url,
			'icon_url':icon_url,
			'mini_url':mini_url,
			'wiki_url':wiki_url,
			'role':role,
			'idx':0
		};
		
		this.ships[shipID] = ship;
	},
	
	setActive:function( nr )
	{
		this.active = nr;
	},
	
	getShip:function( nr )
	{
		if( !this.ships[nr] ) {
			return false;
		}
		
		return this.ships[nr];
	},
	
	getLast:function()
	{
		var last = null;
		for( var nr in this.ships ) {
			last = nr;
			// nothing to do, we just
			// want the last nr to be
			// stored in the variable.
		}
		
		return this.ships[last];
	},
	
	getFirst:function()
	{
		for( var nr in this.ships ) {
			return this.ships[nr];
		}
	},
	
	previous:function()
	{
		var prevID = null;
		for( var nr in this.ships ) {
			if( nr == this.active ) {
				break;
			}
			
			prevID = nr;
		}
		
		if( !this.getShip( prevID ) ) {
			prevID = this.getLast().shipID;
		}
		
		this.setActive( prevID );
		this.display();
	},
	
	next:function()
	{
		var nextID = null;
		var stop = false;
		for( var nr in this.ships ) {
			if( nr == this.active ) {
				stop = true;
				continue;
			}

			if( stop ) {
				nextID = nr;
				break;
			}
		}
		
		if( !this.getShip( nextID ) ) {
			nextID = this.getFirst().shipID;
		}
		
		this.setActive( nextID );
		this.display();
	},
	
	display:function()
	{
		var ship = this.getShip( this.active );
		
		$('ShipName').innerHTML = ship.name;
		$('ShipHull').innerHTML = ship.hull;
		$('ShipRole').innerHTML = ship.role;
		$('ShipHull').setAttribute( 'href', ship['wiki_url'] );
		$('ShipLink').setAttribute( 'href', ship['image_url'] );
		$('ShipLink').setAttribute( 'title', 'Aeon\'s '+ship['hull']+' \''+ship['name']+'\'' );
		$('ShipIcon').setAttribute( 'src', ship['icon_url'] );
		
		if( this.options.zoomLink ) {
			$('ShipLinkText').setAttribute( 'href', ship['image_url'] );
		}
	},
	
	closeSelector:function()
	{
		$('ShipsSelector').hide();
		new Effect.Fade($('ShipsOverlay'), { duration: this.options.overlayDuration });
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
		this.selectorOpen = false;
	},
	
	displayShip:function( nr )
	{
		if( this.selectorOpen == true ) {
			this.closeSelector();
		} 
		
		this.setActive( nr );
		this.display();
	},
	
	hiRow:function( nr )
	{
		$('ShipsSelectorRowIcon'+nr).style.background='#9dbfac';
		$('ShipsSelectorRowText'+nr).style.background='#9dbfac';
		$('ShipsSelectorRowText'+nr).style.color='#fff';
	},
	
	loRow:function( nr )
	{
		$('ShipsSelectorRowIcon'+nr).style.background='#fff';
		$('ShipsSelectorRowText'+nr).style.background='#fff';
		$('ShipsSelectorRowText'+nr).style.color='#000';
	},
	
	openSelector:function()
	{
		var instanceName = 'AeonsShipsObj'+this.options.instanceID;
		
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });

		// stretch overlay to fill page and fade in
		var arrayPageSize = this.getPageSize();
		$('ShipsOverlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });

		new Effect.Appear($('ShipsOverlay'), { duration: this.options.overlayDuration, from: 0.0, to: this.options.overlayOpacity });

		// we only need to generate this html once, as 
		// no new ships are added on runtime.
		if( !this.selectorGenerated ) { 
			var html = 
			'<div><img src="'+this.options.imageFolderUrl+'/shipselector_title.gif" border="0" onclick="'+instanceName+'.closeSelector();" style="cursor:pointer;display:block;"/></div>'+
			'<div style="background:url('+this.options.imageFolderUrl+'/shipselector_bg.gif);width:'+this.options.menuWidth+'px;">'+
				'<div style="padding:0 6px;">'+
					'<div style="height:260px;overflow:auto;">'+
						'<table cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;">'+
							'<tbody>';
								for( var nr in this.ships ) {
									html += 
									'<tr onclick="'+instanceName+'.displayShip( \''+nr+'\' );" style="cursor:pointer;" onmouseover="'+instanceName+'.hiRow( \''+nr+'\' );" onmouseout="'+instanceName+'.loRow( \''+nr+'\' );">';
										if( this.options.menuIcons ) {
											html += '<td id="ShipsSelectorRowIcon'+nr+'" style="padding:5px 0;border-bottom:solid 1px #cacaca;"><img src="'+this.ships[nr]['mini_url']+'" border="0" style="margin-right:6px;"/></td>';
										}
										
										html +=
										'<td id="ShipsSelectorRowText'+nr+'" align="left" width="100%" valign="middle" style="padding-bottom:5px 0;border-bottom:solid 1px #cacaca;font-size:11px;">'+
											'"<b>'+this.ships[nr].name+'</b>"<br/>'+
											this.ships[nr].hull+
										'</td>'+
									'</tr>';
								}
								html +=
							'</tbody>'+
						'</table>'+
					'</div>'+
				'</div>'+
			'</div>'+
			'<div><img src="'+this.options.imageFolderUrl+'/shipselector_bottom.gif" border="0" onclick="'+instanceName+'.closeSelector();" style="cursor:pointer;display:block;"/></div>';

			$('ShipsSelector').innerHTML = html;
			this.selectorGenerated = true;
		}

		var arrayPageScroll = document.viewport.getScrollOffsets();
		var top = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
		var spaceLeft = document.viewport.getWidth() - arrayPageScroll[0];
		var left = ( spaceLeft / 2 ) + ( $('ShipsSelector').getWidth() / 2 );
		$('ShipsSelector').setStyle({ top: top + 'px', left: left + 'px' }).show();
		
		this.selectorOpen = true;
	},
	
    getPageSize: function() 
    {
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
};