var FancyForms = new Class({
	
	Implements: [Options, Events],
	options: {
		debug: false,
		selectClass: ".ff_select"
	},
	
	initialize: function(options){
		this.setOptions(options);
		
		$$('.ff_input .inputText').each(function(inputText) {
			initValue = inputText.get('value');
			inputText.addEvents({
				'focus': function(event) {
					(inputText.get('value')==initValue)? inputText.set('value','') : '';
				},
				'blur': function(event) {
					(inputText.get('value')=='')? inputText.set('value',initValue) : '';
				}
			});
		});
		
		$$('.ff_input .submit').each(function(submitText) {
			submitText.set('value','');
		});
		
		$$(this.options.selectClass).each(function(selectEl,index) {
			
			var orig_select = selectEl.getElement('select');
			orig_select.setStyle('display','none');
			selectEl.getElement('.ff_selectDisplay').setStyle('display','block');
			orig_select.getElements('option').each(function(optionEl,index) {
				if(index!=0) {
					var _class = optionEl.getProperty('class');
					if(_class=='optGroup') {
						new Element('span', {
							'html': optionEl.getProperty('text'),
							'class': optionEl.getProperty('class')
						}).inject(selectEl.getElement('.scrollDropDownInner'));
					} else {
						new Element('a', {
							'href': optionEl.getProperty('value'),
							'html': optionEl.getProperty('text'),
							'class': optionEl.getProperty('class'),
							'rel': optionEl.getProperty('rel'),
							'events': {
								'click': function() {
									// Update the label text
									selectEl.getElement('label').set('html',optionEl.getProperty('html'));
								}
							}
						}).inject(selectEl.getElement('.scrollDropDownInner'));
						
					}
					//this.log(optionEl.getProperty('class'));
					if(optionEl.getProperty('selected')==true) {
						var label = optionEl.get('html');
						selectEl.getElement('label').set('html',label);
						this.log(selectEl.getElement('label').get('html'));
					}
				}
			}.bind(this));
			
			var scrollDirection=null;
			var scrollDropDownInner_top = selectEl.getElement('.scrollDropDownInner').getStyle('top').toInt();
			var scrollDropDownInner_height = selectEl.getElement('.scrollDropDownInner').getStyle('height').toInt();
			var scrollDropDown_height = selectEl.getElement('.scrollDropDown').getStyle('height').toInt();
			
			selectEl.getElement('.dropDownWrap').fade('hide');
			
			var slideFx = new Fx.Tween(selectEl.getElement('.scrollDropDownInner'), {
				duration: 1000,
				transition: 'linear'
			});
			
			selectEl.getElements('.innerInputWrap').addEvent('click', function(event) {
				if (event) {event.preventDefault();}
				selectEl.getElement('.dropDownWrap').fade('toggle');
			}.bind(this));
			//this.log("Click event added");
			
			selectEl.getElements('.upArrow').addEvents({
				'mouseenter': function(event) {
					event.preventDefault();
					scrollDropDownInner_top = selectEl.getElement('.scrollDropDownInner').getStyle('top').toInt();
					scrollDropDownInner_height = selectEl.getElement('.scrollDropDownInner').getStyle('height').toInt();
					scrollDropDown_height = selectEl.getElement('.scrollDropDown').getStyle('height').toInt();
					this.log("Up arrow hovered");
					if(scrollDirection=='up' && scrollDropDownInner_top < 0 && scrollDropDownInner_top > (-(scrollDropDownInner_height - scrollDropDown_height))) {
						slideFx.resume();
						this.log("slideFx.resume()");
					} else {
						if(scrollDropDownInner_top<0) {
							slideFx.options.duration = (scrollDropDownInner_top * -3).round();
						}
						slideFx.start('top', scrollDropDownInner_top, 0);
						this.log("slideFx.start(): "+slideFx.options.duration);
					}
					scrollDirection='up';
				}.bind(this),
				'mouseleave': function(event) {
					event.preventDefault();
					slideFx.pause();
				}.bind(this)
			});
			
			selectEl.getElements('.downArrow').addEvents({
				'mouseenter': function(event) {
					event.preventDefault();
					scrollDropDownInner_top = selectEl.getElement('.scrollDropDownInner').getStyle('top').toInt();
					scrollDropDownInner_height = selectEl.getElement('.scrollDropDownInner').getStyle('height').toInt();
					scrollDropDown_height = selectEl.getElement('.scrollDropDown').getStyle('height').toInt();
					//this.log("Down arrow hovered");
					if(scrollDirection=='down' && scrollDropDownInner_top<0 && scrollDropDownInner_top > (-(scrollDropDownInner_height - scrollDropDown_height))) {
						slideFx.resume();
						//this.log("slideFx.resume()");
					} else {
						if(scrollDropDownInner_top<0) {
							slideFx.options.duration = ((scrollDropDownInner_height - (scrollDropDownInner_top*-1)) * 3).round();
						}
						slideFx.start('top', scrollDropDownInner_top, -(scrollDropDownInner_height - scrollDropDown_height));
						//this.log("slideFx.start(): "+slideFx.options.duration);
					}
					scrollDirection='down';
				}.bind(this),
				'mouseleave': function(event) {
					event.preventDefault();
					slideFx.pause();
				}.bind(this)
			});
			
			selectEl.getElement('.scrollDropDownInner').addEvent('mousewheel', function(e) {
				e.preventDefault();
				//this.log(this.getStyle('top').toInt())
				if(e.wheel<0) { // Down
					if(this.getStyle('top').toInt()<=0 && this.getStyle('top').toInt() > -(this.getStyle('height').toInt() - selectEl.getElement('.scrollDropDown').getStyle('height').toInt())) {
						this.setStyle('top',this.getStyle('top').toInt()-35);
					} else if(this.getStyle('top').toInt()<=0 && this.getStyle('top').toInt()>-35) {
						this.setStyle('top',this.getStyle('top').toInt()-this.getStyle('top').toInt());
					}
				} else { // Up
					//this.log((this.getStyle('height').toInt() - selectEl.getElement('.scrollDropDown').getStyle('height').toInt()) + 35)
					if(this.getStyle('top').toInt()<=0 && this.getStyle('top').toInt() > -(this.getStyle('height').toInt() - selectEl.getElement('.scrollDropDown').getStyle('height').toInt() + 35)) {
						if(this.getStyle('top').toInt()>-35) {
							this.setStyle('top',this.getStyle('top').toInt()+this.getStyle('top').toInt());
						} else {
							this.setStyle('top',this.getStyle('top').toInt()+35);
						}
					}
				}
			});
			
			$$(document.body).addEvent('click',function() {
				if(selectEl.getElement('.dropDownWrap').getStyle('opacity').toInt()>0) {
					selectEl.getElement('.dropDownWrap').fade('out');
				}
			}.bind(this));
			
		},this);
		
	},
	
	log: function(text) {
		if (this.options.debug && window.console) console.log(text);
	}
	
});