/*
////////////////////////////////////////////////////////////////
//
// Allgemeine JavaScript Funktionen
//
// Weblication CMS Strukturdesign Version 5.1.0.0
// erstellt durch Scholl Communications AG, 77694 Kehl, www.scholl.de
// erstellt mit Weblication Content Management Server, www.weblication.de
//
////////////////////////////////////////////////////////////////
*/



////////////////////////////////////////////////////////////////
//
// @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, 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=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('|', '@');
}

function showSubLayer(obj){

  var curElement  = obj;
  var tmpID       = curElement.attr('id');
  var id          = tmpID.substr(tmpID.indexOf('_')+1);
  var url         = curElement.attr('rel');

  url = url.indexOf('?') == -1 ? url + '?viewmode=embed' : url + '&viewmode=embed';
  url = url.replace('[URL]', location.pathname + location.search);

  var embedSrc            = url;
  var embedContainer      = $('div.embedContainer');

  curElement.unbind('click');

  curElement.bind('click',function(e){
    e.preventDefault();
    hideSubLayer($(this));
  });


  embedContainerExists = $('div.embedContainerInner');
  if (embedContainerExists.length > 0){
    contID = embedContainerExists.attr('id');
    contID = contID.substr(contID.indexOf('_')+1);
    embedContainer.slideUp('fast');
    embedContainerExists.slideUp('fast');

    if ($.browser.msie && $.browser.version == '7.0'){
      embedContainer.empty();
    } else {
      window.setTimeout(function(){
        embedContainer.empty();
      },100);
    }

    otherCallerNavPoint = $('a#navpoint_'+contID);

    contUrl = otherCallerNavPoint.attr('rel');
    otherCallerNavPoint.unbind('click');

    otherCallerNavPoint.click(function(e){
      e.preventDefault();
      showSubLayer($(this));
    });

  }


  $.get(embedSrc,
   function(result){
      result = result.replace(/\n/g, '_wNL-').replace(/\s+id="[^"]+"/gim, '').replace(/_wNL-/g, "\n");

      embedContainer.empty();

      var innerHTMLStr = '<div class="embedContainerInner" id="embedContainerInner_'+id+'"></div>';
      embedContainer.html(innerHTMLStr);

      var embedContainerInnerObj = $('div#embedContainerInner_'+id);

      $(embedContainerInnerObj).append(result);

      if ($.browser.msie && $.browser.version == '7.0'){
        slideParam = 'fast';
      } else {
        slideParam = '';
      }

      embedContainer.slideDown(slideParam);
      embedContainerInnerObj.slideDown();

   });
}

function hideSubLayer(obj){

  var curElement  = obj;
  var tmpID       = curElement.attr('id');
  var id          = tmpID.substr(tmpID.indexOf('_')+1);

  var embedContainer          = $('div.embedContainer');
  var embedContainerInnerObj  = $('div#embedContainerInner_'+id);
  var callerNavPoint          = $('a#navpoint_'+id);

  embedContainer.slideUp('fast');
  embedContainerInnerObj.slideUp('fast');

  callerNavPoint.unbind('click');

  callerNavPoint.click(function(e){
    e.preventDefault();
    showSubLayer($(this));
  });

  if ($.browser.msie && $.browser.version == '7.0'){
      embedContainer.empty();
    } else {
      window.setTimeout(function(){
        embedContainer.empty();
      },300);
    }

}


////////////////////////////////////////////////////////////////
//
// @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 setViewmode(viewmode)
//
// @desc Setzt den Ansichtsmodus eines Links
//
// @param viewmode Ansichtmodus
//
// @return void
//
////////////////////////////////////////////////////////////////

jQuery.fn.setViewmode = function(viewmode){

  jQuery.each($(this), function(){
    var hrefNew = this.getAttribute('href');
    var anchor  = '';
    if(/#[^#]+$/.test(hrefNew)){
      anchor  = hrefNew.replace(/.*#/, '');
      hrefNew = hrefNew.replace(/#.*/, '');
    }

    hrefNew = hrefNew.indexOf('?') != -1 ? hrefNew + '&viewmode=' + viewmode: hrefNew + '?viewmode=' + viewmode;

    if(anchor != ''){
      hrefNew += '#' + anchor;
    }

    this.setAttribute('href', hrefNew);
  });
}

////////////////////////////////////////////////////////////////
//
// @method void addAnalytics()
//
// @desc Überwacht Links mit Google Analytics
//
// @return void
//
////////////////////////////////////////////////////////////////

jQuery.fn.addAnalytics = function(){

  jQuery.each($(this), function(){
    var href= this.getAttribute('href');
    this.setAttribute('onclick', 'pageTracker._trackPageview(\'' + href + '\');');
  });
}

////////////////////////////////////////////////////////////////
//
// Initialisieren der Seite
//
////////////////////////////////////////////////////////////////

jQuery(document).ready(function(){
  if(typeof wInitBrowseEdit == 'function'){
    wInitBrowseEdit();
  }
  if(typeof wEditor == 'undefined'){
    jQuery("a.box").fancybox({"type": "iframe", "width": 560, "height": '80%', "titlePosition": "inside", "transitionIn" : "none", "transitionOut" : "none", "hideOnContentClick": true, "zoomOpacity": true, "zoomSpeedChange": 200, "zoomSpeedIn": 400, "zoomSpeedOut": 400, "overlayShow": false, "overlayOpacity": 1});
    jQuery("a.box").setViewmode('blank');
    if(typeof pageTracker != 'undefined'){
      jQuery("a.pdf, a.download").addAnalytics();
    }
  }

  $('a.navLevel1[rel!=""]').each(function(e){

    $(this).bind('click', function(evt){
      evt.preventDefault();
      showSubLayer($(this));
    });

  });

  if ($.browser.msie && $.browser.version == '7.0'){

    var widthElements = new Array(61,131,103,100,126,94,67);

    $('div#navigationMeta ul li').each(function(i){
      elementWidth = widthElements[i];
      $(this).css('width',elementWidth);
    });
  }


});

