﻿/*
COOL STUFF!!! IE and FIREFOX
http://www.mtrkpn.com/ru/InputPlaceholder
*	20.06.2005 17:27
*	InputPlaceholder Class v 0.1a
*
*	30.06.2005 20:36
*	InputPlaceholder Class v 0.1b
*
*	01.07.2005 18:30
*	InputPlaceholder Class v 0.1c
*/
/*	лЕРНДШ:
*	НРЯСРЯРБСЧР
*
*	яБНИЯРБЮ
*	Input	щКЕЛЕМР ТНПЛШ, Я ЙНРНПШЛ ПЮАНРЮЕЛ
*	Value	гМЮВЕМХЕ МЮДОХЯХ ОН СЛНКВЮМХЧ
*	CssFilled	хЛЪ css-ЙКЮЯЯЮ ДКЪ НРНАПЮФЕМХЪ ГЮОНКМЕМНЦН ОНКЪ
*	CssEmpty	хЛЪ css-ЙКЮЯЯЮ ДКЪ НРНАПЮФЕМХЪ МСЯРНЦН ОНКЪ
*/
/*	йНМЯРПСЙРНП
*	оЮПЮЛЕРПШ:
*	input	щКЕЛЕМР ТНПЛШ ( input[@type='text'] ), Я ЙНРНПШЛ ПЮАНРЮЕЛ
*	value	гМЮВЕМХЕ МЮДОХЯХ ОН СЛНКВЮМХЧ, Р.Е. РНР ЯЮЛШИ placeholder
*	cssFilled	хЛЪ css-ЙКЮЯЯЮ ДКЪ НРНАПЮФЕМХЪ ГЮОНКМЕМНЦН ОНКЪ (ОПХЛЕМЪЕРЯЪ Й input)
*	cssEmpty	хЛЪ css-ЙКЮЯЯЮ ДКЪ НРНАПЮФЕМХЪ МСЯРНЦН ОНКЪ (ОПХЛЕМЪЕРЯЪ Й input)
*/
function InputPlaceholder (input, value, cssFilled, cssEmpty)
{
	var thisCopy = this
	this.Input = input
	this.Value = value
	this.SaveOriginal = (input.value == value)
	this.CssFilled = cssFilled
	this.CssEmpty = cssEmpty
	this.setupEvent (this.Input, 'focus', function() {return thisCopy.onFocus()})
	this.setupEvent (this.Input, 'blur', function() {return thisCopy.onBlur()})
	this.setupEvent (this.Input, 'keydown', function() {return thisCopy.onKeyDown()})
	if (input.value == '') this.onBlur();
	return this
}
InputPlaceholder.prototype.setupEvent = function (elem, eventType, handler)
{
	if (elem.attachEvent){
		elem.attachEvent ('on' + eventType, handler)
	}
	if (elem.addEventListener){
		elem.addEventListener (eventType, handler, false);
	}
}
InputPlaceholder.prototype.onFocus = function()
{

	if (!this.SaveOriginal && this.Input.value == this.Value){
		this.Input.value = ''
	}else{
		this.Input.className = this.CssFilled
	}
}

InputPlaceholder.prototype.onKeyDown = function()
{
	this.Input.className = this.CssFilled
}

InputPlaceholder.prototype.onBlur = function()
{
	if (this.Input.value == '' || this.Input.value == this.Value){
		this.Input.value = this.Value
		this.Input.className = this.CssEmpty
	}else{
		this.Input.className = this.CssFilled
	}
}
