////////////////////////////////////////////////////////////////
//
// @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 wOpenURL(url)
//
// @desc Öffnet eine Seite in einem neuen Fenster
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenWindowURL(url){
  if(location.pathname.indexOf('/weblication/grid5/') != 0){
		window.open(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 wOpenPrintPopup(url, width, height, name)
//
// @desc Öffnet die Druckvorschau in einem Popup
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenPrintPopup(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=yes,toolbar=yes,resizable=yes,scrollbars=yes,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();
  }
	
	// Toggle der Spacer für NavLevel1 bei Mouseover
  $('ul.navLevel1Container > li').children('a:not([class *= "Selected"])').parent().hover(function(){
    $(this).next().children('a').addClass('noSpacer');
		$(this).next().addClass('noDoubleSpacer');
  }, function(){
    if($(this).next().children('a[class *= "Selected"]').length == 0){
      $(this).next().children('a').removeClass('noSpacer');
    }
		$(this).next().removeClass('noDoubleSpacer');
  });
  
  
  $('#selectDownload').change(function(){
     var id = $(this).val();
     location.href = '?category='+id;
  });
  
  // Standortwahl
  $('#selectStandortInternational').change(function(){
     var countryId = $(this).val();
     var path = window.location.pathname;
     $.ajax({
     url: "/wLayout/wGlobal/scripts/php/ajax.php",
     //dataType: "json",
     data: "type=standort&countryId="+countryId+'&path='+path,
     success: function(html){
       $('.elementStandortwahlIntErgebnisInner').html(html);
       spamSpan();
       /*
       $('.elementStandortwahlIntErgebnisInner .title').html(standort.title);
       $('.elementStandortwahlIntErgebnisInner .title2').html(standort.title2);
       $('.elementStandortwahlIntErgebnisInner .adr1').hide();
       $('.elementStandortwahlIntErgebnisInner .adr2').hide();
       $('.elementStandortwahlIntErgebnisInner .adr3').hide();
       $('.elementStandortwahlIntErgebnisInner .country').hide();
       if(standort.adr1 != ''){
         $('.elementStandortwahlIntErgebnisInner .adr1').css('display', 'block').html(standort.adr1);
       }
       if(standort.adr2 != ''){
         $('.elementStandortwahlIntErgebnisInner .adr2').css('display', 'block').html(standort.adr2);
       }
       if(standort.adr3 != ''){
         $('.elementStandortwahlIntErgebnisInner .adr3').css('display', 'block').html(standort.adr3);
       }
       if(standort.country != ''){
         $('.elementStandortwahlIntErgebnisInner .country').css('display', 'block').html(standort.country);
       }
       $('.elementStandortwahlIntErgebnisInner .telefon').html(standort.telefon);
       $('.elementStandortwahlIntErgebnisInner .fax').html(standort.fax);
       $('.elementStandortwahlIntErgebnisInner .email').html(standort.email);
       */ 
     }
    });
  });
  
  // Standortwahl Kontinent
  $('.selectKontinent').click(function(){
     var path = window.location.pathname;
     var kontinentId = $(this).attr('href').replace(/#/, '');
     $.ajax({
     url: "/wLayout/wGlobal/scripts/php/ajax.php",
     //dataType: "json",
     data: "type=standortKontinent&id="+kontinentId+'&path='+path,
     success: function(standorte){
       $('.elementStandortwahlIntSelect').show();
       $('#selectStandortInternational').html(standorte);
     }
    });
  });
  
  // Hover-Effekt Weltkarte
  $('.contMap').hover(function(){
    $('.element_welt:not(.active)').hide();
    var id = $(this).attr('id');
    $('#IDwelt'+id).show();
  },
  function(){
  });
  $('.elementStandortwahlInt').mouseleave(function(){
    $('.element_welt:not(.active)').hide();
  });
  $('.selectKontinent').click(function(){ 
    $('.element_welt').removeClass('active').hide();
    var id = $(this).parent().prev().attr('id');
    $('#'+id).addClass('active').show(); 
  });
  
  
  // Papierfinder
  $('select.finderSelect').change(function(event, type){
    var sent = false;
    var curSelectId = $(this).attr('id');
    
    var valKategorie = '';
    var valOberflaeche = '';
    var valFarbe = '';
    var valGrammatur = '';
    var valDicke = '';
    var valAnwendung = '';
    
    valKategorie = $('#finderKategorie').val();
    valAnwendung = $('#finderAnwendung').val();

    if(curSelectId == 'finderKategorie' && type != 'sent'){
      $("#finderDicke option[value='']").attr('selected', 'selected');
      $("#finderGrammatur option[value='']").attr('selected', 'selected');
      $("#finderFarbe option[value='']").attr('selected', 'selected');
      $("#finderOberflaeche option[value='']").attr('selected', 'selected');
    }
    
    if(curSelectId == 'finderDicke' || curSelectId == 'finderGrammatur' || curSelectId == 'finderFarbe' || curSelectId == 'finderOberflaeche'){
      valOberflaeche = $('#finderOberflaeche').val();
      if(curSelectId == 'finderOberflaeche'){
        $("#finderDicke option[value='']").attr('selected', 'selected');
        $("#finderGrammatur option[value='']").attr('selected', 'selected');
        $("#finderFarbe option[value='']").attr('selected', 'selected');
      }
    }
    if(curSelectId == 'finderDicke' || curSelectId == 'finderGrammatur' || curSelectId == 'finderFarbe'){
      valFarbe = $('#finderFarbe').val();
      if(curSelectId == 'finderFarbe'){
        $("#finderDicke option[value='']").attr('selected', 'selected');
        $("#finderGrammatur option[value='']").attr('selected', 'selected');
      }
    }
    if(curSelectId == 'finderDicke' || curSelectId == 'finderGrammatur'){
      valGrammatur = $('#finderGrammatur').val();
      if(curSelectId == 'finderGrammatur'){
        $("#finderDicke option[value='']").attr('selected', 'selected');
      }
    }
    if(curSelectId == 'finderDicke'){
      valDicke = $('#finderDicke').val();
    }
    
    if(type == 'sent'){
      valDicke = $('#finderDicke option:selected').val();
      valGrammatur = $('#finderGrammatur option:selected').val();
      valFarbe = $('#finderFarbe option:selected').val();
      valOberflaeche = $('#finderOberflaeche option:selected').val();
    }
    
    
    $.ajax({
     url: "/wLayout/wGlobal/scripts/php/ajax.php",
     dataType: "json",
     data: "type=papierfinder&kategorie="+valKategorie+"&oberflaeche="+valOberflaeche +"&farbe="+valFarbe +"&grammatur="+valGrammatur +"&dicke="+valDicke +"&anwendung="+valAnwendung,
     success: function(response){
       $('#finderOberflaeche').removeClass('visible');
       $('#finderFarbe').removeClass('visible');
       $('#finderGrammatur').removeClass('visible');
       $('#finderDicke').removeClass('visible');
       
       
       // alle gefundenen Kategorien durchlaufen
       if(response){
         $.each(response, function(katName,katItem){
           // Anzeigen der Selectbox
           $('#finder'+katName).addClass('visible');
           
           // Options alle auf disable setzen
           if(katName == 'Anwendung'){
             $("#finderAnwendung option").each(function () {
               if($(this).val()){
                 $(this).attr('disabled', 'disabled');
               }
             });
           }
           if(katName == 'Dicke'){
             $("#finderDicke option").each(function () {
               if($(this).val()){
                 $(this).attr('disabled', 'disabled');
               }
             });
           }
           if(katName == 'Grammatur'){
             $("#finderGrammatur option").each(function () {
               if($(this).val()){
                 $(this).attr('disabled', 'disabled');
               }
             });
           }
           if(katName == 'Farbe'){
             $("#finderFarbe option").each(function () {
               if($(this).val()){
                 $(this).attr('disabled', 'disabled');
               }
             });
           }
           if(katName == 'Oberflaeche'){
             $("#finderOberflaeche option").each(function () {
               if($(this).val()){
                 $(this).attr('disabled', 'disabled');
               }
             });
           }
           if(katName == 'Anwendung'){
             $("#finderAnwendung option").each(function () {
               if($(this).val()){
                 $(this).attr('disabled', 'disabled');
               }
             });
           }
           
           // alle gefundenen Einträge durchlaufen und aktivieren
           $.each(katItem, function(entryName,entryItem){
             $('#finder'+katName).children('option[value = '+entryItem+']').attr('disabled', '');
           });
         });
       }
      
    }
    });
  });
  
  // Fancybox
  $('a[rel ^= "fancy"]').fancybox({
    'titlePosition' 		: 'inside'
  });
  
  $('a[target = "fancy"]').fancybox({
    'titlePosition' 		: 'inside'
  });


  
});



////////////////////////////////////////////////////////////////
//
// Standorte International
//
////////////////////////////////////////////////////////////////





/*
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0 angepasst: display:none/block
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v=='hide')?'none':v; }
    obj.display=v; }
}

function displayStandorteInt(hideID, id, mode) {
	if(hideID == "alleInfoBoxen") {
		var laenderIDs = alleLaenderIDs.split("|");
		for(i=0; i<laenderIDs.length; i++) {
			if(laenderIDs[i] != "" && document.getElementById('IDmap'+laenderIDs[i]+'InfoBox')) {
				MM_showHideLayers('IDmap'+laenderIDs[i]+'InfoBox','','hide');
			}
		}
	} else if(hideID == "alleKontinente") {
		var kontinenteIDs = alleKontinenteIDs.split("|");
		for(i=0; i<kontinenteIDs.length; i++) {
			if(kontinenteIDs[i] != "" && document.getElementById('IDwelt'+kontinenteIDs[i])) {
				MM_showHideLayers('IDwelt'+kontinenteIDs[i],'','hide');
			}
		}
	} else if(hideID == "selektiereInfoBox") {
		var laenderIDs = alleLaenderIDs.split("|");
		for(i=0; i<laenderIDs.length; i++) {
			if('IDmap'+laenderIDs[i] == id) {
				//alert("MOUSEOUT: "+laenderIDs[i]+"\n"+document.getElementById('IDmap'+laenderIDs[i]).style.display);
			}
			if(laenderIDs[i] != "" && document.getElementById('IDmap'+laenderIDs[i])) {
				MM_showHideLayers('IDmap'+laenderIDs[i],'','hide');
				if(document.getElementById('IDmap'+laenderIDs[i]+'InfoBox')) {
					if(document.getElementById('IDmap'+laenderIDs[i]+'InfoBox').style.display == 'block') {
						MM_showHideLayers('IDmap'+laenderIDs[i],'','hide');
					}
				}
			}
		}
		return;
	}
	
	
	//Ende ausblenden, Beginn IDs einblenden
	if(id != "" && mode != "") {
		var alleIDs 	= id.split("|");
		var alleModes = mode.split("|");
		if(alleIDs.length == alleModes.length) {
			for(i=0; i<alleIDs.length; i++) {
				if(alleIDs[i] != "" && document.getElementById(alleIDs[i])) {
					MM_showHideLayers(alleIDs[i],'',alleModes[i]);
				}
			}
		}
	}
}
*/
