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);
		}
	}
});

//extend the slide class to handle header class changes
Fx.SlidePlus = Fx.Slide.extend({
	initialize: function(el, toggle, options){
		var ready, toggle;
		this.ready=true;
		this.toggle_elt=toggle;
		this.toggle_elt.addEvent('click', this.callToggle.bind(this, 'vertical'));
		this.addEvent('onComplete', function(){ this.ready=true; });
		this.parent(el,options);
	},
	callToggle: function(mode){
		if(this.ready){
			this.ready=false;
			//remove slider knobs when closing new search panel. On open they are added in an oncomplete event
			if(this.toggle_elt.id=='new_search_panel_heading' && !this.toggle_elt.hasClass('closed')){
				$$('#new_search_panel_holder .knob_to', '#new_search_panel_holder .knob_from').each(function(elt){
					elt.addClass('hidden');
				});
			}
			this.toggle_elt.toggleClass('closed');
			this.toggle(mode);
		}
	}
});

//extend the tips class to include an iframe shim for IE6
ShimTips = Tips.extend({
	options:{
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 200, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(0.9);
				if(this.shim){
				if(this.toolTip.getStyle('z-index').toInt()<1 || isNaN(this.toolTip.getStyle('z-index').toInt())){
					this.toolTip.setStyle('z-index',301);
				}
				this.shim.setStyle('z-index', this.toolTip.getStyle('z-index')-1);
				this.shim.setStyle('display', 'block');
				this.shim.setStyle('width', this.toolTip.getStyle('width').toInt()+this.toolTip.getStyle('padding-right').toInt()+this.toolTip.getStyle('padding-left').toInt()+'px');
				this.shim.setStyle('height', this.toolTip.getStyle('height').toInt()+this.toolTip.getStyle('padding-top').toInt()+this.toolTip.getStyle('padding-bottom').toInt()+'px');
			}
		},
		onHide: function(toolTip) {
			this.fx.start(0);
			if(this.shim){
				this.shim.setStyle('display', 'none');
			}
		},
		offsets:{ x:20, y:10 }
	},
	initialize: function(elements, options){
		this.shim=$('DivShim');
		//create an iframe shim for select boxes in ie6 and firefox on mac
		if((window.ie6 || (window.gecko && navigator.userAgent.test('mac', 'i'))) && !this.shim){
			var divShimProperties = {
				'id': "DivShim", 
				'src': "javascript:void(0);",
				'scrolling': "no",
				'frameborder': 0
			}; 
			var divShimStyles = {
				'position': "absolute",
				'display': "none",
				'padding': "0px",
				'border':'none',
				'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)'
			}; 
			var divShim = new Element('iframe').setProperties(divShimProperties).setStyles(divShimStyles);
			divShim.injectInside(document.body);
			this.shim=divShim;
		}
		//make the shim move with the tip
		if(this.shim){
			elements.each(function(el){
				el.addEvent('mousemove', this.moveShim.bind(this));
			}, this);
		}
		this.parent(elements, options);
	},
	//function to update the tooltip text. Will remove the title if set
	updateContent: function(el, text){
		el.$tmp.myText=text;
		this.wrapper.empty();
		this.text = new Element('span').inject(
				new Element('div', {'class': this.options.className + '-text'}
		).inject(this.wrapper)).setHTML(text);
	},
	//function to move the iframe with the tip
	moveShim: function(el){
		if(this.shim){
			this.shim.setStyle('left', this.toolTip.getStyle('left'));
			this.shim.setStyle('top', this.toolTip.getStyle('top'));
		}
	}
});

//add a function to the slider to let us refresh the value
Slider.implement({
	updateContent: function(){
		this.fireEvent('onChange', this.step);
	}
});

