var Site = {
  initAccordion : function(e){
      var e = $(e);
      var bodies = e.getElements("dd div.content");
      var h = 0;
      bodies.getHeight().each(function(e){
        if(e > h){ h = e; }
      });
      bodies.setStyle("height",h);
      
      var fx = new Fx.Accordion(
        e.getElements("dt"),
        e.getElements("dd"),
        {
          width: false,
          returnHeightToAuto: false,
          initialDisplayFx: false
        }
      );

  }
}

// ================================================
// = Fader : Fade a set of images over each other =
// ================================================
var Fader = new Class({
	options : {
		delay : 5000, // Duration of image display in ms
		fadeDuration : 500 // Duration of fade effect
	},
	initialize : function(container){
		// Set options
		if(arguments.length > 1){	$extend(this.options,arguments[1]); }
		
		// Initialize container
		this.container = $(container);		
		this._setupContainer();

		this.images = $A([]);
		this.pickList = $A([]);
		this.running = false;
		this.timer = null;
		
	},
	add : function(imgUrl){
		var i = $(new Image());
		i.src = imgUrl;
		if (arguments.length > 1) {
		  i.setProperty("alt", arguments[1]);
		  i.setProperty("title", arguments[1]);
		}
		this.images.push(i);		
	},
	place : function(){
		this._populateElement(this.bottom);
		this._populateElement(this.top);	
		this.top.setStyles({display: "inline",position: "absolute",zIndex:1});					
		this.bottom.setStyles({display: "inline",position: "relative",zIndex:0});
	},
	start : function(){
		if(!this.running && this.images.length > 0){
			this.place();			
			this.running = true;
			this.timer = this._cycle.delay(this.options.delay,this);
		}		
	},
	// Private methods
	_cycle : function(){
	  var complete = function(){
	    this._swapElement();	    
  		if(this.running){
  			this.timer = this._cycle.delay(this.options.delay,this);
  		}
	  }
	  
		var f = new Fx.Tween(this.top, {property: "opacity", duration: this.options.fadeDuration, onComplete: complete.bind(this)}).start(1,0);

	},
	_swapElement : function(){	
		var tmp = this.bottom;
		this.bottom = this.top;
		this.top = tmp;				
		
		this.top.setStyles({display: "inline",position: "absolute",zIndex:1, opacity: 1,top: 0, left:0});					
		this.bottom.setStyles({display: "inline",position: "relative",zIndex:0, opacity: 1});
		
		this._populateElement(this.bottom);		
	},
	_populateElement : function(element){
		if(this.pickList.length === 0){ this.pickList = $A(this.images); }

		var image = element.retrieve("image");	  				
		var curLogo = this.pickList.getRandom();
		this.pickList.erase(curLogo);

		image.src = curLogo.src;
		$(image).setProperty("alt",curLogo.getProperty("alt"));
		$(image).setProperty("title",curLogo.getProperty("title"));
	},
	_setupContainer : function(){
		this.container.empty();
		this.container.setStyle("display","block");
		
		this.wrapper = new Element("div");
		this.wrapper.setStyles({display:"block",position:"relative"});		
		this.container.adopt(this.wrapper);
		
		this.top = this._setupLogoElement();
		this.bottom = this._setupLogoElement();
		
		this.wrapper.adopt(this.top);
		this.wrapper.adopt(this.bottom);
	},
	_setupLogoElement : function(){
		var lnk = $(document.createElement("div"));		
		lnk.store("image", new Image());
		lnk.setStyles({display:"none",position:"relative"});
		lnk.adopt(lnk.retrieve("image"));
		return lnk;
	}	
});

