// revision: Michael Krips, Pametan IT, 201105

var textcache = "textcache";
var browser = navigator.userAgent.toLowerCase ();

// directories addressed by document URL
var absURLDir;      // last directory of document's URL (document.location.href)
var hostURL;        // host name part of document's URL
var localURLDir;    // local part of directory path addressed by URL
var menuLocalDir;   // local part of directory path to menu base (== menu set key!)
var menuAbsDir;     // absolute directory path to menu base addressed by URL

// preload images
var preloadCounter;
var preloadImages;
var preloadImages_rot;

// menu definitions
var mainMenu = [
   // menu text,              page path relative to hostURL (== local root)
   [ 'über die bücher',      'buecher/index.html'     ],
   [ 'über den autor',       'autor/index.html'       ],
   [ 'über die inspiration', 'inspiration/index.html' ],
   [ 'kontakt',              'kontakt/index.html'     ]
];

var subMenu = { // menuLocalDir : [ itemKey1, ..., itemKeyN ]
   'buecher'     : [ 'allBooks','newBooks','fictnBooks','chldBooks','poemBooks','audioBooks' ],
   'autor'       : [ 'faqAutor','itrvwAutor','bgrphyAutor','awardsAutor','linksAutor' ],
   'inspiration' : [ 'inspBooks','inspMusic','inspFilm', 'inspArchive' ],
   'kontakt'     : [ 'contact','cntctAgency','cntctPhoto','cntctImprint' ]
};
var menuItem = { // itemKey : [ menu text, page path relative to hostURL (== local root)]
   // menuSet buecher
   'allBooks'    : [ 'alle bücher',          'buecher/index.html'              ],
   'newBooks'    : [ 'neu',                  'buecher/neu/index.html'          ],
   'fictnBooks'  : [ 'belletristik',         'buecher/belletristik/index.html' ],
   'chldBooks'   : [ 'kinder-/jugendbücher', 'buecher/kinderjugend/index.html' ],
   'poemBooks'   : [ 'gedichte',             'buecher/gedichte/index.html'     ],
   'audioBooks'  : [ 'hörbücher',            'buecher/hoerbuecher/index.html'  ],
   // menuSet autor
   'faqAutor'    : [ 'fragen & antworten',   'autor/index.html'                ],
   'itrvwAutor'  : [ 'interviews',  'autor/interviews/index.html'              ],
   'bgrphyAutor' : [ 'biographie',           'autor/biographie/index.html'     ],
   'awardsAutor' : [ 'preise & stipendien',  'autor/preise/index.html'         ],
   'linksAutor'  : [ 'links',                'autor/links/index.html'          ],
   // menuSet inspiration
   'inspBooks'   : [ 'bücher',               'inspiration/index.html#buecher'  ],
   'inspMusic'   : [ 'musik',                'inspiration/index.html#musik'    ],
   'inspFilm'    : [ 'filme',                'inspiration/index.html#filme'    ],
   'inspArchive' : [ 'archiv',               'inspiration/archiv/index.html'   ],
   // menuSet kontakt
   'contact'     : [ 'kontakt',              'kontakt/index.html'              ],
   'cntctAgency' : [ 'agentur',              'kontakt/agency/index.html'       ],
   'cntctPhoto'  : [ 'photos',               'kontakt/photos/index.html'        ],
   'cntctImprint': [ 'impressum',            'kontakt/impressum/index.html'    ]
};
var subMenuSpacer = ' &nbsp;:&nbsp; ';

function checkit()
{
   if ( preloadCounter > 7 ) // wenn mit laden fertig
   {
      // content wird eingeblendet - loading.gif abgeschalten
      document.getElementById("site").style.visibility = "visible";
      document.body.style.backgroundImage = "url('')";
   }
   else
   {
      for ( i=0; i<mainMenu.length; i++ )
      {
         if ( preloadImages[i].complete     == true ) preloadCounter++;
         if ( preloadImages_rot[i].complete == true ) preloadCounter++;
      }
      var timerID = setTimeout ( "checkit()", 5 )
	}
}

function control(img)
{
   if ( (foto1.width!=0) && (foto1.height!=0) ) viewFoto ( foto1.src ); 
   else
   {
      var function1 = "control('"+img+"')";
      var interval = setTimeout ( function1, 20 ); 
   }
} 

function Foto(tnsource)
{
   var schneidemark1 = tnsource.search ( "_tn.jpg" );
   var schneidemark2 = tnsource.search ( menuLocalDir );
   var img = hostURL + menuLocalDir + "/" + tnsource.slice(schneidemark2+menuLocalDir.length+1, schneidemark1) + '.jpg';
   foto1 = new Image (); 
   foto1.src = (img);
   control ( foto1.src ); 
} 

