/*
 * slide show based on YUI
 */
YAHOO.namespace("lfg");

YAHOO.lfg.slideshow = function (container, o) {
  this.container = YAHOO.util.Dom.get(container);
  this.frames = YAHOO.util.Dom.getElementsByClassName("yui-slideshow-frame", null, this.container);
  this.init();
}

YAHOO.lfg.slideshow.prototype = {
  init: function()
    {
      this.active_frame = this.get_active_frame();
      this.prepare_next_frame();
    },
  get_active_frame: function()
    {
      var current_frame = YAHOO.util.Dom.getElementsByClassName("yui-slideshow-visible", null,  this.container)[0];
      return current_frame;
    },
  get_frame_index: function(frame)
    {
      for(var i=0; i<this.frames.length;i++)
      {
        if (this.frames[i]==frame)
          return i;
      }
      return -1;
    },
  prepare_next_frame : function()
    {
      var current_index = this.get_frame_index(this.get_active_frame());
      if (current_index<0)
        current_index=0;
      var all_frames = this.frames;
      var next_index = (current_index+1) % all_frames.length
      var next_frame = all_frames[next_index];

      YAHOO.util.Dom.replaceClass(next_frame, "yui-slideshow-hidden", "yui-slideshow-next");
      this.next_frame = next_frame;
//      YAHOO.util.Dom.setStyle(this.next_frame, 'opacity', '1');
    },
  clean_up_transition : function() 
    { 
      YAHOO.util.Dom.replaceClass(this.active_frame, "yui-slideshow-visible", "yui-slideshow-hidden");
      YAHOO.util.Dom.replaceClass(this.next_frame, "yui-slideshow-next", "yui-slideshow-visible");
      this.active_frame = this.next_frame; 
        this.prepare_next_frame();
    },
  transition: function()
    {
            var hide = new YAHOO.util.Anim(this.active_frame, { opacity: { to: 0 }}, 0.6, YAHOO.util.Easing.none);
            var show = new YAHOO.util.Anim(this.next_frame, { opacity: { to: 1 }}, 0.6, YAHOO.util.Easing.none);

        show.onComplete.subscribe(this.clean_up_transition, this, true);
        hide.animate();
                    show.animate();
    },
  loop: function()
    {
      var self;
      self=this;
      this.loop_interval = setInterval( function(){ self.transition();}, 5000 );
    }
 }

/*
 * effects based on scriptaculous
 */

var effect_status = new Array();

function expandInfobox(sender)
{
  var collapsed = $(sender);
  var expanded  = collapsed.next('div.box-expanded');

  expandBox(collapsed, expanded);
}

function expandPressbox(sender)
{
  var collapsed = $(sender);
  var expanded  = collapsed.next('div.box-expanded');

  expandBox(collapsed, expanded);
}

function expandBox(collapsed, expanded)
{
  if (effect_status[collapsed] || effect_status[expanded])
    return false;

  $$('div.box-expanded').each(function(obj)
  {
    if(obj.visible()&&(obj!=expanded))
      collapseInfobox(obj);
  });

  effect_status[collapsed] = 1;
  effect_status[expanded] = 1;

  new Effect.BlindUp(collapsed,
  {
    afterFinish: function()
    {
      delete effect_status[collapsed];
    }
  });
  new Effect.BlindDown(expanded,
  {
    afterFinish: function()
    {
      delete effect_status[expanded];
    }
  });

  return false;
}

function collapseInfobox(sender)
{
  var expanded = $(sender);
  var collapsed = expanded.previous('div.box-collapsed');

  collapseBox(expanded, collapsed);
}

function collapsePressbox(sender)
{
  var expanded = $(sender).up('div.box-expanded');
  var collapsed = expanded.previous('div.box-collapsed');

  collapseBox(expanded, collapsed);
}

function collapseBox(expanded, collapsed)
{
  if (effect_status[collapsed] || effect_status[expanded])
    return false;

  effect_status[collapsed] = 1;
  effect_status[expanded] = 1;

  new Effect.BlindUp(expanded,
  {
    afterFinish: function()
    {
      delete effect_status[expanded];
    }
  });
  new Effect.BlindDown(collapsed,
  {
    afterFinish: function()
    {
      delete effect_status[collapsed];
    }
  });
  return false;
}


