////////////////////////////////////////////////////////////////
//
// @method void wOpenURL(url)
//
// @desc Öffnet eine Seite
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenURL(url){
  if(location.pathname.indexOf('/weblication/grid5/') != 0){
    if(url.match(/\.php$/) || url.match(/\/[^\.]+$/)){
      top.location.href = url;    
    }
    else{
      window.open(url);
    }
  }
}

////////////////////////////////////////////////////////////////
//
// @method void wOpenPopupURL(url, width, height)
//
// @desc Öffnet eine Seite in einem Popup
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenPopupURL(url, width, height){

  var name = '';
  
  if(width == '' || width == ' '){
    width = 786;
  }
  if(height == '' || height == ' '){
    height = 677;
  }
  
  url = url.replace(/&amp;/g, '&');
  options = ',location=no, menubar=no,toolbar=no,resizable=no,scrollbars=no,status=no';
  width   = parseInt(width);
  height  = parseInt(height);

  var posLeft = (screen.width / 2)  - (width  / 2);
  var posTop  = (screen.height / 2) - (height / 2);
  window.open(url, name, 'top='+posTop+',left='+posLeft+',width='+width+',height='+height+',' + options);
}

////////////////////////////////////////////////////////////////
//
// @method void wShowLayerBookmarklet()
//
// @desc Schlägt vor, diese Webseite als App abzulegen
//
// @return void
//
////////////////////////////////////////////////////////////////

function wShowLayerBookmarklet(){

  if(/iPhone|iPod/.test(window.navigator.userAgent)){
    if(window.localStorage){             
      if(typeof(window.localStorage['numberBookmarketShown']) == 'undefined' || Number(window.localStorage['numberBookmarketShown']) < 2){
        window.localStorage['numberBookmarketShown'] = String(Number(window.localStorage['numberBookmarketShown'] || 0) + 1);
        var bookmarkletInfo = document.createElement('div');   
        
        bookmarkletInfo.id             = 'wBookmarkletInfo';
        bookmarkletInfo.style.width    = '100%';
        bookmarkletInfo.style.position = 'absolute';
        bookmarkletInfo.style.bottom   = '0';                
        
        var bookmarkletInfoInner = document.createElement('div');
        bookmarkletInfoInner.style.color      = '#ffffff';
        bookmarkletInfoInner.style.border     = 'solid 4px #ffffff';                                                                       
        bookmarkletInfoInner.style.margin     = '20px';
        bookmarkletInfoInner.style.padding    = '8px';
        bookmarkletInfoInner.style.overflow   = 'hidden';
        bookmarkletInfoInner.style.position   = 'relative';                
        bookmarkletInfoInner.style.background = '#cddcf3 -webkit-gradient(linear, left top, left bottom, from(#B5C9D9), to(#6886B0)) no-repeat bottom';                  
        bookmarkletInfoInner.style.WebkitBorderRadius = '8px';
        bookmarkletInfoInner.style.WebkitBoxShadow    = '0 4px 6px #a0a0a0';   
        
        bookmarkletInfo.appendChild(bookmarkletInfoInner);
        
        var bookmarkletInfoIcon = document.createElement('img');
        bookmarkletInfoIcon.style.margin = '8px';
        bookmarkletInfoIcon.style.position = 'absolute';                               
        
        var linkTags = document.getElementsByTagName('link');
        for(var i = 0; i != linkTags.length; i++){
          if(linkTags[i].getAttribute('rel') == 'apple-touch-icon'){
            bookmarkletInfoIcon.src = linkTags[i].getAttribute('href');
          }
        }
        
        bookmarkletInfoInner.appendChild(bookmarkletInfoIcon);
        
        var bookmarkletInfoCloser = document.createElement('img');
        bookmarkletInfoCloser.src = '/weblication/grid5/gui/images/style/bookmarklet_close.png';
        bookmarkletInfoCloser.style.margin = '-4px';
        bookmarkletInfoCloser.style.padding = '2px';
        bookmarkletInfoCloser.style.background = '#ffffff';
        bookmarkletInfoCloser.style.border = 'solid 1px #c0c0c0';
        bookmarkletInfoCloser.style.float  = 'right';
        
        bookmarkletInfoCloser.onclick = function(){document.getElementById('wBookmarkletInfo').parentNode.removeChild(document.getElementById('wBookmarkletInfo'));window.localStorage['numberBookmarketShown'] = '99'};                            
        bookmarkletInfoInner.appendChild(bookmarkletInfoCloser);                
        
        var bookmarkletInfoText = document.createElement('div');
        bookmarkletInfoText.style.margin = '8px 8px 8px 100px';
        bookmarkletInfoText.style.position  = 'left';
        bookmarkletInfoText.style.backgroundColor  = 'none';  
        bookmarkletInfoText.innerHTML    = 'Klicken Sie unten auf das "+" Zeichen und dann auf "Zum Home-Bildschirm hinzufügen", um diese Seiten als App abzulegen.';   
        
        bookmarkletInfoInner.appendChild(bookmarkletInfoText); 
        
        var bookmarkletArrow = document.createElement('div');
        bookmarkletArrow.style.height             = '23px';
        bookmarkletArrow.style.width              = '100%';
        bookmarkletArrow.style.marginTop          = '-23px';
        bookmarkletArrow.style.position           = 'absolute';                         
        bookmarkletArrow.style.zIndex             = '10';        
        bookmarkletArrow.style.backgroundRepeat   = 'no-repeat';
        bookmarkletArrow.style.backgroundPosition = 'center';        
        bookmarkletArrow.style.backgroundImage    = 'url(/weblication/grid5/gui/images/style/arrow_bookmarklet.png)'; 
        
        bookmarkletInfo.appendChild(bookmarkletArrow);                                                               
        
        document.body.appendChild(bookmarkletInfo);
      }
      else{
      }  
    }
  }
}

