/*
* Dependent on var MAGNET.pluck.serverUrl
* Dependent on /js/pluck/DirectProxy.js
*/
if (typeof MAGNET.pluck == "undefined") {
    MAGNET.pluck = {};
}
//Guard against multiple inclusions
if (typeof MAGNET.pluck.widgets =="undefined"){
MAGNET.pluck.widgets = function(tarDiv) {
  var oThis = this;
  this.tarDiv = tarDiv;
 
 MAGNET.pluck.widgets.prototype.init= function() {
 
 var domain = location.host.toString();
  var domainSplit = domain.split(".");
  domain = domainSplit[domainSplit.length-2] + "." + domainSplit[domainSplit.length-1];
  var removePort = domain.split(":");
  domain = removePort[0];
  document.domain=domain;
}
  function discoveryArticle (activity, resultCount, cbHandler){
         var searchSections = new Array();  
         searchSections[0] = new Section("All");
         var searchCategories = new Array();  
         searchCategories[0] = new Category("All");  
   
         var activityDisco = new Activity(activity);  
         var contentType = new ContentType("Article");  
         var limitToContributorsDisco = new Array();
         
         //assuming all user tiers are desired
         limitToContributorsDisco[0] = new UserTier("All");  
         //max allowable age for content (in days)
         var age = 15;
         //batched amount to be returned
         var maximumNumberOfDiscoveries = resultCount;  
   
         var requestBatch = new RequestBatch();
         var discoveryAction = new DiscoverContentAction(  
             searchSections,  
             searchCategories,  
             limitToContributorsDisco,  
             activityDisco,
             contentType,
             age,  
             maximumNumberOfDiscoveries);  
         //requestBatch.AddToRequest(discoveryAction);  
         //requestBatch.BeginRequest(MAGNET.pluck.serverUrl, cbHandler);  
 }

	/* Keep the title length under a lineMax length (trims ending whitespace and non-word characters)
	   title : title string
	   moreChars : addtinal string to be added to the title (comment count or ther)
	*/
   function titleTrunc(title,moreChars){
   		var lineMax='41'; //keep in mind that we're adding '...' to the string as well as the comment count
   		lineMax-=(moreChars+"").length; //take into account the comment count string length
   		var filter=/[\W_]/; //True if we find non-word chars or '_'
   		setMax=function (){while(filter.test(title.charAt(lineMax-1)) && lineMax > 2)
   					{
   					lineMax--;
   					}		
    			return lineMax;}();
    	return title.substring(0,setMax) + (title.length <=setMax?"":"...");    
      
   }

   MAGNET.pluck.widgets.prototype.renderCommentedRail = function(responseBatch) {
    //console.dir(responseBatch) 
    if (document.getElementById(oThis.tarDiv)){
        var html = ""
        html = html + "<h3 class=\"header\">Most Commented</h3>";

        for(var i=0; i<responseBatch.Responses.length; i++) {
            var sortedItems = [];
            var pages = responseBatch.Responses[i].DiscoverContentAction.DiscoveredContent;
            for(var j=0; j<pages.length; j++) {
                if(pages[j].PageUrl == null) { continue; }
            
                var temp_html = "";
                var rawPageUrl=pages[j].PageUrl.toString();
                var myPageUrl = rawPageUrl.replace('http://','');
                myPageUrl = myPageUrl.substring(myPageUrl.indexOf('/'),myPageUrl.length);

                if(pages[j].PageTitle != null && pages[j].PageTitle.trim != '') {
                    temp_html = temp_html + "<span class=\"commentTitle\"><a href=\""+myPageUrl+"\">";
                    temp_html = temp_html + titleTrunc(pages[j].PageTitle,pages[j].Comments.NumberOfComments);
                } else {
                    temp_html = temp_html + "<span style=\"display:none\"><a href=\""+myPageUrl+"\">";
                    temp_html = temp_html + "";
                }
                temp_html = temp_html + "</a></span><span class=\"numComments\">" + pages[j].Comments.NumberOfComments + "</span><br/>";                

                sortedItems.push({"html":temp_html,"commentTotal":pages[j].Comments.NumberOfComments});        
            }
            sortedItems.sort(function(a,b) {
                var x = parseInt(a.commentTotal);
                var y = parseInt(b.commentTotal);
                if(x==y) {
                    return (a.html < b.html ? 1 : (( a.html > b.html) ? -1 : 0));
                }
                return ((x < y) ? 1 : ((x > y) ? -1 : 0));
            });
            for(var j=0; j<sortedItems.length; j++) {
                html = html + sortedItems[j].html;            
            }
        }
        document.getElementById(oThis.tarDiv).style.display="block";
        document.getElementById(oThis.tarDiv).innerHTML = html;  
    } else {
        return false;
    }
 }
    
   MAGNET.pluck.widgets.prototype.mostCommented = function() {
    //discoveryArticle("Commented",10,oThis.renderCommentedRail);
 }
 }
}
