$.browser.iphone = (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i));

$(document).ready(function() {
   
   (function(){
      var sb_open = false;
      Shadowbox.init({
         continuous: true,
         players: ["img"],
         slideshowDelay: 5,
         onOpen: function() {
            sb_open = true;
         },
         onFinish: function() {
            if (sb_open) {
               sb_open = false;
               Shadowbox.pause();
            }
         }
      });
   }());

   $('img[src$=png]:not(.nofixpng), div:not(.nofixpng)').ifixpng();

   /*
    * iPhone styles
    */
   if ($.browser.iphone) {
      $('.mainMenu > a').css({
         'font-size': '7px'
      });
   }

   /*
    * alternative shadowbox opener
    */
   $('.open_shadowbox').click(function() {
      var rel = $(this).attr('rel').replace(/^open_sb/, 'shadowbox');
      $($('a[rel="'+rel+'"]')[0]).click();
      return false;
   });

   /*
    * navigation
    */
   var navigationGetChild = function() {
      var selector = $('.mainMenu > a.hovered').find('span.children').html();
      var identifier = $('<span class="hidden selector">'+selector+'</span>');
      var child = $('.mainMenu').find('span.'+selector).clone().append(identifier);
      return child;
   };
   var navigationGetParent = function() {
      var selector = $('.subMenu > span.selector').html();
      var parent = null;
      $('.mainMenu > a').each(function() {
         if ($(this).find('span.children').html() == selector) {
            parent = $(this);
         }
      });
      return parent;
   };
   var navigationAjustPosition = function() {
      if ($.browser.iphone) {
         $('.subMenu a').css({
            'font-size': '6px'
         });
      }
      var parent = navigationGetParent();
      var parentPos = $(parent).offset().left - $('.mainMenu').offset().left;
      var pos = parentPos - 40;
      var subMenuWidth = 0;
      $('.subMenu a').each(function(){
         subMenuWidth += $(this).outerWidth(true);
      });
      var mainMenuWidth = $('.mainMenu').width();
      if (pos + subMenuWidth > mainMenuWidth) {
         pos = mainMenuWidth - subMenuWidth;
      }
      if ($.browser.iphone) {
         pos -= 20;
      }
      if (pos < 0) {
         pos = 0;
      }
      $('.subMenu').css({
         'padding-left': pos+'px'
      });
   };
   var navigationActivateActiveLink = function() {
      $('.mainMenu > a').each(function() {
         if ($(this).hasClass('active')) {
            $(this).mouseenter();
         }
      });
   };
   
   var navigationTimeout = null; 
   $('.mainMenu > a').hover(function() {
      var element = $(this);
      navigationTimeout = setTimeout(function() {
         $('.mainMenu > a').removeClass('hovered').removeClass('preActive');
         element.addClass('hovered');
         $('.subMenu').html(navigationGetChild().html()).mouseenter(function() {
            $('.mainMenu > a').removeClass('preActive');
            var parent = navigationGetParent();
            parent.addClass('preActive');
         });
         navigationAjustPosition();
         navigationAjustPosition(); // what the heck? seems like the first calculation is not allways right...
      }, 150);
   }, function() {
      clearTimeout(navigationTimeout);
   });
   
   navigationActivateActiveLink();
   $('.menu').mouseleave(function() {
      navigationActivateActiveLink();
   });
   
});

function changeHomeButton(from, to) {
   var button = $('img[name=change]');
   button.attr('src', button.attr('src').replace(from, to));
}

function isValidEmail()
{
   str = document.forms.newsletter.email.value;
   var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
   var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
   if ((!reg1.test(str) && reg2.test(str)) && str !="")
   {
      window.document.forms.newsletter.submit()
   }
   else
   {
      alert ('Bitte geben Sie eine gültige E-Mailadresse an!');
   }
}

function closeThickbox() {
 jQuery("body, html").css("overflow","");
   $('#TB2_overlay, #TB2_window').remove();
   $('#TB2_overlay, #TB2_window, #TB2_HideSelect').remove();
}

