// $Id: slides.js,v 1.0 2009/09/30 andrew Exp $

// store the timer and current div data
r_slideshow_data = new Array();

// this stores all our static data
function roo_slideshow_data(timer_delay, sort_order, fade, fade_speed, fade_value) {
  this._timer_delay = timer_delay;
  this._sort_order = sort_order;
  this._fade = fade;
  this._fade_speed = fade_speed;
  this._fade_value = fade_value;
  this._current_div = 0;
  this._pause = false;
  this._url = "http://roo.emu.id.au/newsatch/random_item.php";
// always start by loading up both
  $("#roo_slideshow_div_1_0").load(this._url);
  $("#roo_slideshow_div_1_1").hide();
  $("#roo_slideshow_div_1_1").load(this._url);
}

// set the timer on or off
function roo_slideshow_timer(slideshow_main, slideshow_status) {
  // stop the current timer
  clearTimeout(r_slideshow_data[slideshow_main]._timer_id);

  // start a new timer, if slideshow_status is true, unless we're currently paused
  if (slideshow_status && !r_slideshow_data[slideshow_main]._pause) {
    // our timer will call roo_slideshow_switch, which fades out the current slide
    r_slideshow_data[slideshow_main]._timer_id = setTimeout("roo_slideshow_switch('" + slideshow_main + "', roo_slideshow_next_div('" + slideshow_main + "'))", r_slideshow_data[slideshow_main]._timer_delay);
  }
}

function roo_slideshow_pause(slideshow_main) {
  r_slideshow_data[slideshow_main]._pause = true;
  roo_slideshow_timer(slideshow_main, false);
}

function roo_slideshow_resume(slideshow_main) {
  r_slideshow_data[slideshow_main]._pause = false;
  roo_slideshow_timer(slideshow_main, true);
}

// fade out to the new div indicated
function roo_slideshow_switch(slideshow_main, new_div) {
  // get the id for the main element
  _main_div = "#roo_slideshow_main_" + slideshow_main;

  // turn off our timer
  roo_slideshow_timer(slideshow_main, false);

  // check to see if we fade or not
  if (r_slideshow_data[slideshow_main]._fade) {
    // fade out -- at the end, switch to the next slide in the slideshow
    $(_main_div).fadeTo(r_slideshow_data[slideshow_main]._fade_speed, r_slideshow_data[slideshow_main]._fade_value, function() { roo_slideshow_set_div(slideshow_main, new_div); });
  }
  else {
    // if we don't have a fade, then just switch without fading
    roo_slideshow_set_div(slideshow_main, new_div);
  }
}

// set the main div html to the new node
function roo_slideshow_set_div(slideshow_main, new_div_number) {
  // this is the id of the main div to change
  _main_div = "#roo_slideshow_main_" + slideshow_main;

  _old_breakout = "#roo_slideshow_div_" + slideshow_main + "_" + r_slideshow_data[slideshow_main]._current_div;
  _new_breakout = "#roo_slideshow_div_" + slideshow_main + "_" + new_div_number;

  //alert("hide " + _old_breakout + ", show " + _new_breakout);
  $(_old_breakout).hide();
  $(_new_breakout).show();
//alert("load " + r_slideshow_data[slideshow_main]._url);
  // so now double buffer the next item
  $(_old_breakout).load(r_slideshow_data[slideshow_main]._url);

  // set the current_div number to the new node
  r_slideshow_data[slideshow_main]._current_div = new_div_number;

  // check to see if we fade or not
  if (r_slideshow_data[slideshow_main]._fade) {
    // fade in -- at the end, turn on our timer
    $(_main_div).fadeTo(r_slideshow_data[slideshow_main]._fade_speed, 1, function() { roo_slideshow_timer(slideshow_main, true); });
  }
  else {
    // if we don't have a fade, then just turn on our timer without fading
    roo_slideshow_timer(slideshow_main, true);
  }
}

// in our implementation, we just double-buffer with ajax updates to the off-screen
function roo_slideshow_next_div(slideshow_main) {
  if (r_slideshow_data[slideshow_main]._current_div == 0) {
    return 1;
  }
  return 0;
}
