/*-------------
FREERANGE GALLERY SHOWCASE RADIANT EXTENSION
© 2007 FREERANGE FUTURE PTY LTD
--------------*/

buildShowcase = function() {
  current_element = 0;
  element_width = 820;
  total_elements = $("showcase-slider").immediateDescendants().length;
  if (total_elements <= 1) lock_controls(true);
  if (total_elements > 0) randomise();
  if (total_elements > 1) {
    lock_left(true);
    startCycle();
  }
  if (total_elements > 0) update_description();
}

show_next = function(){
  if (current_element == total_elements - 1) return;
  move_content(true);
  stopCycle();
}

show_previous = function(){
  if (current_element == 0) return;
  move_content(false);
  stopCycle();
}

move_content = function(forward){
  if (forward){
    if (++current_element == total_elements - 1) {
      lock_right(true);
      stopCycle();
    } 
    lock_left(false);

  } else {
    if (--current_element == 0) {
      lock_left(true);
    }
    lock_right(false);
  }
  new Effect.Move('showcase-slider',{x:current_element * -element_width, y:0, mode: 'absolute', afterFinish: function(){update_description()}});
}

update_description = function(){
  var new_text;
  if (Prototype.Browser.Gecko){
    visible_element() ? new_text = visible_element().readAttribute('title') : new_text = "";
  } else {
    new_text = $(visible_element()).readAttribute('title');
  }
  $("showcase-caption").replace('<div id="showcase-caption">' + new_text + '</div>');
}

visible_element = function(){
  if (total_elements == 0) return undefined;
  var first_node = $("showcase-slider").immediateDescendants()[current_element].firstChild;
  if (first_node.nodeName == "A") return first_node.firstChild;
  return first_node;
}

lock_controls = function(bool){
  var controls = [$("sc-left"),$("sc-right")];
  if (bool){
    controls.each(function(control){
      control.setAttribute("onclick","return(false);");
      control.setStyle({cursor:"default"});
    });
    controls[0].setStyle({"background-position":"0 -23px"});
    controls[1].setStyle({"background-position":"-13px -23px"});
  }
}

lock_left = function(bool){
  if (bool){
    $("sc-left").setStyle({cursor:"default"});
    $("sc-left").setStyle({"background-position":"0 -23px"});
  } else {
    $("sc-left").setStyle({"background-position":""});
    $("sc-left").setStyle({cursor:"pointer"});
  }
}

lock_right = function(bool){
  if (bool){
    $("sc-right").setStyle({cursor:"default"});
    $("sc-right").setStyle({"background-position":"13px -23px"});
  } else {
    $("sc-right").setStyle({"background-position":""});
    if (Prototype.Browser.IE) $("sc-right").setStyle({"background-position":"13px 0"});
    $("sc-right").setStyle({cursor:"pointer"});
  }
}

hide_controls = function(bool){
  var controls = $("showcase-controls");
  bool ? controls.setStyle({display:"none"}) : controls.setStyle({display:"block"});
}

randomise = function(){
  var count = Math.round(Math.random()*5) + 5;
  if (total_elements > 1){
    for (var i = 0; i < count; i++){
      var removed = $("showcase-slider").immediateDescendants()[Math.floor(Math.random()*total_elements)].remove();
      var first = $("showcase-slider").immediateDescendants()[0];
      $("showcase-slider").insertBefore(removed, first);
    }
  }
  $('showcase-slider').setStyle({"visibility":"visible"})
}

startCycle = function(){
  cycle_loop = new PeriodicalExecuter(cycle,5);
}

stopCycle = function(){
  cycle_loop.stop();
}

cycle = function() { 
  move_content(true);
} 

//window.onload = startCycle();