/*
	jQuery follow manager
	Version 1.0 (09/30/2009)

	Author: Heidi Hysell (heidi.hysell@mtvnmix.com)

	About:
		This plugin allows for a type of content to be followed by a user and provides the appropriate calls for it.

	API:
		// tells the application to start following the piece of content for the logged in user
		$.followManager.startFollowing
		
		// tells the application to stop following the piece of content for the logged in user - removes the content from the internally stored list for that user
		$.followManager.stopFollowing
		
		// tells the application to stop following all the pieces of content for the logged in user - removes the content from the internally stored list for that user
		$.followManager.stopFollowingAll
		
		// tells the application to enable email notifications for a particular piece of content that the user is already following
		// if you are just adding the piece of content please use the startFollowing function and set the notify parameter
		$.followManager.startEmailNotifications

		// tells the application to disable email notifications for a particular piece of content that the user is already following
		// if you are just removing the piece of content please use the stopFollowing function and set the notify parameter
		$.followManager.stopEmailNotifications

		// set the sort order of the item
		$.followManager.setSortOrder

	 	// get the content that the user is following
	 	$.followManager.getContentUserIsFollowing
		     
		// get followers emails addresses for requested content
		$.followManager.getFollowersForContent
		     
		// find out whether the user is following the requested piece of content
		$.followManager.isFollowing

		// this is called when a content item has been updated - it will update the way we store that item for sorting purposes - this is not for end users
		$.followManager.setLastUpdatedForContent
		     
		// this is called when a content item needs a full service process - follow item, set last updated and return the list of all users following that content
		$.followManager.processContentType
		
		// this is called when a content item needs a full service process - follow item, set last updated and return the list of all users following that content
		$.followManager.sendToFriend
		     
	Example Usage:

		$.followManager.getContentUserIsFollowing({
			successCallback: contentListSuccess,
			failureCallback: contentListFailure,
			page: 1,
			pageSize: 20,
			sort: "RecentlyAdded"
		});

		function contentListSuccess(list) {
			// perform updates on success
		}
		
		function contentListFailure(list) {
			// perform updates on failure
		}

	Dual licensed under the MIT and GPL licenses:
	http://www.opensource.org/licenses/mit-license.php
	http://www.gnu.org/licenses/gpl.html

*/
// TODO: put a namespace on this class
// TODO: refactor from followManager to trackingManager
(function($){ 
	jQuery.followManager = function(options) {

	 };  
	 
	 jQuery.followManager.getEnvUrl = function() {
		 var fullPath = $("script[src$='jquery.follow.js']").attr("src");
		 var regexp = /http:\/\/[^\/]+/;
		 var re = new RegExp(regexp);
		 var envUrl = re.exec(fullPath);
		 if (envUrl == null) {
			 envUrl = "http://" + window.location.host;
		 }
		 return envUrl;
	 }
	 
	 jQuery.followManager.startFollowing = function(options) {
		 //alert("jQuery.followManager.startFollowing just triggered!");
		 
		 var container = this;
		 var settings = jQuery.extend({
		     type: "post", // post, person
		     mgid: "123",
		     notify: true,
		     sortOrder: 1,
		     service: "following",
		     envUrl: "http://gocitykids.parentsconnect.com",
		     successCallback: startFollowSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: startFollowFailure // if ajax failed then we get sent to this function
		   }, options);

		 // use ajax to call start email notifications
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/start/" + settings.mgid + "/" + settings.notify + "/" + settings.sortOrder + "?callback=?",
			  success: settings.successCallback,
			  failure: settings.failureCallback,
			  cache: false,
			  dataType: "jsonp"
			});
		 
		 function startFollowSuccess() {
			 alert("SUCCESS");
		 }
		 
		 function startFollowFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
		 
		 return true;
	 };
	 
	 jQuery.followManager.stopFollowing = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     type: "post", // post, person
		     mgid: "123",
		     envUrl: "http://gocitykids.parentsconnect.com",
		     service: "following",
		     successCallback: stopFollowSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: stopFollowFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call stop following
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/stop/" + settings.mgid + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});
		 
		 function stopFollowSuccess() {
			 ;
		 }
		 
		 function stopFollowFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
		 
		 return true;
	 };
	 
	 jQuery.followManager.stopFollowingAll = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     type: "post", // post, person
		     mgid: "123",
		     envUrl: "http://gocitykids.parentsconnect.com",
		     service: "following",
		     successCallback: stopFollowSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: stopFollowFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call stop following
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/stop/all?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});
		 
		 function stopFollowSuccess() {
			 ;
		 }
		 
		 function stopFollowFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
		 
		 return true;
	 };

	 jQuery.followManager.startEmailNotifications = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     type: "post", // post, person
		     mgid: "123",
		     envUrl: "http://gocitykids.parentsconnect.com",
		     service: "following",
		     successCallback: notificationSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: notificationFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call start email notifications
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/start/notifications/" + settings.mgid + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});
		 
		 function notificationSuccess() {
			 ;
		 }
		 
		 function notificationFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
		 
		 return true;
	 };
	 
	 jQuery.followManager.stopEmailNotifications = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     type: "post", // post, person
		     mgid: "123",
		     envUrl: "http://gocitykids.parentsconnect.com",
		     service: "following",
		     successCallback: notificationSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: notificationFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call stop email notifications
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/stop/notifications/" + settings.mgid + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});
		 
		 function notificationSuccess() {
			 ;
		 }
		 
		 function notificationFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
		 return true;
	 };
	 
	 // set the sort order of the item
	 jQuery.followManager.setSortOrder = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
			 type: "post", // post, person
		     envUrl: "http://gocitykids.parentsconnect.com",
		     service: "following",
		     mgid: "123",
		     autoIncrease: true,
		     order: -1,
		     successCallback: contentSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: contentFailure // if ajax failed then we get sent to this function
		 }, options);
		 
		 var sortCall = (settings.order > -1) ? "/" + settings.order : "/increase/" + autoIncrease;
		 
		 // use ajax to call set sort order
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/order/" + settings.mgid + "" + sortCall + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});

		 function contentSuccess() {
			 ;
		 }
		 
		 function contentFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 
	 jQuery.followManager.getContentUserIsFollowing = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     type: "post", // post, person
		     page: "1",
		     pageSize: "20",
		     sort: "RecentlyCommented",
		     envUrl: "http://gocitykids.parentsconnect.com",
		     service: "following",
		     userId: "",
		     successCallback: contentSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: contentFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 var userId = (settings.userId != "") ? settings.userId + "/" : settings.userId;
		 
		 // use ajax to call get content
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/list/" + userId + settings.page + "-" + settings.pageSize + "-" + settings.sort + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});

		 function contentSuccess() {
			 ;
		 }
		 
		 function contentFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 	 
	 jQuery.followManager.isFollowing = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     type: "post", // post, person
		     mgid: "123",
		     envUrl: "http://gocitykids.parentsconnect.com",
		     service: "following",
		     successCallback: isFollowSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: isFollowFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call followers for content
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/" + settings.mgid + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});
		 
		 function isFollowSuccess() {
			 ;
		 }
		 
		 function isFollowFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 
	 /*
	 =======================================
	 BELOW IS FRIENDING FUNCTIONS
	 =======================================
	 */
	 /** FIXME: DEPRECATED - this should call startFollowing with the proper service and type passed in */
	 jQuery.followManager.startFriending = function(options) {
		 //alert("jQuery.followManager.startFriending just triggered!");
		 var container = this;
		 var settings = jQuery.extend({
		     type: "friend_request",
		     mgid: "",
		     msg: "",
		     notify: true,
		     service: "friending",
		     envUrl: "http://" + window.location.hostname,
		     successCallback: startFriendSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: startFriendFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 	//console.log("msg: " + settings.msg);
		 	
		   // console.log( "about to trigger ajax call using: "+settings.envUrl + "/friending/" + settings.type + "/start/" + settings.mgid + "?callback=?");
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/friending/" + settings.type + "/add/" + settings.mgid + "?msg=" + settings.msg + "&callback=?",
			  success: settings.successCallback,
			  failure: settings.failureCallback,
			  cache: false,
			  dataType: "jsonp"
			});
		 
		 function startFriendSuccess() {
			 alert("SUCCESS");
		 }
		 
		 function startFriendFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
		 
		 return true;
	 };

	 /** FIXME: DEPRECATED - this should call stopFollowing with the proper service and type passed in */
	 jQuery.followManager.stopFriending = function(options) {
		 //alert("jQuery.followManager.stopFriending just triggered!");
		 var container = this;
		 var settings = jQuery.extend({
		     type: "friend",
		     mgid: "",
		     envUrl: "http://" + window.location.hostname,
		     successCallback: stopFriendSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: stopFriendFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call stop following
		 //console.log( "about to trigger ajax call using: "+settings.envUrl + "/friending/" + settings.type + "/remove/" + settings.mgid + "?callback=?");
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/friending/" + settings.type + "/remove/" + settings.mgid + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});
		 
		 function stopFriendSuccess() {
			 ;
		 }
		 
		 function stopFriendFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
		 
		 return true;
	 };


	 jQuery.followManager.getPendingFriendsList = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     page: "1",
		     pageSize: "20",
		     envUrl: "http://" + window.location.hostname,
		     successCallback: contentSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: contentFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call get content
		 //console.log("getPendingFriendsList: "+settings.envUrl + "/friending/friend_request/list");
		 $.ajax({
			  type: "GET",
			  //url: settings.envUrl + "/following/" + settings.type + "/list/" + settings.page + "-" + settings.pageSize + "-" + settings.sort + "?callback=?",
			  url: settings.envUrl + "/friending/friend_request/list?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});
		 

		 function contentSuccess() {
			 alert("contentSuccess");
		 }
		 
		 function contentFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 
	 /** FIXME: DEPRECATED - this should call getContentUserIsFollowing */
	 jQuery.followManager.getFriendsList = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     type: "friend", // post, friend
		     page: "1",
		     pageSize: "20",
		     sort: "RecentlyAdded",
		     envUrl: "http://" + window.location.hostname,
		     successCallback: contentSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: contentFailure // if ajax failed then we get sent to this function
		   }, options);
		 //console.log(settings.envUrl + "/friending/" + settings.type + "/list/" + settings.page + "-" + settings.pageSize + "-" + settings.sort + "?callback=?");
		 // use ajax to call get content
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/friending/" + settings.type + "/list/" + settings.page + "-" + settings.pageSize + "-" + settings.sort + "?callback=?",
			  cache: false,
			  dataType: "jsonp",

			  success: settings.successCallback,
			  failure: settings.failureCallback
			});

		 function contentSuccess() {
			 ;
		 }
		 
		 function contentFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 jQuery.followManager.getFriendsListOfSomeone = function(options) {
		 //console.log("getFriendsListOfSomeone:::::> ");
		 var container = this;
		 var settings = jQuery.extend({
		     type: "friend", // post, friend
		     page: "1",
		     mgid: "1",
		     pageSize: "20",
		     sort: "RecentlyAdded",
		     envUrl: "http://" + window.location.hostname,
		     successCallback: contentSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: contentFailure // if ajax failed then we get sent to this function
		   }, options);
		 //console.log("getFriendsListOfSomeone: =============================== \n " + settings.envUrl + "/friending/list-their-friends/" + settings.mgid + "/" + settings.page + "-" + settings.pageSize + "-" + settings.sort + "?callback=?");
		 // use ajax to call get content
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/friending/list-their-friends/" + settings.mgid + "/" + settings.page + "-" + settings.pageSize + "-" + settings.sort + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});

		 function contentSuccess() {
			 ;
		 }
		 
		 function contentFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 
	 jQuery.followManager.getRelationship = function(options) {
		 //alert("jQuery.followManager.getRelationship is called!");
		 var container = this;
		 var settings = jQuery.extend({
			 service: "friending",
		     type: "status", // post, friend
		     mgid: "123",
		     envUrl: "http://" + window.location.hostname,
		     successCallback: isFriendSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: isFriendFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 //console.log( "about to trigger ajax call using: "+settings.envUrl + "/friending/status/" + settings.mgid + "?callback=?");
		 // use ajax to call followers for content
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/" + settings.mgid + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});
		 
		 function isFriendSuccess() {
			;
		 }
		 
		 function isFriendFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 
	 /*
	 =======================================
	 BELOW IS NAMESLIST FUNCTIONS
	 =======================================
	 */
	 jQuery.followManager.getSharableList = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     envUrl: "http://gocitykids.parentsconnect.com",
		     mgid: "123",
		     service: "nameslist",
		     type: "babyname",
		     successCallback: contentSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: contentFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call get content
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/list/" + settings.mgid + "?callback=?",
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});

		 function contentSuccess() {
			 ;
		 }
		 
		 function contentFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 
	 jQuery.followManager.registerVote = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     envUrl: "http://gocitykids.parentsconnect.com",
		     mgid: "",
		     nameid: "",
		     service: "nameslist",
		     type: "babyname",
		     vote: true,
		     successCallback: contentSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: contentFailure // if ajax failed then we get sent to this function
		   }, options);
		 var mgid = (settings.mgid=="") ? "" : settings.mgid + "/";
		 
		 // use ajax to call get content
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/vote/" + mgid + settings.nameid + "/" + settings.vote + "?callback=?", // /vote/{mgid}/{nameid}/{trueFalse}
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});

		 function contentSuccess() {
			 ;
		 }
		 
		 function contentFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 
	 jQuery.followManager.clearVotes = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     envUrl: "http://gocitykids.parentsconnect.com",
		     service: "nameslist",
		     type: "babyname",
		     nameid: "",
		     successCallback: contentSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: contentFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call get content
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/clear/" + settings.nameid + "?callback=?", // /vote/{mgid}/{nameid}/{trueFalse}
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});

		 function contentSuccess() {
			 ;
		 }
		 
		 function contentFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 
	 
	 jQuery.followManager.sendToFriend = function(options) {
		 var container = this;
		 var settings = jQuery.extend({
		     envUrl: "http://gocitykids.parentsconnect.com",
		     service: "nameslist",
		     type: "babyname",
		     friendName: "",
		     friendEmail: "",
		     yourName: "",
		     message: "",
		     successCallback: contentSuccess,	// returns item if it saved successfully (make sure item id is not null)
		     failureCallback: contentFailure // if ajax failed then we get sent to this function
		   }, options);
		 
		 // use ajax to call get content
		 $.ajax({
			  type: "GET",
			  url: settings.envUrl + "/" + settings.service + "/" + settings.type + "/share?friend-name=" + settings.friendName + "&friend-email=" + settings.friendEmail + "&your-name=" + settings.yourName + "&message=" + settings.message + "&callback=?", // /vote/{mgid}/{nameid}/{trueFalse}
			  cache: false,
			  dataType: "jsonp",
			  success: settings.successCallback,
			  failure: settings.failureCallback
			});

		 function contentSuccess() {
			 ;
		 }
		 
		 function contentFailure() {
			 alert("We are unable to process your request at this time. Please try again later.");
		 }
	 };
	 	 
})(jQuery);
