var areaCode = '';

////////////////////////////////////////////////////////////////
//
// @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=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 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();
      $('#navEmbedIframe_' + id).height(frames['navEmbedIframe_' + id].$('#blockContent').height() + 50);
      $('#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 - 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');
        $('#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();
      });
    }
  }
}

jQuery.fn.shadow = function(options){

  var boxOuter        = $('<div style="position:relative" />');
  var boxInner        = $('<div style="background-color:white;" />');

  var boxOuter1       = $('<div style="position:relative;"><div style="height:' + options.top + 'px"><div style="background-image:url(/wGlobalOrtenberg/wGlobal/layout/images/shadows/bg-h.png);background-position:top left;left:0;position:absolute;width:50%;height:' + options.top + 'px"/><div style="background-image:url(/wGlobalOrtenberg/wGlobal/layout/images/shadows/bg-h.png);background-position:top right;right:0;position:absolute;width:50%;height:' + options.top + 'px"/></div></div>');
  var boxOuter2       = $('<div style="background-image:url(/wGlobalOrtenberg/wGlobal/layout/images/shadows/bg-v.png);background-repeat:no-repeat;background-position:right ' + ( -options.top) + 'px;padding-right:' + options.right + 'px"/>');
  var boxOuter3       = $('<div style="position:relative;"><div style="width:100%;height:' + options.bottom + 'px"><div style="background-image:url(/wGlobalOrtenberg/wGlobal/layout/images/shadows/bg-h.png);background-position:bottom left;left:0;position:absolute;width:50%;height:' + options.bottom + 'px"/><div style="background-image:url(/wGlobalOrtenberg/wGlobal/layout/images/shadows/bg-h.png);background-position:bottom right;right:0;position:absolute;width:50%;height:' + options.bottom + 'px"/></div></div>');
  var boxOuter4       = $('<div style="background-image:url(/wGlobalOrtenberg/wGlobal/layout/images/shadows/bg-v.png);background-repeat:no-repeat;background-position:left  ' + ( -options.top) + 'px;padding-left:' + options.left + 'px"/>');

  if($(this).css('marginTop') != '' && $(this).css('marginTop') != 'auto'){
    boxOuter.css('marginTop', String(parseInt($(this).css('marginTop')) - options.top) + 'px');
    $(this).css('marginTop', '0');
  }

  if($(this).css('marginRight') != '' && $(this).css('marginRight') != 'auto'){
    //boxOuter2.css('margin-right', String(parseInt($(this).css('marginRight')) - options.right) + 'px');
    //$(this).css('marginRight', '0');
  }

  if($(this).css('marginLeft') != '' && $(this).css('marginLeft') != 'auto'){
    //boxOuter4.css('margin-left', String(parseInt($(this).css('marginLeft')) - options.left) + 'px');
    //$(this).css('marginLeft', '0');
  }

  if($(this).css('marginBottom') != '' && $(this).css('marginBottom') != 'auto'){
    boxOuter.css('marginBottom', String(parseInt($(this).css('marginBottom')) - options.bottom) + 'px');
    $(this).css('marginBottom', '0');
  }

  $(this).wrap(boxOuter);

  if(options.top > 0){
    boxOuter1.insertBefore($(this));
  }
  if(options.bottom > 0){
    boxOuter3.insertAfter($(this));
  }

  $(this).wrap(boxOuter2);
  $(this).wrap(boxOuter4);
  $(this).wrap(boxInner);


  //if($(this).attr('id') != ''){
  //  var id = $(this).attr('id');
  //  $(this).attr('id', '');
  //  boxOuter.attr('id', id);
  //}
}

jQuery(document).ready(function(){
  if(typeof wInitBrowseEdit == 'function'){
    wInitBrowseEdit();
  }

  var expires = new Date();
  expires.setTime(expires.getTime() + (365 * 86400 * 1000));
  document.cookie = "pb=1;path=/;expires=" + expires.toGMTString();

  //jQuery(".elementBoxSheet").shadow({top: 0, right: 3, bottom: 3, left: 0, color : "#c0c0c0"});
  //jQuery("#blockMain").shadow({top: 3, right: 3, bottom: 3, left: 3, color : "#c0c0c0"});

});
