////////////////////////////////////////////////////////////////
//
// NKF JS
//
////////////////////////////////////////////////////////////////

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function isEmpty( inputStr ) { if ( null == inputStr || "" == inputStr ) { return true; } return false; }

jQuery(document).ready(function() {
    
        // Fade In Animation wegen IE7/8 Problem mit dem Verstecken der divs nach dem h3(mann sieht noch den Content befor er versteckt wird)
        jQuery("ul#Navigation").fadeIn(200);
        jQuery("#Main").fadeIn(200);
        jQuery("#Footer").fadeIn(200);

								
	// MELDUNG JAVASCRIPT deaktiviert -> wird nur angezeigt wenn Javascript deaktiviert ist und der entsprechende DIV nicht entfernt werden kann.
	jQuery('#jsalert').remove();								
	
	// Content Nav
	jQuery('.nkfContentNav').show();
	
	// Anpassen der Navigation im Content-Bereich Oben
	jQuery('.Selected ~ .nkfContentNav:not(.navBottom)').hide(); // Alle .nkfContentNav auf der gleichen Stufe und nach .Selected ausblenden
	jQuery('.Open ~ .nkfContentNav:not(.navBottom)').hide(); // Alle .nkfContentNav auf der gleichen Stufe wie .Open und unterhalb von .Open ausblenden
	jQuery('.Selected .nkfContentNav:not(.navBottom)').hide(); // Ausblenden der Kinder von .Selected
	
	// Anpassen der Navigation im Content-Bereich Unten
	jQuery('.navBottom').hide(); // Alles ausblenden
	jQuery('.Open:not(.navTop)').show(); // Offene Gruppe wieder einblenden
	jQuery('.Open:not(.navTop) > a').hide(); // Offener Link ausblenden
	jQuery('.Selected ~ .nkfContentNav:not(.navTop)').show(); // Alle .nkfContentNav auf der gleichen Stufe wie .Selected und unterhalb von .Selected einblenden
	jQuery('.Open ~ .nkfContentNav:not(.navTop)').show(); // Alle .nkfContentNav auf der gleichen Stufe wie .Open und unterhalb von .Open einblenden
	jQuery('.Selected .nkfContentNav:not(.navTop)').show(); // Einblenden der Kinder von .Selected
	
	if (jQuery('*').hasClass('Level3')) {
		jQuery('.Level2:not(.Open)').hide(); 
		$('#Navigation .act').clone().appendTo('#nkfContentNavBottom');
		$('#nkfContentNavBottom .act').html($('#backLinkText').html() + " " + $('#Navigation .act ').html());
		$('#nkfContentNavBottom .act').addClass('backToOverView');
	} else {
		
	}
	
	
	// Left Padding des Content anpassen
	var marginLeft = jQuery.trim( jQuery('#navDeep').html() );
	if (marginLeft != '') {
		jQuery('#Content').css('margin-left',marginLeft + "px");
	} 
	
	// Slide Funktion f�r Anw�lte-�bersicht unter den Kernkompetenzen
	jQuery('.pa_attorney_overview').hide();
	jQuery('.slideNavLevel4').each(function() { 
		jQuery(this).click(function() {
			jQuery(this).next().slideToggle(); 
			if (jQuery(this).hasClass('slideNavLevel4act')) {
				jQuery(this).removeClass('slideNavLevel4act');
			} else {
				jQuery(this).addClass('slideNavLevel4act');
			}
		});
	});
	
	// Slide H3
	// blendet alle nachfolgenden elmenten von .elementHeadline aus die ein h3 einthalten
	// ausgenommen sind h3 die die klasse startOpen haben (z.b in der suche) oder den Inhalt Areas of Practice oder Contact (Anwalt-Profil-Seite)
	jQuery('.slide-h3').find('h3').next().hide();
	jQuery('.slide-h3').find('h3.startOpen').next().show();
	jQuery('.slide-h3').find('h3.startOpen').addClass('act');
	
	var i = 0;
	jQuery('.slide-h3').find('h3').each(function() { 

                // die ersten beiden boxen in den anwaltsprofilen werden beim laden der seite geöffnet
                if (jQuery('*').hasClass('attorney_contact')) {
			if (i == 0 || i == 1) {
				jQuery(this).next().show();
				jQuery(this).addClass('act');
			}
		}
		
		$(this).click(function() {
			
			$(this).siblings('.act').next().slideToggle();
			$(this).siblings('.act').removeClass('act');
			
			$(this).next().slideToggle();
			if ($(this).hasClass('act')) {
				$(this).removeClass('act');
			} else {
				$(this).addClass('act');
			}
		});
		
		i++;
	});
	
	
	
	jQuery('.slide-h3 h3').hover(
	  function () { jQuery(this).addClass('over'); },
	  function () { jQuery(this).removeClass('over'); }
	);	
	
	jQuery('.slide-h3 .attorney_name h3').hover(
	  function () { jQuery(this).addClass('over');  },
	  function () { jQuery(this).removeClass('over'); }
	);
	
	jQuery('#ListSearchResult li').hover(
	  function () { jQuery(this).addClass('over'); },
	  function () { jQuery(this).removeClass('over'); }
	);	
	
	// �ffnet alle Slide H3's in der printview
	jQuery('.printview').find('.slide-h3').find('h3').next().show();
	
	// versteckt den Balken Publikation wen keine Publikation vorhanden ist
	jQuery('#noPublication').parent().hide();
	jQuery('#noPublication').parent().prev().hide();
	
	// erste news oeffnen wenn open im query string
	if (getUrlVars()["open"] == 'faelle') {
		jQuery('.newsFaelle .toggle:first-of-type ').show();
		jQuery('.newsFaelle h3:first-of-type').addClass('act');
	}
	if (getUrlVars()["open"] == 'info') {
		jQuery('.newsInfo .toggle:first-of-type ').show();
		jQuery('.newsInfo h3:first-of-type').addClass('act');
	}
	
	// oeffnen der kernkompetenzen - wenn kopen im query-string
	if (!isEmpty(getUrlVars()["kopen"])) {
		jQuery('#kkomp'+getUrlVars()["kopen"]).next().show();
		jQuery('#kkomp'+getUrlVars()["kopen"]).addClass('act');
	}
	
	// ScrollTop -> show MicroNavigation
	jQuery(window).scroll(function(){
		if (jQuery('html').scrollTop() > 150 || jQuery('body').scrollTop() > 100) {
			jQuery('#MicroNav').fadeIn("slow");
		} else {
			jQuery('#MicroNav').fadeOut("slow");
		}
	});
        
	// Toggle MicroNavigation
	jQuery("#OpenCloseBtn").toggle(
		function () {
			jQuery(this).css("background-image", "url(/wGlobal-nkf2/wGlobal/layout/images/layout/arrow-down.png)");
			jQuery(this).css("background-position", "center 3px");
			jQuery('#MicroNavInner').fadeOut('slow');
		},
		function () {
			jQuery(this).css("background-image", "");
			jQuery(this).css("background-position", "");
			jQuery('#MicroNavInner').fadeIn('slow');
		}
	);    
	if (jQuery('*').hasClass('slideshow')) {
		jQuery('.slideshow').cycle({
			height: '295',
			fx:	'fade',
			speed: 2000,
			timeout: 10000
		});	
	}

	// init fancybox - publikationen die kein pdf haben
    $('.publication a[href$=".html"]').fancybox({
		'width'     : 930,
        'height'    : 550,
		'speedIn'	: 600,
		'speedOut'  : 200,
        'autoDimensions' : false,
        'type': 'iframe'
	});	
	
	// init fancybox - publikationen die kein pdf haben
    $('.publication a[href$=".HTML"]').fancybox({
		'width'     : 930,
        'height'    : 550,
		'speedIn'	: 600,
		'speedOut'  : 200,
        'autoDimensions' : false,
        'type': 'iframe'
	});		
	
});

