jQuery(function($){
  
  // enable the photo gallery
  $('#photo-gallery a').zoomimage({caption: false});

  // Registration box
  $('#home_registration .button a').click(function(){
    $('#home_registration_form').toggleClass('drop');
    return false;
  });

  $.extend($.fn, {

    // randomizes child elements and puts sets of them in containers
    remix: function(){

      // randomize function: http://sedition.com/perl/javascript-fy.html
      function randomize(arr){
        var i = arr.length;
        if ( i == 0 ) return false;
        while ( --i ) {
          var j = Math.floor( Math.random() * ( i + 1 ) );
          var tempi = arr[i];
          var tempj = arr[j];
          arr[i] = tempj;
          arr[j] = tempi;
        }
      }

      var iterate = 3;

      var elements = [];
      $(this).children().each(function(i, element){
        elements.push(element);
      });

      randomize(elements);

      var container = $(this).clone().empty();
      var current_subcontainer;

      var counter = 0;

      $.each(elements, function(i, element){
        if(counter >= iterate | i == elements.length){
          counter = 0;
          container.append(current_subcontainer);
          current_subcontainer = null;
        } else {
          if(!current_subcontainer) current_subcontainer = $('<div></div>');
          current_subcontainer.append(element);
          counter++
        }
      });
      $(this).replaceWith(container);
    }
  });

  $('#home_sponsors .content').remix();
  $('#home_sponsors .content').cycle({
    timeout: 0,
    next: '#next'
  });

});