////////////////////////////////////////////////////////////////
//
// @method void wSendMailNospam(data)
//
// @desc Öffnet eine E-Mail zum Versenden
//
// @return void
//
////////////////////////////////////////////////////////////////

function wSendMailNospam(data){
  location.href = 'mailto:' + data.replace('|', '@');
}

function wEmbedNavpoint(id, url, mode){

  if(typeof wEditor == 'object'){
    return false;
  }         
 
  if(mode == 'embed'){ 
    if($('#navpoint_' + id).data('hasLoaded') == '1'){             
      $('#navEmbed_' + id).show();                                    
    }
    else{        
      jQuery('#navpoint_' + id).mouseout(function(){
        $('#navEmbed_' + id).hide();
      });    
       
      url = url.indexOf('?') == -1 ? url + '?viewmode=embed' : url + '&viewmode=embed';
      url = url.replace('[URL]', location.pathname + location.search);
      $.ajax({
        type: "GET",
        url: url,
        success: function(result){
        
          result = result.replace(/\n/g, '_wNL-').replace(/\s+id="[^"]+"/gim, '').replace(/_wNL-/g, "\n");
  
          $('#navpoint_' + id).data('hasLoaded', '1');                  
          $('#navEmbed_' + id).html(result);
          
          var navpointLeft = $('#navpoint_' + id).offset().left;
          
          $('#navEmbed_' + id).show();
          
          var layerLeft = $('#navEmbed_' + id).offset().left;
          
          var blockMainRight = $('#blockMain').offset().left + $('#blockMain').width();        
          
          if(layerLeft < navpointLeft){
            $('#navEmbed_' + id).css('left', String(navpointLeft - layerLeft - 6) + 'px');
          }
          
          var layerRight = $('#navEmbed_' + id).offset().left + $('#navEmbed_' + id).width();
                       
          if(layerRight > blockMainRight){
            $('#navEmbed_' + id).css('marginLeft', String( - (layerRight - blockMainRight) - 22) + 'px');
          }          
          
          jQuery('#navEmbed_' + id).mouseover(function(){
            $('#navEmbed_' + id).show();
          });
          
          jQuery('#navEmbed_' + id).mouseout(function(){
            $('#navEmbed_' + id).hide();
          });          
        }
      });
    }  
  }
  else{ 
    if($('#navpoint_' + id).data('hasLoaded') == '1'){             
      $('#navEmbed_' + id).show();                                 
      try{$('#navEmbedIframe_' + id).height(frames['navEmbedIframe_' + id].$('#blockContent').height() + 50);}catch(e){}
      $('#navEmbed_' + id).height(frames['navEmbedIframe_' + id].$('#blockContent').height() + 60);            
    }
    else{                      
      url = url.indexOf('?') == -1 ? url + '?viewmode=blank&parentIframeId=navEmbedIframe_'+id : url + '&viewmode=blank&parentIframeId=navEmbedIframe_'+id;
      url = url.replace('[URL]', location.pathname + location.search);
      $('#navpoint_' + id).data('hasLoaded', '1');                  
      
      var iframeEmbed    = document.createElement('iframe');

      iframeEmbed.id        = 'navEmbedIframe_' + id;
      iframeEmbed.name      = 'navEmbedIframe_' + id;
      iframeEmbed.className = 'navLevel1Embed';
      iframeEmbed.frameBorder = '0';
      iframeEmbed.scrolling   = 'no';
      iframeEmbed.src       = url;
             
      var embedContainer = document.getElementById('navEmbed_' + id);                
      embedContainer.appendChild(iframeEmbed);
      
      var navpointLeft = $('#navpoint_' + id).offset().left;
      
      $('#navEmbed_' + id).show();
      
      var layerLeft = $('#navEmbed_' + id).offset().left;
      
      var blockMainRight = $('#blockMain').offset().left + $('#blockMain').width();        
                      
      if(layerLeft < navpointLeft){
        $('#navEmbed_' + id).css('left', String(navpointLeft - layerLeft - 1) + 'px');
      }
      
      var layerRight = $('#navEmbed_' + id).offset().left + $('#navEmbed_' + id).width();
                   
      if(layerRight > blockMainRight){
        //$('#navEmbed_' + id).css('left', String(parseInt($('#navEmbed_' + id).css('left')) - (layerRight - blockMainRight)) + 'px');
        $('#navEmbed_' + id).css('marginLeft', String( - (layerRight - blockMainRight) - 22) + 'px');
      }
      
      jQuery('#navpoint_' + id).mouseout(function(){
        $('#navEmbed_' + id).hide();
      });
      
      jQuery('#navEmbed_' + id).mouseover(function(){
        $('#navEmbed_' + id).show();
      });
      
      jQuery('#navEmbed_' + id).mouseout(function(){
        $('#navEmbed_' + id).hide();
      });        
    }       
  }  
}

