/**
 * File containing the ShipsQuickinfo class
 * 
 * @see ShipsQuickinfo
 */

/**
 * Clientside support script. See the usage examples 
 * to see how this can be configured.
 *
 * @access public
 * @package ShipsQuickinfo
 * @subpackage Clientside Scripts
 * @author AeonOfTime <eve@aeonoftime.com>
 * @license http://creativecommons.org/licenses/BSD/
 * @link http://aeonoftime.com/EVE_Online_Tools/Ships_Quickinfo/
 */
var ShipsQuickinfo = {
		
   /**
    * Stores the relative or absolute path to the image folder
    * 
    * Note: Include the trailing slash!
    * 
    * @access public
    */
	imgFolder:'./images/',

   /**
    * Stores the relative or absolute path to the scripts folder
    * 
    * Note: include the trailing slash!
    * 
    * @access public
    */
	scriptFolder:'./',
	
   /**
    * Sets the title of the tooltip (the title bar)
    * 
    * @access public
    */
	tooltipTitle:'Aeon\'s ShipsQuickinfo v1.6',
	
   /**
    * The tooltip library to use. 
    * 
    * NOTE: You can add your own by implementing the 
    * according tooltipify_libraryname() method and 
    * setting this parameter.
    *  
    * @access public
    */
	tooltipLibrary:'default',
	
   /**
    * Whether to add a graphical button to the link which
    * inherits the href attribute of the initial link.
    * 
    * @access public
    */
	appendButton:true,

   /**
    * Sets the tab that should be opened when the info
    * popup is first displayed. The following values are
    * possible:
    * 
    * descr - The ship's description
    * data - The ship's detailed specifications
    * eyecandy - The ship preview (if available and enabled) 
    * 
    * Note: if you specify a tab that does not exist or
    * that is not available, the description tab will be used.
    */
	defaultTab:'descr',
	
   /**
    * The default ship view to show in the preview tab
    * (when available). The following values are possible:
    * 
    * front
    * side
    * rear
    * 
    * Note: if you specify an unknown view, the front view
    * will be used.
    */
	defaultShipView:'front',

   /**
    * Sets the maximum width of the content of the tooltips
    * 
    * @access public
    */
	contentMaxWidth:460,

   /**
    * Tooltips counter.
    * 
    * @access private
    */
	counter:0,
	
   /**
    * Goes through all links in the page's DOM and extends
    * all links whose rel attribute is "ShipsQuickinfo".
    * 
    * @access public
    */
	init:function()
	{
		// make sure that having jQuery included in the same page does not 
		// create any conflicts.
		if( typeof( jQuery ) != 'undefined' ) {
			jQuery.noConflict();
		}

		var els = document.getElementsByTagName( 'a' );
		for( var i=0; i < els.length; i++ ) {
			if( els[i].getAttribute( 'rel' ) == 'ShipsQuickinfo' ) {
				try{
					anchorEl = els[i];
					// First off, we modify the anchor tag with our
					// own functionality, and store the original
					// href attribute in the more button if enabled.
					Element.extend( anchorEl );
					var moreEl = false;
					var href = anchorEl.getAttribute( 'href' );
					
					// only add the more button if there is an href 
					// attribute and if the more button is enabled at all
					if( href && this.appendButton ) {
						var moreEl = new Element( 
							'img', 
							{ 
								'src':this.imgFolder+'more.png', 
								'title':'External information on the '+anchorEl.innerHTML,
								'class':'SQTMoreButton'
							} 
						);
						Element.insert( anchorEl, { after: moreEl } );
					}
					
					if( !this['tooltipify_'+this.tooltipLibrary] ) {
						this.tooltipLibrary = 'default';
					}
					
					// create the tooltip object
					this['tooltipify_'+this.tooltipLibrary]( anchorEl, moreEl );
				} catch( err ) {
					// continue silently if this link cannot be extended
				}
			}
		}
	},

   /**
    * Adds the tooltip functionality to a link
    * using the built-in tooltips solution.
    */
	tooltipify_default:function( sourceElement, buttonElement )
	{
		this.counter++;

	   /**
	    * Individual tooltip class used to handle
	    * operations on a specific tooltip.
	    * 
	    * @package ShipsQuickinfo
	    * @subpackage Clientside Scripts
	    * @author AeonOfTime
	    */
		var tip = { 
				
		   /**
		    * The ID of this tooltip
		    * 
		    * @var int
		    */
			id:this.counter,
			
		   /**
		    * Reference to the source element 
		    * (the link that was clicked to open
		    * the tooltip)
		    * 
		    * @var DOMElement
		    */
			source: sourceElement,
			
		   /**
		    * Reference to the button element, if present
		    * (the dynamically added "more" button)
		    * 
		    * @var DOMElement
		    */
			button: buttonElement,
			
		   /**
		    * The title to use for the tooltip
		    * 
		    * @var string
		    */
			title:this.tooltipTitle,
			
		   /**
		    * The folder in which the images reside
		    * 
		    * @var string
		    */
			imgFolder:this.imgFolder,
			
		   /**
		    * The folder in which all the scripts reside
		    * 
		    * @var string
		    */
			scriptFolder:this.scriptFolder,
			
		   /**
		    * The horizonzal offset from the source element
		    * for the tooltip to show at.
		    * 
		    * @var int
		    */
			offsetX:3,
			
		   /**
		    * The vertical offset from the source element
		    * for the tooltip to show at.
		    * 
		    * @var int
		    */
			offsetY:3,
			
		   /**
		    * The maximum width for the content of the 
		    * tooltip, used to enforce this width regardless
		    * of contents, in pixels.
		    * 
		    * @var int
		    */
			contentMaxWidth:this.contentMaxWidth,
			
		   /**
		    * Flag used to check whether the HTML structure
		    * of the tooltip has been built or not.
		    * 
		    * @var bool
		    */
			built:false,
			
		   /**
		    * Initializes the tooltip by setting the relevant attributes
		    * in the button and link so that a click will actually display
		    * the tooltip.
		    */
			init:function() {
				// the button is not always shown, but if it is active
				// we want it to have the original link as target.
				if( this.button ) {
					this.button.writeAttribute( 'onclick', 'document.location=\''+this.source.href+'\'' );
				}
				
				this.source.writeAttribute( 'href', 'javascript:void(0);' );
				this.source.writeAttribute( 'title', 'Data sheet for the '+this.source.innerHTML );
				this.source.onclick = function() { tip.show(); };
			},
			
		   /**
		    * Shows the tooltip, and builds the HTML
		    * structure if that has not been done yet.
		    */
			show:function()
			{
				this.build();
				$('SQTContainer'+this.id).style.display = 'block';
				
				if( typeof( Draggable ) != 'undefined' ) {
					new Draggable( 'SQTContainer'+this.id, { handle:'SQTDragHandle'+this.id } );
				}
			},
			
		   /**
		    * Hides the tooltip. The HTML structure is kept intact
		    * so it does not have to be done again.
		    */
			hide:function()
			{
				$('SQTContainer'+this.id).style.display = 'none';
			},
			
		   /**
		    * Builds the DOM structure of the tooltip.
		    * 
		    * Note: This is only done once.
		    */
			build:function()
			{
				if( this.built ) {
					return true;
				}
				
				// the main tooltip element
				var el = new Element( 
					'div', 
					{ 
						'id':'SQTContainer'+this.id, 
						'class':'SQTFrame', 
						'style':'display:none' 
					} 
				);
				
				Element.insert( $$('body')[0], { 'after': el } );
				
				// the header
				var title = this.source.innerHTML+' :: '+this.title;
				Element.insert( 
					el, 
					new Element( 
						'div', 
						{ 
							'class':'SQTHeader', 
							'id':'SQTHeader'+this.id 
						} 
					).update( title ) 
				);
				
				// the content
				Element.insert( 
					el, 
					new Element( 
						'div', 
						{ 
							'class':'SQTContent', 
							'id':'SQTItem'+this.id 
						} 
					).update( 'Please wait...' ) 
				);
				
				// the drag handle
				if( typeof( Draggable ) != 'undefined' ) {
					Element.insert( 
						$('SQTHeader'+this.id), 
						new Element( 
							'img', 
							{ 
								'id':'SQTDragHandle'+this.id, 
								'class':'SQTDragHandle', 
								'src':this.imgFolder+'drag-handle.png' 
							} 
						) 
					);
				}

				// the close button
				Element.insert( 
					$('SQTHeader'+this.id), 
					new Element( 
						'img', 
						{ 
							'class':'SQTCloseButton', 
							'src':this.imgFolder+'close.png', 
							'onclick':"$('SQTContainer"+this.id+"').style.display = 'none';" 
						} 
					) 
				);
				
				// load the ship details
				new Ajax.Updater(
					$('SQTItem'+this.id), 
					this.scriptFolder+
						'ShipsQuickinfo.php?ship_name='+
							sourceElement.innerHTML+
								'&tab='+ShipsQuickinfo.defaultTab+
									'&ship_view='+ShipsQuickinfo.defaultShipView,
					{
						'evalScripts':true
					}
				);
				
				// position the whole thing
				posEl = this.source;
				if( this.button ) {
					posEl = this.button;
				}
				
				$('SQTItem'+this.id).style.width = this.contentMaxWidth+'px';
				
				var pos = posEl.positionedOffset();
				el.style.left = (pos[0]+posEl.getWidth()+this.offsetX)+'px';
				el.style.top = (pos[1]+posEl.getHeight()+this.offsetY)+'px';
				
				this.built = true;
			}
		};
		
		tip.init();
	},
	
   /**
    * Used by the preview window to switch ship views. 
    *
    * @param string popupID The ID of the popup
    * @param string viewID The view to activate
    * @param string peviewURL The URL of the preview image to show
    */
	chooseView:function( popupID, viewID, previewURL )
	{
		var views = [
		    {'id':'f','label':'front'},
		    {'id':'s','label':'side'},
		    {'id':'r','label':'rear'}
		];
		
		for( var i=0; i < views.length; i++ ) {
			if( views[i].id == viewID ) {
				var previewLabel = views[i].label;
			}
			$('SQTButton'+popupID+views[i].id).src = this.imgFolder+'/preview_'+views[i].label+'.png';
		}
		
		$('SQTButton'+popupID+viewID).src = this.imgFolder+'/preview_'+previewLabel+'_active.png';
		$('SQTPreview'+popupID).style.background='url('+previewURL+')';
	}
};

