
jQuery(document).ready(function(){
   var popupStatus = 0;  

   jQuery('#shows_table img').click(function(event){
      var position = jQuery(this).position();
      var img = jQuery(this).attr('name');
      var bildunterschrift = jQuery(this).attr('alt');
      var id = jQuery(this).attr('id');
      jQuery('#popupContact p.count').text(id);     
     jQuery('#popupContact h1').text(bildunterschrift +  ' ( Bild Nr. '+ id + ')');
      jQuery('#popupContact img').attr('src', img);
      jQuery('#popupContact img').attr('id', id);

      //centering with css
      centerPopup(position.top,position.left);
      //load popup
      loadPopup();
   });
    
   jQuery('#popupContact #next').click(function(event){ 
      var count = 0;
      var i = 0;
      var lastid = jQuery('table img:last').attr('id');
      var id= jQuery('#popupContact img').attr('id');
      var neuid = (parseInt(id) < parseInt(lastid)) ? parseInt(id)+1 : 1;
      if(neuid == id){}
      else{ 
         jQuery('#popupContact img').hide();
         jQuery('#popupContact p.count').text(neuid);
         var jid = '#'+ neuid; 
         var img = jQuery(jid).attr('name');
         var bildunterschrift = jQuery(jid).attr('alt');
         var id = jQuery(jid).attr('id');
         jQuery('#popupContact h1').text(bildunterschrift +  ' ( Bild '+ neuid + ')');
         jQuery('#popupContact img').attr('src', img);
         jQuery('#popupContact img').attr('id', id);
         jQuery('#popupContact img').fadeIn('slow');
     }
   });     
   jQuery('#popupContact img').click(function(event){ 
      var count = 0;
      var i = 0;
      var lastid = jQuery('table img:last').attr('id');
      var id= jQuery('#popupContact img').attr('id');
      var neuid = (parseInt(id) < parseInt(lastid)) ? parseInt(id)+1 : 1;
      if(neuid == id){}
      else{
         jQuery('#popupContact img').hide();
         var jid = '#'+ neuid; 
         var img = jQuery(jid).attr('name')
         var bildunterschrift = jQuery(jid).attr('alt');
         var id = jQuery(jid).attr('id');
         jQuery('#popupContact h1').text(bildunterschrift +  ' ( Bild '+ neuid + ')');
         jQuery('#popupContact img').attr('src', img);
         jQuery('#popupContact img').attr('id', id);
         jQuery('#popupContact img').fadeIn('slow');
     }
     });     
   jQuery('#popupContact #prev').click(function(event){
      var count = 0;
      var i = 0;
      var lastid = jQuery('table img:last').attr('id');
      var id= jQuery('#popupContact img').attr('id');
      var neuid = ((parseInt(id)-1) != 0) ? parseInt(id)-1 : lastid;
      if(neuid == id){}
      else{ 
         jQuery('#popupContact img').hide();
         jQuery('#popupContact p.count').text(neuid); 
         var jid = '#'+ neuid; 
         var img = jQuery(jid).attr('name');
         var bildunterschrift = jQuery(jid).attr('alt');
         var id = jQuery(jid).attr('id');
         jQuery('#popupContact h1').text(bildunterschrift +  ' ( Bild '+ neuid + ')')
         jQuery('#popupContact img').attr('src', img);
         jQuery('#popupContact img').attr('id', id);
         jQuery('#popupContact img').fadeIn('slow');
      }  
      });     

   $("#popupContactClose").click(function(){
      disablePopup();
   });

   $("#backgroundPopup").click(function(){
      disablePopup();
   });

   $(document).keypress(function(e){
      if(e.keyCode==27 && popupStatus==1){
         disablePopup();
      }
   });



//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
   //loads popup only if it is disabled
   if(popupStatus==0){
      $(".inhalt #backgroundPopup").css({
         "opacity": "0.7"
      });
      $(".inhalt #backgroundPopup").fadeIn("slow");
      $("#popupContact").fadeIn("slow");
      popupStatus = 1;
   }
}

//disabling popup with jQuery magic!
function disablePopup(){
   //disables popup only if it is enabled
   if(popupStatus==1){
      $("#backgroundPopup").fadeOut("slow");
      $("#popupContact").fadeOut("slow");
      popupStatus = 0;
   }
}

//centering popup
function centerPopup(top, left){
   //request data for centering
   
   var windowWidth = document.documentElement.clientWidth;
   var windowHeight = document.documentElement.clientHeight;
   var popupHeight = $("#popupContact").height();
   var popupWidth = $("#popupContact").width();
   //centering
   $("#popupContact").css({
      "position": "fixed",
      "top": 100,
      "left": windowWidth/2 -popupWidth/2
   });
   //only need force for IE6
   
   $("#backgroundPopup").css({
      "height": windowHeight
   });
   
}
});
