(function($)
{
  $.fn.reverse = function() {
      return this.pushStack(this.get().reverse(), arguments);
  };
  
  function replaceLabels()
  {
    // Inputfelder mit den Texten ihrer Labels vorbelegen
    $('form .formtext,form .formtextarea,#rexsearch_form div').each(function()
    {
      if($(this).hasClass('visible-label'))
        return false;
      
      var $input = $('input', this);
      var $label = $('label', this);
      
      if(!$input.length)
        var $input = $('textarea', this);
      
      var definedValue = $('label', this).text();
      var changed = false;
      
      if(!($input.val()).length)
      $input.val(definedValue);
    
      $input.change(function()
      {
        changed = true;
      });
      
      $input.focusin(function()
      {
        if(!changed && ($input.val() == definedValue))
        {
          $input.val('');
        }

        $label.show();
      });
      
      $input.focusout(function()
      {
        if(!$input.val().length || ($input.val() == definedValue))
        {
          $input.val(definedValue);
          changed = false;
        }
        
        $label.hide();
      });
    });
  }
  
  $(document).ready(function()
  {
    $('body').addClass('js');
    
    /*/ Inputfelder mit den Texten ihrer Labels vorbelegen
    replaceLabels();
    
    // Checkboxen ersetzen
    $('input[type=checkbox]').each(function()
    {
      var $checkbox = $(this);
      var state = $checkbox.is(':checked');
      var $replacement = $('<div>').addClass('checkbox').click(function()
      {
        if(state)
        {
          $(this).removeClass('checked').text('')
          $checkbox.attr('checked', false);
        }
        else
        {
          $(this).addClass('checked').text('✔');
          $checkbox.attr('checked', true);
        }
        
        state = !state;
      });
      
      if(state)
      {
        $replacement.addClass('checked').text('✔');
      }
      
      if($checkbox.hasClass('form_warning'))
        $replacement.addClass('form_warning');
      
      $checkbox.hide().before($replacement).change(function()
      {
        $replacement.click();
      });
    });
    //*/
    
    // animate clients
    $('#clients h3').click(function()
    {
      var top = $(this).parent().offset().top;
      var count = $('ul > li', $(this).parent()).size();
      var id = $(this).parent().attr('id');
      var visible = $('h4', $(this).parent()).is(':visible');
      var speed = 100;
      
      if(!count)
        return false;
      
      $('#clients dd').removeClass('active');
      
      if(visible)
      {
        $('#clients dd').reverse().each(function()
        {
          $(this).animate({
            paddingBottom: 6+'px'
          }, 500);
        });
        
        $('.projects', $(this).parent()).hide(300 + speed*count);
      }
      else
      {
        var maxleft = 0;
        var lastelem = null;
        $('#clients dd').each(function()
        {
          if(($(this).offset().top == top) && ($(this).offset().left > maxleft))
          {
            lastelem = $(this);
            maxleft = lastelem.offset().left;
          }
        });
        
        if(lastelem)
        {
          lastelem.animate({
            paddingBottom: (23 + count * 20)+'px'
          }, 500);
        }
        
        var $this = $(this);
        window.rr_delayed = function()
        {
          $('#clients dd').each(function()
          {
            if($(this).offset().top != top)
            {
              $(this).animate({
                paddingBottom: '6px'
              }, 500);
            }
          });
          
          $('#clients dd .projects').hide(300 + speed*count);
          $('.projects', $this.parent()).show(300 + speed*count);
        }
        
        $this.parent().addClass('active');
        
        setTimeout('rr_delayed()', 40);
      }
    }); //*/
    
    var nav_width = $('#nav-main').outerWidth() + 13;
    var nav_height = $('#nav-main').outerHeight();
    var breadcrumb_width = $('#breadcrumb').innerWidth();
    //var breadcrumb_padding = $('#breadcrumb').innerWidth() - breadcrumb_width;
    /*var breadcrumb_left = $('#breadcrumb').position().left;
    
    // animate navigation
    $('#head').mouseenter(function()
    {
      $('#breadcrumb').animate({left: breadcrumb_left - breadcrumb_width, opacity: 0}, breadcrumb_width, function()
      {
        $('#nav-main').css('left', -nav_width);
        $('#nav-main').css('opacity', 0);
        $('#nav-main').show();
        $('#nav-main ul').hide();
        $('#nav-main').animate({left: 0, opacity: 1}, nav_width, function()
        {
          $('#nav-main ul').slideDown(500);
        });
      });
    }).mouseleave(function()
    {
      $('#nav-main ul').slideUp(500, function()
      {
        $('#nav-main').css('left', 0);
        $('#nav-main').animate({left: -nav_width, opacity: 0}, nav_width, function()
        {
          $('#nav-main').hide();
          $('#breadcrumb').animate({left: breadcrumb_left, opacity: 1}, breadcrumb_width);
        });
      });
    });
    
    // animate navigation
    $('#head').mouseenter(function()
    {
      $('#breadcrumb').animate({width: 0}, breadcrumb_width, function()
      {
        $('#breadcrumb').animate({paddingLeft: 0}, breadcrumb_padding, function()
        {
          $('#nav-main ul').hide();
          $('#nav-main').append(
            $('<div>').addClass('nav-overlay').height(32).width(nav_width).css('position', 'absolute').css('right', '0').css('top', '0').css('background', '#fff')
          );
          $('#nav-main').show();
          $('.nav-overlay').animate({width: 0}, nav_width, function()
          {
            $('.nav-overlay').remove();
          });
          $('#nav-main ul').slideDown(nav_height);
        });
      });
    }).mouseleave(function()
    {
      $('#breadcrumb').width(0).css('padding-left', '0');
      $('#nav-main').width(nav_width).append(
        $('<div>').addClass('nav-overlay').height(32).width(0).css('position', 'absolute').css('right', '0').css('top', '0').css('background', '#fff')
      );
      $('.nav-overlay').animate({width: nav_width}, nav_width, function()
      {
        $('#nav-main').hide();
        $('.nav-overlay').remove();
      });
      $('#nav-main ul').slideUp(nav_height, function()
      {
        $('#breadcrumb').animate({paddingLeft: breadcrumb_padding}, breadcrumb_padding, function()
        {
          $('#breadcrumb').animate({width: breadcrumb_width}, breadcrumb_width);
        });
      });
    });
    
    /*$('#main').mousemove(function()
    {
      $('#head').mouseleave();
    });*/
    
    // animate navigation
    $('#head').mouseenter(function()
    {
      $('#breadcrumb').append(
        $('<div>').addClass('nav-overlay').height(32).width(0).css('position', 'absolute').css('right', '0').css('top', '0').css('background', '#fff')
      ).show();
      
      $('#breadcrumb .nav-overlay').animate({width: breadcrumb_width}, breadcrumb_width, function()
      {
        $('#breadcrumb').hide();
        $('#nav-main ul').hide();
        $('#nav-main').append(
          $('<div>').addClass('nav-overlay').height(32).width(nav_width).css('position', 'absolute').css('right', '0').css('top', '0').css('background', '#fff')
        ).show();
        
        $('#nav-main ul').slideDown(nav_height);
        $('#nav-main .nav-overlay').animate({width: 0}, nav_width, function()
        {
          $('#nav-main .nav-overlay').remove();
        });
      });
    }).mouseleave(function()
    {
      $('#nav-main').width(nav_width).append(
        $('<div>').addClass('nav-overlay').height(32).width(0).css('position', 'absolute').css('right', '0').css('top', '0').css('background', '#fff')
      );
      
      $('#nav-main ul').slideUp(nav_height);
      $('#nav-main .nav-overlay').animate({width: nav_width}, nav_width, function()
      {
        $('#nav-main').hide();
        $('#breadcrumb').append(
          $('<div>').addClass('nav-overlay').height(32).width(breadcrumb_width).css('position', 'absolute').css('right', '0').css('top', '0').css('background', '#fff')
        ).show();
        
        $('#breadcrumb .nav-overlay').animate({width: 0}, breadcrumb_width, function()
        {
          $('#breadcrumb .nav-overlay').remove();
        });
      });
    });
    
    // animate projects
    $('#projects dd').mouseenter(function()
    {
      $(this).animate({
        backgroundPosition: '0 132px'
      }, 500);
    }).mouseleave(function()
    {
      $(this).animate({
        backgroundPosition: '0 85px'
      }, 500);
    });
    
    // sliding projects
    /*if($('#project-slide li').length > 1)
    {
      $('#project-slide').prepend(
        ($('<div>').attr('id', 'slide-nav')).append(
          $('<div>').attr('id', 'slide-next')
        ).append(
          $('<div>').attr('id', 'slide-prev')
        )
      );
    }
    
    var screenwidth = $(document).width();
    if(screenwidth < 980) screenwidth = 980;
    var easingfunc = 'easeInOutQuad';
    
    // adjusting slice-positions
    var adjustSlidePos = function(init)
    {
      var oldscreenwidth = screenwidth;
      screenwidth = $(document).width();
      if(screenwidth < 980) screenwidth = 980;
      
      $('#project-slide ul li').each(function(i)
      {
        var pos;
        if(init)
          pos = i;
        else
          pos = parseInt($(this).position().left / oldscreenwidth);
        
        $(this).css('left', (screenwidth * pos) + 'px');
      });
      
      $('#project-slide ul li img').css('top', '50%');
      $('#project-slide ul li img').each(function()
      {
        $(this).css('margin-top', '-' + ($(this).height() >> 1) + 'px');
      });
    }
    
    // show next slice
    var shownextSlice = function()
    {
      var $current = null;
      var $prev = null;
      var cont = false;
      
      $('#project-slide ul li').each(function()
      {
        if($(this).position().left == 0)
        {
          $current = $(this);
          $prev = $current.next();
          return false;
        }
      });
      
      if(!$prev || !$prev.length)
        return false;
      
      $current = null;
      $prev = null;
      
      $('#project-slide ul li').each(function()
      {
        if($(this).position().left == 0)
        {
          $current = $(this);
          $prev = $current.next();
          cont = true;
        }
        else if(cont)
        {
          cont = false;
        }
        else
        {
          $(this).css('left', ($(this).position().left - screenwidth) + 'px');
        }
      });
      
      $prev.animate({left: '0px'}, 1000, easingfunc);
      $current.animate({left: (-screenwidth) + 'px'}, 1000, easingfunc);
    }
    
    // show prev slice
    var showprevSlice = function()
    {
      var $current = null;
      var $next = null;
      var cont = false;
      
      $elems = $('#project-slide ul li');
      
      for(var i = $elems.length - 1; i >= 0; i--)
      {
        $this = $elems.eq(i);
        if($this.position().left == 0)
        {
          $current = $this;
          $next = $current.prev();
          break;
        }
      }
      
      if(!$next || !$next.length)
        return false;
      
      $current = null;
      $next = null;
      
      for(var i = $elems.length - 1; i >= 0; i--)
      {
        $this = $elems.eq(i);
        if($this.position().left == 0)
        {
          $current = $this;
          $next = $current.prev();
          cont = true;
        }
        else if(cont)
        {
          cont = false;
        }
        else
        {
          $this.css('left', ($this.position().left + screenwidth) + 'px');
        }
      }
      
      $next.animate({left: '0px'}, 1000, easingfunc);
      $current.animate({left: (screenwidth) + 'px'}, 1000, easingfunc);
    }
    
    adjustSlidePos(true);
    $(window).resize(function()
    {
      adjustSlidePos();
    });
    
    // start auto-sliding
    var direction = 'left';
    var autoslide = setInterval(function()
    {
      if(!$('#project-slide li').length)
        return false;
      if(direction == 'left')
        shownextSlice();
      
      if(direction == 'right')
        showprevSlice();
      
      if((direction == 'left') && ($('#project-slide ul li:last').position().left == 0))
      {
        direction = 'right';
        showprevSlice();
      }
      
      if((direction == 'right') && ($('#project-slide ul li:first').position().left == 0))
      {
        direction = 'left';
        shownextSlice();
      }
    }, 5000);
    
    $('#slide-prev').click(function()
    {
      clearInterval(autoslide);
      showprevSlice();
    });

    $('#slide-next').click(function()
    {
      clearInterval(autoslide);
      shownextSlice();
    });*/
    
    var screenwidth = $(document).width();
    if(screenwidth < 980) screenwidth = 980;
    var easingfunc = 'easeInOutQuad';
    var $sliding_items = $('#project-slide li');
    var current_sliding_index = 0;
    var sliding_count = $sliding_items.length;
    var autoslide;
    
        // adjusting slice-positions
    window.adjustSlidePos = function()
    {
      var oldscreenwidth = screenwidth;
      screenwidth = $(document).width();
      if(screenwidth < 980) screenwidth = 980;
      
      //$('#project-slide ul li:visible').css('left', (screenwidth * parseInt($(this).position().left / oldscreenwidth)) + 'px');
      
      $('#project-slide ul li img').css('top', '50%');
      $('#project-slide ul li img').each(function()
      {
        var $this = $(this);
        $this.css('margin-top', '-' + ($this.height() >> 1) + 'px');
      });
    }
    
    setTimeout('adjustSlidePos()', 40);
    $(window).resize(function()
    {
      adjustSlidePos();
    });

    if($sliding_items.length > 1)
    {
      $('#project-slide').prepend(
        ($('<div>').attr('id', 'slide-nav')).append(
          $('<div>').attr('id', 'slide-next')
        ).append(
          $('<div>').attr('id', 'slide-prev')
        )
      );
    }
    
    $sliding_items.each(function(i)
    {
      $(this).attr('rel', i);
      
      if(i==0)
        $(this).css('z-index', 4);
    });
    
    window.showNextSlice = function()
    {
      $('#project-slide li[rel='+((current_sliding_index + 1) % sliding_count)+']').css('left', screenwidth + 'px').css('z-index', 4).animate({left:0}, Math.round(screenwidth * 2));
      $('#project-slide li[rel='+current_sliding_index+']').animate({left:-screenwidth}, Math.round(screenwidth * 2), function()
      {
        $(this).css('z-index', 3);
      });
      current_sliding_index = (current_sliding_index + 1) % sliding_count;
    }
    
    window.showPrevSlice = function()
    {
      $('#project-slide li[rel='+((current_sliding_index + sliding_count - 1) % sliding_count)+']').css('left', -screenwidth + 'px').css('z-index', 4).animate({left:0}, Math.round(screenwidth * 2));
      $('#project-slide li[rel='+current_sliding_index+']').animate({left:screenwidth}, Math.round(screenwidth * 2), function()
      {
        $(this).css('z-index', 3);
      });
      current_sliding_index = (current_sliding_index + sliding_count - 1) % sliding_count;
    }
    
    $('#slide-next').click(function()
    {
      clearInterval(autoslide);
      showNextSlice();
    });
    
    $('#slide-prev').click(function()
    {
      clearInterval(autoslide);
      showPrevSlice();
    });
    
    autoslide = setInterval('showNextSlice()', Math.round(screenwidth * 2) + /*Math.round*/(4000));
    
    // vertical centering images
    $('#project-slide ul li img').load(function()
    {
      $('#project-slide ul li img').css('top', '50%');
      $('#project-slide ul li img').each(function()
      {
        $(this).css('margin-top', '-' + ($(this).height() >> 1) + 'px');
      });
    });
    
    window.adjustEmployeeHeight = function()
    {
      employee_height = $current_employee.outerHeight(true);
      if($current_image.outerHeight(true) > employee_height)
        employee_height = $current_image.outerHeight(true);
      
      // show first employee with first image
      $('#employees,#employees .images').height(employee_height);
    }
    
    // employees
    var $current_employee = $('#employees > li:first-child');
    var $current_image = $('#employees li:first-child .images > li:first-child');
    var $next_image, $next_employee, $prev_image, $prev_employee, employee_height;
    
    var adjustingEmployeeHeight = setInterval('adjustEmployeeHeight()', 50);
    setTimeout(function()
    {
      clearInterval(adjustingEmployeeHeight);
    }, 2000);
    
    // show first employee with first image
    $('#employees li').hide();
    $('#employees > li:first-child').show();
    $('#employees > li:first-child .images > li:first-child').show();
    
    if(($('#employees > li').length > 1) || (($('#employees > li .images > li').length > 1)))
    {
      $('#employees .images li').append(
        ($('<div>').addClass('slide-employee-nav')).append(
          $('<img>').addClass('slide-employee-prev').attr('src', './css/slide-prev-dark.png').addClass('hide')
        ).append(
          $('<img>').addClass('slide-employee-next').attr('src', './css/slide-next-dark.png')
        )
      );
    }
    
    $('.slide-employee-next').click(function()
    {
      $next_image = $current_image.next();
      $next_employee = $current_employee.next();
      
      if($next_image.length)
      {
        $current_image.fadeOut(500);
        $next_image.fadeIn(500);
        
        $current_image = $next_image;
      }
      else if($next_employee.length)
      {
        $('.images > li', $next_employee).hide();
        $('.images > li:first-child', $next_employee).show();
        
        $current_employee.fadeOut(500);
        $next_employee.fadeIn(500);
        
        $current_employee = $next_employee;
        $current_image = $('.images > li:first-child', $next_employee);
      }
      
      $next_image = $current_image.next();
      $next_employee = $current_employee.next();
      
      if(($next_image.length == 0) && ($next_employee.length == 0))
        $('.slide-employee-next').hide();
      
      $('.slide-employee-prev').show();
      adjustEmployeeHeight();
    });

    $('.slide-employee-prev').click(function()
    {
      $prev_image = $current_image.prev();
      $prev_employee = $current_employee.prev();
      
      if($prev_image.length)
      {
        $current_image.fadeOut(500);
        $prev_image.fadeIn(500);
        
        $current_image = $prev_image;
      }
      else if($prev_employee.length)
      {
        $('.images > li', $prev_employee).hide();
        $('.images > li:last-child', $prev_employee).show();
        
        $current_employee.fadeOut(500);
        $prev_employee.fadeIn(500);
        
        $current_employee = $prev_employee;
        $current_image = $('.images > li:last-child', $prev_employee);
      }
      
      $prev_image = $current_image.prev();
      $prev_employee = $current_employee.prev();
      
      if(($prev_image.length == 0) && ($prev_employee.length == 0))
        $('.slide-employee-prev').hide();
      
      $('.slide-employee-next').show();
      adjustEmployeeHeight();
    });
    
    // project detail
    if($('#project-images li').length > 1)
    {
      var project_images_showing = false;
      $('#project-images').append(
        $('<ul>').attr('id', 'project-images-nav')
      );
      
      $('#project-images-wrapper li').each(function(i, elem)
      {
        var $this = $(this);
        
        $this.attr('rel', i);
        
        $('#project-images-nav').append(
          $('<li>').append(
            $('<a>').attr('rel', i).click(function()
            {
              if(project_images_showing)
              {
                $('#project-images-wrapper li').fadeOut(1000);
                //return false;
              }
              
              project_images_showing = true;
              var $li = $('#project-images-wrapper li[rel='+$(this).attr('rel')+']');
              $('#project-images-nav a').removeClass('active');
              $(this).addClass('active');
              
              $('#project-images-wrapper li.active').fadeOut(1000);
              
              $li.css('z-index', '10').fadeIn(1000, function()
              {
                $('#project-images-wrapper li').css('z-index', '1').removeClass('active');
                $li.css('z-index', '5').addClass('active');
                project_images_showing = false;
              });
              
              if(parseInt($(this).attr('rel')) == 0)
                $('#reference-image-prev').fadeOut(500);
              else
                $('#reference-image-prev').fadeIn(500);
              
              if((parseInt($(this).attr('rel')) + 1) == $('#project-images-nav a[rel]').length)
                $('#reference-image-next').fadeOut(500);
              else
                $('#reference-image-next').fadeIn(500);
            })
          )
        );
      });
      
      $('#project-images-nav a[rel=0]').addClass('active');
      $('#project-images-wrapper li').hide();
      $('#project-images-wrapper li[rel=0]').show().css('z-index', '5').addClass('active');
      
      $('#project-images-wrapper').prepend(
        $('<a>').attr('id', 'reference-image-prev').click(function()
        {
          $('#project-images-nav a[rel='+(parseInt($('#project-images-nav a.active').attr('rel')) - 1) +']').click();
          $('#reference-image-next').fadeIn(500);
          
          if(parseInt($('#project-images-nav a.active').attr('rel')) == 0)
            $(this).fadeOut(500);
        })
      ).prepend(
        $('<a>').attr('id', 'reference-image-next').addClass('visible').click(function()
        {
          $('#project-images-nav a[rel='+(parseInt($('#project-images-nav a.active').attr('rel')) + 1) +']').click();
          $('#reference-image-prev').fadeIn(500);
          
          if((parseInt($('#project-images-nav a.active').attr('rel')) + 1) == $('#project-images-nav a[rel]').length)
            $(this).fadeOut(500);
        })
      );
    }
    
    $('#project-images-nav').append(
      $('<li>').addClass('lightbox').append(
        $('<a>').click(function()
        {
          $('#project-images li[rel='+$('#project-images-nav a.active').attr('rel')+'] a').click();
        })
      )
    );
    
    $('#project-images-wrapper ul a').lightBox({fixedNavigation:true});
    
    window.adjustProjectDetailHeight = function()
    {
      $('#project-images-wrapper ul').height($('#project-images li').outerHeight(true));
    }
    
    var adjustingProjectDetailHeight = setInterval('adjustProjectDetailHeight()', 50);
    setTimeout(function()
    {
      clearInterval(adjustingProjectDetailHeight);
    }, 2000);
    
    // position of meta-nav
    window.adjustMetaNavPosition = function()
    {
      var max = 0;
      $('#clients dd,#projects dd').each(function()
      {
        if($(this).position().left > max)
          max = $(this).position().left;
      });
      
      if($('#clients').length)
      {
        if(max > 900)
          $('#order-nav').css('max-width', '1043px');
        else
          $('#order-nav').css('max-width', '860px');
      }
        
      if($('#projects').length)
      {
        if(max > 900)
          $('#order-nav').css('max-width', '1116px');
        else
          $('#order-nav').css('max-width', '891px');
      }
      //alert(max);
      
      // 794   970
      // 765   990
    }
    
    adjustMetaNavPosition();
    $(window).resize(function()
    {
      adjustMetaNavPosition();
    });
    
    // adjust screentool layout headings
    $('#screentool-packages.tiles dd.layout-name').each(function()
    {
      if($(this).height() < 40)
        return true;
      
      $(this).attr('title', $(this).text());
      $(this).text($(this).text() + '…')
      while($(this).height() >= 40)
      {
        $(this).text($(this).text().substr(0,$(this).text().length - 2) + '…');
      }
    });
  });
}(jQuery));

