/**
 * PAGE.JS
 *
 * Mouse handling functions to show article information if mouse is moved over the page image.
 *
 * Author         Dave Snyckers [X-Cago BV]
 * Created        2006-05-17
 */


// global variables
// these variables need to be global to be accessed from the mouse move event handling functions
var infolayer_obj     = false;
var infolayer_active  = false;
var pageimage_id      = "";

/**
 * ArticleBlocks constructor.
 *
 * @param headers       the article headers
 * @param abstracts     the article abstracts
 * @param infolayer_id  the element id of the info layer
 * @param pimage_id     the element id of the page image
 */
function ArticleBlocks( headers, abstracts, infolayer_id, pimage_id )
{
  this.headers          = headers;
  this.abstracts        = abstracts;
  this.infolayer_id     = infolayer_id;

  pageimage_id          = pimage_id;
}

// initialize the ArticleBlocks instance
ArticleBlocks.prototype.init = init;

// show a single article (this includes all blocks associated with that article)
ArticleBlocks.prototype.show = showArticle;

// hide a single article (this includes all blocks associated with that article)
ArticleBlocks.prototype.hide = hideArticle;





/**
 * Initializes the ArticleBlocks instance.
 */
function init()
{
	ie  = document.all;
	ns4 = document.layers;
	dom = document.getElementById;
	ie4 = document.all && !dom;
	ie5 = ( dom && navigator.userAgent.indexOf( 'MSIE 5' ) > 0 );
	ope = ( dom && navigator.userAgent.indexOf( 'Opera' ) > 0 );
	moz = ( dom && navigator.userAgent.indexOf( 'Gecko' ) > 0 );
	bok = ns4 || ie4 || dom;

	if( bok )
	{
		document.onmousemove = mouseMove;
		if( ns4 )
			document.captureEvents( Event.MOUSEMOVE );
	}
}

/**
 * Show a single article and all of its associated blocks.
 */
function showArticle( article_id )
{
  // first time > retrieve info layer object by its reference id
  if( !infolayer_obj )
    infolayer_obj = document.getElementById( this.infolayer_id )


	if( ( this.headers[article_id] != "" ) || ( this.abstracts[article_id] != "" ) )
	{
	  rowHdr = document.getElementById( "rowHeader" );
	  rowAbs = document.getElementById( "rowAbstract" );

		// set article header
		if( this.headers[article_id] != "" )
		{
		  dataHdr = document.getElementById( "dataHeader" );

		  dataHdr.innerHTML = this.headers[article_id];
		  rowHdr.style.display = "";
		}
	  else
		  rowHdr.style.display = "none";

    // set article abstract
		if( rowAbs )
		{
      if(  this.abstracts[article_id] != "" )
      {
        dataAbs = document.getElementById( "dataAbstract" );

        dataAbs.innerHTML = this.abstracts[article_id];
        rowAbs.style.display = "";
      }
      else
        rowAbs.style.display = "none";
    }

    // show article info layer
		move( lastx, lasty );
		MM_showHideLayers( this.infolayer_id, "", "show" );
		infolayer_active = true;

		// show article blocks
    showArticleBlocks( "b" + article_id );
	}
}


/**
 * Hide a single article and all of its associated blocks.
 */
function hideArticle( article_id )
{
	// hide article info layer
	MM_showHideLayers( this.infolayer_id, "", "hide" );
  infolayer_active = false;

  // hide article blocks
  hideArticleBlocks( "b" + article_id );
}


/**
 * Mouse move event handling.
 */
function mouseMove( e )
{
  // get the absolute mouse position coordinates
  if ( ie )
  {
    posX = event.clientX + document.body.scrollLeft;
    posY = event.clientY + document.body.scrollTop;
  }
  else
  {
    posX = e.clientX + document.body.scrollLeft;
    posY = e.clientY + document.body.scrollTop;
  }

	lastx = posX;
	lasty = posY;

	if ( infolayer_active )
  	move( posX, posY );

	return true;
}

/**
 * Move info layer to specified coordinates.
 * Calculates if info layer is within page image bounds. Otherwise, info layer position is updated.
 */
function move( posX, posY )
{
	var offsetX = 10;
	var offsetY = 20;

  newX = posX + offsetX;
  newY = posY + offsetY;

	var pgImage = document.getElementById( this.pageimage_id );
	if( pgImage )
	{
    var minposX = 10;
    var maxposX = pgImage.width + 10;
    var minposY = 10;
    var maxposY = pgImage.height + 10;

    if ( bok )
    {
      newX = posX + offsetX;
      if ( newX < minposX )
        newX = minposX;
      if ( newX - 30 > maxposX )
        newX = maxposX + 30;
      newY = posY + offsetY;
      if ( newY < minposY )
        newY = minposY;
      if ( newY + getInfoLayerHeight() + 10 > maxposY )
        newY = ( posY - offsetY ) - getInfoLayerHeight();
    }
  }

  if( this.infolayer_obj )
    moveObjectTo( this.infolayer_obj, newX, newY );
}

/**
 * Move an object to the specified x, y position.
 */
function moveObjectTo( obj, x, y )
{
	if ( !(obj.style) )
	{
		obj.left = x;
		obj.top = y;
	}
	else
	{
		obj.style.left = x;
		obj.style.top = y;
	}
}

/**
 * Get the height of the info layer.
 */
function getInfoLayerHeight()
{
	if( ns4 )
		return infolayer_obj.clip.bottom;
	else
		return infolayer_obj.offsetHeight;
}



/**
 * Show layer containing the article overlay blocks.
 */
function showArticleBlocks( layer_id )
{
	MM_showHideLayers( layer_id, "", "show" );
}

/**
 * Hide layer containing the article overlay blocks.
 */
function hideArticleBlocks( layer_id )
{
	MM_showHideLayers( layer_id, "", "hide" );
}




// utility methods ////////////////////////////////////////////////////////////////////////////////

function MM_findObj( n, d )
{ //v4.0
	var p, i, x;

	if( !d )
		d=document;

	if( ( p=n.indexOf("?") ) > 0 && parent.frames.length )
	{
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}

	if( !( x=d[n] ) && d.all )
		x=d.all[n];
	for ( i=0; !x && i < d.forms.length; i++ )
		x=d.forms[i][n];
	for( i=0; !x && d.layers && i < d.layers.length; i++ )
		x=MM_findObj(n,d.layers[i].document);
	if( !x && document.getElementById )
		x=document.getElementById(n);
	return x;
}

function MM_showHideLayers()
{ //v3.0
	var i, p, v, obj, args=MM_showHideLayers.arguments;

	for ( i=0; i < (args.length-2); i+=3 )
		if ( ( obj=MM_findObj(args[i]) ) != null )
		{
			v=args[i+2];
			if ( obj.style )
			{
				obj=obj.style;
				v = (v=='show')?'visible':(v=='hide')?'hidden':v;
			}
			obj.visibility=v;
		}
}


