$(document).ready(function(){

$.history.init(pageload);

$('#Menu a').click(function() {
  
 if ( $(this).attr('href') != '') {
  $.history.load( $(this).attr('href').slice($(this).attr('href').search(/\Page=/)+5) );  
 } else {	//activate first <a> of the group
  AFirstHref = $(this).parents('li').children('ul').children('li').children('a:first').attr('href');
  $.history.load( AFirstHref.slice(AFirstHref.search(/\Page=/)+5) );
 };
 $(this).blur();
 return false;
 
});

//keep the menu in view on page scroll
$(window).scroll(function () { $('#Menu').css('top', $(window).scrollTop() +'px' ); });

})


function pageload(hash) {	//jquery load history

if (!hash) {
 if (document.URL.search(/\?Page=/) != -1) { document.location = document.URL.replace(/index.php\?Page=/, "#"); };
 if (document.URL.search(/&Page=/) != -1) { document.location = document.URL.replace(/&Page=/, "#"); };
 hash = StartPage; 
};

$('#Main').prepend("<div style='margin-bottom:10px; font: bold 12px arial'>Inhalt wird geladen...</div>");

if (Number(hash)) {
 File = BaseDir + hash +".htm";
} else {
 if (hash.search(/\?/) != -1) {	//if hash contains GET data
  File = BaseDir + hash.slice(0, hash.search(/\?/)) +".php"+ hash.slice(hash.search(/\?/));
  hash = hash.slice(0, hash.search(/\?/));
 } else {
  File = BaseDir + hash +".php";
 }; 
};

FileActive = File;

if (hash == StartPage ) {
 $('#Topbar').hide();
} else {
 $('#Topbar').show();
 $('#TopbarText').html( $('#Menu a[href*='+ hash +']').html() );
};

ThisLinkHref = $('#Menu a[href*='+hash+']').attr('href');

$.ajax({ url: File, context: $('#Menu a[href*='+hash+']'), cache: false, success: function(content) {
 if ( $(this).attr('href') == ThisLinkHref ) {	//display the loaded content only if during its loading, another content hasn't been displayed 
  $('#Main').html(content);
  $('#Main a').click(function() {
   if ( $(this).attr('href').slice(0, 10) == 'index.php?' ) {
    $.history.load( $(this).attr('href').slice($(this).attr('href').search(/\Page=/)+5) );  
    return false;
   };
  });
  if ( $('#Main img').length > 0 && $('#Main img').eq(0).attr('src').search(location.host+"/"+BaseDir) != -1 ) { $('#Main img').each(function() { $(this).attr('src', $(this).attr('src').slice( $(this).attr('src').search(location.host+"/"+BaseDir) + location.host.length+1+BaseDir.length , $(this).attr('src').length ) ); }); };	//webkit img proxy workaround
  MenuHilite(hash);
  quiteview();
  Gallery();
  
  //reset page scroll position and menu position (for ie, safari, chrome)
  window.scrollTo(0, 0);
  $('#Menu').css('top', '0px');
 };
}});

}


function MainLoad(hash) { $.historyLoad(hash); };


function MenuHilite(hash) { 
 $('#Menu a').css('color', '');
 //$('#Menu a').css('font-weight','normal');
 $('#Menu a[href*='+hash+']').css('color','#000');
 $('#Menu a[href*='+hash+']').css('font-weight','bold');
 $('#Menu a[href*='+hash+']').blur();
 
 $('#Menu a[href*='+hash+']').parents('ul').parents('li').children('a:first').css('color', '#000');	//higlight group name
 $('#Menu ul').slideUp(200); $('#Menu a[href*='+hash+']').parents('li').children('ul').stop(false,false);
 $('#Menu a[href*='+hash+']').parents('li').children('ul').slideDown(200);
 //UlActiveEq = $('#Menu ul').index( $('#Menu a[href*='+hash+']').parents('ul') );
 //$('#Menu ul').not('#Menu ul:eq('+ UlActiveEq +')').slideUp(200); 
}

function Gallery() {
 $('#Gallery h1').click(function() {
 GalleryActive = $("#Gallery ul:eq("+ $('#Gallery h1').index( $(this) ) +")");
 if (GalleryActive.css('display') == 'none') {
  GalleryActive.slideDown(300);
 } else {
  GalleryActive.slideUp(300);
 };
 });
}