function init()
{
   var idx = rSearch ( document.location.href, '/' );
   if ( idx > 0 ) absURLDir = document.location.href.slice ( 0, idx );
   else           absURLDir = document.location.href;
   if ( absURLDir.charAt(absURLDir.length-1) != '/' ) absURLDir += '/';

   idx = absURLDir.search ('drvenkar.de') + 12;
   if ( absURLDir.length>idx+1 && absURLDir.substr(idx,4) == 'test' )
   {
      for ( idx+=4; absURLDir.charAt(idx) != '/'; idx++ ) ;
      idx++;
   }

   hostURL      = absURLDir.substr ( 0, idx );
   localURLDir  = absURLDir.substr ( idx );
   menuLocalDir = localURLDir.substr ( 0, rSearch(localURLDir,'/') );
   
   if ( menuLocalDir.length )
   {
      // If menuLocalDir is not empty, there could be an assosiated submenu.
      // Get the longest matching path by cutting off the tail and try.
      while ( typeof subMenu[menuLocalDir] == 'undefined' )
      {
         if ( (idx=rSearch(menuLocalDir,'/')) < 0 )
         {  // there is no matching submenu
            menuLocalDir = '';
            break;
         }
         menuLocalDir = menuLocalDir.substr ( 0, idx );
      }
   }

   menuAbsDir = hostURL + ((menuLocalDir.length>0)?menuLocalDir+'/':'');
}

function leseprobe(myurl){
	var endurl = hostURL + menuLocalDir + "/_weiterlesen/" + myurl;
	var fenster = window.open ( endurl, "Leseprobe", "width=600,height=465,left=50,top=50,resizeable=yes,scrollbars=yes"); 
} 

function leseprobe_hb(myurl)
{
   var endurl  = hostURL + menuLocalDir + "/hoerbuecher/_weiterlesen/" + myurl;
   var fenster = window.open ( endurl, "Leseprobe", "width=600,height=465,left=50,top=50,resizeable=yes,scrollbars=yes" );
} 

// Funktion bei Mouseover von NavBild
function nav_in(src,imghandle)
{
   var focusedTargetDir = imghandle.id.substr ( 3 ); // id=nav....
   for ( i=0; i<mainMenu.length; i++ )
   {
      var menuDef  = mainMenu[i];
      var menuTxt  = menuDef[0];
      var menuURL  = hostURL + menuDef[1];
      var menuDir  = menuDef[1].substr ( 0, rSearch(menuDef[1],'/') );
      var imgID    = 'nav' + menuDir;
      var menuImg  = menuAbsDir + imgID + '.jpg';
      var menuImgR = menuAbsDir + imgID + '_rot.gif';
      if ( focusedTargetDir == menuDir )
      {  // highlight focused item
         document.getElementById('mainnav').innerHTML = menuTxt; 
//         imghandle.src = menuImgR;
document.images[imgID].src = menuImgR;
//         document.getElementById(imgID).src = 'url("'+menuImgR+'")';
//         document.getElementById("navgraph"+i).innerHTML =
//            '<a href="' + menuURL + '"><img src="' + menuImgR + '" id="' + imgID +
//            '" onMouseOver="nav_in(this.src, this);" onMouseOut="nav_out(this.src, this);" /></a>';
      }
      else if ( menuDir == menuLocalDir ) 
      {  // if not in top level page: reactivate the statically highlighted item
         document.getElementById('navgraph'+i).innerHTML =
            '<img src="' + menuImg + '" id="'+imgID+'" />';
      }
   }
}

// Funktion bei Mouseout von NavBild
function nav_out(src,imghandle)
{
   var focusedTargetDir = imghandle.id.substr ( 3 ); // id=nav....
   for ( i=0; i<mainMenu.length; i++ )
   {
      var menuDef  = mainMenu[i];
      var menuTxt  = menuDef[0];
      var menuURL  = hostURL + menuDef[1];
      var menuDir  = menuDef[1].substr ( 0, rSearch(menuDef[1],'/') );
      var imgID    = 'nav' + menuDir;
      var menuImg  = menuAbsDir + imgID + '.jpg';
      var menuImgR = menuAbsDir + imgID + '_rot.gif';
      if ( focusedTargetDir == menuDir )
      {  // reactivate (de)focused item
//         imghandle.src = menuImg;
         document.getElementById(imgID).src = 'url("'+menuImg+'")';
//         document.getElementById("navgraph"+i).innerHTML =
//            '<a href="' + menuURL + '"><img src="' + menuImg + '" id="' + imgID +
//            '" onMouseOver="nav_in(this.src, this);" onMouseOut="nav_out(this.src, this);" /></a>';
//alert ( 'innerHTML: '+document.getElementById('navgraph'+i).innerHTML );
document.images[imgID].src = menuImg;
//            document.getElementById("navgraph"+i).innerHTML =
//               '<a href="' + menuURL + '"><img src="' + menuImg + '" id="' + imgID + '" name="' + imgID +
//               '" onMouseOver="nav_in(this.src, this);" onMouseOut="nav_out(this.src, this);" /></a>';
      }
      else if ( menuDir == menuLocalDir ) 
      {  // if not in top level page: highlight menu item statically currently acive
         document.getElementById('mainnav').innerHTML = menuTxt;
         document.getElementById('navgraph'+i).innerHTML = 
            '<img src="'+menuImgR+'" id="'+imgID+'" />';
      }
   }
   if ( menuLocalDir == '' )
   {
      document.getElementById('unterzeile').style.visibility = 'hidden';
      document.getElementById('mainnav').innerHTML = "";
   }
}

