$(document).ready(function() {
						   
	var name = $("#name"),
			email = $("#email"),
			email_a = $("#email_a"),
			name_a = $("#name_a"),
			password = $("#password"),
			allFields = $([]).add(name).add(email).add(name_a).add(password),
			tips = $("#validateTips");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}


	$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 600,
			width: 500,
			modal: true,
			buttons: {
				'Submit': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(email,"email",6,80);

					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. foo@bar.com");
					
					if (bValid) {
						/*$('#users tbody').append('<tr>' +
							'<td>' + name.val() + '</td>' + 
							'<td>' + email.val() + '</td>' + 
							'<td>' + password.val() + '</td>' +
							'</tr>'); */
						//$(this).dialog('close');
						// tähän lähetys
						form = $(this).find('form');
						$.ajax({
							type: form.attr('method'),
							url: form.attr('action'),
							dataType: 'text',
							data: form.serialize(),
							success: function(data) {
								//alert('succeess');
								//$("#dialog").dialog('close');
								//alert(data);
								$("#dialog form").prepend('<p style="margin: 20px 0; font-weight: bold;" class="thankyou">Thank you! Your message has been sent.</p>');
								var t = setTimeout('$("#dialog").dialog(\'close\')', 2000);
							},
							error: function() {
								$("#dialog").dialog('close');
								alert('Uh-oh, something went wrong...');
							}
						});

					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
	
	$("#dialog2").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 760,
			width: 500,
			modal: true,
			buttons: {
				'Submit': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(email_a,"email_a",6,80);
					bValid = bValid && checkLength(name_a,"name_a",1, 100);

					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email_a,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. foo@bar.com");
					
					if (bValid) {
						/*$('#users tbody').append('<tr>' +
							'<td>' + name.val() + '</td>' + 
							'<td>' + email.val() + '</td>' + 
							'<td>' + password.val() + '</td>' +
							'</tr>'); */
						//$(this).dialog('close');
						// tähän lähetys
						form = $(this).find('form');
						$.ajax({
							type: form.attr('method'),
							url: form.attr('action'),
							dataType: 'text',
							data: form.serialize(),
							success: function(data) {
								//alert('succeess');
								//$("#dialog").dialog('close');
								//alert(data);
								$("#dialog2 form").prepend('<p style="margin: 20px 0; font-weight: bold;" class="thankyou">Thank you! Your message has been sent.</p>');
								var t = setTimeout('$("#dialog2").dialog(\'close\')', 2000);
							},
							error: function() {
								$("#dialog2").dialog('close');
								alert('Uh-oh, something went wrong...');
							}
						});

					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		


	$('a#contact-us').click(function() {
		$("#dialog form p.thankyou").remove();							 	
		$('#dialog').dialog('open');
	});
	
	$('a#closed-beta').click(function() {
		$("#dialog2 form p.thankyou").remove();		
		$('#dialog2').dialog('open');
	});
	
	$('a#show-more').click(function() {
		$('div#partners-more').slideToggle();
		if ($(this).text() == 'Show more')
			$(this).text('Hide');
		else
			$(this).text('Show more');
	});
	
	$('div.athlete-info a').click(function(event) {
		event.preventDefault();
		var id = $(this).attr('id').substring(5);
		var win_h = $(window).height();
		var popup_h = $('div#ac-' + id).height();
		var popup_y = $(window).scrollTop() + parseInt((win_h - popup_h) / 2);
		$('body').append('<div id="popup-bg"></div>');	
		$('div#popup-bg').css({
			opacity: '0.7',
			display: 'none',
			width: $(document).width(),
			height:	$(document).height()
		}).show();
		$('div#ac-' + id).css({ display: 'block', top: popup_y + 'px' });
	});
	
	$('a.ac-close').click(function() {
		$(this).parent().parent().parent().hide();
		$('div#popup-bg').remove();
	});
	
	$('div#popup-bg').live('click', function() {
		$('div.athlete-card').hide();
		$(this).remove();
	});
	
	$('a#show-more-athlete').click(function() {
		/*alert($('div.athletes-more').css('display'));
		if ($('div.athletes-more').css('display') == 'block')
		{
			alert('toimii');
			$('div.athletes-more').hide();
		}
		else
			$('div.athletes-more').slideDown();*/
		$('div#athletes-more').slideToggle();
		if ($(this).text() == 'Show more')
			$(this).text('Hide');
		else
			$(this).text('Show more');
	});
});
