function makeFooterStick() {
	var footerHeight = $('#footer').height();
	var pageHeight = $('#page').height();
	var currentWindowHeight = $(window).height();
	if (pageHeight < currentWindowHeight) {
		var mainHeight = currentWindowHeight - footerHeight - 114;
		$('#main').css({'height': mainHeight + 'px'});
	};
};

$(function(){

	// Random picture on all pages except attorney-profile ones
	if (!$('body').hasClass('attorney-profile')) {
		var randomSlide = Math.floor(Math.random()*3+1);
		$('#main').addClass('slide-' + randomSlide);
	};
	
	// Emails' obfuscation
	$('.attorney-profile #content h3').each(function(){
		if ($(this).text() === 'Contact') {
			var names = $(this).next().attr('id').split('_');
			var eAddress = names[0] + '.' + names[1] + '@s' + 'pvleg' + 'al.' + 'ro';			
			$(this).next().children('a').attr('href', 'mai' + 'lto' + ':' + eAddress).text(eAddress);
		};
	});
	$('span.contact').html('Ema' + 'il: <a' + ' href="ma' + 'ilt' + 'o:off' + 'ice' + '@s' + 'pvle' + 'gal' + '.' + 'ro">off' + 'ice' + '@s' + 'pvlegal' + '.' + 'ro </a>');


	// Make footer stick to the bottom if content is too short
	makeFooterStick();
	$(window).resize(function() {
		makeFooterStick();
	})
	
	// Hover on forms
	$('form fieldset.submit input').hover(
		function() {$(this).attr('src', 'img/bg-form-submit-hover.png')},
		function() {$(this).attr('src', 'img/bg-form-submit.png')}			
	);
	
	// Apply fancy styling to forms
	var fancyForms = ['subscribe-form', 'contact-form'];
	$.each(fancyForms,
	   function() {
			// Apply 'fancy' class to form
			var currentForm = $('form#' + this);
			currentForm.addClass('fancy');

			// Remove label on non-null fields
			var currentFormFieldsets = $('form#' + this + ' fieldset.text, form#' + this + ' fieldset.textarea');
			currentFormFieldsets.each(
				function() {
					if ($(this).children('input, textarea').attr('value') != undefined) {
						if($(this).children('input, textarea').attr('value') != '') {
							$(this).addClass('nonempty');
						}
					}
				}
			);

			// Red labels on error fields
			var errorFields = $('form#' + this + ' input.error, form#' + this + ' textarea.error');
			errorFields.each(
				function() {
					$(this).parent().children('label').css({color: '#990000'});
				}
			);

			// Remove label on active fields
			var currentFormInputs = currentFormFieldsets.children('input, textarea');
			currentFormInputs.focus(
				function() {
					$(this).parent().addClass('active');
					$(this).removeClass('error');
				}
			);
			currentFormInputs.change(
				function() {
					$(this).parent().addClass('active');
					$(this).removeClass('error');
				}
			);
			currentFormInputs.blur(
				function() {
					if ($(this).val() == '') {
						$(this).parent().removeClass('active');
						$(this).removeClass('error');
						$(this).removeClass('nonempty')
					}
					else {
						$(this).parent().removeClass('active');
						$(this).parent().addClass('nonempty');
					}
				}
			);
	   }
	)
});
