/* COOKIES */

function SetCookie( c_name, value, expires )
{
  var expire_date = new Date();
  expire_date.setTime( expire_date.getTime() + ( expires * 24 * 60 * 60 * 1000 ) );
  document.cookie = c_name + "=" + escape( value ) + ";path=/;expires=" + expire_date.toGMTString();
}

function GetCookie( c_name, default_value )
{
  if ( document.cookie.length > 0 )
  {
    c_start = document.cookie.indexOf( c_name + "=" );
    if ( c_start != -1 )
    {
      c_start = c_start + c_name.length + 1;
      c_end = document.cookie.indexOf( ";", c_start );
      if ( c_end == -1 )
        c_end = document.cookie.length;
      return unescape( document.cookie.substring( c_start, c_end ) );
    }
  }
  if ( default_value )
    return default_value;
  return "";
}

/* RELATIVE PATHS */

var base_path;

function InitRelativePaths( script_name, script_to_root )
{
  var scripts = document.getElementsByTagName( "script" );
  var script_relative;
  for ( var i = 0; i < scripts.length; i++ )
  {
    script_relative = scripts[ i ].getAttribute( "src" );
    if ( script_relative.length >= script_name.length && script_relative.substr( script_relative.length - script_name.length, script_relative.length ) == script_name )
      break;
  }
  if ( i == scripts.length )
    base_path = "";
  else if ( script_relative.length == script_name.length )
    base_path = ".";
  else
    base_path = script_relative.substring( 0, script_relative.length - script_name.length ) + script_to_root;
}

/* absolute_path should start with a '/' */
function GetRelativePath( absolute_path )
{
  return base_path + absolute_path;
}

/* TOGGLE DOCENT CONTENT */

function InitShowHideDocent()
{
  ShowId( "showdocent" );
}

function ShowDocent()
{
  HideId( "showdocent" );
  ShowId( "hidedocent" );
  ShowId( "docent" );
}

function HideDocent()
{
  ShowId( "showdocent" );
  HideId( "hidedocent" );
  HideId( "docent" );
}

function HideId( id )
{
  element = document.getElementById( id );
  if ( element )
    element.style.display = "none";
}

function ShowId( id )
{
  element = document.getElementById( id );
  if ( element )
    element.style.display = "block";
}

/* EXTERNAL LINKS */

function InitExternalLinks()
{
  var anchors = document.getElementsByTagName( "a" );
  for ( var i = 0; i < anchors.length; i++ )
  {
    var anchor = anchors[ i ];
    if ( anchor.getAttribute( "href" ) && anchor.getAttribute( "rel" ) == "external" )
      anchor.target = "_blank";
  }
}

/* STYLE */

var styles;

function InitStyle()
{
  styles = DefineStyles();
  SetStyle( GetCookie( "style" ) );
}

function SetStyle( style_name )
{
  for ( var i = 0; i < styles.length; i++ )
    if ( styles[ i ][ 0 ] == style_name )
      break;
  if ( i == styles.length )
    i = 0;
  SetCookie( "style", styles[ i ][ 0 ], 356 );

  css = {
    id_all : document.getElementById( "all" ).style,
    id_content : document.getElementById( "content" ).style,
    id_header : document.getElementById( "header" ).style,
    id_footer : document.getElementById( "footer" ).style }
  styles[ i ][ 1 ]( css );
}

/* STYLE OVERVIEW */

function InitStyleOverview()
{
  var element = document.getElementById( 'style_overview' );
  if ( !element )
    return;
  for ( var i = 0; i < styles.length; i++ )
    element.innerHTML = element.innerHTML + '<p><img class="clickable" src="images/' + styles[ i ][ 0 ] + '_preview.jpg" alt="' + styles[ i ][ 0 ] + '" onclick="SetStyle(\'' + styles[ i ][ 0 ] + '\')"/></p>';
}

/* INIT */

function Init()
{
  if ( !document.getElementsByTagName || !document.getElementById )
    return;

  InitRelativePaths( "general.js", ".." );
  InitShowHideDocent();
  InitExternalLinks();
}

window.onload = Init;
