
var ms_activeSlide=new Object();
var ms_upcomingSlide=new Object();
var ms_slideTimers=new Object();
var ms_autoSlideInterval=8000;
var ms_effectDuration=0.3;

function ms_switchComplete() {
  var ps=html.getElementById(this.target.group+"_"+ms_activeSlide[this.target.group]);
  if (ps) ps.setDisplay(false);
  ms_activeSlide[this.target.group]=this.target.idx;
  ms_upcomingSlide[this.target.group]=null;
}

function ms_blendUpdate() {
  this.target.object.setOpacity(this.target.opacity);
}

function ms_switchTo(group,idx) {
  var activeSlide=ms_activeSlide[group];
  var upcomingSlide=ms_upcomingSlide[group];

  if ((idx==activeSlide) || (idx==upcomingSlide)) return;

  JSTweener.removeTweensByProperties({tag:'mainstage_'+group});

  if (upcomingSlide) {
    html.getElementById(group+"_"+activeSlide).setDisplay(false);
    ms_activeSlide[group]=upcomingSlide;
    activeSlide=upcomingSlide;
  }

  // clear active slide
  var slide=html.getElementById(group+"_"+activeSlide);
  slide.setOpacity(100);

  //slide.removeClass("mactive");
  html.getElementById(slide.id).style.zIndex="5";
  html.getElementById(slide.id+"_btn").removeClass("mactive");
  html.getElementById(slide.id+"_arrow").removeClass("amactive");
  html.getElementById(slide.id+"_teaser").removeClass("mactive");

  // prepare new slide
  slide=html.getElementById(group+"_"+idx);

  //slide.addClass("mactive");
  html.getElementById(slide.id).style.zIndex="6";
  html.getElementById(slide.id+"_btn").addClass("mactive");
  html.getElementById(slide.id+"_arrow").addClass("amactive");
  html.getElementById(slide.id+"_teaser").addClass("mactive");

  ms_upcomingSlide[group]=idx;

  var tweener=new Object();
  tweener.object=slide;
  tweener.opacity=0;
  tweener.group=group;
  tweener.idx=idx;

  slide.setOpacity(tweener.opacity);
  JSTweener.addTween(tweener, {tag:'mainstage_'+group,time:ms_effectDuration,transition:'linear',opacity:100,onUpdate:ms_blendUpdate,onComplete:ms_switchComplete});
}

function ms_autoSlideTimer(group) {
  var idx=(ms_activeSlide[group] || 0)+1;
  var o=html.getElementById(group+"_"+idx);
  if (!o) idx=0;
  ms_switchTo(group,idx);
}

var slideoff="";
function ms_startAutoSlide(group) {
  if (!slideoff) ms_slideTimers[group]=window.setInterval("ms_autoSlideTimer('"+group+"')",ms_autoSlideInterval);
}

function ms_stopAutoSlide(group) {
  window.clearInterval(ms_slideTimers[group]);
  ms_slideTimers[group]=null;
}

function ms_initMainStage(group,idx) {
  idx=idx || 0;
  var o=html.getElementById(group+"_"+idx);
  if (!o) return;

  o.setOpacity(100);
  o.addClass("mactive");
  o.addClass("amactive");
  html.getElementById(o.id+"_btn").addClass("mactive");
  html.getElementById(o.id+"_arrow").addClass("amactive");
  html.getElementById(o.id+"_teaser").addClass("mactive");

  ms_activeSlide[group]=idx;
  ms_upcomingSlide[group]=null;

  ms_startAutoSlide(group);
}

