/**
 * VIEWTYPE.JS
 *
 * Handles the viewing type settings for articles.
 * Available viewing types for the article are:
 *  - text        simple text representation
 *  - original    image representation (DEFAULT)
 *  - pdf         pdf representation
 *
 * To use this script you need to add a SELECT field with
 * 3 OPTION elements that contain the above values as there
 * 'value' attribute.
 * The 'onChange' event should call the 'selectViewType' method.
 * Furthermore, the 'viewtypeID' variable should be set to match
 * the SELECT ID attribute.
 *
 * The selected article representation is stored in a cookie with
 * name 'selection'.
 *
 * Dependent javascript libraries:
 *  - cookies.js
 *
 * Author         Dave Snyckers [X-Cago BV]
 * Version        1.2
 */

// cookie name containing the viewtype selection
var cookiename = "viewtype";

// set the default representation
var selectedoption = 'original';

// cookie expiry date
var expdate = new Date();

// debug mode
var debugMode = false;

/**
 * General debug message.
 *
 * param  message   the debug message to display
 */
function debug( message )
{
  if( debugMode )
    alert( message );
}

/**
 * Initialize the viewtype based on the cookie value.
 */
function initViewType()
{
  debug( "initViewType()" );
	FixCookieDate( expdate );
	expdate.setTime( expdate.getTime() + (1000 * 24 * 60 * 60 * 1000) );

	if( GetCookie( "selection" ) == null )
		setViewType( selectedoption );
	else
		setViewType( GetCookie( "selection" ) );
}

/**
 * Set the selected viewtype in the cookie and select
 * the viewtype in the combobox.
 */
function setViewType( selectedType )
{
  debug( "setViewType( " + selectedType + " )" );
  SetCookie( "selection", selectedType, expdate, "/" );
  highlightViewType( selectedType );
}

/**
 * Viewtype combobox handling.
 */
function selectViewType()
{
  debug( "selectViewType()" );
  var selectObject = document.getElementById( viewtypeID );
  if( selectObject )
  {
    var index = selectObject.selectedIndex;
    if( index >= 0 )
    {
      selectedOption = selectObject.options[index];
      setViewType( selectedOption.value );
    }
  }
}

/**
 * Highlights the article viewing type (text, original or pdf).
 *
 * param  viewType    the article viewing type
 */
function highlightViewType( viewType )
{
  debug( "highlightViewType( " + viewType + " )" );
  if( viewtypeID )
  {
    var selectObject = document.getElementById( viewtypeID );
    if( selectObject )
    {
      var viewTypes = selectObject.options;
      for( i=0; i<viewTypes.length; i++ )
      {
        if( viewTypes[i].value == viewType )
        {
          selectObject.selectedIndex = i;
          return;
        }
      }
    }
    else
      alert( "viewtype object not found!" );
  }
  else
    alert( "'viewtypeID' not set in INDEX!" );
}