jQuery.fn.getEmptyFields = function() {
	var ret = [];
	
	this.each(function() {
		if(/form/.test(this.nodeName)) {
			for(var i=this.elements.length - 1; i>=0; i--) {
				var element = this.elements[i];
		
				if(!/button|submit|image/i.test(element.type) && !$(element).val()) {
					ret.push(element);
				}
			}
		}
	});

	return jQuery(ret);
};

$(function() {
	var LANG = document.body.parentNode.lang,
		currentHash = '';

	function openBox(box) {
		$box = $(box);
		
		if($box.length && !$box.hasClass('selected')) {
			currentHash = location.hash = $('#language')[0].hash = '#' + $box[0].id;

			var trans = {
						'height':390,
						'width':390,
						'backgroundPositionX':-590,
						'backgroundPositionY':-12
					};

			var contents = $box.first().children(':not(.boxshadow)');

			if($box.css('margin-top') !== '0px') {
				trans['margin-top'] = -195;
			}

			if($box.css('margin-left') !== '0px') {
				trans['margin-left'] = -195;
			}

			$box.animate(trans, {
				duration: 400,
				queue: false,
				complete: function() {
					$box.removeAttr('style');
					contents.show();
				}
			});

			contents.hide();
			
			$box.addClass('selected');
		}
	}

	function closeBox(box) {
		$box = $(box || '.box.active.selected');

		if($box.length) {
			var trans = {
					'height':120,
					'width':120
				};

			var contents = $box.first().children(':not(.boxshadow)');

			if($box.css('margin-top') !== '0px') {
				trans['margin-top'] = -60;
			}

			if($box.css('margin-left') !== '0px') {
				trans['margin-left'] = -60;
			}

			$box.animate(trans, {
				duration: 200, 
				queue: false,
				complete: function() {
					$box.removeAttr('style').removeClass('selected');
				}
			});

			contents.hide();
		}

		currentHash = location.hash = $('#language')[0].hash = '';
	}

	$('.active.box').each(function(){
		$('<a>').prop({
			href: '#',
			className: 'closebutton',
			title: 'Close'
		})
		.text('Close')
		.click(function(evt) {
			closeBox(this.parentNode.parentNode);
			evt.stopPropagation();
			evt.preventDefault();
		})
		.prependTo($(this).children()[0]);

		$(this).click(function(evt) {
			openBox(this);
		});
	});

	$('#contact form').bind('submit', function(evt) {
		if($(this).getEmptyFields().length) {
			alert(LANG == 'el'? 'Παρακαλώ συμπληρώστε όλα τα πεδία.' : 'Please complete all fields.');

			evt.preventDefault();
		}
	});

	function navigate() {
		if(location.hash != currentHash) {
			if(location.hash) {
				// A box needs to be opened
				var box = $(location.hash);

				if(box && box.hasClass('active')) {
					openBox(box);
					var currentHash = location.hash;
				}
			}
			else {
				// Home, sweet home
				closeBox();
			}
		}
	}

	if(location.hash) {
		navigate();
	}
	else if($('#contact .message').length) {
		// A mail was sent, navigate to #contact
		$('#contact dl').hide();

		$('.message a.ifjs').click(function(evt) {
			evt.preventDefault();
			
			$(this).parent().hide();
			$('#contact dl').show();
		})

		openBox($('#contact'));
	}

	// Check every half second to see whether the hash has changed (for back/forward buttons)
	setInterval(navigate, 500);
});

