// ((c) Copyright 2008 Stanislav Müller, 3st kommunikation. All Rights Reserved.

jQuery(function(){
	var navigation = $("#navigation ul");
	navigation.each(function(){
		$(this).hide();		
		$(this).prev().addClass("vorzeichen");
	});
	
	var numberForURL = function(number){
		if(number < 10){
			return "0"+number;
		}
		return ""+number;
	};
	
	var givePageNumberString = function(str, operation){
		var page_number = new Number(str.match(/p_(\d*).jpg/)[1]);
		
		
		switch(operation){
			case "minus":
				page_number--;
				return page_number;
			break;
			case "plus":
				page_number++;
				return page_number;
			break;
		}
	};
	
	var showLoading = function(){
	  $("#loading").fadeIn("fast");
	  $("#image").fadeTo("fast", 0.3);
	};
	
	var setActiveTrail = function(element){  
    $("#navigation .active").removeClass("active");
	  $(element).parents("li").addClass("active");
	};

	$("body").click(function(e){
		
		var is_parent_navigation = $(e.target).parents().filter(function(){
			return this.id=="navigation";
		}).length;
		
		var is_parent_paging = $(e.target).parents().filter(function(){
			return this.className=="paging";
		}).length;
		
		if($(e.target).parent().hasClass("next") || $(e.target).parent().hasClass("previous") || $(e.target).hasClass("next") || $(e.target).hasClass("previous")){
			var image_url = $("#image").attr("src");
			
			if($(e.target).parent().hasClass("next") || $(e.target).hasClass("next")){
			  var number = givePageNumberString(image_url, "plus");
				$("#image")[0].src = "images/p_"+number+".jpg";
				$("#print").attr("href", "pdf/p_"+number+".pdf");
			}
			
			if($(e.target).parent().hasClass("previous") || $(e.target).hasClass("previous")){
			  var number = givePageNumberString(image_url, "minus");
				$("#image")[0].src = "images/p_"+number+".jpg";
				$("#print").attr("href", "pdf/p_"+number+".pdf");
			}
			
			window.location.hash = "#p_"+number;
			
			$("#page_number").val(number);
			
			if(number == "148"){
			  $(".next").hide();
			}else{
			  $(".next").show();
			}
			
			if(number == "1"){
			  $(".previous").hide();
			}else{
			  $(".previous").show();
			}
			showLoading();
			return false;
		}

		if(is_parent_navigation && e.target.nodeName == "A"){
			setActiveTrail(e.target);
			
		  if($(e.target).hasClass("vorzeichen")){
        $("#navigation ul:visible").filter(function(){
          return $(e.target).next()[0] != this;
        }).slideToggle("fast");
        
  			$(e.target).next().slideToggle("fast");  			
  		}
  		showLoading();
			$("#image").attr("src", e.target.href);
      $("#print").attr("href", "pdf/p_"+e.target.href.split("_")[1].split(".")[0]+".pdf");
			window.location.href = "#p_"+e.target.href.split("_")[1].split(".")[0];
					
			return false;
		}
		
		if(e.target.className == "first" || e.target.className=="last")	{
      showLoading();
			$("#image").attr("src", e.target.href);
      $("#page_number").val(e.target.href.split("_")[1].split(".")[0]);
      
      $(".previous").show();
      $(".next").show();
      
      if(e.target.className == "first") $(".previous").hide();
      if(e.target.className == "last") $(".next").hide();
		  return false;
		}

	});
	
  if(window.location.hash){
    if(window.location.hash == "#") window.location.hash = "#p_1";
    $("#image").attr("src", "images/"+window.location.hash.substring(1, window.location.hash.length)+".jpg");
    $("#print").attr("href", "pdf/"+window.location.hash.substring(1, window.location.hash.length)+".pdf");
  } else {
    window.location.hash = "#p_1";
    $("#image").attr("src", "images/p_1.jpg");
    $("#print").attr("href", "pdf/p_1.pdf");
  }
  
  if(window.location.hash == "#p_1"){
    $(".previous").hide();
  }
  
  if(window.location.hash == "#p_148"){
    $(".next").hide();
  }
  
  $("#image").load(function(){ 
    $("#loading").fadeOut("fast"); 
    $(this).fadeTo("fast", 1);
  });
  
  $("#page_number").val(window.location.hash.split("_")[1]).bind("keyup", function(){
    this.value = this.value.split(" ").join("");
    
    window.location.hash = "#p_"+this.value;
    showLoading();
    
    $("#image").attr("src", "images/p_"+this.value+".jpg");
    $("#print").attr("href", "pdf/p_"+this.value+".pdf");
  });
  
  $("#sub-navi, #content").show();
  $("noscript").hide();
});

function popUp(URL) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=300');");
}
