/**
 * Flyout-Objekt
 */
var flyout;
var RoroFlyout = function(){

  var script_path = 'http://www.rollingrock.ch/wordpress/wp-content/themes/roro/flyouts/';

  /**
   * Konstruktor
   */
  this.init = function(){
  
    var that = this; // save context
    
    // Klassen zuweisen */
    if (!jQuery('#access .menu').hasClass('shop')){ // nur für Wordpress! Im Shop muss das Menu die Klasse 'shop' haben.
      jQuery('#access .menu').attr('role', 'tablist');
      jQuery('#access .menu > li > a').addClass('tab').attr('role', 'tab');
      jQuery('#menu-item-532 a').addClass('home_link').removeClass('tab').attr('role', 'link'); // Startseite
      jQuery('#flyout').attr('role', 'tabpanel');
    }
    if (jQuery('#access .current-page-ancestor').size() !== 0){
      jQuery('#access .flyout_wrapper').removeClass('hidden').attr('aria-live', 'polite');
    }
    
    /* Klick-Event für Tabs */
    jQuery('#access .menu .tab').click(function(e){
      e.preventDefault();
      e.stopPropagation();
      that.toggle(jQuery(this));
    });
    
    /* Klick-Event für Handle */
    jQuery('#access .flyout_wrapper .flyout_push').click(function(e){
      e.preventDefault();
      e.stopPropagation();
      if (jQuery(this).hasClass('down')){
        that.open(jQuery('#menu-primary > .current-page-ancestor').attr('id'));
      } else {
        that.close();
      }
    });
    
    /* Klick-Event ausserhalb des Flyouts */
    jQuery('body').click(function(e){
      if (jQuery('#flyout').is(':visible')){
        that.close();
      }
    });
    
    /* Klick-Event innerhalb des Flyouts */
    jQuery('#flyout').click(function(e){
      e.stopPropagation();
    });
  
  };
  this.init();
  
  /**
   * Zustand wechseln
   */
  this.toggle = function(obj){
    f = obj.parent().attr('id');
    if (jQuery('#flyout').is(':visible') && obj.hasClass('open')){
      this.close();
    } else {
      this.open(f);
    }
  };
  
  /**
   * Flyout öffnen
   */
  this.open = function(f){
    var that = this; // save context
    jQuery('#access .menu .tab.open').removeClass('open');
    jQuery('#access .menu #' + f + ' .tab').addClass('open');
    if (jQuery('#access .menu .current-page-ancestor').attr('id') !== f){
      jQuery('#access .menu .current-page-ancestor > a').css('position', 'static');
    } else {
      jQuery('#access .menu .current-page-ancestor > a').css('position', 'relative');
    }
    
    // blendet Flash-Objekte aus
    if (jQuery.browser.msie){ // mit display: none
      jQuery("object, embed, iframe").hide();
    } else { // mit visibility: hidden
      jQuery("object, embed, iframe").css("visibility", "hidden");
    }
    
    jQuery.ajax({
      url: script_path + f + '.php' + (jQuery('#access .menu').hasClass('shop') ? '?site=shop' : ''),
      success: function(data) {
        jQuery('#flyout').html(data);
        that.expand();
      }
    });
  };
  
  /**
   * Flyout maximieren
   */
  this.expand = function(){
    jQuery('#access .flyout_wrapper .flyout_push').removeClass('down').addClass('flyout');
    if (jQuery('#access .current-page-ancestor').size() === 0){
      jQuery('#access .flyout_wrapper').removeClass('hidden');
    } else {
      jQuery('#access .current-page-ancestor ul.sub-menu').fadeOut('fast');
    }
    jQuery('#flyout').slideDown('fast');
  };
  
  /**
   * Flyout minimieren
   */
  this.close = function(){
    jQuery('#flyout').slideUp('fast', function(){
      if (jQuery('#access .current-page-ancestor').size() === 0){
        jQuery('#access .flyout_wrapper').addClass('hidden');
      } else {
        jQuery('#access .current-page-ancestor ul.sub-menu').show();
        jQuery('#access .flyout_wrapper .flyout_push').addClass('down');
      }
      jQuery('#access .menu .current-page-ancestor > a').css('position', 'relative');
      jQuery('#access .menu .tab.open').removeClass('open');
      
      // blendet Flash-Objekte wieder ein
      if (jQuery.browser.msie){ // mit display: none
        jQuery("object, embed, iframe").show();
      } else { // mit visibility: hidden
        jQuery("object, embed, iframe").css("visibility", "visible");
      }
    });
  };

};

/**
 * Beim Laden der Seite
 */
jQuery(document).ready(function(){
  flyout = new RoroFlyout();
});
