/************************************************************
Genius Awards javascript
************************************************************/

var container = "ScrollerBody";  // container id of profile divs
var distance = 220;  // the distance to scroll profiles
var cycle = true;  // cycle profiles around in a circle
var busy = false;  // currently executing flag

function scroll(direction) {
  if (!busy) {
    busy = true;
    
    setTimeout(function() { // make sure earlier calls have finished
    
      var shift = (direction == "left") ? -distance : distance;
      var profiles = $(container).childElements("div");
  
      var left = true;
      var right = true;
  
      if (cycle) {
        var first = null;
        var last = null;

        var lowest = null;
        var highest = null;

        for (var p = 0; p < profiles.length; p++) {
          var pos = $(profiles[p]).getStyle("left");
          pos = pos.substr(0, pos.length - 2) * 1;

          if (p == 0 || pos <= lowest) {
            lowest = pos;
            first = p;
          }
          if (p == 0 || pos >= highest) {
            highest = pos;
            last = p;
          }
        }
    
        if (direction == "left" && $(profiles[last]).getStyle("left") == highest + "px") {
          $(profiles[first]).setStyle({left: (highest + distance) + "px"});
        }
        if (direction == "right" && $(profiles[first]).getStyle("left") == lowest + "px") {
          $(profiles[last]).setStyle({left: (lowest - distance) + "px"});
        }
    
      } else {
        var first = 0;
        var last = profiles.length - 1;
        
        if (direction == "left") {
          if ($(profiles[last]).getStyle("left") == (distance * 2) + "px") {
            left = false;
          }
        } else if (direction == "right") {
          if ($(profiles[first]).getStyle("left") == "0px") {
            right = false;
          }
        }
      }
  
      if ((direction == "left" && left) || (direction == "right" && right)) {
        for (p = 0; p < profiles.length; p++) {
          var after = null;
          if (p == last) after = function() { busy = false; }

          new Effect.Move(profiles[p], {
            x: shift, 
            transition: Effect.Transitions.sinoidal, 
            duration: 0.75, 
            afterFinish: after
          });

        }
      } else busy = false;
    
    }, 25); // end timeout
  
  }
}

function init() {
  var profiles = $(container).childElements("div");
  
  if (document.images) {
    pic1 = new Image(16,16); 
    pic1.src = "/minisites/genius/images/button_left_dark.png";
    pic2 = new Image(16,16); 
    pic2.src = "/minisites/genius/images/button_right_dark.png";
  }
  
  for (var p = 0; p < profiles.length; p++) {
    $(profiles[p]).setStyle({left: -distance + (distance * p) + "px"});
    $(profiles[p]).setStyle({visibility: "visible"});
  }
  
  $(container).appear({duration: 2.0});
}

