// Slideshow Component
var Slideshow = Class.create();
Slideshow.prototype = {
    initialize: function(obj)
    {
        this.elem        = obj;
        this.items       = $A();
        this.active_item = null;
        this.paging      = $("navigation");
        this.paging_items = $A(this.paging.getElementsByTagName("li"));
        this.height      = 0;
        this.updater     = null;
        
        this.initItems();
        
        if (this.items.length > 0) this.items[0].show();
        if (this.height > 0 )
          this.elem.style.height = this.height+"px";
        this.startCycle();
    },
    cycle: function() 
    {
        var active_item = this.items.indexOf(this.active_item)+1;
        if (active_item == this.items.length) active_item = 0;
        this.items[active_item].show();
    },
    startCycle: function()
    {
        this.updater = new PeriodicalExecuter(this.cycle.bind(this), 6);
    },
    stopCycle: function()
    {
        this.updater.stop();
    },
    initItems: function()
    {
        var items = $A(this.elem.getElementsByClassName("hw_slide"));

        if (items.length == 0) {  // no paging needed if there is only one item 
          return;
        }
        if (items.length == 1) { // no paging needed if there is only one item 
          this.height = items[0].getHeight();
          return;
        }

        items.each(function(item) {
            var news_item = new Slideshowitem(item, this);
            
            if (news_item.height > this.height) this.height = news_item.height;
        }.bind(this))
    }
}

var Slideshowitem = Class.create();
Slideshowitem.prototype = {
    initialize: function(obj, parent)
    {
        this.elem   = obj;
        this.parent = parent;
        this.height = this.elem.getHeight();        

        this.createPagingItem();
        
        this.parent.items.push(this);
        this.elem.hide();
    },
    createPagingItem: function()
    {
        this.parent.paging_items.each(function(item){
            if (!Element.hasClassName(item,"pagingitem")){
                item.addClassName("pagingitem");
                this.paging_item = item;
                Event.observe(this.paging_item, "mouseover", this.show.bindAsEventListener(this), false);
                Event.observe(this.paging_item, "mouseover", this.parent.stopCycle.bind(this.parent), false);
                Event.observe(this.paging_item, "mouseout", this.parent.startCycle.bind(this.parent), false);
                Event.observe(this.elem, "mouseover", this.parent.stopCycle.bind(this.parent), false);
                Event.observe(this.elem, "mouseout", this.parent.startCycle.bind(this.parent), false);
                throw $break;
            } 
        }.bind(this));
    },
    show: function(e)
    {
        if (e) Event.stop(e);
        if (this.parent.active_item != null && this.parent.active_item != this) this.parent.active_item.hide();

        this.parent.active_item = this;
        Effect.Appear(this.elem, {duration:0.1});
        this.paging_item.addClassName("selected");
    },
    hide: function()
    {
        Effect.Fade(this.elem, {duration:0.2});
        this.paging_item.removeClassName("selected");
    }
}

function initSlideshow() {
    var slideshows = $A(document.getElementsByClassName("hw_slideshow"));
    slideshows.each(function(obj)
    {
        new Slideshow(obj);
    });
}

function registerSlideshow(count){
  if (!count) {
    count = 1;
  }
  else {
    count++;
  }
  
  if(typeof utiljs_registerWindowOnloadFunction == "function") {
    utiljs_registerWindowOnloadFunction(initSlideshow, count);
  } 
  else {
    if (count <= 10) {  // stop trying after 10 seconds
      registerResizer.delay(1, count);
    }
  }
}
registerSlideshow();

