/**
 * @author Sascha Quasthoff
 */


if (typeof ps == 'undefined') {

  var ps = {



  /**
   * Der Pfad zum Root-Verzeichnis der Webseite
   */
  root_path : '/',
  to_root   : '',



  /**
   * Startet die Verarbeitung der Seite
   */
  InitPage : function () {

    if (typeof layout == 'object') return;

    self.location.pathname.sub(ps.root_path,'').scan (/\//, function () { ps.to_root += '../' });

    if (!!$$('.print')[0])
        ps.Print();

    if (!!$$('.seite_empfehlen')[0])
        ps.Empfehlen();

    // do we have an image list?
    if (!!$$(ps.Animation.imageListSelector)[0]) {
      ps.Animation.initialize();
    }

    this.galleryInit();
    this.lightboxInit();
    if (typeof this.ShoppingCart != 'undefined') this.ShoppingCart.initialize();
    if (typeof this.Fairplay != 'undefined') this.Fairplay.Init();

    if (!!$$('.bestenliste')[0]) this.Bestenliste.initialize();

    if ( $('highlight_gallery') ) this.HighlightGallery.initialize();

    this.fixIE();

  },



  /**
   * Blendet eine Meldung ein.
   */
  showMessage : function (text, clss, fx) {

    if (!$('message')) return;

    if (!!text && text != '') {
      if (fx == 'fade') {
        new Effect.Opacity('message', {
          duration : 4.0, from : 1.0, to : 0,
          beforeStart : function () { $('message').show().addClassName(clss).update(text); },
          afterFinish : function () { $('message').hide().removeClassName(clss).update(''); }
        } );
      } else {
        $('message').show().addClassName(clss).update(text);
      }
    } else {
      $('message').hide().removeClassName(clss).update('');
    }

  },



  /**
   * Print-Funktion
   */
  Print : function () {

    $$('.print').each (
      function (n) {
        Event.observe( n, 'click', function () { window.print(); } );
      }
    );

  },



  /**
   *
   */
  Empfehlen : function () {

    $$('.seite_empfehlen').each (
      function (n) {
        var my_url = String(self.location);
        n.setAttribute('href', n.readAttribute('href').replace('###url###', my_url));
      }
    );

  },



  /**
   *
   */
  galleryInit : function () {

    if (!$$('.picture.gallery')[0]) return;

    // welche Sprache haben wir?
    if ($$('html')[0].readAttribute('xml:lang') == 'de') {
      var tab_menu = '<div id="tab_menu_frame"><p id="tab_menu_text">Die schönsten Bilder</p><ul id="tab_menu">';
    } else if ($$('html')[0].readAttribute('xml:lang') == 'nl') {
      var tab_menu = '<div id="tab_menu_frame"><p id="tab_menu_text">de mooiste foto´s</p><ul id="tab_menu">';
    }

    $$('.picture.gallery').each (
        function (n, i) {
            n.id = 'pic_' + (i+1);
            tab_menu += '<li><a href="#" id="show_pic_' + (i+1) + '">' + (i+1) + '</a></li>';
            n.setStyle ( {
                position: 'absolute',
                top: '0px',
                left: '0px'
            } );
            var inner = n.innerHTML;
            var imgLow = n.down('img').readAttribute('src');
            var imgHi = imgLow.substring( 0, imgLow.lastIndexOf('.') )+'_hi'+imgLow.substring( imgLow.lastIndexOf('.') );
            var title = n.down('span').innerHTML;
            var lupe = '<img class="lupe" src="' + to_root + 'grafik/zoom_lupe.gif" alt="Lupe" />';
            n.update('<a href="' + imgHi + '" rel="lightbox[Galerie]" title="' + title + '">' + inner + lupe + '</a>');
        }
    );

    tab_menu += '</ul></div>';

    new Insertion.Before (
        $$('.picture.gallery')[0],
        tab_menu
    );

    $('pictures').setStyle ( {
        position: 'relative'
    } );

    if (!!$$('.picture.gallery')[$$('.picture.gallery').length-1].next('div')) {
      $$('.picture.gallery')[$$('.picture.gallery').length-1].next('div').setStyle ( {
        //cssFloat: 'left',
        marginTop: '217px'
      } );
    }

    $$('#tab_menu a').each (
      function (n, i) {
        n.removeAttribute('href');
        Event.observe(
          n, 'click',
          function () { ps.showPic(n.id); }
        );
        Event.observe(
          n, 'mouseover',
          function () { n.setStyle ( { cursor: 'pointer' } ); }
        );
        Event.observe(
          n, 'mouseout',
          function () { n.setStyle ( { cursor: 'auto' } ); }
        );
      }
    );
    ps.showPic('show_pic_1');

  },



  /**
   *
   */
  lightboxInit : function() {

    $$('a').each ( function (n) {
      if ( n.readAttribute('href') == '#zoom' ) {
        var img = n.down('img');
        var imgLow = img.readAttribute('src');
        var imgAlt = img.readAttribute('alt');

        n.setAttribute( 'href', imgLow.substring( 0, imgLow.lastIndexOf('.') )+'_hi'+imgLow.substring( imgLow.lastIndexOf('.') ) );
        n.hasClassName('aktuelle_angebote') ? n.setAttribute( 'rel', 'lightbox[Angebote]' ) : n.setAttribute( 'rel', 'lightbox' );
        n.setAttribute( 'title', ( imgAlt.length ? imgAlt : 'Bild zoomen' ) );
      }
    } );

    // fire it up!
    if (!!window.initLightbox)
      initLightbox();

  },



  /**
   *
   * @param {Object} i
   */
  showPic : function (i) {

      $$('.picture.gallery').each (
          function (n) {
              if (i == 'show_'+n.id) {
                  new Effect.Opacity ( n, {
                      duration: 0.25,
                      to: 1
                  } );
                  n.setStyle ( {
                      zIndex: 1
                  } );
              } else {
                  n.setStyle ( {
                      opacity: 0,
                      zIndex: 0
                  } );
              }
          }
      );

  },



  /**
   *
   */
  fixIE : function () {

      if (!!window.ActiveXObject && !window.XMLHttpRequest) {
        $$('img.png_fix').each( function (n) {
          n.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+n.src+"')";
          n.src = ps.to_root+'grafik/blank.gif';
        });
      }

  }

  }

}

ps.OnlineAnfrage = {
    Hotelname : '',

    open : function (to_root, pe_id) {
      var link = to_root + 'online_anfrage.html?pe_id=' + pe_id;
      if(this.Hotelname != '') link+= '&hotelname=' + this.Hotelname;
      document.location.href = encodeURI(link);
    },

    setName : function (Hotelname) {
      this.Hotelname = Hotelname;
    }
}


ps.Bestenliste = {

    initialize : function () {

        if ( !$$('.bestenliste')[0] ) return;

        $$('.bestenliste h2').each( function (n, i) {
            if ( typeof $(n.className) == 'undefined' ) return;
            Element.observe(n, 'mouseover', function () { n.setStyle( {'cursor' : 'pointer'} ); });
            Element.observe(n, 'mouseout', function () { n.setStyle( {'cursor' : 'default'} ); });
            Element.observe(n, 'click', function () {
                Effect.BlindUp($$('.bestenliste .visible')[0]); // ausblenden
                $$('.bestenliste .visible')[0].removeClassName('visible'); // ausblenden
                Effect.BlindDown(n.className); // einblenden
                $(n.className).addClassName('visible');
            });
            if (i != 0) Effect.BlindUp(n.className);
            if (i == 0) $(n.className).addClassName('visible');
        });

    }

}



/**
 *
 */
ps.Animation = {

    imageListSelector : '#header_pix img',
    imageListTop      : '3px',
    imageListToH      : '0px',
    imageLastPos      : -1,


    /**
     * startet die Animation der Kopf-Bilder, blendet alle Bilder aus und 3 Bilder wieder ein.
     */
    initialize : function () {

        this.imageList = $$(this.imageListSelector);

        // first set opacity to null at startup...
        this.imageList.each ( (function (n) {
            n.setStyle ( {
                top: this.imageListToH,
                left: '0px',
                opacity: 0
            } );
            n.pos = -1;
        }).bind(this) );

        var list = this.getHiddenImages(3);
        this.imageList.each (
          function (n, i) {
            var pos = list.indexOf(i);
            if (pos >= 0) {
              // this image is in list - show it
              new Effect.Opacity ( n, {
                  duration: 1.5,
                  to: 1,
                  beforeStart: function () {
                      n.setStyle( {
                          top: ps.Animation.imageListTop,
                          left: (pos*83) + 'px'
                      } );
                      n.pos = pos;
                  }
              } );
            }
          }
        );

        new PeriodicalExecuter(this.animateImages, 3);

    },



    /**
     * Liefert (ein) nicht angezeigte(s) Bilder(er) zurück.
     * @param {Integer} num : Die Anzahl der Bilder
     * @return (Array) list : Die Bilder
     */
    getHiddenImages : function (num) {

        var list = [ ];
        var max = this.imageList.length;
        if (max <= num) { return; }

        for (var i=0; i<num; i++) {
            do {
                var a = Math.round(max * Math.random());
            } while (
                a < 0 ||
                a > (max-1) ||
                list.indexOf(a) >- 1 ||                     // ... should not be in list already
                this.imageList[a].style.top == this.imageListTop  // ... should not be shown yet
            );
            list[i] = a;
        }
        return list;

    },



    /**
     *
     * @param {Object} num :
     * @param (Integer) pos :
     */
    getImagePos : function (num) {

        do {
          var pos = Math.round( num * Math.random() );
        } while (
            pos < 0 ||
            pos > (num-1) ||
            pos == this.imageLastPos
        );
        this.imageLastPos = pos;
        return pos;

    },



    /**
     * Die eigentliche Animation, das Einblenden eines neuen Bildes
     */
    animateImages : function () {

        var list = ps.Animation.getHiddenImages(1);
        var pos = ps.Animation.getImagePos(3);
        ps.Animation.imageList.each (
            function (n, i) {
                // fade out and hide visible image
                if (n.pos == pos) {
                    new Effect.Opacity ( n, {
                        duration: 0.75,
                        to: 0,
                        afterFinish: function () {
                            n.setStyle ( {
                                top: ps.Animation.imageListToH,
                                left: '0px'
                            } );
                            n.pos = -1;
                        }
                    } );
                }

                // fade in and hide visible image
                if (i == list[0]) {
                    new Effect.Opacity ( n, {
                        duration: 1.5,
                        to: 1,
                        beforeStart: function () {
                            n.setStyle ( {
                                top: ps.Animation.imageListTop,
                                left: (pos*83)+'px'
                            } );
                            n.pos = pos;
                        }
                    } );
                }
            }
        );

    }

};




/**
 * Die Interaktion für die Highlight-Galerie auf der Startseite
 */
ps.HighlightGallery = {

    // Start
    initialize : function () {

        if ( !$('highlight_gallery') ) return;

        // ein Bild zurück
        Element.observe( $$('#highlight_gallery #previous')[0], 'mouseover', this.mouseOver );
        Element.observe( $$('#highlight_gallery #previous')[0], 'mouseout', this.mouseOut );
        Element.observe( $$('#highlight_gallery #previous')[0], 'click', this.previousImage );

        // ein Bild weiter
        Element.observe( $$('#highlight_gallery #next')[0], 'mouseover', this.mouseOver );
        Element.observe( $$('#highlight_gallery #next')[0], 'mouseout', this.mouseOut );
        Element.observe( $$('#highlight_gallery #next')[0], 'click', this.nextImage );

        var breite = $$('#highlight_gallery li').size();
        $$('#highlight_gallery ul')[0].setStyle({ width: breite * 322+'px' });

    },

    // MouseOver-Zustand
    mouseOver : function (evt) {
        var me = evt.target;
        me.setStyle({ cursor: 'pointer' });
    },

    // MouseOut-Zustand
    mouseOut : function (evt) {
        var me = evt.target;
        me.setStyle({ cursor: 'auto' });
    },

    // zeige das vorherige Bild an
    previousImage : function (evt) {
        var pos = parseInt($$('#highlight_gallery var')[0].innerHTML);
        if ( pos == 1 ) $$('#highlight_gallery var')[0].innerHTML = $$('#highlight_gallery li').size();
        else $$('#highlight_gallery var')[0].innerHTML = pos - 1;

        new Effect.Move($$('#highlight_gallery ul')[0], { x: 322, y: 0, mode: 'relative', beforeStart: function () {
            Event.stopObserving( $$('#highlight_gallery #previous')[0], 'click', ps.HighlightGallery.previousImage);
            Event.stopObserving( $$('#highlight_gallery #next')[0], 'click', ps.HighlightGallery.nextImage);

            if ( parseInt($$('#highlight_gallery ul')[0].getStyle('left')) >= 0 ) {
                var last_li = $$('#highlight_gallery li')[$$('#highlight_gallery li').size()-1].remove();
                new Insertion.Top($$('#highlight_gallery ul')[0], last_li.inspect() + last_li.innerHTML + '</li>');
                $$('#highlight_gallery ul')[0].setStyle({ left: '-322px' });
            }
        },
        afterFinish: function () {
            Element.observe( $$('#highlight_gallery #previous')[0], 'click', ps.HighlightGallery.previousImage );
            Element.observe( $$('#highlight_gallery #next')[0], 'click', ps.HighlightGallery.nextImage );
        } });
    },

    // zeige das nächste Bild an
    nextImage : function (evt) {
        var pos = parseInt($$('#highlight_gallery var')[0].innerHTML);
        if ( pos == $$('#highlight_gallery li').size() ) $$('#highlight_gallery var')[0].innerHTML = 1;
        else $$('#highlight_gallery var')[0].innerHTML = pos + 1;

        new Effect.Move( $$('#highlight_gallery ul')[0], { x: -322, y: 0, mode: 'relative', beforeStart: function () {
            Event.stopObserving( $$('#highlight_gallery #previous')[0], 'click', ps.HighlightGallery.previousImage);
            Event.stopObserving( $$('#highlight_gallery #next')[0], 'click', ps.HighlightGallery.nextImage);

            if ( parseInt($$('#highlight_gallery ul')[0].getStyle('left')) <= -($$('#highlight_gallery li').size()*322 - 322) ) {
                var last_li = $$('#highlight_gallery li')[0].remove();
                new Insertion.Bottom($$('#highlight_gallery ul')[0], last_li.inspect() + last_li.innerHTML + '</li>');
                $$('#highlight_gallery ul')[0].setStyle({ left: '-' + ( $$('#highlight_gallery li').size()*322 - 644 ) + 'px' });
            }
        },
        afterFinish: function () {
            Element.observe( $$('#highlight_gallery #previous')[0], 'click', ps.HighlightGallery.previousImage );
            Element.observe( $$('#highlight_gallery #next')[0], 'click', ps.HighlightGallery.nextImage );
        } });
    }

}



Event.observe ( window, 'load', function () {
  ps.InitPage();
} );


