CCI.Pluck["LoadScripts"] = function() {
		if (CCI.Pluck.Globals.Enabled) {
			var urls = CCI.Pluck.Urls;
			// for old widgets:
 			document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + urls.SiteLifeProxyUrl + "\"></sc" + "ript>\n");
 			 			// for DAAPI:
 			document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + urls.DirectProxyUrl + "\"></sc" + "ript>\n");
 			// for apps (pluck 4)
 			document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + urls.PluckAppsUrl + "\"></sc" + "ript>\n");
 		}
 	};

CCI.Pluck.LoadScripts();

CCI.Pluck["Users"] = {
	currentUserWidget: function(bag) {
		bag = bag || {};
	//	console.dir(bag);
		var pluckResponseDiv = bag.pluckResponseDiv || 0;
		var clickshareResponseDiv = bag.clickshareResponseDiv || 0;
		var clickshareIsDefault = bag.clickshareIsDefault || 0;
		var guestResponseDiv = bag.guestResponseDiv || 0;
		var html = "";
		
		var cb = function(r) {
		//	if (console) { console.dir(r); }
			if (!pluckResponseDiv || r.Responses[0].User.DisplayName == "anonymous" || r.Responses[0].User.DisplayName == r.Responses[0].User.UserKey) {
				if (clickshareIsDefault && clickshareResponseDiv) {
					
					var csdemos = new CCI.util.getCSCookie();
				//	if (console) { console.dir(csdemos); }
					if (csdemos.Cookie != null) {
					//	console.log("clickshare output");
						html += '<p class="cci-current-user-avatar"><img src="/assets/gif/pluck/sitelife_gen.gif" /></p><p><strong>' + csdemos.Cookie.nameFirst + ' ' + csdemos.Cookie.nameLast + '</strong></p><p><strong>No Public Profile</strong> | <a href="'+CCI.Pluck.Urls.EditAccountUrl+'"><strong>Edit Account</strong></a></p>';
						document.getElementById(clickshareResponseDiv).innerHTML = html;
						document.getElementById(clickshareResponseDiv).style.display = "block";
					}
					else {
						// console.log("guest output");
						html += '<p class="cci-current-user-avatar"><img src="/assets/gif/pluck/sitelife_gen.gif" /></p><p><strong>Not logged in</strong><br /><a href="'+CCI.Pluck.Urls.LoginUrl+'"><strong>Log in</strong></a></p>';
						document.getElementById(guestResponseDiv).innerHTML = html;
						document.getElementById(guestResponseDiv).style.display = "block";
						}
				} else {
				//	console.log("guest output");
				html += '<p class="cci-current-user-avatar"><img src="/assets/gif/pluck/sitelife_gen.gif" /></p><p><strong>Not logged in</strong><br /><a href="'+CCI.Pluck.Urls.LoginUrl+'"><strong>Log in</strong></a></p>';
						document.getElementById(guestResponseDiv).innerHTML = html;
						document.getElementById(guestResponseDiv).style.display = "block";
						}
				}
			else {
				// console.log("pluck output");
				var user = r.Responses[0].User;
				var personaUrl = "/section/sitelife_profile?uid=" + user.UserKey.Key;
				html += '<p class="cci-current-user-avatar"><a href="'+personaUrl+'"><img src="/apps/pbcsi.dll/urlget?url='+user.AvatarPhotoUrl+'&maxw=60" /></a></p><p><strong>'+user.DisplayName+'</strong></p><p><a href="'+CCI.Pluck.Urls.EditAccountUrl+'"><strong>Edit Account</strong></A> | <a href="'+CCI.Pluck.Urls.LogoutUrl+'"><strong>Log Out</strong></a></p>';
				document.getElementById(pluckResponseDiv).innerHTML = html;
				document.getElementById(pluckResponseDiv).style.display = "block";
				}
		};
		
		var response = function() {
			if (pluckResponseDiv) {
				var rb = new RequestBatch();
				rb.AddToRequest(new UserKey());
				rb.BeginRequest(CCI.Pluck.Urls.DaapiProcessUrl, cb);
			}
		};
		
		response();
	}
};

CCI.Pluck["Reactions"] = {
	getArticleReactions: function(bag) {

	bag = bag || {};
	var reactionsClass = bag.reactionsClass || 0;
	var commentsPrefix = bag.commentsPrefix || 0;
	var recommendationsPrefix = bag.recommendationsPrefix || 0;
		
	var articleReactionsCallback = function(responseBatch) {
	// loop through the responses returned in the responseBatch
	for (i=0;i<responseBatch.Responses.length;i++) {
	
		// get the response object	
		var response = responseBatch.Responses[i];
			
		// if the response is an article, format the HTML to be inserted
		if (response.Article) {
			// console.dir(response.Article);
				
			// get the article comments from the response... all the available properties for the article object are documented
			var articleComments = response.Article.Comments.NumberOfComments;
			var articleRecommendations = response.Article.Recommendations.NumberOfRecommendations;
					
			// format the html
			var articleCommentsHtml = articleComments;
			var articleRecommendationsHtml = articleRecommendations;
						
			if (commentsPrefix) {
			// get the element that the comments HTML will be inserted into
			var articlecommentsElement = commentsPrefix + response.Article.ArticleKey.Key;
			// insert the comments HTML
			document.getElementById(articlecommentsElement).innerHTML = articleCommentsHtml;
			}
			
			if (recommendationsPrefix) {
			// get the element that the recommendations HTML will be inserted into
			var articlerecommendationsElement = recommendationsPrefix + response.Article.ArticleKey.Key;
			// insert the recommendations HTML
			document.getElementById(articlerecommendationsElement).innerHTML = articleRecommendationsHtml;
			}
			
			}
		}
	};	
	
	var articleReactionsArray = []; // an empty array to fill with article keys we want
	
	// put all tags into an array
	var articleArray = $("." + reactionsClass);
		
	// loop through articleArray and dump tags matching our class name into articleReactionsArray
	for (i=0;i<articleArray.length;i++) {
		var thisArticle = articleArray[i];
		if (thisArticle.className.indexOf(reactionsClass) > -1) {
			articleReactionsArray.push(thisArticle.id);
			}
	}
	
	// console.dir(articleReactionsArray);

	// SiteLife limits batches to 20 requests, 
	// so we have to check if there are more than 20 articles on the page 
	// and if so divide them into separate batches...

	// how many articles will be requested?
	var numArticles = articleReactionsArray.length;
	
	// how many batches will we need?
	var numBatches = Math.ceil(numArticles / 20);
	
	// loop through and format request batches
	for (j=0;j<numBatches;j++) {
		
		// create the RequestBatch
		var requestBatch = new RequestBatch();
		
		// determine the starting point and limit point depending on where we are in the articleReactionsArray
		var start = 0;
		if (j>0) {
			start = j * 20;
			start = Math.ceil(start);
		}
		var limit = start + 20;
		
		// add the articleKey to the RequestBatch
		for (k=start;k<limit;k++) {
			if (articleReactionsArray[k]) {
				var articleKey = articleReactionsArray[k];
				requestBatch.AddToRequest(new ArticleKey(articleKey));
			}
		}
		
		// send the RequestBatch
		requestBatch.BeginRequest(CCI.Pluck.Urls.DaapiProcessUrl, articleReactionsCallback);
		}
	
	}
	
};