/*
QuiteCMS
 by Zolo Kallay, ZoA
 http://www.zollo.sk
*/


$(document).ready(function() {

$.history.init(pageload);

$('#Menu a').click(function() {
  
 $.history.load( $(this).attr('href').slice($(this).attr('href').search(/\Page=/)+5) );
 /*if ( $(this).parents('ul').attr('id') != 'Menu' || $(this).parents('li').children('ul').length == 0) {
  $.history.load( $(this).attr('href').slice($(this).attr('href').search(/\Page=/)+5) );
 } else {
  AFirstHref = $(this).parents('li').children('ul').children('li').children('a:first').attr('href');
  $.history.load( AFirstHref.slice(AFirstHref.search(/\Page=/)+5) );
 };	//if !GroupsContent	*/
 $(this).blur();
 return false;
 
});

})


function pageload(hash) {	//jquery load history

if (!hash) {
 if (document.URL.search(/index.php\?Page=/) != -1) { document.location = document.URL.replace(/index.php\?Page=/, "#"); };
 if (document.URL.search(/index.php\?Lang=/) != -1 && document.URL.search(/&Page=/) != -1) { document.location = document.URL.replace(/&Page=/, "#"); };
 if (document.URL.search(/index.php\?Redir=true&Page=/) != -1) { document.location = document.URL.replace(/index.php\?Redir=true&Page=/, "#"); };
 if (document.URL.search(/index.php\?Redir=true&Lang=/) != -1) { document.location = document.URL.replace(/Redir=true&/, "").replace("&Page=", "#"); };
 hash = StartPage; 
};

$('#Main').prepend("<div style='margin-bottom:10px; font: bold 12px arial'>Lade Inhalt...</div>");

if (hash.match(/^\d+$/)) {	//is numeric
 File = BaseDir + hash +".htm";
} else {	//is string
 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";
 }; 
};

ThisLink = $("#Menu a[href$='Page="+ hash +"']");

$.ajax({ url: File, context: ThisLink, cache: false, success: function(content) {
 if ( $(this).attr('href') == ThisLink.attr('href') ) {	//display the loaded content only if during its loading, another content hasn't been displayed
  if ( $(window).scrollTop() > $('#Main').offset().top ) {
   $('html, body').animate({scrollTop : $('#Main').offset().top}, 200);
  };
  $('#Main').html(content);
  PostProcessing();
  $('#Main a').click(function() {
   if ( $(this).attr('href').slice(0, 15) == 'index.php?Page=' || $(this).attr('href').slice(0, 15) == 'index.php?Lang=' ) {
    $.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 ) ); }); };	//image proxy workaround
  MenuHilite(hash);
  quiteview();
  Gallery();
 };
}});

}


function MainLoad(hash) { $.history.load(hash); };


function MenuHilite(hash) { 
 $('#Menu a').css({'color': ''});
 ThisLink.css({'color': '#fe6'});
 $('#Menu a[href*='+hash+']').blur();
 
 //ThisLink.parents('ul').parents('li').children('a:first').css({'color': '#fe9'});	//highlight group name
 ThisLink.parents('li').children('ul').slideDown(200);
 UlActiveEq = $('#Menu ul').index( ThisLink.parents('ul') );
 $('#Menu ul').not('#Menu ul:eq('+ UlActiveEq +')').not( ThisLink.parents('li').children('ul') ).slideUp(200); 
}


function PostProcessing() {};


function Gallery() {
 $('#Gallery h1').click(function() {
 GalleryActive = $("#Gallery ul:eq("+ $('#Gallery h1').index( $(this) ) +")");
 GalleryActive.slideToggle(300);
 });
}


