$(document).ready(function() {
  
  $("#tab-box").tabs();
  
  var cookieHeaderSlideshow = $.cookie('headerSlideshow'),
  tplHeader = $("#tpl_header"),
  tplPic    = $(".tpl_pic"),
  btnOpen   = $("#open"),
  btnClose  = $("#close");
  
  btnClose.click(function() {
    tplHeader.animate({
      height: "161px"
    }, 1500);
    tplPic.animate({
      height: "50px"
    }, 1500 , function() {
      $.cookie('headerSlideshow', 0, { path:'/' });
    });
    btnOpen.css('display', 'block');
    btnClose.css('display', 'none');
  });
  
  btnOpen.click(function() {
    tplHeader.animate({
      height: "356px"
    }, 1500 );
    tplPic.animate({
      height: "244px"
    }, 1500 , function() {
      $.cookie('headerSlideshow', 1, { path:'/' });
    });
    btnOpen.css('display', 'none');
    btnClose.css('display', 'block');
  });
  
  if(cookieHeaderSlideshow == '0') {
    tplHeader.css("height","161px");
    tplPic.css("height","50px");
    btnOpen.css('display', 'block');
    btnClose.css('display', 'none');
  } else if(cookieHeaderSlideshow == null) {
    tplHeader.css("height","161px");
    tplPic.css("height","50px");
    btnOpen.trigger('click');
  }
  
  // Babygalerie
  
  var blockContent = $('.simple_box.BABY_babygallery .HP-pageblock-content');
  
  if(blockContent.length != 0) {
    
    var layer = $('.simple_box.BABY_babygallery .BABY_babygallery');
    var leftButton = document.createElement('span');
    var rightButton = document.createElement('span');
    var link = document.createElement('a');
    var linkText = document.createTextNode('zu den Babygalerien');
    var layerWidth = 0;
    var isAnim = false;
    
    leftButton.className = 'leftButton gallery-btn disabled';
    rightButton.className = 'rightButton gallery-btn';
    
    link.className = 'link';
    link.href='/med-fachabteilung/hauptabteilungen/gynaekologie-geburtshilfe/geburtshilfe/babygalerien/babygalerie-kreisssaalfotos/';
    link.appendChild(linkText);
    blockContent.append(link);
    
    layer.find('a').css('cursor', 'pointer');
    layer.find('form').remove();
    layer.find('div:last').remove();
    layer.find('br').css({
      "clear":"none",
      "height":0,
      "width":0,
      "display":"none"
    });
    layer.find('.baby').each(function() {
      
      this.parentNode.className = 'container';
      layerWidth += 205;
      
    });
    
    $('.baby .pic').find('a').each(function() {
      $(this).attr('href', '/med-fachabteilung/hauptabteilungen/gynaekologie-geburtshilfe/geburtshilfe/babygalerien/babygalerie-kreisssaalfotos' +  $(this).attr('href'));
    });
    
    
    layer.css('width', layerWidth + 'px');
    
    blockContent.prepend(leftButton);
    blockContent.append(rightButton);
    
    leftButton.onclick = function() {
      
      if(isAnim == false && (parseInt(layer[0].style.left) || 0) < 0) {
        isAnim = true;
        layer.animate({left: "+=205px"}, 500, 'swing', function() {
           
          if((parseInt(layer[0].style.left) || 0) < 0) {}
          else {$('.leftButton').addClass('disabled');}
          
          $('.rightButton').removeClass('disabled');
          isAnim = false; 
          
        });
      } else {
      	if(isAnim == false) {
          $('.leftButton').addClass('disabled');
        }
      }
      
    };
    
    
    rightButton.onclick = function() {
      
      var container = $('.container', layer);
      container.css('left','0');
      
      if(isAnim == false && (parseInt(container.css('left')) || 0) > -layerWidth) {
        isAnim = true;
        layer.animate({left: "-=205px"}, 500, 'swing', function() { 
          
          if((parseInt(layer[0].style.left) || 0) > -layerWidth + 415) {
          } else {
	    $('.rightButton').addClass('disabled');
	  }
          
          $('.leftButton').removeClass('disabled');
          isAnim = false; 
        });
      } else {
        if(isAnim == false) {
          $('.rightButton').addClass('disabled');
        }
      }
      
      
    };
    
  }
  
  
  /* Baby Auto Slider */
  var interval     = 0,
      BabyCount    = 0,
      BabyCounter  = 1,
      BabyPrev     = false;
      
  BabyCount = $('.BABY_babygallery .container .baby').length;
  
  var scrollNext = function() {
    
    if((BabyCount != BabyCounter) && BabyPrev == false && isAnim == false) {
      BabyCounter++;
      $('.rightButton').trigger('click');
    } else {
      BabyPrev = true;
      if(BabyCounter > 1 && isAnim == false) {
        --BabyCounter;
        $('.leftButton').trigger('click');
      } else {
        BabyPrev = false;
      }
    }
  }
  
  interval = window.setInterval(scrollNext, 6000);
  
  $('.rightButton').mouseup(function() { window.clearInterval(interval); });
  $('.leftButton').mouseup(function() { window.clearInterval(interval); });
  
  
  
  
  var imgs = $('.tpl_pic .HP-pageblock img');
  
  if(imgs.length > 1) {
    
    var i = 0;
    var zIndex = 10000;
    var first = $(imgs.get(0));
    var next = first.next();
    var tmp = null;
    var timeoutThread = null;
    var effectThread = null;
    
    var switchRadio = function() {
      
      var currentSwitcher = $('.tpl_pic .switcher a.active');
      var nextSwitcher = currentSwitcher.next();
      
      if(nextSwitcher.length == 0)
        nextSwitcher = $('.tpl_pic .switcher a:first');
      
      currentSwitcher[0].className = '';
      nextSwitcher[0].className = 'active';
      
    };
    
    var func = function() {
      
      timeoutThread = window.setTimeout(function() {
        switchRadio();
        next.css('zIndex', ++zIndex);
        
        if(next.prev().length == 0)
          last = next.parent().children(':last');
        else
          last = next.prev();
        
        last.fadeOut(150, function() {
	  
	  next.fadeIn(300, function() {
	    
	    tmp = next.next();
	    
	    if(tmp.length != 0)
	      next = tmp;
	    else
	      next = first;
	    
	    func();
	    
	  });
	  
        });
        
        
      }, 3000);
      
    };
    
    var switchers = [];
    var switcherLayer = document.createElement('div');
    
    switcherLayer.className = 'switcher';
    
    imgs.each(function() {
      
      var img = $(this);
      
      img.css({
	'zIndex': zIndex - i++,
	'display':'none'
      });
      
      var switcher = document.createElement('a');
      
      switcher.swoImg = img;
      
      $(switcher).click(function() {
        
        window.clearTimeout(timeoutThread);
        imgs.stop();
        imgs.hide();
        this.swoImg.css('opacity', '1').fadeIn(500);
        $(switchers).removeClass('active');
        this.className = 'active';
        
      });
      
      switcher.appendChild(document.createTextNode(i));
      switchers.push(switcher);
      switcherLayer.appendChild(switcher);
      $('.tpl_pic').append(switcherLayer);
      
    });
    
    imgs.first().show();
    
    $(switchers).addClass("number");
    
    if(switchers.length > 0)
      switchers[0].className = 'active';
    
    func();
    
  }
  
  /* Slideshow next/prev */
  $('a.next').click(function() {
    if($('.active').next().length > 0)
      $('.active').next().click();
    else
      $('.active').parent().children().first().click();
  });
  
  $('a.prev').click(function() {
    if($('.active').prev().length > 0)
      $('.active').prev().click();
    else
      $('.active').parent().children().last().click();
  });
  
  
  /* Slideshow play */
  $('#play').click(function() {
    func();
  });
  
  /* Slideshow pause */
  $('#pause').click(function() {
    window.clearTimeout(timeoutThread);
    imgs.stop();
  });
  
  
  ///* News Switcher */
  //var running = false;
  //var news = $('.HP-pageblock.simple_box.news .HP_listing .pages');
  //var news_content = $('.page', news);
  //
  //var buttonDown = document.createElement("div");
  //buttonDown.className      = "button down";
  //$('.HP-pageblock.simple_box.news').append(buttonDown);
  //
  //var buttonUp = document.createElement("div");
  //buttonUp.className      = "button up";
  //$('.HP-pageblock.simple_box.news').append(buttonUp);
  //
  //$(buttonUp).click(function() {
  //  switchNews("up");
  //});
  //
  //$(buttonDown).click(function() {
  //  switchNews("down");
  //});
  
  
  /* News Switcher */
  var running = false;
  var news = $('.HP-pageblock.simple_box.news ul');
  var news_content = $('li', news);
  
  var buttonDown = document.createElement("div");
  buttonDown.className      = "button down";
  $('.HP-pageblock.simple_box.news').append(buttonDown);
  
  var buttonUp = document.createElement("div");
  buttonUp.className      = "button up";
  $('.HP-pageblock.simple_box.news').append(buttonUp);
  
  $(buttonUp).click(function() {
    switchNews("up");
  });
  
  $(buttonDown).click(function() {
    switchNews("down");
  });
  
  
  
  /* Termin Switcher */
  var run = false;
  var termin = $('.HP-pageblock.simple_box.termin ul');
  var termin_content = $('li', termin);
  
  var btnDown = document.createElement("div");
  btnDown.className      = "button down";
  $('.HP-pageblock.simple_box.termin').append(btnDown);
  
  var btnUp = document.createElement("div");
  btnUp.className      = "button up";
  $('.HP-pageblock.simple_box.termin').append(btnUp);
  
  $(btnUp).click(function() {
    switchTermin("up");
  });
  
  $(btnDown).click(function() {
    switchTermin("down");
  });
  
  
  /* MENU SLIDE */
  $('.tpl_menu .menu li').mouseenter(function() {
      
      window.clearTimeout(this.timeout);
      
      var li = $(this);
      
      li.siblings().each(function() {
        
        if(this.timeout)
          window.clearTimeout(this.timeout);
        
        var sibling = $(this);
        
        sibling.removeClass("active");
        sibling.children('ul').hide();
        
      });
      
      li.children('ul').slideDown('slow');
      li.children('ul').css('display', 'block');
      li.addClass("active");
    
  }).mouseleave(function() {
    
    var self = $(this);
    
    this.timeout = window.setTimeout(function() {
        
      self.find('ul').hide();
      self.removeClass("active");
      
    }, 1000);
    
  });
  
  
  /* SUBLISUB :o */
  $('.tpl_menu .menu li ul').each(function() {
    
    var ul = $(this);
    
    ul.hide();  //FIX FOR MENU SLIDE
    ul.parent().addClass('sublisub');
    
  });
  
  
  /* target blank menu */
  $('.tpl_menu .menu a[target="_blank"]').each(function() {
    $(this).addClass('extern');
  });
  
  /* BABY LINK BACK */
  $('.BABY_babygallery p a[href=""]').attr("href", '/med-fachabteilung/hauptabteilungen/gynaekologie-geburtshilfe/geburtshilfe/babygalerie');
  
  
});


