CCI.Pluck["Messages"] = {
	Persona: {
		EditPersona: "Change photo / Edit profile",
		AddCompany: "Add a company profile"
	},
	Contacts: {
		TechnicalContactEmail: "webmaster@crainsdetroit.com",
		TechnicalContactName: "Alan Baker"
	},
	
	Errors: {
		SOSE: {
			MessageFromPluckNotOK: "Sorry, there is a temporary problem displaying this content. Please check back later.",
			NeedToReload: "Sorry, there is a temporary problem displaying this page."
		}
	},
	Forms: {
		MembersOnlyFormInput: {
			"Anonymous": "Please <a href='"+CCI.Pluck.Urls.LoginUrl+"'>log in or register</a> to contribute.",
			"NonMember": "Please <a href='#' onclick='CCI.Pluck.Users.CommunityRequests({action:\"join\"});'>join this community</a> to participate.",
			"Pending": "You may contribute when your request to join this community is approved."
		}
	},
	HandleErrors: function() {
			
			var noResponseDivs = $(".slError-SOSE");
			for (i=0;i<noResponseDivs.length;i++) {
				if (noResponseDivs[i].innerHTML.length == 0) {
					noResponseDivs[i].innerHTML = "<p>"+CCI.Pluck.Messages.Errors.SOSE.MessageFromPluckNotOK+"</p>";
					noResponseDivs[i].style.display = "block";
					}
			}
			var soseTerm = $("pre");
//			console.dir(soseTerm);
			for (p=0;p<soseTerm.length;p++) {
				// if SOSE stops for any request, show a global error message on the page
				if (soseTerm[p].innerHTML.indexOf("Error on line") > -1) {
				var emailUsLink = document.createElement("a");
				emailUsLink.innerHTML = "send us an e-mail";
				emailUsLink.onclick = function() {
					email();
					}
				document.getElementById("main-content").innerHTML = "<h1>"+CCI.Pluck.Messages.Errors.SOSE.NeedToReload+"</h1><ul><li><a href='"+location.href+"&userReload=1'>Try refreshing this page</a> or coming back a little later.</li><li>If the problem persists, please make sure to <a href='#' onclick='CCI.Pluck.Messages.ContactEmail({to:CCI.Pluck.Messages.Contacts.TechnicalContactEmail})'>send us an e-mail</a>.</li><li>Thank you for your patience</li></ul>";
				break;
				}
			}
				
	},
	ContactEmail: function(bag) {
			bag = bag || {};
			  var to = bag.to || "webmaster@crainsdetroit.com";
			  var subject = bag.subject || "Crain's Detroit Business communities";
			  var body = bag.body || "";
			  var email = "mailto:" + to + "&subject=" + escape(subject) + "&body=" + escape(body); 
			  window.location = email;
			}			
};



CCI.Pluck["LoadScripts"] = function() {
		if (CCI.Pluck.Globals.Enabled) {
			var version = CCI.Pluck.Globals.Version || 3.9;
			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");
 			if (version >= 4) {
 			// for apps (pluck 4)
 			document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + urls.PluckAppsUrl + "\"></sc" + "ript>\n");
			}
 		}
 	};

CCI.Pluck.LoadScripts();

CCI.Pluck.Debug = {
	Console: {
		Callback: function(r) {
			console.dir(r);
		}
	}
};

CCI.Pluck.Abuse = {
	reportAbuse: function(bag) {
	    bag = bag || {};
	    var contentType = bag.contentType || 0;
	    var itemKey = bag.itemKey || 0;
	    var reason = bag.reason || "";
	    var desc = bag.desc || "";
	    var abusiveContent = 0;
	    var itemURL = bag.itemURL || 0;
	    var itemTitle = bag.itemTitle || 0;
	    var itemSection = bag.itemSection || null;
	    if (itemSection) {
	        itemSection = new Section(itemSection);
	    }
	    var itemCategories = bag.itemCategories || [];
	
	    if (itemKey && contentType && itemURL && itemTitle) {
	
	    switch(contentType) {
	    case "ArticleKey":
	        var abusiveContent = new ArticleKey(itemKey);
	        var updateAction = new UpdateArticleAction(abusiveContent,itemURL,itemTitle,itemSection,itemCategories);
	        break;
	    }
	
	    if (abusiveContent && reason) {
	
	    var cb = function(r) {
	        console.dir(r);
	    }
	
	    var rb = new RequestBatch();
	    var reportAction = new ReportAbuseAction(abusiveContent, reason, desc);
	    rb.AddToRequest(updateAction);
	    rb.AddToRequest(reportAction);
	    console.dir(rb);
	    rb.BeginRequest(CCI.Pluck.Urls.DaapiProcessUrl, cb);
	    	}
		}

	}
};

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 commentsImage = "<img src='/images/icons/comment.png'>";
			var articleRecommendations = response.Article.Recommendations.NumberOfRecommendations;
			var recommendationsImage = "<img src='/images/sitelife/vote_yes_inactive.png'>";
					
			// format the html
			var articleCommentsHtml = (articleComments == 0 ? "" : commentsImage + " " + articleComments);
			var articleRecommendationsHtml = (articleRecommendations == 0 ? "" : recommendationsImage + " " + 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);
		}
	}
};
