// +------------------------------------------------------------------------+
// | GLOBALS                                                                |
// +------------------------------------------------------------------------+
try {
  GLOBALS.ie6 == GLOBALS.ie7;
}
catch (eExc)
{
  var GLOBALS = {
      ie6: false,
      ie7: false
  };
}



// +------------------------------------------------------------------------+
// | Page                                                                   |
// +------------------------------------------------------------------------+
var Page = {


  // ~~~~~~~~~~~~~~~~~~~~~~~
  // Image rotation interval
  // ~~~~~~~~~~~~~~~~~~~~~~~
  rotationInterval : 6000,
  
  
  // ~~~~~~~~~~~~~~~~~~~~~
  // Image fading duration
  // ~~~~~~~~~~~~~~~~~~~~~
  fadingDuration : 800,
  
  
  // ~~~~~~~~~~
  // Page size
  // ~~~~~~~~~~
  size : false,

  
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Load up general page functionalities
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  init : function()
  {  
    Page.size = env.getPageSize(); // Get window viewport
    
    $(window).load(function() { Page.scrolling(); }); // Custom scrolling behaviour    
    
    Page.lightbox();               // Lightbox
    Page.rotateImages();           // Image rotator
    Page.tooltips();               // Initialize tooltips
    Page.anchors();                // Custom anchor behaviour    
    
    $('.toggle').toggleVal();      // Toggle field values    
  },
  
  
  // ~~~~~~~~~
  // Tooltips 
  // ~~~~~~~~~
  tooltips : function()
  {      
    var tooltip = $('<p id="tooltip"></p>');
    var title   = false;
    
    
    // Show tooltip when hovering
    // an element with the classname "tooltip"
    // and take it's title-attribute as content
    $('.tooltip').hover(function(){

    
      // Save elements title
      title = $(this).attr('title');
    
      // Clear title
      $(this).attr('title', '');

      // Prepend tooltip to body tag
      $('body').prepend(tooltip.text(title));

      // Let the tooltip follow the user's mouse
      $().mousemove(function(e) {
      
        tooltip.css('top', e.pageY + 10);
        tooltip.css('left', e.pageX + 10);
              
      });      
      
    }, function(){
      
      // Remove tooltip on blur
      tooltip.remove(); 
      
      // Restore title attribute
      $(this).attr('title', title);
    
    });
  },
  
  
  // ~~~~~~~~~~~~~~~~~~~~~~~
  // Custom anchor behaviour
  // ~~~~~~~~~~~~~~~~~~~~~~~
  anchors : function()
  {    
    if (document.location.hash.match(/^(#[a-z0-9_-]+)/i))
    {
      Page.scrollTo(RegExp.$1);
      return;
    }
    
    
    // Get all link-elements and check if they
    // contain an anchor reference. If so, register
    // a click event to trigger the Page.scrollTo-method.
    $('a').click(function(){
    
      if($(this).attr('href').match(/(#[a-z0-9_-]+)$/i))
      {
        Page.scrollTo(RegExp.$1);        
        
        return false;
      }
      
    });
  },

  
  // ~~~~~~~~~~~~~~~~~
  // Scroll to element
  // ~~~~~~~~~~~~~~~~~
  scrollTo : function(_sIdentifier)
  {     
    try
    {
      var aOffset = $(_sIdentifier).offset();   
      var iTop    = $('#top').height() + 10;

      document.location.hash = _sIdentifier;

      //if(GLOBALS.ie6)
      //  $('#container').get(0).scrollTop(0, aOffset.top - iTop);
      //else
        window.scrollTo(0, aOffset.top - iTop);
    }
    catch(exc){}
  },
    
  
  // ~~~~~~~~~~~~~~~~~~~~~~~~~
  // Initialize image-rotator
  // ~~~~~~~~~~~~~~~~~~~~~~~~~
  rotateImages : function()
  {      
    $('.rotator .wrapper').addClass('absolute');
    
    var firstImage = $('.rotator .image:first');
    var images     = $('.rotator .image');
  
    images.hide();
    firstImage.show();
    
    
    // Start animation when 
    // all images are loaded
    $(window).load(function() {    
        
      $('.rotator').everyTime(Page.rotationInterval, function(index) {
    
        var crntImage = $('.rotator .image:visible');
        var nextImage = (index % images.length == 0) ? firstImage : crntImage.parent().next().children('.image');
        
        crntImage.fadeOut(Page.fadingDuration);
        nextImage.fadeIn(Page.fadingDuration);
      });
      
    });
  },
  
  

  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Enable lightbox for gallery images
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  lightbox : function()
  {
    // Trigger the click event on the 
    // first item of the gallery
    $('a.lightbox-handle').click(function() {
    
      $(this).unbind('click');
      $('a[@rel*=gallery-image]:first').click();
    
    });

    $('a[@rel*=gallery-image]').lightBox({
        txtImage        : 'Bild',
        txtOf           : 'von',
        fixedNavigation : true
    });
  },
  
  
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Behaviour when resizing the browser window
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  resize : function()
  {  
    Page.size = env.getPageSize();
    Page.scrolling();
  },
  
  
  // ~~~~~~~~~~~~~~~~~~~~~~~~
  // Page scrolling behaviour
  // ~~~~~~~~~~~~~~~~~~~~~~~~
  scrolling : function()
  {      
    if(GLOBALS.ie6)
    {
      Page.scrollingIE6();
      return;
    }
  
    var eBody      = $('body');
    var eContainer = $('#container');
    var eCenter    = $('#center');
    var iScope     = eContainer.height();

    
    // If the page is larger than the browser
    // viewport, add a class name to the body tag
    if(iScope >= Page.size[3])
    {
      eBody.addClass('scrolling');
    }
    else
    {
      eBody.removeClass('scrolling');
    }
  },
  
  
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Alternative method for ie6
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  scrollingIE6 : function()
  {    
    var eHeader    = $('#top');
    var eFooter    = $('#footer');
    var eCenter    = $('#center');
    var iScope     = eCenter.height() + eHeader.height() + eFooter.height();

    
    // If the page is larger than the browser
    // viewport, add a class name to the body tag.
    if(iScope >= Page.size[3])
    {          
      $('body').addClass('scrolling');
      $('html').addClass('scrolling');
      
      eHeader.remove().prependTo($('body'));      
      eFooter.remove().appendTo($('body'));
            
      eFooter.css('top', Page.size[3] - eFooter.height() - 3);
    }
    else
    {
      $('body').removeClass('scrolling');
      $('html').removeClass('scrolling');
      
      eHeader.remove().prependTo($('#container'));      
      eFooter.remove().appendTo($('#container'));      
    }
	$('span.dropDownLabel').click(function(_oEvent) 
	{
		eLanguages = this.parentNode.getElementsByTagName('div')[0];
		DropDown.pushBox(this, eLanguages);
	});
	$('span.dropDownLabel').bind('click',DropDown.check);
  
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Apply text of clicked link to dropdown label
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	$('.dropDownItems a').click(function()
	{
		$(this).parents('.dropDown:first').find('.dropDownLabel:first').text($(this).text());
	});
  }

}



// +------------------------------------------------------------------------+
// | DropDown                                                               |
// +------------------------------------------------------------------------+
$(document).ready(function()
{
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // external Links in a new window
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  $('a.ext').click(function(_oEvent)
  {
    if (GLOBALS.ie6 || GLOBALS.ie7 || GLOBALS.ie8)
	{
		window.open(Event.element(_oEvent));
	}
    else
	{
		window.open(this.href);
	}
	
	_oEvent.preventDefault();  
  });
  
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Apply text of clicked link to dropdown label
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  $('.dropDownItems a').click(function()
  {
    $(this).parents('.dropDown:first').find('.dropDownLabel:first').text($(this).text());
  });
  
});


var DropDown = {


  /**
   * DropDown box stack.
   *
   * @var array
   */
  aBox : new Array(),

  
  /**
   * Document event observer registered or not.
   *
   * @var boolean
   */
  bDocumentEvent : false,

  
  /**
   * Push box to DropDown stock.
   *
   * @param  element _eLabel
   * @param  element _eItem
   * @return void
   */
  pushBox : function (_eLabel, _eItem)
  {
    $(_eLabel).bind('click',DropDown.check);

    DropDown.aBox.push(
      {
        label  : _eLabel,
        item   : _eItem,
        active : false
      }
    );
  },

  
  /**
   * Returns the stock-index of given box.
   *
   * @param  element         _eLabel
   * @return integer|boolean
   */
  getIndex : function (_eLabel)
  {
    var iLength = DropDown.aBox.length;
    var iIndex  = 0;

    while (iLength--)
    {
      if (DropDown.aBox[iIndex++].label == $(_eLabel).get(0))
      {
        return iIndex;
      }
    }

    return false;
  },

  
  /**     
   *
   * @param  object _oEvent Fired event
   * @return void
   */
  check : function (_oEvent)
  {
    var eTarget = $(this);
    var iIndex  = DropDown.getIndex(eTarget);

    if (iIndex && false == DropDown.aBox[iIndex-1].active)
    {
      DropDown.close(null);
      
      _oEvent.preventDefault();
      _oEvent.stopPropagation();

      DropDown.show(iIndex-1);
    }
  },

  
  /**
   * Show DropDown items
   *
   * @param  integer _iIndex
   * @return void
   */
  show : function (_iIndex)
  {
    // ~~~~~~~~~~~~~~~~~
    // Show DropDown box
    // ~~~~~~~~~~~~~~~~~
    DropDown.aBox[_iIndex].active = true;

    $(DropDown.aBox[_iIndex].item).css({display:'block'});

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // If not already registered, register document event observer
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if (!DropDown.bDocumentEvent)
    {
      DropDown.bDocumentEvent = true;

      $(document).bind('click', DropDown.close);
    }
  },

  
  /**
   * Hide all DropDown items
   *
   * @param  object _oEvent
   * @return void
   */
  close : function (_oEvent)
  {
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Unregister document event observer
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    DropDown.bDocumentEvent = false;

    $(document).unbind('click', DropDown.close);

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Run all boxes to close them
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    var iLength = DropDown.aBox.length;
    var iIndex  = 0;

    while (iLength--)
    {
      if (false == DropDown.aBox[iIndex++].active)
        continue;

      DropDown.aBox[iIndex-1].active = false;

      $(DropDown.aBox[iIndex-1].item).css({display : 'none'});
    }
  }
}


// +------------------------------------------------------------------------+
// | Quickfinder                                                            |
// +------------------------------------------------------------------------+
var Quickfinder = {


  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Toggle dropdowns by classname
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  switchCat : function(_sClass)
  {
    $('.quickfinder .links .dropDown:not(.hidden)').addClass('hidden');
    $('.quickfinder .links .' + _sClass).removeClass('hidden');
  }

}



// +------------------------------------------------------------------------+
// | Contact form                                                           |
// +------------------------------------------------------------------------+
var Contact = {

  
  // ~~~~~~~~~~~~
  // Set industry
  // ~~~~~~~~~~~~
  switchIndustry : function(_sIndustry)
  {
    $('.contactForm #industry').val(_sIndustry);
  }
  
}



// +------------------------------------------------------------------------+
// | Events                                                                 |
// +------------------------------------------------------------------------+
var Events = {

  
  // ~~~~~~~~~
  // Set month 
  // ~~~~~~~~~
  setMonth : function(_iMonth)
  {
    $('.datepicker .months .dropDownLabel').attr('title', _iMonth);
  },
  

  // ~~~~~~~~~~~~
  // Jump to date
  // ~~~~~~~~~~~~
  jump : function()
  {
    var sYear  = $('.datepicker .years .dropDownLabel').text();
    var iMonth = $('.datepicker .months .dropDownLabel').attr('title');
  
    var sIdentifier = '#m-' + iMonth + '-' + sYear;
  
    Page.scrollTo(sIdentifier);
  }
  
}


  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Enable click event for dropdowns
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$(document).ready(function() {
  $('span.dropDownLabel').click(function(_oEvent) 
  {
	eLanguages = this.parentNode.getElementsByTagName('div')[0];		
    DropDown.pushBox(this, eLanguages);
  });
  $('span.dropDownLabel').bind('click', DropDown.check);
});

// ~~~~~~~~~~~~~~~
// Initialize page
// ~~~~~~~~~~~~~~~
$(document).ready(function() { Page.init(); });
$(window).wresize(function() { Page.resize(); });