/***********************************************
*** Global configurations for Pluck SiteLife ***
************************************************/

var SITELIFE_ENABLED = true;
var SITELIFE_HOST = "sitelife.pionline.com";	

// Proxy URLs
var siteLifeProxyUrl = "http://" + SITELIFE_HOST + "/ver1.0/SiteLifeProxy?sid=" + SITELIFE_HOST;
var directProxyUrl = "http://" + SITELIFE_HOST + "/ver1.0/Direct/DirectProxy?sid=" + SITELIFE_HOST;

// DAAPI Process URL
// The name of this variable may be different for your implementation of DAAPI
var daapiProcessUrl = "http://" + SITELIFE_HOST + "/ver1.0/Direct/Process?sid=" + SITELIFE_HOST;

// Domain trust
document.domain = "pionline.com";

//var mydomain = "<%CurrentDomain$r(\"http://=\")%>";		
	//alert(mydomain);

/***********************
*** Include SiteLife ***
************************/

if (SITELIFE_ENABLED) {
	// for DAAPI:
    document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + directProxyUrl + "\"></sc" + "ript>\n"); 
    
    // for widgets:
    document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + siteLifeProxyUrl + "\"></sc" + "ript>\n");
	} else {
	// if SITELIFE IS TURNED OFF, LET JAVASCRIPT FAIL SILENTLY
	function handleErrors() {
		return true;
		}
	window.onerror = handleErrors;
}

