// Simulate placeholder on appropriate elements

YUI.hho.instance.use('node', function(Y) {

	// First apply it to specific elements when available
	//Y.on('available',fixPlaceholder,'form input#topSearch');

	// Then apply it to all input elements when DOM ready
	Y.on(
		'domready',
		function(domreadyEvt) {
			Y.all('form input').each(fixPlaceholder);
		}
	)

	// Here's the actual function to be applied to the input elements
	function fixPlaceholder(availableEvt) {
		// Check it already implements "placeholder", and if not, does it have a placeholder attribute?
		this.placeholder = this.getAttribute('placeholder');
		if (!Modernizr.input.placeholder && this.placeholder) {
			// Get the current color
			this.mainColor = this.getStyle('color');
			if(!this.mainColor) {this.mainColor = false;}

			this.addPlaceholder = function () {
				if(this.get('value') === '') {
					// Set placeholder text
					this.set('value',this.placeholder);
					// Set the colour to grey (only if we've saved the main colour)
					this.setStyle('color','rgb(150,150,150)');
				}
			}

			this.removePlaceholder = function () {
				if(this.get('value') === this.placeholder) {
					// Remove the placeholder text
					this.set('value','');
					// Set the colour back to original
					if(this.mainColor) {
						this.setStyle('color',this.mainColor);
					} else {
						this.setStyle('color','');
					}
				}
			}

			// Add placeholder text to element right now, if not selected
			if(!this.test(':selected')) {
				this.addPlaceholder();
			}

			// Add placeholder text whenever the input is deselected
			this.on( 'blur', this.addPlaceholder );
			// Remove the placeholder whenever the input is selected again
			this.on( 'focus', this.removePlaceholder );
		}
	}
});

