function Controller() {
	var controller = this;
	
	window.onload = function(event) { controller.onload(event); };
}

Controller.prototype = {
	loginFormActivated: false,

	onload: function(event) {
		this.init();
		
		this.initLoginForm();
		this.initSearchForm();
	},

	init: function() {
		var elementsIds = {
			loginField: 'loginForm_email',
			passwordField: 'loginForm_password',
			searchForm: 'search-form',
			searchField: 'search'
		};

		for (var property in elementsIds) {
			this[property] = window.document.getElementById(elementsIds[property]);
			
			if (this[property]) {
				this[property].controller = this;
			}
		}

		this.searchField.defaultTextColor = this.searchField.style.color;
	},
	
	initLoginForm: function() {
		if (this.isLoginElementsExists()) {
			this.replacePasswordToText();
			
			this.loginField.onfocus		= this.onFocusLoginForm;
			this.passwordField.onfocus	= this.onFocusLoginForm;
		}
	},

	initSearchForm: function() {
		this.searchForm.onsubmit	= this.validateSearchForm;
		this.searchField.onfocus	= this.onFocusSearchField;
		this.searchField.onblur		= this.onBlurSearchField;
	},
	
	isLoginElementsExists: function() {
		return this.loginField != null && this.passwordField != null;
	},
	
	onFocusLoginForm: function (event) {
		if (!this.controller.loginFormActivated) {
			this.controller.loginFormActivated = true;
			
			this.controller.replaceTextToPassword();
			this.controller.clearLoginFormFields();
			if (this != this.controller.loginField) {
				this.controller.passwordField.focus();
			}
		}
	},
	
	onFocusSearchField: function(event) {
		this.controller.clearSearchErrors();
		this.controller.clearDefaultSearchText();
	},
	
	onBlurSearchField: function(event) {
		this.controller.renewDefaultSearchText();
	},
	
	validateSearchForm: function() {
		if (this.controller.isSearchFieldHasNoValue(this.controller.searchField.value)) {
			this.controller.searchField.value = this.controller.errorMessage();
			this.controller.searchField.style.color = '#fdd068';
			
			return false;
		}
		
		return true;
	},
	
	isSearchFieldHasNoValue: function(value) {
		return !value ||
			(value == this.errorMessage()) ||
				(value == this.defaultSearchText());
	},
	
	clearSearchErrors: function() {
		if (this.searchField.value == this.errorMessage()) {
			this.searchField.value = '';
			this.searchField.style.color = this.searchField.defaultTextColor;
		}
	},
	
	clearDefaultSearchText: function() {
		if (this.searchField.value == this.defaultSearchText()) {
			this.searchField.value = '';
		}
	},
	
	renewDefaultSearchText: function() {
		if (this.isSearchFieldHasNoValue(this.searchField.value)) {
			this.searchField.value = this.defaultSearchText();
		}
	},
	
	errorMessage: function() {
		return 'Field can not be empty';
	},
	
	defaultSearchText: function() {
		return 'QUICK SEARCH';
	},
	
	replacePasswordToText: function() {
		var el = document.createElement('input');
		
		el.setAttribute('id', 'unhide_password');
		el.setAttribute('value', 'PASSWORD');
		el.onfocus = this.onFocusLoginForm;
		el.controller = this;
		
		this.passwordField.setAttribute('value', '');
		this.passwordField.style.display = 'none';
		this.passwordField.parentNode.appendChild(el);
	},
	
	replaceTextToPassword: function() {
		this.passwordField.parentNode.removeChild(document.getElementById('unhide_password'));
		this.passwordField.style.display = 'inline';
	},
	
	clearLoginFormFields: function() {
		this.loginField.setAttribute('value', '');
		this.passwordField.setAttribute('value', '');
	}
}


var aController = new Controller();