function nospam(user,domain,suffix)
{
   document.write ( '<a class=\'normal\' href="' + 'mailto:' + user + '@' + domain + '.' +
     suffix + '">' + user + '@' + domain + '.' + suffix + '</a>');
} 

// Find last occurence of x within s and return it's 0-based position.
// Return -1, if s don't contain x.
function rSearch(s,x)
{
   var sLen = s.length;
   var xLen = x.length;

   if ( xLen >0 )
      for ( var i=sLen-xLen; i>=0; i-- )
         if ( s.substr(i,xLen) == x ) return i;

   return -1;
}

function start()
{
   init ();
   if ( document.getElementById("site") != "undefined" )
   {
      document.body.style.backgroundImage = 'url("'+hostURL+'loading.gif")';
      writeMainMenu ();
      writeSubMenu ();
   }
}
		
function toggleDisplay(control)
{
	var elem = document.getElementById ( control );
	
	if ( elem.style.display == "none" )
   {
		elem.style.display = "block";
	}
   else
   {
		elem.style.display = "none";
	}
}

function viewFoto(img)
{
   var width   = foto1.width + 20;
   var height  = foto1.height + 20;
   var string  = "width=" + width + ",height=" + height;
	var fenster = window.open ( img, "Buchcover", string ); 
} 

function writeMainMenu()
{
   preloadCounter    = 0;
   preloadImages     = new Array ();
   preloadImages_rot = new Array ();
   
   document.getElementById("grau").innerHTML = '<a href="' + hostURL +
     'index.html">zoran drvenkar</a>&nbsp;';
   
   for ( i=0; i<mainMenu.length; i++ )
	{
      var menuDef  = mainMenu[i];
      var menuTxt  = menuDef[0];
      var menuURL  = hostURL + menuDef[1];
      var menuDir  = menuDef[1].substr ( 0, rSearch(menuDef[1],'/') );
      var imgID    = 'nav' + menuDir;
      var menuImg  = menuAbsDir + imgID + '.jpg';
      var menuImgR = menuAbsDir + imgID + '_rot.gif';

      // preload images
      preloadImages[i]         = new Image();
      preloadImages[i].src     = menuImg;
      preloadImages_rot[i]     = new Image();
      preloadImages_rot[i].src = menuImgR;

      if ( menuLocalDir != "" )
      {  // we are not at top level page
         if ( menuDir != menuLocalDir )
         {  // menu item is not for current page: link + picture
//            document.getElementById("navgraph"+i).innerHTML =
//               '<a href="' + menuURL + '"><img src="' + menuImg + '" id="' + imgID +
//               '" onMouseOver="nav_in(this.src, this);" onMouseOut="nav_out(this.src, this);" /></a>';
            document.getElementById("navgraph"+i).innerHTML =
               '<a href="' + menuURL + '"><img src="' + menuImg + '" id="' + imgID + '" name="' + imgID +
               '" onMouseOver="nav_in(this.src, this);" onMouseOut="nav_out(this.src, this);" /></a>';
         } 
         else
         {  // menu item is for current page: only dummy picture
            document.getElementById('navgraph'+i).innerHTML =
               '<img src="' + menuImgR + '" id="' + imgID + '" />';
            document.getElementById('mainnav').innerHTML = menuTxt;
         }
      }
      else
      {  // we are at top level page, all items have link + picture
//         document.getElementById("navgraph"+i).innerHTML =
//            '<a href="' + menuURL + '"><img src="' + menuImg+'" id="' + imgID +
//            '" onMouseOver="nav_in(this.src, this);" onMouseOut="nav_out(this.src, this);" /></a>';
            document.getElementById("navgraph"+i).innerHTML =
               '<a href="' + menuURL + '"><img src="' + menuImg + '" id="' + imgID + '" name="' + imgID +
               '" onMouseOver="nav_in(this.src, this);" onMouseOut="nav_out(this.src, this);" /></a>';
      }
   }
   checkit (); // check, if preloadImages[] are loaded and enable visibility
}

function writeSubMenu()
{
   var itemSet = subMenu[menuLocalDir];
   if ( typeof itemSet != 'undefined' )
   {
      var menuStr = '';
      var currURL = document.location.href;
      for ( i=0; i<itemSet.length; i++ ) // foreach menu item ...
      {
         var itemKey = itemSet[i];
         var itemDef  = menuItem[itemKey];
         var itemTxt  = itemDef[0];
         var itemURL  = hostURL + itemDef[1];
         if ( itemURL == currURL ) menuStr += '<span class=\'active\'>'+itemTxt+'</span>'; // text only needed
         else menuStr += '<a href="' + itemURL + '">' + itemTxt + '</a>';
         if ( i+1 < itemSet.length ) menuStr += subMenuSpacer;
      }
      document.getElementById('submenu').innerHTML = menuStr;
   }
}

window.onload = start

