var page;
var uri;
var scn = 0; 

(function($) {
  var imgList = [];
  $.extend({
    preload: function(imgArr, option) {
      var setting = $.extend({
        init: function(loaded, total) {},
        loaded: function(img, loaded, total) {},
        loaded_all: function(loaded, total) {}
      }, option);
      var total = imgArr.length;
      var loaded = 0;
      
      setting.init(0, total);
      for(var i in imgArr) {
        imgList.push($("<img />")
          .attr("src", imgArr[i])
          .load(function() {
            loaded++;
            setting.loaded(this, loaded, total);
            if(loaded == total) {
              setting.loaded_all(loaded, total);
            }
          })
        );
      }
      
    }
  });
})(jQuery);
 
function scrollBack() {
  var y = scrollY();
	if ((scn == 0) && (y == 0)) {
	} else {
	  $('html, body').animate({ scrollTop: scn }, "slow");
	}
}
function scrollY() {
	var h = $("html").scrollTop();
	var b = $("body").scrollTop();
	if (b > h) {
	  h = b;
	}
	return(h);
}



$(document).ready(function() {
  page = $("body").attr("name");
  uri =  $("body").attr("uri");

  if (!uri) {
    uri = "/";
  }
  $(".menuentry").each(function() {
    if($(this).children().attr("href") == uri) {
      $(this).addClass("menuselected");
    }
    
  });
  $(".galleryImg").live("click", function() {
	  scn = scrollY();
		if (scn != 0) {
		  $('html, body').animate({ scrollTop: 0 }, "slow");
		}
    $(".galleryImgBig").remove();
    $(".galleryImgBigImg").remove();
    var iurl = '/img/preview/500/' + $(this).attr("name");
    $.preload([
      iurl
    ], {
      init: function(loaded, total) {
        $("body").append('<div class="galleryImgBig"></div>');     
        $("body").append('<table class="galleryImgBigImg"><tr><td class="loading">loading...</td></tr></table>');        
      },
      loaded: function(img, loaded, total) {     
        $(".galleryImgBigImg").html('<tr><td class="galleryImgBigImgImg"></td></tr>');
        $(".galleryImgBigImgImg").append(img);
      }
   });    
	 return(false);
    
  });
  $(".galleryImgBig").live("click", function() {
	  scrollBack();
    $(".galleryImgBig").remove();
    $(".galleryImgBigImg").remove();
  });
  $(".galleryImgBigImg").live("click", function() {
	  scrollBack();
    $(".galleryImgBig").remove();
    $(".galleryImgBigImg").remove();
  });
  
  
});




function zitat() {
  var n = Math.round(Math.random() * (Z.length-1));
  $("#zitat").html(Z[n] + '<div id="autor">' + ZP[n] + '</div>');
}


function siteStatus(message) {
  $("#siteStatusText").html(message);  
  $("#siteStatus").show();
  if (message.match(/^error.*/)) {
    $("#siteStatus").click(function() {
      $(this).fadeOut(1000);
    });
  } else {
    $("#siteStatus").fadeOut(1000);
  }
}

