$(document).ready(function() {



	$(document.body).append('<div id="overlay-container"><div id="overlay-close"><\/div><div id="overlay-bg"><\/div><div id="overlay"><\/div><\/div>');
	

	$('.overlay a').each(function() {
							
		$(this).click(function() {
			
			var href = $(this).attr('href');
			
			var w = $(window).width();
			var h = $(window).height();

			var st = $(document).scrollTop();
			var sl = $(document).scrollLeft();

			var padding = 170;
			
			var pos = $(this).offset($(document.body));

			var to_css = {
				top: padding + 'px',
				left: padding + 'px',
				width: (w-2*padding) + 'px',
				height: (h-2*padding) + 'px',
				opacity: 1
			};
	
	
			pos.top -= st;
			pos.left -= sl;
	
			$('#overlay-bg').fadeTo(0,0).fadeTo(700,0.3);
			$('#overlay-container').show();
	
			$('#overlay')
			.css({
				top: pos.top + 'px',
				left: pos.left + 'px',
				width: $(this).width() + 'px',
				height: $(this).height() + 'px',
				display: 'block',
				opacity: 0.2
			})
			.animate(to_css, {
				easing: 'easeInOutExpo',
				duration: 500
			});
			
			
			$('#overlay-close').hide();
			setTimeout(function() {
				$.get(href, {ajax: 'true'}, function(html) {
										
					$('#overlay').html('<div class="overlay-content" style="display: none;">' + html + '<\/div>');
					if ($.browser.msie) {
						// IE doesn't handle the fade in properly.
						$('#overlay .overlay-content').show();
					}
					else {
						$('#overlay .overlay-content').fadeIn();
					}

				},'html');
			},400);

			setTimeout(function() {
				if ($.browser.msie) {
					$('#overlay-close').show();
					$('#overlay-close').ifixpng();
				}
				else {
					$('#overlay-close').fadeIn();
				}
			}, 600);
			
			return false;
		});
		
		$('#overlay-close, #overlay-container').click(function() {
			$('#overlay-container').fadeOut(300, function() {
				$('#overlay').text('');
			});
		});
		
		$('#overlay').click(function() {
			return false;
		});
		
	});
	
	
	$.ifixpng('/docroot/macforstudents/images/pixel.gif');
	
	
	 $("a.showBigOne").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true, 'imageScale': false });
	  
});



MFS = {

	validate: function(form) {
		var valid = true;

		$(form).find('input').each(function() {
			if ($(this).hasClass('required')) {
				if ($(this).attr('type') == 'text') {
					$(this).removeClass('error');
					if ($.trim($(this).val()) == '') {
						valid = false;
						$(this).addClass('error');
					}

					if ($(this).hasClass('email')) {
						if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($(this).val())) {
							$(this).addClass('error');
						}
					}
				}
				else if ($(this).attr('type') == 'checkbox') {
					$(this).parent().removeClass('error');
					if (!$(this).attr('checked')) {
						valid = false;
						$(this).parent().addClass('error');
					}
				}
			}
		});

		return valid;
	},
	
	submit: function(form) {

		if (!this.validate(form)) return false;
		
		var args = {};
				
		$(form).find('input').each(function() {
			
			if ($(this).attr('type') == 'checkbox' && $(this).attr('checked')) {
				args[$(this).attr('name')] = $(this).val();
			}
			else if ($(this).attr('type') == 'text' || $(this).attr('type') == 'hidden'){
				args[$(this).attr('name')] = $(this).val();
			}
		});


		args.ajax = 'true';
		
		$('#main .form').fadeTo(200,0);
		
		$.post($(form).attr('href'), args, function(r) {
			$('#main').html(r);
			$('#main .form').stop().fadeTo(400,1);
		});	
		

		if (form.id == 'competitionform') {
			setTimeout(function() {
				window.location.href = 'store';
			},5000);
		}
		
		
		return false;
	}
}