/**
 * Tabs management object for the tooltips. Handles switching
 * between the available tabs.
 *
 * @package ShipsQuickinfo
 * @subpackage Clientside Scripts
 * @author AeonOfTime
 * @license http://creativecommons.org/licenses/BSD/
 * @link http://aeonoftime.com/EVE_Online_Tools/Ships_Quickinfo/
 */
var ShipsQuickinfo_Tabs = 
{
   /**
    * Stores individual tab objects
    * 
    * @var array
    */
	'tabs':[],
	
   /**
    * Adds a tab to handle. 
    * 
    * @param string popupID
    * @para string tabID
    */	
	add:function( popupID, tabID ) {
	
	   /**
	    * Individual tab management object.
	    * 
	    * @package ShipsQuickinfo
	    * @subpackage Clientside Scripts
	    */
		var tab = 
		{
		   /**
		    * The ID of the tooltip this tab is placed in.
		    * 
		    * @var string
		    */
			'popupID':popupID,
		
		   /**
		    * The ID of the tab itself.
		    * 
		    * @var string
		    */
			'tabID':tabID,
			
		   /**
		    * Activates the tab by displaying its contents.
		    */
			activate:function() {
				var contentEl = $('TabContent'+this.popupID+'_'+this.tabID);
				if( contentEl ) {
					contentEl.style.display = 'block';
				} 
			},
			
		   /**
		    * Deactivates the tab by hiding its contents.
		    */
			deactivate:function() {
				var contentEl = $('TabContent'+this.popupID+'_'+this.tabID);
				if( contentEl ) {
					contentEl.style.display = 'none';
				}
			}
		};
		
		this.tabs.push( tab );
	},
	
   /**
    * Activates the specified tab in the specified
    * tooltip. Previously deactivates all tabs
    * and then activates this one.
    * 
    * @param string popupID
    * @param string tabID
    */
	activate:function( popupID, tabID ) {
		this.deactivateAll( popupID );
		var tab = this.get( popupID, tabID );
		if( tab ) {
			tab.activate();
		} else {
			alert( 'Tab not found: '+popupID+' '+tabID );
		}
	},
	
   /**
    * Deactivates all tabs in the specified tooltip.
    * 
    * @param string popupID
    */
	deactivateAll:function( popupID ) {
		for( var i=0; i < this.tabs.length; i++ ) {
			if( this.tabs[i].popupID == popupID ) {
				this.tabs[i].deactivate();
			}
		}
	},
	
   /**
    * Retrieves the specified tab in the specified 
    * tooltip. Returns false if it does not exist.
    * 
    * @param string popupID
    * @param string tabID
    */
	get:function( popupID, tabID ) {
		for( var i=0; i < this.tabs.length; i++ ) {
			if( this.tabs[i].popupID == popupID && this.tabs[i].tabID == tabID ) {
				return this.tabs[i];
			}
		}
		
		return false;
	}
};