/***********************************************
*** Configurations for Pluck COMMENTS ***
************************************************/


		function addArticlesToRequest(idArray, requestBatch) {
			/* creates the DAAPI request for the featured articles;
			 * pass in:
			 * 		array of IDs 
			 * 		optionally: an existing request batch (if not passed, one will be created)
			 * 
			 * DAAPI limits us to 20 items per batch, so that's the 
			 * absolute maximum number of featured articles
			 */
			if (typeof requestBatch != "object") requestBatch = new RequestBatch();
			
			//loop over the array and create the requests
			for (var itemIdx = 0; itemIdx < idArray.length; itemIdx++) {
				requestBatch.AddToRequest( new ArticleKey(idArray[itemIdx]) );
			}
			
			return requestBatch;
		}
		
		//callback manager
		function daapiCallback(responseBatch) {
			/* loops through the responses, finds the article keys, and if there's a target element
			 * for the Nyx output, calls the DiscoWriter for that article
			 */
			var thisResponse, domTarget, respIdx;
			
			//log the response -- for whatever reason (some scope issue), we have to re-fix the Safari console on async callbacks
			NYX.fixConsole();
			console.dir(responseBatch);
			
			/* now we loop over the responses, find a matching DIV per our naming convention,
			 * and call the gadget each time with only one article (unlike other uses of DiscoWriter)
			 * 
			 * we have to fake the object expected by Nyx.DiscoWriter 
			 * -- it expects a DiscoveredContent array, so we put our one article inside an array literal
			 */
			for (respIdx = 0; respIdx < responseBatch.Responses.length; respIdx++) {
				var thisResponse = responseBatch.Responses[respIdx];
				if (typeof thisResponse.Article == "object") {
					thisResponse = thisResponse.Article;
					domTarget = document.getElementById("nyx_" + thisResponse.ArticleKey.Key + "_output");
					if (domTarget != null) {
						myGadget.idRoot = "nyx_" + thisResponse.ArticleKey.Key;
						myGadget.writeGui([thisResponse]);
					}
				}
				
			}
		}
		
		//instantiate the gadget and set any parameters we want
		var myGadget = new NYX.DiscoWriter();	//unlike other instantiations, we leave the idRoot undefined... it will be set later
		myGadget.discoLimit = 6;
		myGadget.unnamedSection = "none";
		
			/*
		 * The Nyx gadget allows us to override some methods that are called during the gui-writing process,
		 * so let's say we want to replace a 0-comments situation with by hiding the whole part of the result
		 * that shows the comment count; because of our cleverness in the template HTML, we
		 * can just do a search-and-replace to add a style attribute that hides the element
		 * -- this code gets that done
		 */
			myGadget.customizeItem = function(dataObj, itemHtml, domElemThisWritesTo, itemIndex) {
				//override this method as needed to add your own GUI customizations
		
				//if no comments no need to show the count
				if ( Math.abs(dataObj.NumberOfComments) < 1 ) 
				{
					//poll archive styling
					if(itemHtml.indexOf("pollComments") > 0)
					{
						//var templateMarker = 'class="pollComments"';	
						//itemHtml = itemHtml.replace(templateMarker, templateMarker + ' style="display: none !important"');
						var templateMarker = /(class="?'?pollComments"?'?)/gi;
						itemHtml = itemHtml.replace(templateMarker, '$1 style="display: none !important"');
					}
					else
					{
						//var templateMarker = 'class="comments"';	
						//itemHtml = itemHtml.replace(templateMarker, templateMarker + ' style="display: none !important"');
						var templateMarker = /(class="?'?comments"?'?)/gi;
						itemHtml = itemHtml.replace(templateMarker, '$1 style="display: none !important"');
					}

				}
			
				//if no recommendations no need to show the count
				if ( Math.abs(dataObj.NumberOfRecommendations) < 6 ) {
						var templateMarker = /(class="?'?recommend"?'?)/gi;
						itemHtml = itemHtml.replace(templateMarker, '$1 style="display: none !important"');
				}

				//if no reviews no need to show the count
				if ( Math.abs(dataObj.NumberOfRatings) < 1 ) {
						var templateMarker = /(class="?'?rating"?'?)/gi;
						itemHtml = itemHtml.replace(templateMarker, '$1 style="display: none !important"');
				}

				return itemHtml;
			}

		
		
		//instantiate article ids array for loading
		var articleIds = [];                                 	
		var fobArticleIds = [];


/***********************************************
*** Configurations for Pluck 	Most Recommended ***
************************************************/	
		//encapsulate the discovery request into a factory function
		function makeDiscoRequest() {
			/* this is "exploded" to make it clearer, but the entire disco request
			 * could be created on a single line
			 */
			var sections   = [ new Section("All") ] ;
			var categories = [ new Category("All") ] ;
			
			//what do we want to discover?
			//var activity = new Activity("Commented");
			var activity = new Activity("Recommended");
			var contentType = new ContentType("Article");
			
			//who from?
			var tiersToAllow = [ new UserTier("All") ];
			
			//set other limits
			var contentAgeLimit = 15;
			var maxToReturn = 5;
			
			//create the request
			var discoveryAction = new DiscoverContentAction(sections, categories,
				tiersToAllow, activity, contentType, contentAgeLimit, maxToReturn);
			return discoveryAction;
		}
		
		/* your callback function will handle one or more responses; 
		 * in this sample, it's just one, but a real page might have many
		 */
		function daapiCallbackArt(responseBatchArt) {
			//log the response -- for whatever reason (some scope issue), we have to re-fix the Safari console on async callbacks
			NYX.fixConsole();
			console.dir(responseBatchArt);
			
			//test to see whether the response is valid
			if (typeof responseBatchArt.Responses[0] != "object" || typeof responseBatchArt.Responses[0].DiscoverContentAction != "object") {
				alert("Invalid response -- see the Firebug console (if you have one) to see what happened");
			} else {
				//response is what we were expecting, so dispatch it to the gadget, which will write the GUI
				myGadgetArt.writeGui(responseBatchArt.Responses[0].DiscoverContentAction.DiscoveredContent);
			}
		}
		
		//instantiate the gadget and set any parameters we want
		//var myGadgetArt = new NYX.DiscoWriter("nyxOutput");
		//myGadgetArt.discoLimit = 10;
		//myGadgetArt.unnamedSection = "none";
		
		//create the request
		//var requestBatchArt = new RequestBatch();
		//requestBatchArt.AddToRequest( makeDiscoRequest() );


		// Flag for the Most Recommended section of the tabbed box
		var myMostRecommended = false; //place on top of page
		var myMostRecommendedPage = false; //place on top of page

	<!--END PLUCK JS-->