
$(function() {

	$(".rowIcon li").hover( function () {
		$(this).children("ul").show();
	        },function(){
		$(this).children("ul").hide();
	});//hover

	$(".rowIcon li a.imageLink").click( function (event) {
		event.preventdefault;
		
		return false;
	});//hover

	$(".rowIconFriends li").hover( function () {
		$(this).children("ul").show();
	        },function(){
		$(this).children("ul").hide();
	});//hover

	$(".rowIconFriends li a.imageLink").click( function (event) {
		event.preventdefault;
		
		return false;
	});//hover

	// attach a target to all the external links.
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});

	// form validation happy or sad.
	$('input[validation*=required]').blur(function(event) {

    	var correct = true;

		if ( !$(this).attr('validationType') ) {
			$(this).attr('validationType', 'text');
		}

		// check for blank
		if ( $(this).val() == '' ) {
			correct = false;
		}

		// if it should be an email address, validate that.
		if ( $(this).val() != '' && $(this).attr('validationType') == 'email' && !isEmail($(this).val()) ) {
			correct = false;
		}

		// see if this is a paired entry.
		if ( $(this).attr('linked') ) {
			if ( $(this).val() != $('#' + $(this).attr('linked')).val() ) {
				correct = false;
			}
		}

		if ( correct ) {
			$('#' + $(this).attr('name') + 'Marker').html('<img src="/images/smiley.gif" alt="correct" />');
		} else {
			$('#' + $(this).attr('name') + 'Marker').html('<img src="/images/saddey.gif" alt="incorrect" />');
		}


	});

	$('.facebookLogin').click(function(event) {
		FB.login(function(response) {
			if (response.authResponse) {
				FB.api('/me', function(response) {

					var postData = '';
					postData += '&action=login';
					postData += '&username=' + response.email;

					// send it
					$.ajax({
						type: 'POST',
						url: '/sign-in.php',
						data: postData,
						success : function (data, statusText) {
							if ( data.status == 'ok' ) {
                                                                top.location = '/user/';
                                                             }
                                                        else {
                                                            //top.location = '/sign-up?unregistered=true/';
                                                            window.location.href = '/sign-up?facebook-sign-up/';
                                                            //FB.logout(function(r){});
                                                        }
						},
						complete: function (event, ui) { /*alert('complete');*/ }
					});
				});
			} else {
		    // user cancelled login
		  	}
		}, {scope:'email'});
	});

	$('#sendAnInvite').focus(function() {
		if ( $(this).val() == 'Sent!' ) {
			$(this).val('')
		}
	});

	$('#sendAnInvite').click(function(event) {

		event.preventdefault;

		var postData = '';

		postData += 'action=submit';
		postData += '&emails=' + $('#emails').val();

		// send it
		$.ajax({
			type: 'POST',
			url: '/invite-friends.php',
			data: postData,
			success : function (data, statusText) {
				if ( data.status == 'ok' ) {
					$('#emails').val('Sent!');
				}
			},
			complete: function (event, ui) { }
		});


		return false;

	});

});


function isEmail(value) {
	// contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
	return /^((([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.test(value);
}

