/**
 * shuffle
 */
Array.implement({  
	shuffle: function() {  
		//destination array  
		for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);  
		return this;  
	}
});
/**
 * patch Selectors for use with namespaced attributes
 */ 
Selectors.RegExps.combined= (/\.([\w-]+)|\[((?:\w+:)?\w+)(?:([!*^$~|]?=)(["']?)([^\4]*?)\4)?\]|:([\w-]+)(?:\(["']?(.*?)?["']?\)|$)/g);

/**
 * we introduce wheel up and wheel down events
 */
Element.Events.extend({
	'wheelup': {
		base: (Browser.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
		condition: function(event){
			//event = new Event(event);
			return event.wheel >= 0;
		}
	},
	'wheeldown': {
		base: (Browser.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
		condition: function(event){
			//event = new Event(event);
			return event.wheel <= 0;
		}
	}
})
/**
 * I was missing this
 * @param {Object} display
 */
Element.implement({
	opacity: function(options, from, to) {
		this.get('tween',options).start('opacity', from, to)
	},
	appear: function(options){
		options = $merge.run([{// defaults
			duration:1000,
			display: 'block',
			to: 1
		}].extend(arguments), {// must haves
			onStart: function(options){
				this.show(options.display)
				if (typeof options.onStart == "function") options.onStart.call()
			}.bind(this,options)
		});
		this.get('tween',options).start('opacity', options.to);
		return this;
	},
	disappear: function(options){
		options = $merge.run([{// defaults
			duration:1000,
			to: 0
		}].extend(arguments), {// must haves
			onComplete: function(options){
				this.hide();
				if (typeof options.onComplete == "function") options.onComplete.call()
			}.bind(this, options)
		});
		options = options || {}
		this.get('tween',options).start('opacity', options.to);
		return this;
	}
})

/**
 * brings hidden-overflown divs the option to use the mousewheel or 
 * connect them to up and down buttons 
 */
init_scrollables = function() {
	$$('[da:scrollable]').each(function(scrollable){
		scrollable.fx_scroll = new Fx.Scroll(
			scrollable,{
				amount:scrollable.getAttribute('da:scrollable').toInt() || 10,
				transition: Fx.Transitions.Quad.easeInOut,
				duration: scrollable.scrollHeight
				})
		scrollable.addEvents({
			'wheelup': function(e){
				this.fx_scroll.set(this.scrollLeft, this.scrollTop - this.fx_scroll.options.amount);
			}.bind(scrollable),
			'wheeldown': function(e){
				this.fx_scroll.set(this.scrollLeft, this.scrollTop + this.fx_scroll.options.amount);
			}.bind(scrollable)
		})
	})
	$$('[da:scrollup]').each(function(scroller){
		var scrollable = $(scroller.getAttribute('da:scrollup'));
		if (!scrollable.fx_scroll) scrollable.fx_scroll = new Fx.Scroll(scrollable);
		scroller.addEvents({
			'mousedown':function(){
				this.pause();
				this.options.duration = this.element.scrollTop
				this.toTop();
			}.bind(scrollable.fx_scroll),
			'mouseup':function(){
				this.pause();
			}.bind(scrollable.fx_scroll),
			'mouseout':function(){
				this.pause();
			}.bind(scrollable.fx_scroll)
		})
	})
	$$('[da:scrolldown]').each(function(scroller){
		var scrollable = $(scroller.getAttribute('da:scrolldown'));
		if (!scrollable.fx_scroll) scrollable.fx_scroll = new Fx.Scroll(scrollable);
		scroller.addEvents({
			'mousedown':function(){
				this.pause();
				this.options.duration = this.element.scrollHeight - this.element.scrollTop
				this.toBottom();
			}.bind(scrollable.fx_scroll),
			'mouseup':function(){
				this.pause();
			}.bind(scrollable.fx_scroll),
			'mouseout':function(){
				this.pause();
			}.bind(scrollable.fx_scroll)
		})
	})
}