﻿// This is open source, based on GNU's General Public License (GPL).
// See http://www.gnu.org/copyleft for further details about the GNU GPL.




// Configurable valid site base addresses
var rootDir = "kulturscheune";
var sites = new Array()
{
  // domain name for website development
  sites[ 0 ] = "kulturscheune.localhost";
  // official domain names of website
  sites[ 1 ] = "www.die-simmelsdorfer-muehle.de";
  sites[ 2 ] = "www.die-simmelsdorfer-mühle.de";
  sites[ 3 ] = "die-simmelsdorfer-muehle.de";
  sites[ 4 ] = "die-simmelsdorfer-mühle.de";
  sites[ 5 ] = "www.kulturscheune.de";
  sites[ 6 ] = "www.kulturscheune-schnaittach.de";
  sites[ 7 ] = "kulturscheune.de";
  sites[ 8 ] = "kulturscheune-schnaittach.de";
}
var siteName = "Die Simmelsdorfer Mühle";
var startPage = "Startseite";

// UTF-8 character codes
var Ae = String.fromCharCode( 196 );
var ae = String.fromCharCode( 228 );
var Oe = String.fromCharCode( 214 );
var oe = String.fromCharCode( 246 );
var Ue = String.fromCharCode( 220 );
var ue = String.fromCharCode( 252 );
var sz = String.fromCharCode( 223 );
var at = "@";



// Breadcrumb Generator 1.4.0, Copyright (c) 2003-2010, Stefan Hofmann

// The breadcrumb generator inserts a breadcrumb path at the location in
// the document at which it is called. It further replaces the docment's
// default title by the breadcrumb path as well.

// The breadcrumb path of a document is derived from the menu hierarchy
// as encoded in the directory structure of the URL by which the document
// has been accessed. To produce a valid breadcrumb path, the following
// building principle needs to be followed for the whole Web site: If an
// item of a specific menu opens a subordinate menu, all documents of that
// subordinate menu must be located in a directory with the name of the
// parent menu item, using underscore characters instead of spaces. The
// main document that is openend by the parent menu must be contained in
// the file "index.htm". If a menu item does not have a subordinate menu,
// its related document must be in the same directory as the referring
// document "index.htm", but it can have an arbitrary name.

// If the script detects a directory starting with "cgi-" in the URL, it
// does not expand the breadcrumb path beyond this point. Instead, the
// script skips all further fragments and only displays the final one.

// The start of the breadcrumb path, i.e. the web site's start page, is
// always represented by a configurable, constant term. The names of all 
// intermediate breadcrumbs are derived from their respective directory
// names, replacing underscores by spaces, transcriptions for German
// umlauts/ligatures by their special characters, and capitalizing each 
// word. If a part within the breadcrumb shall be displayed with all 
// capital letters, that part must start with an exclamation mark. If a 
// part's capitalization shall be suppressed, that part needs to start 
// with an dollar sign. If a sequence of characters must not be interpreted 
// as transcription and thus not be replaced, these specific characters 
// must be separated by a comma, which will later be removed. For instance, 
// to avoid that a directory "aktuelles" is displayed as "Akt&uuml;les", 
// the directory needs to be named "aktu,elles", which results in the 
// intended breadcrumb "Aktuelles". A plus sign as separator is replaced 
// by a forward slash, which allows to have a list of words in the breadcrumb.
 
// If the trailing breadcrumb does not refer to a file "index.htm", the 
// breadcrumb is derived from the document's title. If the file should be 
// "index.htm", the breadcrumb is derived from the directory name as 
// described above.

// The script can be configured in various ways by variables located at
// the beginning of the script. Especially, the script can be told for
// which sites it shall display breadcrumbs and for which not. If the
// site is located on the local disk, the script can be configured at
// which point in the file:// or http:// URL the breadcrumb processing 
// should start. If a virtual file: URL is given, which could be inserted 
// by some post-processing tools, this virtual URL is used instead of the
// actual URL of the loaded page.

