// JavaScript Document

var SimpleSlideShow = Class.create();
SimpleSlideShow.prototype = {
	initialize: function(container, delay){
		this.container = container;
		this.delay = delay;
		if (this.container === null) return false;
		this.images = $$('#'+container+' img');
		if (this.images.length == 0) return false;
		this.curIndex = 0;
		this.images.each(function(image, index){
			if (index==0) image.show();
			else image.hide();
		});
		setTimeout(this.fadeNext.bind(this), this.delay * 1000);
	},
	fadeNext: function(){
		var nextIndex = (this.curIndex+1)%this.images.length;
		Effect.Fade(this.images[this.curIndex]);
		Effect.Appear(this.images[nextIndex]);
		this.curIndex = nextIndex;
		setTimeout(this.fadeNext.bind(this), this.delay * 1000);
	}
};

Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});
