FeatureFader = new Class({
	options: {
		delay: 0, //automatically fades if set 4000=4 sec
		duration: 1000, //how long to take to do the transition
		transition: Fx.Transitions.linear
	},
	initialize: function(collection, options){
		var itemCount, curItem, prvItem, ready, curFx, prvFx;
		this.elements=collection;
		this.setOptions(options);
		this.itemCount = this.elements.length;
		this.curItem = 0;
		this.prvItem = 0;
		this.elements.each(function(elt, i){
			if(i==0){
				elt.setStyles({position:'absolute', opacity:1, display:'block'});
			}
			else{
				elt.setStyles({position:'absolute', opacity:0, display:'none'});
			}
		});
		if(this.itemCount>1 && this.options.delay>0){
			this.fadeNext.periodical(this.options.delay, this);
		}
		this.ready=true;
	},
	fadeNext:function(){
		this.fade(1);
	},
	fadePrev:function(){
		this.fade(-1);
	},
	fade:function(multi){
		if(this.ready){
			this.ready=false;
			this.prvItem=this.curItem;
			this.curItem+=multi;
			if(this.curItem==this.itemCount){
				this.curItem=0;
			}
			else if(this.curItem<0){
				this.curItem=this.itemCount-1;
			}
			this.elements[this.curItem].setStyle('display','block');
			this.elements[this.curItem].effect('opacity', this.options).start(0,1);
			this.elements[this.prvItem].effect('opacity', this.options).start(1,0).addEvent('onComplete', this.endFade.bind(this));
		}
	},
	endFade:function(){
		this.elements[this.prvItem].setStyle('display','none');
		this.ready=true;
	}
});
FeatureFader.implement(new Options);

FeatureScroller = Fx.Styles.extend({
	options: {
		automatic: ['left', 4000], //scroll left automatically, wait 4 sec between transitions
		duration: 1000, //how long to take to do the transition
		transition: Fx.Transitions.Cubic.easeInOut,
		minItems: 2, //the minimum number of items needed before scrolling
		grid: false //for a 2D box of items- pass as [width, height]- cannot use with automatic
	},
	initialize: function(container, cssSelector, options){
		var container, wrapper, positionX, positionY, itemCountX, itemCounterX, itemCountY, itemCounterY, itemWidth, itemHeight, ready;
		this.addEvent('onComplete', this.endScroll);
		this.setOptions(options);
		this.positionX=0;
		this.positionY=0;
		this.container = $(container);
		var items = this.container.getElements(cssSelector);
		var wrapperWidth=0;
		var wrapperHeight=0;
		if(items.length>=this.options.minItems){
			this.itemWidth = items[0].offsetWidth;
			this.itemHeight = items[0].offsetHeight;
			if(this.options.grid){
				this.itemCountX = this.options.grid[0];
				this.itemCountY = this.options.grid[1];
				this.itemCounterX = 1;
				this.itemCounterY = 1;
				wrapperWidth=this.itemWidth*this.itemCountX;
				wrapperHeight=this.itemHeight*this.itemCountY;
			}
			else if(this.options.automatic){
					if(this.options.automatic[0]=='left' || this.options.automatic[0]=='right'){
						this.itemCountX = items.length;
						this.itemCountY = 1;
						this.itemCounterX = 1;
						this.itemCounterY = 1;
						wrapperWidth=this.itemWidth*(this.itemCountX+1);
						wrapperHeight=this.itemHeight;
						if(this.options.automatic[0]=='right'){
							this.positionX=-this.itemWidth*this.itemCountX;
							this.itemCounterX = this.itemCountX+1;
						}
					}
					else if(this.options.automatic[0]=='up' || this.options.automatic[0]=='down'){
						this.itemCountX = 1;
						this.itemCountY = items.length;
						this.itemCounterX = 1;
						this.itemCounterY = 1;
						wrapperHeight=this.itemHeight*(this.itemCountY+1);
						wrapperWidth=this.itemWidth;
						if(this.options.automatic[0]=='down'){
							this.positionY=-this.itemHeight*this.itemCountY;
							this.itemCounterY = this.itemCountY+1;
						}
					}
				}
			else{
				this.itemCountX = items.length;
				this.itemCountY = items.length;
				this.itemCounterX = 1;
				this.itemCounterY = 1;
				wrapperWidth=this.itemWidth*(this.itemCountX+1);
				wrapperHeight=this.itemHeight*(this.itemCountY+1);
			}
			//create the wrapper div that moves
			this.container.setStyle('position', 'relative');
			this.wrapper=new Element('div', {
				'styles': {
					'position': 'relative',
					'left': '0',
					'top': '0',
					'width':wrapperWidth+'px',
					'height':wrapperHeight+'px'
				}
			}).injectBefore(items[0]);
			this.wrapper.adopt(items);
			if(!this.options.grid){
				//duplicate the first item at the end of the list
				items[0].clone().injectInside(this.wrapper);
			}
			this.ready=true;
			this.parent(this.wrapper, this.options);
			if(this.options.automatic){
				this[this.options.automatic[0]+'Scroll'].delay(this.options.automatic[1], this);
			}
			this.set({'top':this.positionY, 'left':this.positionX});
			
		}
	},
	leftScroll:function(){
		if(this.ready && (this.itemCounterX<this.itemCountX || (this.options.automatic && this.itemCounterX==this.itemCountX))){
			this.ready=false;
			this.horizontalScroll(-1);
		}
	},
	rightScroll:function(){
		if(this.ready && this.itemCounterX>1){
			this.ready=false;
			this.horizontalScroll(1);
		}
	},
	horizontalScroll:function(multi){
		this.start({'left':[this.positionX, this.positionX+(this.itemWidth*multi)]});
		this.positionX = this.positionX+(this.itemWidth*multi);
		this.itemCounterX-=multi;
		if(this.itemCounterX==1 && this.options.automatic){
			this.positionX=-this.itemWidth*this.itemCountX;
			this.itemCounterX = this.itemCountX+1;
		}
		else if(this.itemCounterX>this.itemCountX){
			this.positionX=0;
			this.itemCounterX=1;
		}
	},
	upScroll:function(){
		if(this.ready && (this.itemCounterY<this.itemCountY || (this.options.automatic && this.itemCounterY==this.itemCountY))){
			this.ready=false;
			this.verticalScroll(-1);
		}
	},
	downScroll:function(){
		if(this.ready && this.itemCounterY>1){
			this.ready=false;
			this.verticalScroll(1);
		}
	},
	verticalScroll:function(multi){
		this.start({'top':[this.positionY, this.positionY+(this.itemHeight*multi)]});
		this.positionY = this.positionY+(this.itemHeight*multi);
		this.itemCounterY-=multi;
		if(this.itemCounterY==1 && this.options.automatic){
			this.positionY=-this.itemHeight*this.itemCountY;
			this.itemCounterY = this.itemCountY+1;
		}
		else if(this.itemCounterY>this.itemCountY){
			this.positionY=0;
			this.itemCounterY=1;
		}
	},
	endScroll:function(){
		this.set({'left':this.positionX, 'top': this.positionY});
		this.ready=true;
		if(this.options.automatic){
			this[this.options.automatic[0]+'Scroll'].delay(this.options.automatic[1], this);
		}
	}
});


/* removed at request 
window.addEvent('domready',function() {
	if ( !window.ie ) {
		return;
	}
	els = $$('p.MsoNormal, span');
	els.each(function(e,i) {
		if ( e.tagName != "SPAN" || (e.tagName == "SPAN" && /^<span style/i.test(e.outerHTML)) ) {
			e.setStyles({
				color: '#000'
			});
		}
	});
});
*/