/*
////////////////////////////////////////////////////////////////
//
// 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 wOpenPrintPreview(url)
//
// @desc Öffnet die aktuelle Seite in der Druckvorschau
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenPrintPreview(width, height, name){

  var name = name || 'printpreview';

  var url = location.href.indexOf('?') == -1 ? location.href + '?viewmode=print' : location.href + '&viewmode=print';

  wOpenPopupURL(url, width, height, name);
}

////////////////////////////////////////////////////////////////
//
// @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('|', '@');
}

////////////////////////////////////////////////////////////////
//
// @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() - 2;

      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{
      }
    }
  }
}

////////////////////////////////////////////////////////////////
//
// @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;
    }
    if(this.innerHTML.indexOf('<img ') != -1){
      this.style.background  = 'none';
      this.style.paddingLeft = 'none';
    }
    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
//
////////////////////////////////////////////////////////////////

function homeAnimation(li) {
         jQuery(li).addClass('hover');

         if( jQuery(li).hasClass('last') ) {
            jQuery(li).find('img').fadeIn(5000, function() {
               var newli = "#homeanimation ul li:first";
               jQuery(li).removeClass('hover');
               jQuery(li).find('img').fadeOut(5000);
               homeAnimation(newli);
            });
         } else {
            jQuery(li).find('img').fadeIn(5000, function() {
               var newli = jQuery(li).next('li');
               jQuery(li).removeClass('hover');
               jQuery(li).find('img').fadeOut(5000);
               homeAnimation(newli);
            });
         }

}



function setTime() {

}


function noWeekendsOrHolidays(date) {
  var noWeekend = jQuery.datepicker.noWeekends(date);
  return noWeekend;
}

function noSunday(date) {
  var day = date.getDay();
  return [(day > 0 ), ''];
}

/**
*-----------------------------------------------------------------------------
* DOCUMENT_READY : START
*-----------------------------------------------------------------------------
**/
jQuery(document).ready( function() {



   /**
   * Settings for the animation of the mainpage
   **/
   jQuery('#homeanimation ul li img').hide();
   jQuery('#homeanimation ul li:hover').stop();
   homeAnimation('#homeanimation ul li.black:first');



   /**
   * Settings for the contactform 'Terminvereinbarung'
   **/
   


   // Validation starts here
   // ------------------------------------------------------

   jQuery('#contactform-step1, #contactform-step2').submit(function() {
      if( jQuery('label.errormsg').length > 0 ) {
        return false;
      }
   });

   jQuery('input[name="date|od"], input[name="escort|o"], input[name="firstname|o"], input[name="lastname|o"], input[name="email|oe"], input[name="telefon|on"], input[name="clothsize|o"],  input[name="bodysize|o"], textarea[name="look|o"]').change( function() {
     var val = jQuery.trim(jQuery(this).val());
      if(val != '') {
        jQuery(this).removeClass('error');
        jQuery(this).next('label.errormsg').remove();
      } else {
        jQuery(this).next('label.errormsg').remove();
        jQuery(this).addClass('error');
        jQuery(this).after('<label class="errormsg">Bitte füllen Sie dieses Feld aus!</label>');
      }
   });



   jQuery('input[name="alternate_date|od"]').change( function() {

  if( jQuery(this).val() == jQuery('input[name="datewish|od"]').val() ) {
    jQuery(this).removeClass('error');
    jQuery('input[name="alternate_date|od"] ~ label.errormsg:first').remove();
    jQuery(this).addClass('error').after('<label class="errormsg">Bitte neues Datum waehlen!</label>');
  } else {

          var day = jQuery(this).val().substr(0,2);
          var month = jQuery(this).val().substr(3,2);
          var year = jQuery(this).val().substr(6,4);
          var d = new Date(year, month-1, day, 0, 0, 0, 0, 0);

var value = setDateOfConsulting(d.getDay(), 0);
jQuery('#alternate_datetime').html(value);

          if (d.getDay() == 0 || d.getDay() == 6 || d.getDay() == 1 ) {
               jQuery(this).removeClass('error');
               jQuery('input[name="alternate_date|od"] ~ label.errormsg:first').remove();
               jQuery(this).addClass('error').after('<label class="errormsg">Dieser Tag ist nicht zugelassen</label>');
         } else {
            jQuery(this).removeClass('error');
               jQuery('input[name="alternate_date|od"] ~ label.errormsg:first').remove();
          }
  }
   });

  jQuery('input[name="datewish|od"]').change( function() {

  if( jQuery(this).val() == jQuery('input[name="alternate_date|od"]').val() ) {
    jQuery(this).removeClass('error');
    jQuery('input[name="datewish|od"] ~ label.errormsg:first').remove();
    jQuery(this).addClass('error').after('<label class="errormsg">Bitte neues Datum w&auml;hlen!</label>');
  } else {

    var day =  jQuery(this).val().substr(0,2);
         var month =  jQuery(this).val().substr(3,2);
         var year =  jQuery(this).val().substr(6,4);
         var d = new Date(year, month-1, day, 0, 0, 0, 0, 0);

var value = setDateOfConsulting(d.getDay(), 0);
jQuery('#datewishtime').html(value);

          if (d.getDay() == 0 || d.getDay() == 1 ) {
            jQuery(this).removeClass('error');
               jQuery('input[name="datewish|od"] ~ label.errormsg:first').remove();
               jQuery(this).addClass('error').after('<label class="errormsg">Dieser Tag ist nicht zugelassen</label>');
          } else {
            jQuery(this).removeClass('error');
               jQuery('input[name="datewish|od"] ~ label.errormsg:first').remove();
          }
  }
   });


   // Validation ends here
   // ------------------------------------------------------

   // Timesettings start
   // ------------------------------------------------------


/*
 * Returns an integer of the day (0 = sunday - 6 = saturday)
 * @param input object
 * @return int date
 */
function getDateOfDay(input) {
  var day   = jQuery(input).val().substr(0,2);
  var month   = jQuery(input).val().substr(3,2);
  var year   = jQuery(input).val().substr(6,4);

  var d     = new Date(year, month-1, day, 0, 0, 0, 0, 0);

  return d;
}

/*
 * Returns the value of the consulting
 * @return string consulting
 */
function getConsultingValue() {
  consulting = jQuery('select[name="consulting"]').val();
  return consulting;
}
/*
 * Sets the timevalues out of the day and the choosen consulting
 * @param integer day
 * @param integer alternate
 * @return string value
 */
function setDateOfConsulting(day, alternate) {
  var value;
  var consulting   = getConsultingValue();
  var wishdate   = new Array();

  if ( consulting == 'Brautmode' ) {
    wishdate['weekdays']   = new Array('10:00','14:00','16:30');
    wishdate['saturdays']   = new Array('09:00','12:00','15:00');
  } else {
    wishdate['weekdays']  = new Array('10:00','11:30','14:00','15:30','17:00');
    wishdate['saturdays']  = new Array('09:00','10:30','12:00','13:30','15:00','16:30','18:00');
  }

  if( day == 6 && alternate == 0) {
    var times = wishdate['saturdays'];
  } else if ( day != 6 ) {
    var times = wishdate['weekdays'];
  }

  if( times ) {
    for( var i = 0; i<times.length; i++ ) {
      value += '<option value='+ times[i] +'>'+ times[i] +' Uhr</option>';
    }
  }
  return value;
}


jQuery('select[name="consulting"]').change(function() {
  var value = setDateOfConsulting(1, 0);
  jQuery('#datewishtime, #alternate_datetime').html(value);
  jQuery('#alternate_date, #datewish').val('');
});



   // Timesettings ends
   // ------------------------------------------------------


   jQuery('input[name="date|od"]').datepicker({
      minDate: +0,
      firstDay: 1,
      changeMonth: true,
      changeYear: true,
      showWeek: true,
      dateFormat: 'dd.mm.yy',
      dayNamesMin: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
      monthNamesShort: ['Jan','Feb','Mar','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez']
   });

   function noSunMonDay(date) {
      var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
      var closedDays = [[Sunday], [Monday]];
      for (var i = 0; i < closedDays.length; i++) {
          if (day == closedDays[i][0]) {
              return [false];
          }
      }
      return [true];
   }
   function noSunSatMonDay(date) {
      var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
      var closedDays = [[Sunday], [Monday], [Saturday]];
      for (var i = 0; i < closedDays.length; i++) {
          if (day == closedDays[i][0]) {
              return [false];
          }
      }
      return [true];
   }

   jQuery('input[name="datewish|od"]').datepicker({
      beforeShowDay: noSunMonDay,
      firstDay: 1,
      minDate: +0,
      changeMonth: true,
      changeYear: true,
      showWeek: true,
      dateFormat: 'dd.mm.yy',
      dayNamesMin: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
      monthNamesShort: ['Jan','Feb','Mar','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez'],

   });

   jQuery('input[name="alternate_date|od"]').datepicker({
      beforeShowDay: noSunSatMonDay,
      firstDay: 1,
      minDate: +0,
      changeMonth: true,
      changeYear: true,
      showWeek: true,
      dateFormat: 'dd.mm.yy',
      dayNamesMin: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
      monthNamesShort: ['Jan','Feb','Mar','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez']
   });



   /**
   * Settings for guestbook of the website
   **/
   /* jQuery('.gb_image_container').css('height','226px'); */
   jQuery('.mycaroussel').jcarousel();



   /**
   * Settings for sitemap on the mainpage
   **/
   jQuery('div.bottomLast div.col2_33 .navLevel1space').addClass("jcarousel-skin-tango").addClass("jcarousel").jcarousel({
        vertical: true,
        scroll: 1
   });
  /* jQuery('#navigationSitemap .navLevel1space').addClass("jcarousel-skin-tango").addClass("jcarousel").jcarousel({
        vertical: true,
        scroll: 5
   }); */





   /**
   * Rest of the settings of the website
   **/
   jQuery('.accordion').accordion({
       header: 'div.elementHeadline',
       autoHeight: false,
       navigation: true
   });

  if(typeof wInitBrowseEdit == 'function'){
    wInitBrowseEdit();
  }
  if(typeof wEditor == 'undefined'){
    jQuery("a.box").fancybox({"type": "iframe", "width": 640, "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();
    }
  }
});