function CreateBreadcrumb( url, title )
{
  // configurable site-specific parameters
  var virtualTag = "file://" + rootDir + "<virtual-url>";
  var virtualUrl = "file://" + rootDir + "<virtual-url>";
  var indexPage = "index.htm";

  // script variables
  var hasVirtualUrl = ( virtualUrl != virtualTag );
  var fragments =  url.split( "/" );
  var crumbs = new Array();
  var links = new Array();
  var breadcrumbPath = "";
  var windowTitle = "";
  var uplink = "";
  var isCgi = false;
  var root = 0
  var i = 0;
  var k = 0;

  // set default URL if none supplied
  if( url == undefined )
  {
    url = rootDir + indexPage;
  }

  // find root of website within URL, i.e. skip base of URL
  if( fragments[ 0 ] == "file:" )
  {
    // document loaded from disk
    for( i = 1; fragments[ i ] != undefined && fragments[ i ] != rootDir; i++ );
    i++;
  }
  else if( fragments[ 0 ] == "http:" || fragments[ 0 ] == "https:" )
  {
    // document loaded from server
    for( n = 0; sites[ n ] != undefined; n++ )
    {
      if( fragments[ 2 ].substr( 0, sites[ n ].length ) == sites[ n ] )
      {
        i = 3;
        break;
      }
    }
    // also check for root of website, since website may be beneath server root
    for( n = i; fragments[ n ] != undefined; n++ )
    {
      if( fragments[ n ] == rootDir || fragments[ n ] == "~" + rootDir )
      {
        i = n + 1;
        break;
      }
    }
  }
  root = i;

  // build link to root directory
  for( i = root + 1; fragments[ i ] != undefined; i++ )
  {
    uplink = "../" + uplink;
  }

  // if specified, use virtual URL instead of actual URL
  if( hasVirtualUrl )
  {
    fragments = virtualUrl.split( "/" );
    root = 3;
  }

  // collect breadcrumbs from URL after site base address if not on root level
  if( root > 0 )
  {
    crumbs[ k = 0 ] = startPage;
    for( i = root; fragments[ i ] != undefined; i++ )
    {
      var crumb = fragments[ i ];
      var separator;
      var word;
      var p;

      crumbs[ ++k ] = fragments[ i ];

      // skip processing of directory's index page; will use page title instead
      if( crumb.substr( 0, indexPage.length ) != indexPage )
      {

        // replace transcripts of spaces and German umlauts
        // use comma to prevent erroneous transcript conversion
        crumb = crumb.replace( /_/g, " " );
        crumb = crumb.replace( /^ */, "" );
        crumb = crumb.replace( / *$/, "" );

        // capitalize words
        crumbs[ k ] = "";
        while( crumb != "" )
        {
          if( (p = crumb.search( /[-+ ]/ )) >= 0 )
          {
            word = crumb.substr( 0, p );
            separator = crumb.substr( p, 1 );
	        if( separator == "+" )
	        {
	          separator = "/";
	        }
          }
          else
          {
            p = crumb.length;
            word = crumb;
            separator = "";
          }
          crumb = crumb.substr( p + 1 );
          if( word.substr( 0, 1 ) == "!" )
          {
            crumbs[ k ] += word.substr( 1 ).toUpperCase() + separator;
          }
          else if( word.substr( 0, 1 ) == "$" )
          {
          	crumbs[ k ] += word.substr( 1 ) + separator;
          }
          else
          {
            crumbs[ k ] += word.substr( 0, 1 ).toUpperCase() + word.substr( 1 ) + separator;
          }
        }
        crumbs[ k ]= crumbs[ k ].replace( /sz/g, "&szlig;" );
        crumbs[ k ]= crumbs[ k ].replace( /ae/g, "&auml;" );
        crumbs[ k ]= crumbs[ k ].replace( /oe/g, "&ouml;" );
        crumbs[ k ]= crumbs[ k ].replace( /ue/g, "&uuml;" );
        crumbs[ k ]= crumbs[ k ].replace( /ae/gi, "&Auml;" );
        crumbs[ k ]= crumbs[ k ].replace( /oe/gi, "&Ouml;" );
        crumbs[ k ]= crumbs[ k ].replace( /ue/gi, "&Uuml;" );
        crumbs[ k ]= crumbs[ k ].replace( /,/g, "" );
      }
    }

    // do not prefix start page, use original title
    crumbs[ k ] = title;

    // determine last uplink
    if( fragments[ fragments.length - 1 ].substr( 0, 9 ) == indexPage
    ||  fragments[ fragments.length - 1 ] == "" )
    {
      k--;
    }

    // build links for breadcrumb hierarchy
    for( i = 0; i < k; i++ )
    {
      links[ i ] = uplink + indexPage;
      uplink = uplink + fragments[ root++ ] + "/";
    }

    // create breadcrumb path for document and window title
    breadcrumbPath = "";
    for( i = 0; i < k; i++ )
    {
      if( crumbs[ i ].search( /^cgi-/i ) >= 0 )
      {
        isCgi = true;
      }
      else if( ! isCgi )
      {
        breadcrumbPath += "<span class=\"breadcrumb-path\"><a href=\"" + links[ i ] + "\">" + crumbs[ i ] + "</a></span>";
        breadcrumbPath += (( i > 0 )? "</nobr> " : " ");
        breadcrumbPath += "<nobr><span class=\"breadcrumb-separator\">&gt;</span> ";
        windowTitle += crumbs[ i ] + " > ";
      }
    }
    breadcrumbPath += "<span class=\"breadcrumb-current\">" + crumbs[ i ] + "</span></nobr>";
    windowTitle += crumbs[ i ];
    
    // write breadcrumb path do document and update window title
    windowTitle = windowTitle.replace( /&szlig;/g, "\xDF" );
    windowTitle = windowTitle.replace( /&auml;/g, "\xE4" );
    windowTitle = windowTitle.replace( /&ouml;/g, "\xF6" );
    windowTitle = windowTitle.replace( /&uuml;/g, "\xFC" );
    windowTitle = windowTitle.replace( /&Auml;/g, "\xC4" );
    windowTitle = windowTitle.replace( /&Ouml;/g, "\xD6" );
    windowTitle = windowTitle.replace( /&Uuml;/g, "\xDC" );
    document.title = windowTitle.replace( RegExp( "^" + startPage ), siteName );
    document.write( breadcrumbPath );
  }
}



