
// Declare setup function.
var setup;

// Define setup function.
setup = function () {

	//
	// Declare inner setup functions.
	//
	var inputFocus, shadow, unshadow, loginShadowText, searchShadowText, subscribeShadowText, popupWidget;

	//
	// Set up shadow function.
	//
	shadow = (function () {
		if (!this.getProperty('value')) {
			this.setProperty('value', this.getProperty('shadow'));
		}
		if (this.getProperty('value') === this.getProperty('shadow')) {
			this.setStyle('color', '#bbbbbb');
		}
		return true;
	});

	//
	// Set up unshadow function.
	//
	unshadow = (function () {
		if (this.getProperty('value') === this.getProperty('shadow')) {
			this.setProperty('value', '');
		}
		this.setStyle('color', '#000000');
		return true;
	});

	//
	// Set the focus classes for all text input fields.
	//
	inputFocus = (function () {
		var inputs;
		inputs = $(document).getElements('input[type=text],input[type=password],select');
		inputs.addEvent('focus', function () {
			this.addClass('focus');
		});
		inputs.addEvent('blur', function () {
			this.removeClass('focus');
		});
		return true;
	}());

	//
	// Set up the shadow text for the login.
	//
	loginShadowText = (function () {
		var login, email, password;

		// Get references to input fields.
		login = $('login');
		if (login) {
			email = login.getElement('input[name=u_email]');
			password = login.getElement('input[name=u_password]');
		}

		// If references are available, start setup.
		if (email && password) {

			// Set shadow text.
			email.setProperty('shadow', 'Email address');
			password.setProperty('shadow', 'Password');

			// Attach shadowing events.
			email.addEvent('blur', shadow);
			password.addEvent('blur', shadow);

			// Attach unshadowing events.
			email.addEvent('focus', unshadow);
			password.addEvent('focus', unshadow);

			// Fire blur methods.
			email.fireEvent('blur');
			password.fireEvent('blur');
		}

		// Done.
		return true;
	}());

	//
	// Set up the shadow text for the search.
	//
	searchShadowText = (function () {
		var search, keywords;

		// Get reference to input field.
		search = $('search');
		if (search) {
			keywords = search.getElement('input[name=all_keywords]');
		}

		// If a reference is available, start setup.
		if (search) {

			// Set shadow text.
			keywords.setProperty('shadow', 'Keywords');

			// Attach shadowing event.
			keywords.addEvent('blur', shadow);

			// Attach unshadowing event.
			keywords.addEvent('focus', unshadow);

			// Fire blur method.
			keywords.fireEvent('blur');
		}

		// Done.
		return true;
	}());


	//
	// Set up the shadow text for the subscription widget.
	//
	subscribeShadowText = (function () {
		var subscribe, email;

		// Get reference to input field.
		subscribe = $('subscribe-widget');
		if (subscribe) {
			email = subscribe.getElement('input[name=email]');
		}

		// If a reference is available, start the setup.
		if (email) {

			// Set shadow text.
			email.setProperty('shadow', 'Email address');

			// Attach shadowing event.
			email.addEvent('blur', shadow);
			
			// Attach unshadowing event.
			email.addEvent('focus', unshadow);

			// Fire blur method.
			email.fireEvent('blur');
		}

		// Done.
		return true;
	}());

	//
	// Set up the popup widget.
	//
	popupWidget = (function () {
		var popup, close;

		// Get reference to popup widget.
		popup = $('popup-widget');

		// Okay; watch carefully. If we have the popup widget ...
		if (popup) {

			// ... and we can't get the browser to say the user has already closed the ad ...
			if (!Cookie.read('popup-closed')) {

				// ... then show it ...
				popup.setStyle('display', 'block');

				// ... try to find its close button ...
				close = popup.getElement('div.close');

				// ... and if we do ...
				if (close) {

					// ... make the mouse hovering over it a pointer ...
					close.setStyle('cursor', 'pointer');

					// ... and add a click event to close the ad and write the cookie.
					close.addEvent('click', function () {
						Cookie.write('popup-closed', '1', {duration: 1});
						popup.setStyle('display', 'none');
					});
				}
			}
		}
	}());
};

//
// Run setup functions as soon as the DOM is ready.
//
$(window).addEvent('domready', function () {
	setup();
});
