////////////////////////////////////////////////////////////////
//
// Beschreibung: Zeigt die Druckansicht an
//
////////////////////////////////////////////////////////////////

function wShowPrintpreview(){  

  var url = location.href;
  var anchor = location.hash;

  url = url.replace(anchor, '');
  
  if(url.indexOf('?') >= 0){
    url += '&viewmode=print';
  }
  else{
    url += '?viewmode=print';    
  }
  
  //openWindow(url, 'wPrintpreview', 'width=768,height=520,scrollbars=yes');
  wOpenPopupURL(url, 1024, 768);
}



////////////////////////////////////////////////////////////////
//
// @method void wOpenURL(url)
//
// @desc Öffnet eine Seite
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenURL(url){
  if(location.pathname.indexOf('/weblication/grid5/') != 0){
    top.location.href = 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=yes,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 wSendMailNospam(data)
//
// @desc Öffnet eine E-Mail zum Versenden
//
// @return void
//
////////////////////////////////////////////////////////////////

function wSendMailNospam(data){
  pageTracker._trackPageview('/de/mailto/' + data.replace('|', '@'));
  location.href = 'mailto:' + data.replace('|', '@');
}

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');        
    }  
  }
}


////////////////////////////////////////////////////////////////
//
// Beschreibung: Strings auf 64Bit-Basis codieren
//
////////////////////////////////////////////////////////////////
function encode64(inp){
    
    inp = encodeURIComponent(inp);
    // alert('vor:'+inp);
    inp = inp.replace(/%2520/g, " ");
    // alert('nach:'+inp);
    var key="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var chr1,chr2,chr3,enc3,enc4,i=0,out="";
    while(i<inp.length){
        chr1=inp.charCodeAt(i++);if(chr1>127) chr1=88;
        chr2=inp.charCodeAt(i++);if(chr2>127) chr2=88;
        chr3=inp.charCodeAt(i++);if(chr3>127) chr3=88;
        if(isNaN(chr3)) {enc4=64;chr3=0;} else enc4=chr3&63
        if(isNaN(chr2)) {enc3=64;chr2=0;} else enc3=((chr2<<2)|(chr3>>6))&63 
        out+=key.charAt((chr1>>2)&63)+key.charAt(((chr1<<4)|(chr2>>4))&63)+key.charAt(enc3)+key.charAt(enc4);
    }

    return encodeURIComponent(out);
}


////////////////////////////////////////////////////////////////
//
// Beschreibung: ließt ein bestimmtes Cookie aus.
//
////////////////////////////////////////////////////////////////
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


$(document).ready(function() {
	
	jQuery('a[rel^=fancy],a[target^=fancy]').fancybox({
	  'titleShow': false,
    'hideOnContentClick': false,
    'overlayShow':true,
    'overlayOpacity': 0.5,
    'centerOnScroll': false,
    'padding': 0,
    'width': 410,
    'height': 469,
    'scrolling': 'no',
    'type': 'iframe',
    'showCloseButton': false
  });
  
  jQuery('a[rel^=fancyContent],a[target^=fancyContent]').fancybox({
    'titleShow': false,
    'hideOnContentClick': false,
    'overlayShow':true,
    'overlayOpacity': 0.5,
    'centerOnScroll': false,
    'padding': 0,
    'width': 410,
    'scrolling': 'no',
    'showCloseButton': false
  });

  jQuery('a[target^=languageSelector]').click(function(e){ 
    e.preventDefault();
    var url = document.URL;

      $.ajax({
        type: "GET",
        data: "url="+url,
        url: '/global/wGlobal/scripts/php/getLanguageFile.php',
        success: function(result){
         location.href = result;
        }
 
    });

  });
  
  
  $('a.googleIntern').click(function(e) {
							 
    var url    = jQuery(this).attr('href');
    var target = jQuery(this).attr('target');
 	
   	var go1 = readCookie('__utma');
   	if (go1) {
   		url = url+"&go1="+encode64(go1);
   	}	
   	var go2 = readCookie('__utmb');
   	if (go2) {
   		url = url+"&go2="+encode64(go2);
   	}	
   	var go3 = readCookie('__utmc');
   	if (go3) {
   		url = url+"&go3="+encode64(go3);
   	}	
   	var go4 = readCookie('__utmz');
   	if (go4) {
   		url = url+"&go4="+encode64(go4);
   	}

    if(target != '_blank'){
      pageTracker._link(url);
    }
    else {
      pageTracker._getLinkerUrl(url, target);
    }

 //alert('domainwechsel:'+url);
 	  window.open(url);
     
     return false;
  });
  
  $('a.googleExtern').click(function(e) {
    
    var url = jQuery(this).attr('href');
    var queryStr     = url.replace(/^[^\?]+\??/,'');
    url     = url.replace('http://', '');
    url     = url.replace('?'+queryStr, '');    
    var parts = url.split('.');
    if (parts[parts.length - 1].length == 2) {
      var urlExtern = parts.slice(0, 3).join('-');
    } else {
      var urlExtern = parts.slice(0, 2).join('-');
    }
 //alert('/de/outbound/'+urlExtern);
    pageTracker._trackPageview('/de/outbound/'+urlExtern);
    return true;
  });
  
  
  $('a.googleMail').click(function(e) {
    var url = jQuery(this).attr('href');
    // Falls der Link als Spam-freier E-Mail-Link definiert ist steht nur '#' im href, dann greift die Funktion wSendMailNospam() 
    if (url != '#') {
      var email =  url.split(":");
      pageTracker._trackPageview('/de/mailto/'+ email[1]);
    }
    return true;
  });  
  
  $('a.pdf').click(function(e) {
    var url = jQuery(this).attr('href');
      pageTracker._trackPageview(url);
    return true;
  });
  
});