function setSValueFocus(value) {
	
	if (value == 'Suchen'){ $('#Searchfield').val('') }
	if (value == 'Suchen'){ $('#PublicationSearchfield').val('') }
	
}

function setSValueFocusEn(value) {

	if (value == 'Search'){ $('#Searchfield').val('') }
	if (value == 'Search'){ $('#PublicationSearchfield').val('') }

}

function setSValueBlurEn(value) {
	
	if (value == ''){ $('#Searchfield').val('Search') }
	if (value == ''){ $('#PublicationSearchfield').val('Search') }
	
}

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

function wOpenURL(url){
  if(location.pathname.indexOf('/weblication/grid5/') != 0){
    top.location.href = url;
  }
}

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

function wOpenPopupURL(url, width, height, name){

  var name = name || '';
  
  if(width == '' || width == ' ' || typeof(width) == 'undefined'){
    width = 786;
  }
  if(height == '' || height == ' ' || typeof(height) == 'undefined'){
    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);
  alert(name);
  window.open(url, name, 'top='+posTop+',left='+posLeft+',width='+width+',height='+height+',' + options);
}

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

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

////////////////////////////////////////////////////////////////
//
// @method void wEmbedNavpoint(id, url, mode)
//
// @desc Öffnet einen eingebetteten Navigationspunkt
//
// @return void
//
////////////////////////////////////////////////////////////////

function wEmbedNavpoint(id, url, mode){

  if(typeof wEditor == 'object'){
    return false;
  }

  if($('#navpoint_' + id).data('hasLoaded') == '1'){             
    $('#navEmbed_' + id).show();
  }
  else{
    if(mode == 'embed'){
      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('left', String(parseInt($('#navEmbed_' + id).css('left')) - (layerRight - blockMainRight)) + '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();
          });          
        }
      });
    }
    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.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 - 6) + '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');
      }
      
      jQuery('#navpoint_' + id).mouseout(function(){
        $('#navEmbed_' + id).hide();
      });
      
      jQuery('#navEmbed_' + id).mouseover(function(){
        $('#navEmbed_' + id).show();
      });
      
      jQuery('#navEmbed_' + id).mouseout(function(){
        $('#navEmbed_' + id).hide();
      });  
      
      //jQuery('.blockContentEmbed').attr('id', 'blockContent');  
      //jQuery('.blockContentEmbedInner').attr('id', 'blockContentInner');        
    }  
  }
}

////////////////////////////////////////////////////////////////
//
// @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{
      }  
    }
  }
}

////////////////////////////////////////////////////////////////
//
// Initialisieren der Seite
//
////////////////////////////////////////////////////////////////

jQuery(document).ready(function(){
  if(typeof wInitBrowseEdit == 'function'){
    wInitBrowseEdit();
  }
});

