/*------------------------------------------------------------------------------
linkPanel Class Definition
version: 1.0.0
date:      01/31/09
author:    stephen.craggs@essential-lighting.co.uk
email:     [owner's email]
website:   [owner's domain]
version history: [location of file]

Copyright (c) 2009, [Your Company]. All rights reserved.
Code licensed under the [license] License:
[url to license]
------------------------------------------------------------------------------*/


/*******************************************************************************
 * linkPanel creates a small, unobtrusive widget on a Web page that contains a
 * list of links. Unlike a menu, linkPanel is a quick access link list that
 * doesn't interfere with the rest of your document.
 *
 * Object Interface:
 *		Object: linkPanel( elementID )
 *			elementID			A Unique name for the panel (required)
 *								Note: Any panel without a name will not be
 *								displayed. Panels with the same name will
 *								not display correctly.
 *
 *		Methods:
 *			setTitle( label, icon )
 *			This method sets label and associated icon in the title pane
 *				label			Text that appears in the title pane
 *				icon			Path to the icon associated with the label
 *
 *			addLink( label, url, icon )
 *			This method appends a new link to the end of the link list
 *				label			Text that appears for the link
 *				url				The link destination address
 *				icon			Path to the icon associated with the label
 *
 *			open()
 *			This method opens the panel
 *
 *			close()
 *			This method closes the panel
 *
 *			moveTo( top, left )
 *			This method moves the panel to a sepecified position
 *				top				position of the panel from the top of
 *								the browser window
 *				left			position of the panel from the left side of
 *								the browser window
 ******************************************************************************/
 
/*******************************************************************************
						        UTILITY FUNCTION
*******************************************************************************/

/* Add event handler
------------------------------------------------------------------------------*/
function linkPanelAddEvent( obj, evType, fn, cap ) {
	if (obj.addEventListener)
		obj.addEventListener(evType, fn, cap);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evType, fn);
	else
		obj['on'+evType] = fn;
}

/* Get path to linkPanel
------------------------------------------------------------------------------*/
function getLinkPanelScriptPath() {
	// determine the path to the linkPanel folder
	// by evaluating the script tag that loads this library
	var path = null;
	var scripts = document.getElementsByTagName('script');
	for(var i=0; i<scripts.length; i++) {
		if (scripts[i].src.search('linkpanel.js') > -1) {
			path = scripts[i].src.substr(0, scripts[i].src.lastIndexOf('/')+1);
		}
	}
	return path;
}

/*----------------------------------------------------------------------------*/

var linkPanelScriptPath = getLinkPanelScriptPath();

/*******************************************************************************
                           LINKPANEL CLASS DEFINITION
*******************************************************************************/

/* Define the linkPanel Class
------------------------------------------------------------------------------*/
function linkPanel( elementID, target ) {
	//if (!elementID) throw 'elementID must be defined';
	if (!elementID) return;
	
	/* Public Member Variables
	--------------------------------------------------------------------------*/
	var self = this;		// overcome scoping issues
	self.panel = null;		// shortcut to DOM element container
	self.pageTarget = target;
	
	
	
	/* Private Member Variables
	--------------------------------------------------------------------------*/
	var _defaultState = 'lp-close';
	var _defaultTitle = 'Quick Links';
	var _elementID = 'linkPanelMain';
	
	// auto activate the object
	activate();
	
	
	
	/* Private Methods
	--------------------------------------------------------------------------*/
	function activate() {
		// initialize all required elements
		if ( document.getElementById && document.getElementsByTagName ) {
			// perform basic setup operations
			_loadStyleSheet();
			_buildHTML();
			
			// initialize the panels dom element
			self.panel = document.getElementById( elementID );
			
			// register mouse events
			linkPanelAddEvent(self.panel, 'mouseover', function() {self.open() }, false);
			linkPanelAddEvent(self.panel, 'mouseout',  function() {self.close()}, false);
		}
	}
	
	/*------------------------------------------------------------------------*/
	
	function _loadStyleSheet() {
		// check for the presence of linkPanel's stylesheet
		var stylesheet = false;
		var headID = document.getElementsByTagName("head")[0];
		var styles = headID.getElementsByTagName('link');
		for (i=0; i<styles.length; i++) {
			if (styles[i].href) {
				var stylesheetname = styles[i].href.replace(linkPanelScriptPath, '');
				stylesheet = stylesheetname == 'linkpanel.css';
			}
		}
		
		// load a stylesheet located in the same folder as this script
		if (!stylesheet) {
			var cssNode = document.createElement('link');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.href = linkPanelScriptPath + 'linkpanel.css';
			cssNode.media = 'screen,projection';
			headID.appendChild(cssNode);
		}
	}
	
	/*------------------------------------------------------------------------*/
	
	function _buildHTML() {
		/* Create the required HTML and append it to the body */
		var root, div, p, text, list, listitem, span, anc;
		
		root = document.getElementById( elementID );
		root.innerHTML = '&nbsp;'	// give the span some width
		
		divMain = document.createElement('div');
		divMain.id = _elementID;
		divMain.className = _defaultState;
		root.appendChild(divMain);
		
		// Create title pane element
		div  = document.createElement('div');
		p    = document.createElement('p');
		text = document.createTextNode( _defaultTitle );
		p.appendChild(text);
		div.appendChild(p);
		divMain.appendChild(div);
		
		// Create navigation list element
		list = document.createElement('ul');
		list.id = 'lp-navlist';
		divMain.appendChild(list);
		
		// Create bottom border element
		span = document.createElement('span');
		divMain.appendChild(span);
	}
	
}

/* Define linkPanel prototype methods
------------------------------------------------------------------------------*/
linkPanel.prototype.setTitle = function( label, icon ) {
	if (this.panel) {
		var p = this.panel.getElementsByTagName('p')[0];
		text = document.createTextNode( label );
		p.replaceChild(text, p.firstChild);
		p.style.backgroundImage = 'url(' + icon + ')';
	}
}

/*----------------------------------------------------------------------------*/

linkPanel.prototype.addLink = function( label, url, icon ) {
	if (this.panel) {
		var list = this.panel.getElementsByTagName('ul')[0];
		var listitem = document.createElement('li');
		var anc      = document.createElement('a');
		anc.appendChild(document.createTextNode( label ));
		anc.setAttribute( 'href', url );
		if (this.pageTarget) anc.setAttribute( 'target', this.pageTarget );
		listitem.style.backgroundImage = 'url(' + icon + ')';
		listitem.appendChild(anc);
		list.appendChild(listitem);
	}
}

/*----------------------------------------------------------------------------*/

linkPanel.prototype.open = function() {
	if (this.panel) {
		var div = this.panel.getElementsByTagName('div')[0];
		div.className = 'lp-open';
	}
}

/*----------------------------------------------------------------------------*/

linkPanel.prototype.close = function() {
	if (this.panel) {
		var div = this.panel.getElementsByTagName('div')[0];
		div.className = 'lp-close';
	}
}

/*----------------------------------------------------------------------------*/

linkPanel.prototype.moveTo = function(top, left) {
	if (this.panel) {
		with (this.panel) {
			style.position = 'absolute';
			style.display = 'block';
			style.top = top+'px';
			style.left = left+'px';
		}
	}
}

/*----------------------------------------------------------------------------*/
/******************************************************************************/