$(document).ready(function(){

    //Banner-Slider Elemente einblenden
  $('.stripViewer .panelContainer .panel').show();

  // Leeren des Eingabefeldes
  $('#stichwort').click(function(){
    $(this).val('');
  })

  // Toggle der Spacer für NavLevel1 bei Mouseover
  $('ul.navLevel1Container:not(.grenzenlos) > li').children('a:not([class *= "Selected"])').parent().hover(function(){
    $(this).next().children('a').addClass('noSpacer');
    // Trenner falls Mouseover auf NavPoint, dessen Nachfolger aktiv ist 
    if($(this).next().children('a[class *= "Selected"]').length != 0){
      $(this).next().children('a').addClass('borderActiveLeft');
    }
    // Trenner falls Mouseover auf NavPoint, dessen Vorgänger aktiv ist 
    if($(this).prev().children('a[class *= "Selected"]').length != 0){
      $(this).prev().children('a').addClass('borderActiveRight');
    }
    $(this).addClass('active');
    $(this).children('a').css('color', colorNavpointActive);
    moveBgNav($(this));
  }, function(){
    //$(this).css('background', '0 none');
    $(this).children('a').css('color', colorNavpoint);
    $(this).removeClass('active');
    // Trenner entfernen falls Mouseover auf NavPoint, dessen Nachfolger aktiv ist 
    if($(this).next().children('a[class *= "Selected"]').length != 0){
      $(this).next().children('a').removeClass('borderActiveLeft');
    }
    // Trenner falls Mouseover auf NavPoint, dessen Vorgänger aktiv ist 
    if($(this).prev().children('a[class *= "Selected"]').length != 0){
      $(this).prev().children('a').removeClass('borderActiveRight');
    }
    if($(this).next().children('a[class *= "Selected"]').length == 0){
      $(this).next().children('a').removeClass('noSpacer');
    }
  });

  if($('ul.navLevel1Container > li.navLevel1Selected').length > 0){
    moveBgNav($('ul.navLevel1Container > li.navLevel1Selected'));
  }
  if($('ul.navLevel1Container > li.navLevel1OpenSelected').length > 0){
    moveBgNav($('ul.navLevel1Container > li.navLevel1OpenSelected'));
  }
  if($('ul.navLevel1Container > li.navLevel1OpenDescendantSelected').length > 0){
    moveBgNav($('ul.navLevel1Container > li.navLevel1OpenDescendantSelected'));
  }

  // Einblenden zweites NavLevel bei Hover
  $('#navigationFullLayer li[class ^= "navLevel1"]').hover(function(){
    var navLevel2Container = $(this).children('div.navLevel2Container');
    $(this).children('div.navLevel2Container').show();
    moveBgNav(navLevel2Container);
    var height = navLevel2Container.height() - 6;
    navLevel2Container.children('.navLevel2Border').height(height+'px');
  },function(){
    $(this).children('div.navLevel2Container').hide();
  })

  // Schnellfinder auf Startseite
  $('#quickfinder').change(function(){
    location.href = $(this).val();
  })
  
  // Fancybox öffnen
  $('a[rel ^= "fancy_"]').fancybox({
    "type": "iframe",
    "scrolling": "no"
  });

})

// Verschieben des Hintergrundbildes der Navigation
function moveBgNav(element){
  var left = element.position().left;
  var top  = element.position().top;
  if(!heightNav){
    heightNav = 200;
  }
  var posBgTop = heightNav + top;
  var bgPos = '-'+left+'px -'+posBgTop+'px';
  element.css('background-position', bgPos);
  var width = element.width();
  if(width+left > 950){
    var move = 950 - width;
    element.css('left', move);
    bgPos = '-'+move+'px -'+posBgTop+'px';
    element.css('background-position', bgPos);
  }
}