var currentNews = 0;
var running     = false;

var switchNews = function(direction) {
  var news = $('.HP-pageblock.simple_box.news ul li'),
   intNews = news.length;
  
  if(!running) {
    
    if(direction == "up") {
      
      if((currentNews + 3) <= (intNews - 1)) {
	
	currentNews = currentNews + 1;
	
	nextPos = $('a', news[currentNews]).position().top;
	
	if(!running) {
	  
	  running = true;
	  
	  news.parent().animate({
	  top: (nextPos * -1)
	  }, 'normal', function() {
	    running = false;
	  });
	  
	}
	
      }
      
    } else {
      
      if(currentNews >= 1) {
	
	currentNews = currentNews - 1;
	
	nextPos = $('a', news[currentNews]).position().top;
	
	if(!running) {
	  
	  running = true;
	  
	  news.parent().animate({
	  top: (nextPos * -1)
	  }, 'normal', function() {
	    running = false;
	  });
	  
	}
	
      }
      
    }
    
  }
  
}


var currentTermin = 0;
var run     = false;

var switchTermin = function(direction) {
  var termin = $('.HP-pageblock.simple_box.termin .HP-section ul li'),
   intTermin = termin.length;
  
  if(!run) {
    
    if(direction == "up") {
      
      if((currentTermin + 4) <= (intTermin - 1)) {
	
	currentTermin = currentTermin + 1;
	
	nextPos = $(termin[currentTermin]).position().top;
	
	if(!run) {
	  
	  run = true;
	  
	  termin.parent().animate({
	  top: (nextPos * -1)
	  }, 'normal', function() {
	    run = false;
	  });
	  
	}
	
      }
      
    } else {
      
      if(currentTermin >= 1) {
	
	currentTermin = currentTermin - 1;
	
	nextPos = $(termin[currentTermin]).position().top;
	
	if(!run) {
	  
	  run = true;
	  
	  termin.parent().animate({
	  top: (nextPos * -1)
	  }, 'normal', function() {
	    run = false;
	  });
	  
	}
	
      }
      
    }
    
  }
  
}


var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

var uncryptMail = function(email) {
  location.href=Base64.decode(email);
}

