﻿// <copyright file="Facebook.js" company="Fuse Digital">
// Copyright (c) 2011 All Right Reserved
// </copyright>
// <author>Tim Knight/Sean Greasley</author>
// <email>sean.greasley@fusedigital.com</email>
// <summary>Facebook helper javascript file</summary>

// Sign in with facebook
function facebookLogin() {

	FB.login(function (response) {
		if (response.session) {
			$.ajax({
				type: "POST",
				url: "services/Service.asmx/FacebookSignInClient",
				data: "{userID: '{0}'}".format(response.session.uid),
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (data) {

					if (data.d != null && data.d == 'ok') {
						// Refresh page
						location.reload(true);
					}
					else {
						if (data.d != '')
							$("[id$='lblLoginStatus']").html(data.d);

						$("[id$='lblLoginStatus']").show();
					}
				},
				error: function (data) {
					$("[id$='lblLoginStatus']").show();
				}
			});
		}
	}, { perms: 'email,publish_stream' });
}


// Register with facebook
function facebookRegister() {

	FB.login(function (response) {
		if (response.session) {
			populateRegisterFromFacebook();
		}
	}, { perms: 'email,publish_stream' });
}


// Pulls back user information from facebook
// and populates the fields on the registration page
function populateRegisterFromFacebook() {
	FB.api('/me', function (response) {
		var query = FB.Data.query('select first_name, last_name,email,sex  from user where uid={0}', response.id);
		query.wait(function (rows) {

			$("[id$='hdnFacebookRegister']").val(rows[0].uid);
			$("[id$='txtFirstName']").val(rows[0].first_name);
			$("[id$='txtLastName']").val(rows[0].last_name);
			$("[id$='txtEmail']").val(rows[0].email);

			// Gender
			if (rows[0].sex.toLowerCase() == 'male')
				$("[id$='ddlSex'] option[value=m]").attr('selected', 'selected');
			else
				$("[id$='ddlSex'] option[value=f]").attr('selected', 'selected');

			// Tweak reg form for fb connect
			alterRegFormForFacebook();
		});
	});
}

// Posts a message on the users wall to say that they
// have registered with Facebook Connect.
function postRegistrationToUserWall(baseURL) {
	FB.getLoginStatus(function (response) {
		if (response.session) {

			var params = {};
			params['message'] = 'I have just signed up to Get Into Newcastle.';
			params['name'] = 'Get Into Newcastle';
			params['description'] = 'The site that brings you the latest events, offers and discounts so you can enjoy the best that Newcastle has to offer.';
			params['link'] = baseURL;
			if (baseURL != null)
				params['picture'] = baseURL + '_assets/images/fb_feed_picture.jpg';
			params['caption'] = 'NE1 for Everyone';


			FB.api('/me/feed', 'post', params, function (response) {
				if (!response || response.error) {
					// Error occured posting to the fb wall
				}
			});

		} else {
			// User isn't authenticated with facebook
		}
	});
}

// Alters the registration page for FB users
// I.e. no password field.
function alterRegFormForFacebook() {
	// No password for facebook connect
	$("[id$='txtRegPassword']").val("facebook");
	$("#lblRegPassword").hide();
	$("[id$='txtRegPassword']").hide();
}
