/**
 * Interactive elements.
 *
 * REQUIREMENTS
 * - MooTools Core 1.2.4
 *
 * @date     2011-12-12, 2011-12-05
 * @since    2010-08 ~
 * @author   Christian Oellers <c.oellers@alldesign.de>
 * @version  1.1.1
 */
window.addEvent('domready', function()
{
  // --------------------------------------------------------------------------------------------------------- FUNCTIONS

  /**
   * Video gallery page switcher.
   */
  function videoGallery() // :Boolean
  {
    if ($$('.pageswitch').length)
    {
      var PageSwitchInner = $$('.pageswitch div');
      var Pages           = $$('.pageswitch a');
      var currentPage     = 0;

      // Default active status
      $$('.pageswitch a.first').addClass('active');

      // Center page switch container
      $$(PageSwitchInner).setStyles
      ({
        'width' : $$(PageSwitchInner).getComputedSize()[0]['width'],
        'float' : 'none'
      });

      // Switch page on click
      $$(Pages).addEvents
      ({
        'click':function(e)
        {
          e.preventDefault();

          var id      = $(this).getAttribute('rel');
          var page    = '.page.no-' + id;
          currentPage = id;

          $$(Pages).removeClass('active');
          $(this).addClass('active');

          $$('.page').hide();
          $$(page).show();

          return false;
        }
      });
    }

    return false;
  }

  /**
   * Contact form check prevents accidential link
   * redirect if contact form is (partially) filled out.
   *
   * Ignore automatic content and hidden elements.
   * Removes auto-inserted '@' in email field.
   */
  function checkContactForm() // :Boolean
  {
    var contactFields = $$('input.text, textarea');
    var testStr       = '';

    for (var i = 0; i < contactFields.length; i ++)
    {
      testStr += $$(contactFields[i]).get('value');
    }

    testStr = testStr.replace('@', '');

    return testStr.length ? true : false;
  }

  // --------------------------------------------------------------------------------------------------------- CMS PAGES

  /**
   * Do not allow clicking 'fake active' links in head nav.
   */
  if ($$('#nav-header a.trail').length)
  {
      $$('#nav-header a.trail').addEvent('click', function(event)
      {
        event.stop();
        return false;
      });
  }
  
  /**
   * Add pressroom gallery tooltips.
   */
  if ($(document.body).hasClass('pressroom'))
  {
    var ToolTips = new Tips($$('.tooltip'),
    {
      showDelay     : 50,
  		hideDelay     : 50,
  		className     : 'tip-wrap',
  		offset        : {x:15, y:20},
  		windowPadding : {x:0, y:0},
  		fixed         : false
    });
  }

  /**
   * Video gallery pages.
   */
  if ($(document.body).hasClass('video'))
  {
    videoGallery();
  }

  /**
   * Prevent accidential redirect in contact form.
   */
  if ($(document.body).hasClass('contact'))
  {
    $$('#nav-header a, #nav-footer a, #nav-lang a').addEvent('click', function(event)
    {
      if (   $(this).get('href').contains('.html')
          || $(this).get('href').contains('/'))
      {
        if (checkContactForm())
        {
          var confirmText = 'Sie haben einen Teil des Kontakt-Formulares ausgefüllt. \nDamit der Inhalt nicht verloren geht wird die aufgerufene Seite jetzt in einem neuen Fenster geöffnet.';

          if ($(document.body).hasClass('en'))
          {
            confirmText = 'You have filled in a part of the contact form. In order not to lose this content, the site you are about to access will be opened in a new window.';
          }

          if (confirm(confirmText))
          {
            window.open(this.href);
            return false;
          }
          else
          {
            event.stop();
            return false;
          }
        }
      }

      return true;
    });
  }
});

