/*
 * -----------------------------------------------------------------
 * Copyright (c) 2009 Fluid, Inc. All Right Reserved.
 * This software is the proprietary information of Fluid, Inc.
 * Use is subject to strict licensing terms.
 * -----------------------------------------------------------------
 *
 * CVS Information:
 * $Id: fluid.facebook-pdp-connector.js.php,v 1.12 2009/12/23 15:24:56 akerr Exp $
 * $Author: akerr $
 * $Date: 2009/12/23 15:24:56 $
 */

var FluidSocialPDPConnector = function () {
	// Initialize the flXHR plugin so that we can do cross-domain calls.
	$.flXHRproxy.registerOptions("http://fss.fluidretail.net/licensee/Ghirardelli/fb/connect/", {xmlResponseText: false, instancePooling: false});
	jQuery.ajaxSetup({transport: 'flXHRproxy'});
};

FluidSocialPDPConnector.prototype = {
	
	addProduct: function(facebookUserId, productId, productName, pdpUrl, thumbnailUrl, mainImageUrl, callback) {
		var actionUrl = "http://fss.fluidretail.net/licensee/Ghirardelli/fb/connect/add_product.php";		
		this.debug([actionUrl, productId, productName, pdpUrl, thumbnailUrl, mainImageUrl]);
		
		if (mainImageUrl == null)
			mainImageUrl = '';

		$.ajax({
			cache: false,
			type: 'POST',
			url: actionUrl,
			dataType: 'html',
			data: {
				user: facebookUserId,
				productId: productId,
				productName: productName,
				pdpUrl: pdpUrl,
				thumbnailUrl: thumbnailUrl,
				mainImageUrl: mainImageUrl
			},
			success: function(data, textStatus) {
				callback(data);
			}
		});
	},
	
	postReview: function(facebookUserId, productId, productName, pdpUrl, thumbnailUrl, mainImageUrl, rating, review, successCallback, errorCallback) {
		var actionUrl = "http://fss.fluidretail.net/licensee/Ghirardelli/fb/connect/post_review.php";		
		
		// Note that we pass all info about the product just in case no user has ever shared it before.
		// If that's the case, the post review code has to create the product record before it can 
		// create a review for it.
				
		if (mainImageUrl == null)
			mainImageUrl = '';

		$.ajax({
			cache: false,
			type: 'POST',
			url: actionUrl,
			dataType: 'html',
			data: {
				user: facebookUserId,
				productId: productId,
				productName: productName,
				pdpUrl: pdpUrl,
				thumbnailUrl: thumbnailUrl,
				mainImageUrl: mainImageUrl,
				rating: rating,
				review: review
			},
			success: function(data, textStatus) {
				successCallback(data);
			},
			error: function(xhr, textStatus, errorThrown) {
				errorCallback(errorThrown);
			}
		});
	},
	
	getNewestReview: function(facebookUserId, friendIds, productId, callback) {
		var actionUrl = "http://fss.fluidretail.net/licensee/Ghirardelli/fb/connect/get_newest_review.php";		

		$.ajax({
			cache: false,
			type: 'POST',
			url: actionUrl,
			dataType: 'html',
			data: {
				user: facebookUserId,
				friendIds: friendIds.join(),
				productId: productId
			},
			success: function(data, textStatus) {
				callback(data);
			}
		});
	},
	
	getFriendReviews: function(facebookUserId, facebookUserTimezone, friendIds, productId, callback) {
		var actionUrl = "http://fss.fluidretail.net/licensee/Ghirardelli/fb/connect/get_friend_reviews.php";		
		
		$.ajax({
			cache: false,
			type: 'POST',
			url: actionUrl,
			dataType: 'html',
			data: {
				user: facebookUserId,
				timezone: facebookUserTimezone,
				friendIds: friendIds.join(),
				productId: productId
			},
			success: function(data, textStatus) {
				callback(data);
			},
			error: function(xhr, textStatus, errorThrown) {
				fluidSocial.debug("ERROR: " + errorThrown);
			}
		});
	},
	
	getFriendFavorites: function(facebookUserId, friendIds, callback) {
		var actionUrl = "http://fss.fluidretail.net/licensee/Ghirardelli/fb/connect/get_friend_favorites.php";		
		
		$.ajax({
			cache: false,
			type: 'POST',
			url: actionUrl,
			dataType: 'html',
			data: {
				user: facebookUserId,
				friendIds: friendIds.join()
			},
			success: function(data, textStatus) {
				callback(data);
			}
		});
	},
	
	sendFeedback: function(reason, email, subject, message, callback) {
		var actionUrl = "http://fss.fluidretail.net/licensee/Ghirardelli/fb/connect/send_feedback.php";		
		
		$.ajax({
			cache: false,
			type: 'POST',
			url: actionUrl,
			dataType: 'html',
			data: {
				reason: reason,
				email: email,
				subject: subject,
				message: message
			},
			success: function(data, textStatus) {
				callback(data);
			}
		});
	},
	
	debug: function(message) {
		if (window.console)
			console.log(message);
	}
};
