/*
*
* Placeholder.js 1.0
* Creates a placeholder for browsers that don't support it
*
* @ Created by Guillaume Gaubert
* @ http://widgetulous.com/placeholderjs/
* @ © 2011 Guillaume Gaubert
*
* @ Default use :
* 	Placeholder.init();
*
*/


Placeholder = {
	
	// The normal and placeholder colors
	defaultSettings : {
		normal		: '#000000',
		placeholder : '#C0C0C0'
	},

	
	init: function(settings)
	{
		// Merge default settings with the ones provided
		if(settings)
		{
			// Merge the desired settings
			for(var property in settings)
			{
				Placeholder.defaultSettings[property] = settings[property];
			}
		}
		
		// Let's make the funky part...
		// First for input texts
		var inputs = document.getElementsByTagName("input");
		for (var i=0; i < inputs.length; i++) {
			var placeholder = inputs[i].getAttribute("placeholder");
			
			if(placeholder && inputs[i].type == "text")
			{
				inputs[i].onfocus = function(){
					Placeholder.onSelected(this);
				};
				
				inputs[i].onblur = function(){
					Placeholder.unSelected(this);
				};
				
				inputs[i].style.color = Placeholder.defaultSettings.placeholder;
				inputs[i].value = placeholder;
			}
		};
		
		// Then for textareas
		var texts = document.getElementsByTagName("textarea");
		for (var i=0; i < texts.length; i++) {
			var placeholder = texts[i].getAttribute("placeholder");
			
			if(placeholder)
			{
				texts[i].onfocus = function(){
					Placeholder.onSelected(this);
				};
				
				texts[i].onblur = function(){
					Placeholder.unSelected(this);
				};
				
				texts[i].style.color = Placeholder.defaultSettings.placeholder;
				texts[i].value = placeholder;
			}
		};
		
		
	},
	
	
	onSelected: function(input)
	{	
		if(input.value == input.getAttribute('placeholder'))
		{
			input.value = '';
		}
		
		input.style.color = Placeholder.defaultSettings.normal;
	},
	
	unSelected: function(input)
	{
		// Reset a placeholder if the user didn't type text
		if(input.value.length <= 0)
		{
			input.style.color = Placeholder.defaultSettings.placeholder;
			input.value = input.getAttribute("placeholder");
		}
	}
};
