var SlideList = new Class({
	initialize: function(menu, options) {
		this.setOptions(this.getOptions(), options);
		
		this.menu = $(menu), this.current = this.menu.getElement('li.current');
		
		this.menu.getElements('li').each(function(item){
			item.addEvent('mouseover', function(){ this.moveBg(item); }.bind(this));
			item.addEvent('mouseout', function(){ this.moveBg(this.current); }.bind(this));
			item.addEvent('click', function(event){ this.clickItem(event, item); }.bind(this));
		}.bind(this));
				
		this.back = new Element('li').addClass('background').adopt(new Element('div').addClass('left')).injectInside(this.menu);
		this.back.fx = this.back.effects(this.options);
		if(this.current) this.setCurrent(this.current);
	},
	
	setCurrent: function(el, effect){
		this.back.setStyles({left: (el.offsetLeft)+'px', width: (el.offsetWidth)+'px'});
		(effect) ? this.back.effect('opacity').set(0).start(1) : this.back.setOpacity(1);
		this.current = el;
	},
	
	getOptions: function(){
		return {
			transition: Fx.Transitions.sineInOut,
			duration: 500, wait: false,
			onClick: Class.empty
		};
	},

	clickItem: function(event, item) {
		if(!this.current) this.setCurrent(item, true);
		this.current = item;
		this.options.onClick(new Event(event), item);
	},

	moveBg: function(to) {
		if(!this.current) return;
		this.back.fx.custom({
			left: [this.back.offsetLeft, to.offsetLeft],
			width: [this.back.offsetWidth, to.offsetWidth]
		});
	}
});

SlideList.implement(new Options);

window.addEvent('domready', function() {
	// orange menu demo
	if($('menu'))
		FancyExample = new SlideList($E('ul', 'menu'), {transition: Fx.Transitions.backOut, duration: 700 });

});

function divInit() {
 // this code to make it work in most browsers makes a list of every div tag in the page
 if (document.getElementById) {
  var x = document.getElementsByTagName('div');
 } else if (document.all) {
  var x = document.all.tags('div');
 } else {
  return;
 }
 // this code to make the rollover
 // it will take any div whose class is 'inactive' and replace it to 'active'
 // when moving your mouse over it
 // the inverse effect will occur when moving your mouse outside
 for (var i=0;i<x.length;i++) {
  x[i].onmouseover = function () { if (this.className == 'inactive')
   this.className = 'active'; }
  x[i].onmouseout = function () { if (this.className == 'active')
   this.className = 'inactive'; }
 }
}

//Execute function onload event
function addLoadEvent(func) {
 var oldonload = window.onload;
 if (typeof window.onload != 'function') {
  window.onload = func;
 } else {
  window.onload = function() {
   if (oldonload) {
    oldonload();
   }
   func();
  }
 }
}

addLoadEvent(divInit);
addLoadEvent(function() {
  /* more code to run on page load */ 
});