

/*
 * Clears the default value of an input[type="text] when gaining focus and restores it if left empty.
 *
 * myInput = new ClearingInputText(Element[, options]);
 */
var ClearingInputText = new Class({
	Implements: Options,
	
	options:{
		toggleDefault:true,
		focusStyles:{},
		blurStyles:{},
		clearedStyles:{},
		unclearedStyles:{}
	},
	
	initialize: function(el, options){
		this.element = $(el);
		this.setOptions(options);
		this.element.addEvents({
			'focus':this.focus.bind(this),
			'blur':this.blur.bind(this)
		});
		this.blur();
		
	},
	
	focus: function(){
		this.element.setStyles(this.options.focusStyles);
		
		if (this.element.get('value') == this.element.get('defaultValue')){
			if (this.options.toggleDefault) this.element.set('value', '');
			this.element.setStyles(this.options.clearedStyles);
		}
		
	},
	
	blur: function(){
		this.element.setStyles(this.options.blurStyles);
		
		if (this.element.get('value').trim() == ''){
			if (this.options.toggleDefault) this.element.set('value', this.element.get('defaultValue'));
			this.element.setStyles(this.options.unclearedStyles);
		}
		
	}
});

window.addEvent('domready', function(){
	var els = [
		'#siteNewsletter input[type="text"]',
		'#outsiteHeaderInner input[type="text"]',
		'#outsiteHeaderInner input[type="password"]'
	];

	$$(els.join(',')).each(function(el){
		new ClearingInputText(el, {
			focusStyles:{
				'color': '#333'
			},
			blurStyles:{
				'color': '#aaa'
			}
		});
	});
});

var DropDownPanel = new Class({
	drop: 'down',

	initialize: function(element){
		this.element = $(element);
		
		this.element.setStyle('display', 'block');
		
		var elOffset = this.element.getPosition().y;
		var elHeight = this.element.getSize().y;
		
		this.element.setStyle('display', 'none');//to fix MSIE7
		this.element.setStyle('display', '');
		this.element.removeProperty('style');
		
		this.elementBottom = (elOffset + elHeight);
		
		var boundFit = this.fit.bind(this);
		
		window.addEvent('scroll', boundFit);
		window.addEvent('resize', boundFit);
		
		this.fit();
	},
	
	fit: function(){
		var fits = this.fitsVertical();
		
		if (fits && this.drop == 'up'){
			this.drop = 'down';
			this.swapTopBottom();
		}
		else if (!fits && this.drop == 'down') {
			this.drop = 'up';
			this.swapTopBottom();
		}
		
	},
	
	fitsVertical: function(){
		var winHeight = window.getSize().y;
		var winScroll = window.getScroll().y
		
		if (this.elementBottom > (winScroll + winHeight)){
			return false;
		}
		
		return true;
		
	},
	
	swapTopBottom: function(){
		this.element.setStyle('display', 'block');
		
		var top = this.element.getStyle('top');
		var bottom = this.element.getStyle('bottom');
		
		this.element.setStyle('display', 'none');//to fix MSIE7
		this.element.setStyle('display', '');
		this.element.removeProperty('style');
		this.element.setStyles({
			'bottom': top,
			'top': bottom
		});
	}
	
});

window.addEvent('domready', function(){
	$$('.drop-down ul').each(function(el){
		new DropDownPanel(el);
	});
});