// Domain Extractor 1.0.1, Copyright (c) 2010, Stefan Hofmann

// The domain extractor matches the main contained in the supplied URL against
// the list of domain names defined for the website. If the URL is undefined or
// the domain contained in the supplied URL does not match one of the defined 
// sites, the domain name at index 1 will be returned, since index 0 is reserved
// for the domain name of the local development environment.

function GetDomain( url )
{
  if( url )
  {
    url = url.toLowerCase() + "/";
    url = url.replace( RegExp( "^http[^:]*://([^:/]+)[:/].*$" ), "$1" );
    url = url.replace( "www.", "" );
    for( var i = 1; i < sites.length; i++ )
    {
      if( url == sites[ i ] )
      {
        return( sites[ i ] );
      }
    }
  }
  return( sites[ 1 ].replace( "www.", "" ) );
}



// Mail Link Generator 1.2.1, Copyright (c) 2003-2011, Stefan Hofmann

// The mail link generator derives a mailto link from the recipient's name,
// the domain used in the document's URL, the subject and a body template. 
// Underscore characters contained in the body template are converted to 
// CR/LF sequences.

function OpenMailForm( recipient, subject, body )
{
  var domain = GetDomain( document.location.toString() );
  var address = recipient + '\x40' + domain;
  var url = "mailto:" + address;
  var text = "";
  
  if( subject != undefined )
  {
    url = url + "?subject=" + subject;
  }
  if( body != undefined )
  {
    text = body;
    while( text.search( "_" ) != -1 )
    {
      text = text.replace( "_", "%0D%0A" );
    }
    while( text.search( "=" ) != -1 )
    {
      text = text.replace( "=", "%3E" );
    }
    url = url + "&body=" + text;
  }
  parent.location.href = url;
}



// String Descrambler 1.0.0, Copyright (c) 2009, Stefan Hofmann

// The string descrambler retrieves a clear-text string from its scrambled
// represeentation. It is well known that this does not securely protect
// a string's confidentiality. But supposing that automated data harvesters
// won't expand dynamic content, this should give sufficient protection.

function DescrambleString( scramble, seed )
{
	if( scramble && seed )
	{
		var text = "";
		while( scramble.length > 1 )
		{
			var ascii = 0x00;
			var hex = scramble.substring( 0, 2 ).toUpperCase();
			while( hex.length > 0 )
			{
				var nibble = "0123456789ABCDEF".indexOf( hex.substring( 0, 1 ) );
				if( nibble < 0 )
				{
					return( undefined );
				}
				ascii = (ascii << 4) + nibble;
				hex = hex.substring( 1 );
			}
			ascii ^= seed & 0xFF;
			ascii = ((ascii << 4) & 0xF0) + ((ascii >> 4) & 0x0F);
			text += String.fromCharCode( ascii );
			scramble = scramble.substring( 2 );
			seed = ((seed << 1) + (seed >> 7)) & 0xFF;
		}
		if( scramble.length == 0 )
		{
			return( text );
		}
	}
	return( undefined );
}

